@@ -35,7 +35,6 @@ class UserManager implements UserProviderInterface, PasswordUpgraderInterface
3535 protected $ securityController ;
3636 protected $ params ;
3737 protected $ tripleStore ;
38- protected $ userlink ;
3938
4039 private $ getOneByNameCacheResults ;
4140
@@ -56,24 +55,6 @@ public function __construct(
5655 $ this ->params = $ params ;
5756 $ this ->tripleStore = $ tripleStore ;
5857 $ this ->getOneByNameCacheResults = [];
59- $ this ->userlink = '' ;
60- }
61-
62- private function arrayToUser (?array $ userAsArray = null , bool $ fillEmpty = false ): ?User
63- {
64- if (empty ($ userAsArray )) {
65- return null ;
66- }
67- if ($ fillEmpty ) {
68- foreach (User::PROPS_LIST as $ key ) {
69- if (!array_key_exists ($ key , $ userAsArray )) {
70- $ userAsArray [$ key ] = null ;
71- }
72- }
73- }
74-
75- // be carefull the User::__construct is really strict about list of properties that should set
76- return new User ($ userAsArray );
7758 }
7859
7960 public function userExist ($ name ): bool
@@ -128,9 +109,8 @@ function ($userAsArray) {
128109 */
129110 public function create ($ wikiNameOrUser , string $ email = '' , string $ plainPassword = '' )
130111 {
131- $ this ->userlink = '' ;
132112 if ($ this ->securityController ->isWikiHibernated ()) {
133- throw new \ Exception (_t ('WIKI_IN_HIBERNATION ' ));
113+ throw new Exception (_t ('WIKI_IN_HIBERNATION ' ));
134114 }
135115
136116 if (is_array ($ wikiNameOrUser )) {
@@ -161,23 +141,23 @@ public function create($wikiNameOrUser, string $email = '', string $plainPasswor
161141 'signuptime ' => '' ,
162142 ];
163143 } else {
164- throw new \ Exception ('First parameter of UserManager->create should be string or array! ' );
144+ throw new Exception ('First parameter of UserManager->create should be string or array! ' );
165145 }
166146
167147 if (empty ($ wikiName )) {
168- throw new \ Exception ("'Name' parameter of UserManager->create should not be empty! " );
148+ throw new Exception ("'Name' parameter of UserManager->create should not be empty! " );
169149 }
170150 if (!empty ($ this ->getOneByName ($ wikiName ))) {
171151 throw new UserNameAlreadyUsedException ();
172152 }
173153 if (empty ($ email )) {
174- throw new \ Exception ("'email' parameter of UserManager->create should not be empty! " );
154+ throw new Exception ("'email' parameter of UserManager->create should not be empty! " );
175155 }
176156 if (!empty ($ this ->getOneByEmail ($ email ))) {
177157 throw new UserEmailAlreadyUsedException ();
178158 }
179159 if (empty ($ plainPassword )) {
180- throw new \ Exception ("'password' parameter of UserManager->create should not be empty! " );
160+ throw new Exception ("'password' parameter of UserManager->create should not be empty! " );
181161 }
182162
183163 unset($ this ->getOneByNameCacheResults [$ wikiName ]);
@@ -199,93 +179,52 @@ public function create($wikiNameOrUser, string $email = '', string $plainPasswor
199179 );
200180 }
201181
202- /*
203- * Password recovery process (AKA reset password)
204- * 1. A key is generated using name, email alongside with other stuff.
205- * 2. The triple (user's name, specific key "vocabulary",key) is stored in triples table.
206- * 3. In order to update h·er·is password, the user must provided that key.
207- * 4. The new password is accepted only if the key matches with the value in triples table.
208- * 5. The corresponding row is removed from triples table.
182+ /** Part of the Password recovery process: Handles the password recovery email process
183+ *
184+ * Generates the password recovery key
185+ * Stores the (name, vocabulary, key) triple in triples table
186+ * Generates the recovery email
187+ * Sends it
188+ *
189+ * @param User $user
190+ * @return string The link sent to the user
209191 */
210-
211- protected function generateUserLink ($ user )
192+ public function sendPasswordRecoveryEmail (User $ user )
212193 {
213194 // Generate the password recovery key
214195 $ passwordHasher = $ this ->passwordHasherFactory ->getPasswordHasher ($ user );
215196 $ plainKey = $ user ['name ' ] . '_ ' . $ user ['email ' ] . random_bytes (16 ) . date ('Y-m-d H:i:s ' );
216197 $ hashedKey = $ passwordHasher ->hash ($ plainKey );
217- $ tripleStore = $ this ->wiki ->services ->get (TripleStore::class);
218198 // Erase the previous triples in the trible table
219- $ tripleStore ->delete ($ user ['name ' ], self ::KEY_VOCABULARY , null , '' , '' );
199+ $ this -> tripleStore ->delete ($ user ['name ' ], self ::KEY_VOCABULARY , null , '' , '' );
220200 // Store the (name, vocabulary, key) triple in triples table
221- $ tripleStore ->create ($ user ['name ' ], self ::KEY_VOCABULARY , $ hashedKey , '' , '' );
201+ $ this -> tripleStore ->create ($ user ['name ' ], self ::KEY_VOCABULARY , $ hashedKey , '' , '' );
222202
223- // Generate the recovery email
224- $ this -> userlink = $ this ->wiki ->Href ('' , 'MotDePassePerdu ' , [
203+ // Generate the recovery link
204+ $ link = $ this ->wiki ->Href ('' , 'MotDePassePerdu ' , [
225205 'a ' => 'recover ' ,
226206 'email ' => $ hashedKey ,
227207 'u ' => base64_encode ($ user ['name ' ]),
228208 ], false );
229- }
230-
231- /**
232- * Part of the Password recovery process: Handles the password recovery email process.
233- *
234- * Generates the password recovery key
235- * Stores the (name, vocabulary, key) triple in triples table
236- * Generates the recovery email
237- * Sends it
238- *
239- * @return bool True if OK or false if any problems
240- */
241- public function sendPasswordRecoveryEmail (User $ user , string $ title ): bool
242- {
243- $ this ->generateUserLink ($ user );
244- $ pieces = parse_url ($ this ->params ->get ('base_url ' ));
245- $ domain = isset ($ pieces ['host ' ]) ? $ pieces ['host ' ] : '' ;
246-
247- $ message = _t ('LOGIN_DEAR ' ) . ' ' . $ user ['name ' ] . ", \n" ;
248- $ message .= _t ('LOGIN_CLICK_FOLLOWING_LINK ' ) . ' : ' . "\n" ;
249- $ message .= '----------------------- ' . "\n" ;
250- $ message .= $ this ->userlink . "\n" ;
251- $ message .= '----------------------- ' . "\n" ;
252- $ message .= _t ('LOGIN_THE_TEAM ' ) . ' ' . $ domain . "\n" ;
253-
254- $ subject = $ title . ' ' . $ domain ;
255209
256210 // Send the email
257- return send_mail ($ this ->params ->get ('BAZ_ADRESSE_MAIL_ADMIN ' ), $ this ->params ->get ('BAZ_ADRESSE_MAIL_ADMIN ' ), $ user ['email ' ], $ subject , $ message );
258- }
211+ if (!boolval ($ this ->wiki ->config ['contact_disable_email_for_password ' ])) {
212+ $ pieces = parse_url ($ this ->params ->get ('base_url ' ));
213+ $ domain = isset ($ pieces ['host ' ]) ? $ pieces ['host ' ] : '' ;
259214
260- /**
261- * Assessor for userlink field.
262- */
263- public function getUserLink (): string
264- {
265- return $ this ->userlink ;
266- }
215+ $ message = _t ('LOGIN_DEAR ' ) . ' ' . $ user ['name ' ] . ", \n" ;
216+ $ message .= _t ('LOGIN_CLICK_FOLLOWING_LINK ' ) . ' : ' . "\n" ;
217+ $ message .= '----------------------- ' . "\n" ;
218+ $ message .= $ link . "\n" ;
219+ $ message .= '----------------------- ' . "\n" ;
220+ $ message .= _t ('LOGIN_THE_TEAM ' ) . ' ' . $ domain . "\n" ;
267221
268- /**
269- * Assessor for userlink field.
270- */
271- public function getLastUserLink (User $ user ): string
272- {
273- $ passwordHasher = $ this ->passwordHasherFactory ->getPasswordHasher ($ user );
274- $ plainKey = $ user ['name ' ] . '_ ' . $ user ['email ' ] . random_bytes (16 ) . date ('Y-m-d H:i:s ' );
275- $ hashedKey = $ passwordHasher ->hash ($ plainKey );
276- $ tripleStore = $ this ->wiki ->services ->get (TripleStore::class);
277- $ key = $ tripleStore ->getOne ($ user ['name ' ], self ::KEY_VOCABULARY , '' , '' );
278- if ($ key != null ) {
279- $ this ->userlink = $ this ->wiki ->Href ('' , 'MotDePassePerdu ' , [
280- 'a ' => 'recover ' ,
281- 'email ' => $ key ,
282- 'u ' => base64_encode ($ user ['name ' ]),
283- ], false );
284- } else {
285- $ this ->generateUserLink ($ user );
222+ $ subject = _t ('LOGIN_PASSWORD_LOST_FOR ' ) . ' ' . $ domain ;
223+
224+ send_mail ($ this ->params ->get ('BAZ_ADRESSE_MAIL_ADMIN ' ), $ this ->params ->get ('BAZ_ADRESSE_MAIL_ADMIN ' ), $ user ['email ' ], $ subject , $ message );
286225 }
287226
288- return $ this -> userlink ;
227+ return $ link ;
289228 }
290229
291230 /**
@@ -300,7 +239,7 @@ public function getLastUserLink(User $user): string
300239 public function update (User $ user , array $ newValues ): bool
301240 {
302241 if ($ this ->securityController ->isWikiHibernated ()) {
303- throw new \ Exception (_t ('WIKI_IN_HIBERNATION ' ));
242+ throw new Exception (_t ('WIKI_IN_HIBERNATION ' ));
304243 }
305244 $ newKeys = array_keys ($ newValues );
306245 $ authorizedKeys = array_filter ($ newKeys , function ($ key ) {
@@ -378,10 +317,10 @@ public function delete(User $user)
378317 */
379318 public function groupsWhereIsMember (User $ user , bool $ adminCheck = true )
380319 {
381- $ group_list = $ this ->tripleStore ->getMatching (GROUP_PREFIX . '% ' , null , '% ' . $ user ['name ' ]. '% ' , 'LIKE ' , '= ' , 'LIKE ' );
320+ $ group_list = $ this ->tripleStore ->getMatching (GROUP_PREFIX . '% ' , null , '% ' . $ user ['name ' ] . '% ' , 'LIKE ' , '= ' , 'LIKE ' );
382321 $ prefix_len = strlen (GROUP_PREFIX );
383322 $ list = array ();
384- foreach ($ group_list as $ group ) {
323+ foreach ($ group_list as $ group ) {
385324 $ list [] = substr ($ group ['resource ' ], $ prefix_len );
386325 }
387326 return $ list ;
@@ -542,4 +481,21 @@ public function logout()
542481 {
543482 $ this ->wiki ->services ->get (AuthController::class)->logout ();
544483 }
484+
485+ private function arrayToUser (?array $ userAsArray = null , bool $ fillEmpty = false ): ?User
486+ {
487+ if (empty ($ userAsArray )) {
488+ return null ;
489+ }
490+ if ($ fillEmpty ) {
491+ foreach (User::PROPS_LIST as $ key ) {
492+ if (!array_key_exists ($ key , $ userAsArray )) {
493+ $ userAsArray [$ key ] = null ;
494+ }
495+ }
496+ }
497+
498+ // be carefull the User::__construct is really strict about list of properties that should set
499+ return new User ($ userAsArray );
500+ }
545501}
0 commit comments