| 
									
										
										
										
											2024-04-17 01:10:32 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Access\Oidc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Illuminate\Support\Arr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class OidcUserDetails | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function __construct( | 
					
						
							|  |  |  |         public ?string $externalId = null, | 
					
						
							|  |  |  |         public ?string $email = null, | 
					
						
							|  |  |  |         public ?string $name = null, | 
					
						
							|  |  |  |         public ?array $groups = null, | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Check if the user details are fully populated for our usage. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function isFullyPopulated(bool $groupSyncActive): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $hasEmpty = empty($this->externalId) | 
					
						
							|  |  |  |             || empty($this->email) | 
					
						
							|  |  |  |             || empty($this->name) | 
					
						
							|  |  |  |             || ($groupSyncActive && empty($this->groups)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return !$hasEmpty; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2024-04-18 06:24:57 +08:00
										 |  |  |      * Populate user details from the given claim data. | 
					
						
							| 
									
										
										
										
											2024-04-17 01:10:32 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-04-18 01:23:58 +08:00
										 |  |  |     public function populate( | 
					
						
							|  |  |  |         ProvidesClaims $claims, | 
					
						
							| 
									
										
										
										
											2024-04-17 01:10:32 +08:00
										 |  |  |         string $idClaim, | 
					
						
							|  |  |  |         string $displayNameClaims, | 
					
						
							|  |  |  |         string $groupsClaim, | 
					
						
							| 
									
										
										
										
											2024-04-18 01:23:58 +08:00
										 |  |  |     ): void { | 
					
						
							|  |  |  |         $this->externalId = $claims->getClaim($idClaim) ?? $this->externalId; | 
					
						
							|  |  |  |         $this->email = $claims->getClaim('email') ?? $this->email; | 
					
						
							| 
									
										
										
										
											2024-04-18 06:24:57 +08:00
										 |  |  |         $this->name = static::getUserDisplayName($displayNameClaims, $claims) ?? $this->name; | 
					
						
							| 
									
										
										
										
											2024-04-18 01:23:58 +08:00
										 |  |  |         $this->groups = static::getUserGroups($groupsClaim, $claims) ?? $this->groups; | 
					
						
							| 
									
										
										
										
											2024-04-17 01:10:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-18 06:24:57 +08:00
										 |  |  |     protected static function getUserDisplayName(string $displayNameClaims, ProvidesClaims $token): string | 
					
						
							| 
									
										
										
										
											2024-04-17 01:10:32 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $displayNameClaimParts = explode('|', $displayNameClaims); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $displayName = []; | 
					
						
							|  |  |  |         foreach ($displayNameClaimParts as $claim) { | 
					
						
							|  |  |  |             $component = $token->getClaim(trim($claim)) ?? ''; | 
					
						
							|  |  |  |             if ($component !== '') { | 
					
						
							|  |  |  |                 $displayName[] = $component; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return implode(' ', $displayName); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-18 01:23:58 +08:00
										 |  |  |     protected static function getUserGroups(string $groupsClaim, ProvidesClaims $token): array | 
					
						
							| 
									
										
										
										
											2024-04-17 01:10:32 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (empty($groupsClaim)) { | 
					
						
							|  |  |  |             return []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $groupsList = Arr::get($token->getAllClaims(), $groupsClaim); | 
					
						
							|  |  |  |         if (!is_array($groupsList)) { | 
					
						
							|  |  |  |             return []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return array_values(array_filter($groupsList, function ($val) { | 
					
						
							|  |  |  |             return is_string($val); | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |