File tree Expand file tree Collapse file tree 2 files changed +26
-3
lines changed
Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -67,6 +67,7 @@ private function fixDI() {
6767 public function createGroup (string $ name ): ?string {
6868 $ this ->fixDI ();
6969
70+ $ name = $ this ->sanitizeGroupName ($ name );
7071 $ gid = $ this ->computeGid ($ name );
7172 try {
7273 // Add group
@@ -586,12 +587,21 @@ public function getBackendName(): string {
586587 return 'Database ' ;
587588 }
588589
590+ /**
591+ * Merge any white spaces to a single space in group name, then trim it.
592+ */
593+ private function sanitizeGroupName (string $ displayName ): string {
594+ $ cleanedDisplayName = preg_replace ('/\s+/ ' , ' ' , $ displayName );
595+ return trim ($ cleanedDisplayName );
596+ }
597+
589598 /**
590599 * Compute group ID from display name (GIDs are limited to 64 characters in database)
591600 */
592601 private function computeGid (string $ displayName ): string {
593- return mb_strlen ($ displayName ) > 64
594- ? hash ('sha256 ' , $ displayName )
595- : $ displayName ;
602+ $ displayNameWithoutWhitespace = preg_replace ('/\s+/ ' , '_ ' , $ displayName );
603+ return mb_strlen ($ displayNameWithoutWhitespace ) > 64
604+ ? hash ('sha256 ' , $ displayNameWithoutWhitespace )
605+ : $ displayNameWithoutWhitespace ;
596606 }
597607}
Original file line number Diff line number Diff line change @@ -57,4 +57,17 @@ public function testAddLongGroupName(): void {
5757 $ group = $ this ->backend ->getGroupDetails ($ gidCreated );
5858 $ this ->assertEquals (['displayName ' => $ groupName ], $ group );
5959 }
60+
61+ public function testWhiteSpaceInGroupName (): void {
62+ $ randomId = $ this ->getUniqueID ('test_ ' , 10 );
63+ $ groupName = " group name with weird spaces \n" . $ randomId ;
64+ $ expectedGroupName = 'group name with weird spaces ' . $ randomId ;
65+ $ expectedGroupId = 'group_name_with_weird_spaces_ ' . $ randomId ;
66+
67+ $ gidCreated = $ this ->backend ->createGroup ($ groupName );
68+ $ this ->assertEquals ($ expectedGroupId , $ gidCreated );
69+
70+ $ group = $ this ->backend ->getGroupDetails ($ gidCreated );
71+ $ this ->assertEquals (['displayName ' => $ expectedGroupName ], $ group );
72+ }
6073}
You can’t perform that action at this time.
0 commit comments