4444use OCP \IUser ;
4545use OCP \IUserBackend ;
4646use OCP \IUserManager ;
47+ use OCP \L10N \IFactory ;
48+ use OCP \Server ;
4749use OCP \Support \Subscription \IAssertion ;
4850use OCP \User \Backend \IGetRealUIDBackend ;
4951use OCP \User \Backend \ISearchKnownUsersBackend ;
@@ -427,31 +429,7 @@ public function createUser($uid, $password) {
427429 public function createUserFromBackend ($ uid , $ password , UserInterface $ backend ) {
428430 $ l = \OC ::$ server ->getL10N ('lib ' );
429431
430- // Check the name for bad characters
431- // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'"
432- if (preg_match ('/[^a-zA-Z0-9 _.@\- \']/ ' , $ uid )) {
433- throw new \InvalidArgumentException ($ l ->t ('Only the following characters are allowed in a username: '
434- . ' "a-z", "A-Z", "0-9", and "_.@- \'" ' ));
435- }
436-
437- // No empty username
438- if (trim ($ uid ) === '' ) {
439- throw new \InvalidArgumentException ($ l ->t ('A valid username must be provided ' ));
440- }
441-
442- // No whitespace at the beginning or at the end
443- if (trim ($ uid ) !== $ uid ) {
444- throw new \InvalidArgumentException ($ l ->t ('Username contains whitespace at the beginning or at the end ' ));
445- }
446-
447- // Username only consists of 1 or 2 dots (directory traversal)
448- if ($ uid === '. ' || $ uid === '.. ' ) {
449- throw new \InvalidArgumentException ($ l ->t ('Username must not consist of dots only ' ));
450- }
451-
452- if (!$ this ->verifyUid ($ uid )) {
453- throw new \InvalidArgumentException ($ l ->t ('Username is invalid because files already exist for this user ' ));
454- }
432+ $ this ->validateUserId ($ uid , true );
455433
456434 // No empty password
457435 if (trim ($ password ) === '' ) {
@@ -726,7 +704,43 @@ public function getByEmail($email) {
726704 }));
727705 }
728706
729- private function verifyUid (string $ uid ): bool {
707+ /**
708+ * @param string $uid
709+ * @param bool $checkDataDirectory
710+ * @throws \InvalidArgumentException Message is an already translated string with a reason why the id is not valid
711+ * @since 26.0.0
712+ */
713+ public function validateUserId (string $ uid , bool $ checkDataDirectory = false ): void {
714+ $ l = Server::get (IFactory::class)->get ('lib ' );
715+
716+ // Check the name for bad characters
717+ // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'"
718+ if (preg_match ('/[^a-zA-Z0-9 _.@\- \']/ ' , $ uid )) {
719+ throw new \InvalidArgumentException ($ l ->t ('Only the following characters are allowed in a username: '
720+ . ' "a-z", "A-Z", "0-9", and "_.@- \'" ' ));
721+ }
722+
723+ // No empty username
724+ if (trim ($ uid ) === '' ) {
725+ throw new \InvalidArgumentException ($ l ->t ('A valid username must be provided ' ));
726+ }
727+
728+ // No whitespace at the beginning or at the end
729+ if (trim ($ uid ) !== $ uid ) {
730+ throw new \InvalidArgumentException ($ l ->t ('Username contains whitespace at the beginning or at the end ' ));
731+ }
732+
733+ // Username only consists of 1 or 2 dots (directory traversal)
734+ if ($ uid === '. ' || $ uid === '.. ' ) {
735+ throw new \InvalidArgumentException ($ l ->t ('Username must not consist of dots only ' ));
736+ }
737+
738+ if (!$ this ->verifyUid ($ uid , $ checkDataDirectory )) {
739+ throw new \InvalidArgumentException ($ l ->t ('Username is invalid because files already exist for this user ' ));
740+ }
741+ }
742+
743+ private function verifyUid (string $ uid , bool $ checkDataDirectory = false ): bool {
730744 $ appdata = 'appdata_ ' . $ this ->config ->getSystemValueString ('instanceid ' );
731745
732746 if (\in_array ($ uid , [
@@ -740,6 +754,10 @@ private function verifyUid(string $uid): bool {
740754 return false ;
741755 }
742756
757+ if (!$ checkDataDirectory ) {
758+ return true ;
759+ }
760+
743761 $ dataDirectory = $ this ->config ->getSystemValueString ('datadirectory ' , \OC ::$ SERVERROOT . '/data ' );
744762
745763 return !file_exists (rtrim ($ dataDirectory , '/ ' ) . '/ ' . $ uid );
0 commit comments