/
RegistrationService.php
217 lines (188 loc) · 6.94 KB
/
RegistrationService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace Concrete\Core\User;
use Concrete\Core\Application\Application;
use Concrete\Core\Entity\User\User as UserEntity;
use Concrete\Core\Entity\User\UserSignup;
use Concrete\Core\Notification\Type\UserSignupType;
use Concrete\Core\User\Event\AddUser;
use Concrete\Core\User\Event\UserInfoWithPassword;
use Doctrine\ORM\EntityManagerInterface;
use Hautelook\Phpass\PasswordHash;
class RegistrationService implements RegistrationServiceInterface
{
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var Application
*/
protected $application;
/**
* @var UserInfoRepository
*/
protected $userInfoRepository;
public function __construct(Application $application, EntityManagerInterface $entityManager, UserInfoRepository $userInfoRepository)
{
$this->application = $application;
$this->entityManager = $entityManager;
$this->userInfoRepository = $userInfoRepository;
}
/**
* @param string $uPasswordEncrypted
* @param string $uEmail
*
* @return UserInfo|null
*/
public function createSuperUser($uPasswordEncrypted, $uEmail)
{
$dh = $this->application->make('date');
$entity = new UserEntity();
$entity->setUserID(USER_SUPER_ID);
$entity->setUserName(USER_SUPER);
$entity->setUserEmail($uEmail);
$entity->setUserPassword($uPasswordEncrypted);
$entity->setUserIsActive(true);
$this->entityManager->persist($entity);
$this->entityManager->flush();
return $this->userInfoRepository->getByID($entity->getUserID());
}
/**
* @param array $data
*
* @return UserInfo|false|null
*/
public function create($data)
{
$uae = new AddUser($data);
$uae = \Events::dispatch('on_before_user_add', $uae);
if (!$uae->proceed()) {
return false;
}
$config = $this->application->make('config');
$hasher = new PasswordHash($config->get('concrete.user.password.hash_cost_log2'), $config->get('concrete.user.password.hash_portable'));
if (isset($data['uIsValidated']) && $data['uIsValidated'] == 1) {
$uIsValidated = 1;
} elseif (isset($data['uIsValidated']) && $data['uIsValidated'] == 0) {
$uIsValidated = 0;
} else {
$uIsValidated = -1;
}
if (isset($data['uIsFullRecord']) && $data['uIsFullRecord'] == 0) {
$uIsFullRecord = 0;
} else {
$uIsFullRecord = 1;
}
$password_to_insert = isset($data['uPassword']) ? $data['uPassword'] : null;
$hash = $hasher->HashPassword($password_to_insert);
$uDefaultLanguage = null;
if (isset($data['uDefaultLanguage']) && $data['uDefaultLanguage'] != '') {
$uDefaultLanguage = $data['uDefaultLanguage'];
}
$entity = new UserEntity();
$entity->setUserName($data['uName']);
$entity->setUserEmail($data['uEmail']);
$entity->setUserPassword($hash);
$entity->setUserIsValidated($uIsValidated);
$entity->setUserIsFullRecord($uIsFullRecord);
$entity->setUserDefaultLanguage($uDefaultLanguage);
$entity->setUserIsActive(true);
$this->entityManager->persist($entity);
$this->entityManager->flush();
$newUID = $entity->getUserID();
$ui = $this->userInfoRepository->getByID($newUID);
if (is_object($ui)) {
$uo = $ui->getUserObject();
$groupControllers = \Group::getAutomatedOnRegisterGroupControllers($uo);
foreach ($groupControllers as $ga) {
if ($ga->check($uo)) {
$uo->enterGroup($ga->getGroupObject());
}
}
// run any internal event we have for user add
$ue = new UserInfoWithPassword($ui);
$ue->setUserPassword($password_to_insert);
\Events::dispatch('on_user_add', $ue);
// Now we notify any relevant users.
$type = $this->application->make('manager/notification/types')->driver('user_signup');
/* @var UserSignupType $type */
$u = new User();
$createdBy = null;
if (is_object($u)) {
$creator = $u->getUserInfoObject();
if (is_object($creator)) {
$createdBy = $creator->getEntityObject();
}
}
$signup = new UserSignup($ui->getEntityObject(), $createdBy);
$notifier = $type->getNotifier();
$subscription = $type->getSubscription($signup);
$notified = $notifier->getUsersToNotify($subscription, $signup);
$notification = $type->createNotification($signup);
$notifier->notify($notified, $notification);
}
return $ui;
}
/**
* @param array $data
*
* @return UserInfo
*/
public function createFromPublicRegistration($data)
{
// slightly different than add. this is public facing
$config = $this->application->make('config');
if ($config->get('concrete.user.registration.validate_email')) {
$data['uIsValidated'] = 0;
}
$ui = $this->create($data);
return $ui;
}
/**
* Create an unused username starting from user details.
*
* @param string $email The user's email address
* @param string $suggestedUsername A suggestion about the username
* @param string $firstName The user's first name
* @param string $lastName The user's last name
*
* @return string
*/
public function getNewUsernameFromUserDetails($email, $suggestedUsername = '', $firstName = '', $lastName = '')
{
$baseUsername = $this->stringToUsernameChunk($suggestedUsername);
if ($baseUsername === '') {
$firstName = $this->stringToUsernameChunk($firstName);
$lastName = $this->stringToUsernameChunk($lastName);
if ($firstName !== '' || $lastName !== '') {
$baseUsername = trim($firstName . '_' . $lastName, '_');
} else {
$mailbox = strstr((string) $email, '@', true);
$baseUsername = $this->stringToUsernameChunk($mailbox);
}
if ($baseUsername === '') {
$baseUsername = 'user';
}
}
$username = $baseUsername;
$suffix = 1;
while ($this->userInfoRepository->getByName($username) !== null) {
$username = $baseUsername . '_' . $suffix;
++$suffix;
}
return $username;
}
/**
* @param string $string
*
* @return string
*/
private function stringToUsernameChunk($string)
{
$string = trim((string) $string);
$string = preg_replace('/[^a-z0-9]+/', '_', strtolower($string));
$string = preg_replace('/__+/', '_', $string);
$string = trim($string, '_');
return $string;
}
}