]> git.mxchange.org Git - friendica.git/blob - src/Model/UserService.php
Add new possibility to add a user per console
[friendica.git] / src / Model / UserService.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU APGL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use ErrorException;
25 use Friendica\App;
26 use Friendica\Core\Config\IConfig;
27 use Friendica\Core\L10n;
28 use Friendica\Network\HTTPException\InternalServerErrorException;
29 use Friendica\Util\Emailer;
30 use Friendica\Util\Strings;
31 use Friendica\Model\User as UserModel;
32 use ImagickException;
33
34 class UserService
35 {
36         /** @var L10n */
37         private $l10n;
38         /** @var IConfig */
39         private $config;
40         /** @var App\BaseURL */
41         private $baseUrl;
42         /** @var Emailer */
43         private $emailer;
44
45         public function __construct(L10n $l10n, IConfig $config, Emailer $emailer, App\BaseURL $baseUrl)
46         {
47                 $this->l10n = $l10n;
48                 $this->config = $config;
49                 $this->emailer = $emailer;
50                 $this->baseUrl = $baseUrl;
51         }
52
53         /**
54          * Creates a new user based on a minimal set and sends an email to this user
55          *
56          * @param string $name The user's name
57          * @param string $email The user's email address
58          * @param string $nick The user's nick name
59          * @param string $lang The user's language (default is english)
60          *
61          * @return bool True, if the user was created successfully
62          * @throws InternalServerErrorException
63          * @throws ErrorException
64          * @throws ImagickException
65          */
66         public function createMinimal(string $name, string $email, string $nick, string $lang = L10n::DEFAULT)
67         {
68                 if (empty($name) ||
69                     empty($email) ||
70                     empty($nick)) {
71                         throw new InternalServerErrorException('Invalid arguments.');
72                 }
73
74                 $result = UserModel::create([
75                         'username' => $name,
76                         'email' => $email,
77                         'nickname' => $nick,
78                         'verified' => 1,
79                         'language' => $lang
80                 ]);
81
82                 $user = $result['user'];
83                 $preamble = Strings::deindent($this->l10n->t('
84                 Dear %1$s,
85                         the administrator of %2$s has set up an account for you.'));
86                 $body = Strings::deindent($this->l10n->t('
87                 The login details are as follows:
88
89                 Site Location:  %1$s
90                 Login Name:             %2$s
91                 Password:               %3$s
92
93                 You may change your password from your account "Settings" page after logging
94                 in.
95
96                 Please take a few moments to review the other account settings on that page.
97
98                 You may also wish to add some basic information to your default profile
99                 (on the "Profiles" page) so that other people can easily find you.
100
101                 We recommend setting your full name, adding a profile photo,
102                 adding some profile "keywords" (very useful in making new friends) - and
103                 perhaps what country you live in; if you do not wish to be more specific
104                 than that.
105
106                 We fully respect your right to privacy, and none of these items are necessary.
107                 If you are new and do not know anybody here, they may help
108                 you to make some new and interesting friends.
109
110                 If you ever want to delete your account, you can do so at %1$s/removeme
111
112                 Thank you and welcome to %4$s.'));
113
114                 $preamble = sprintf($preamble, $user['username'], $this->config->get('config', 'sitename'));
115                 $body = sprintf($body, $this->baseUrl->get(), $user['nickname'], $result['password'], $this->config->get('config', 'sitename'));
116
117                 $email = $this->emailer
118                            ->newSystemMail()
119                            ->withMessage($this->l10n->t('Registration details for %s', $this->config->get('config', 'sitename')), $preamble, $body)
120                            ->forUser($user)
121                            ->withRecipient($user['email'])
122                            ->build();
123                 return $this->emailer->send($email);
124         }
125 }