@@ -54,14 +54,22 @@ public function create(string $login, #[SensitiveParameter] string $password, st
5454 try {
5555 $ result = $ user ->createUser ($ login , '' , $ domain );
5656 } catch (\Exception $ exception ) {
57- $ this ->configuration ->getLogger ()->info ($ exception ->getMessage ());
57+ $ this ->configuration
58+ ->getLogger ()
59+ ->error (sprintf ('Keycloak user creation failed for "%s": %s ' , $ login , $ exception ->getMessage ()));
60+ return false ;
61+ }
62+
63+ if (!$ result ) {
64+ return false ;
5865 }
5966
6067 $ user ->setStatus ('active ' );
6168 $ user ->setAuthSource (AuthenticationSourceType::AUTH_KEYCLOAK ->value );
6269 $ user ->setUserData ([
6370 'display_name ' => $ this ->getDisplayName (),
6471 'email ' => $ this ->getEmail (),
72+ 'keycloak_sub ' => $ this ->getSubject (),
6573 ]);
6674
6775 if ($ this ->shouldAssignGroups ()) {
@@ -127,6 +135,11 @@ private function getEmail(): string
127135 return trim ((string ) ($ this ->claims ['email ' ] ?? '' ));
128136 }
129137
138+ private function getSubject (): string
139+ {
140+ return trim ((string ) ($ this ->claims ['sub ' ] ?? '' ));
141+ }
142+
130143 private function userExists (string $ login ): bool
131144 {
132145 $ user = $ this ->createUser ();
@@ -156,7 +169,11 @@ private function assignUserToGroups(int $userId): void
156169 $ groupMapping = $ this ->getGroupMapping ();
157170
158171 foreach ($ roleNames as $ roleName ) {
159- $ faqGroupName = $ groupMapping [$ roleName ] ?? $ roleName ;
172+ if (!isset ($ groupMapping [$ roleName ])) {
173+ continue ;
174+ }
175+
176+ $ faqGroupName = $ groupMapping [$ roleName ];
160177 $ groupId = $ mediumPermission ->findOrCreateGroupByName ($ faqGroupName );
161178
162179 if ($ groupId <= 0 ) {
@@ -166,7 +183,7 @@ private function assignUserToGroups(int $userId): void
166183 $ mediumPermission ->addToGroup ($ userId , $ groupId );
167184 $ this ->configuration
168185 ->getLogger ()
169- ->info (sprintf ('Added Keycloak user %s to group %s ' , $ this -> resolvedLogin , $ faqGroupName ));
186+ ->info (sprintf ('Added Keycloak user #%d to group %s ' , $ userId , $ faqGroupName ));
170187 }
171188 }
172189
@@ -188,20 +205,16 @@ private function extractRoleNames(): array
188205 }
189206 }
190207
191- $ resourceAccess = $ this ->claims ['resource_access ' ] ?? [];
192- if (is_array ($ resourceAccess )) {
193- foreach ($ resourceAccess as $ resource ) {
194- $ resourceRoles = is_array ($ resource ) ? $ resource ['roles ' ] ?? null : null ;
195- if (!is_array ($ resourceRoles )) {
196- continue ;
197- }
198-
199- foreach ($ resourceRoles as $ resourceRole ) {
200- if (!is_string ($ resourceRole ) || $ resourceRole === '' ) {
208+ $ clientId = trim ((string ) $ this ->configuration ->get (item: 'keycloak.clientId ' ));
209+ if ($ clientId !== '' ) {
210+ $ clientRoles = $ this ->claims ['resource_access ' ][$ clientId ]['roles ' ] ?? [];
211+ if (is_array ($ clientRoles )) {
212+ foreach ($ clientRoles as $ clientRole ) {
213+ if (!is_string ($ clientRole ) || $ clientRole === '' ) {
201214 continue ;
202215 }
203216
204- $ roleNames [] = $ resourceRole ;
217+ $ roleNames [] = $ clientRole ;
205218 }
206219 }
207220 }
0 commit comments