99use SimpleSAML \Module \oidc \Bridges \SspBridge ;
1010use SimpleSAML \Module \oidc \Exceptions \AuthorizationException ;
1111use SimpleSAML \Module \oidc \Services \AuthContextService ;
12+ use SimpleSAML \Module \oidc \Services \LoggerService ;
1213
1314class Authorization
1415{
1516 public function __construct (
1617 protected readonly SspBridge $ sspBridge ,
1718 protected readonly AuthContextService $ authContextService ,
19+ protected readonly LoggerService $ loggerService ,
1820 ) {
1921 }
2022
2123 public function isAdmin (): bool
2224 {
25+ $ this ->loggerService ->debug ('Authorization::isAdmin ' );
2326 return $ this ->sspBridge ->utils ()->auth ()->isAdmin ();
2427 }
2528
@@ -28,10 +31,19 @@ public function isAdmin(): bool
2831 */
2932 public function requireAdmin (bool $ forceAdminAuthentication = false ): void
3033 {
34+ $ this ->loggerService ->debug ('Authorization::requireAdmin ' );
35+ $ this ->loggerService ->debug (
36+ 'Authorization: Force admin authentication: ' ,
37+ ['forceAdminAuthentication ' => $ forceAdminAuthentication ],
38+ );
3139 if ($ forceAdminAuthentication ) {
40+ $ this ->loggerService ->debug ('Authorization: Forcing admin authentication. ' );
3241 try {
3342 $ this ->sspBridge ->utils ()->auth ()->requireAdmin ();
3443 } catch (Exception $ exception ) {
44+ $ this ->loggerService ->error (
45+ 'Authorization: Forcing admin authentication failed: ' . $ exception ->getMessage (),
46+ );
3547 throw new AuthorizationException (
3648 Translate::noop ('Unable to initiate SimpleSAMLphp admin authentication. ' ),
3749 $ exception ->getCode (),
@@ -41,7 +53,10 @@ public function requireAdmin(bool $forceAdminAuthentication = false): void
4153 }
4254
4355 if (! $ this ->isAdmin ()) {
56+ $ this ->loggerService ->error ('Authorization: User is NOT admin. ' );
4457 throw new AuthorizationException (Translate::noop ('SimpleSAMLphp admin access required. ' ));
58+ } else {
59+ $ this ->loggerService ->debug ('Authorization: User is admin. ' );
4560 }
4661 }
4762
@@ -50,16 +65,29 @@ public function requireAdmin(bool $forceAdminAuthentication = false): void
5065 */
5166 public function requireAdminOrUserWithPermission (string $ permission ): void
5267 {
68+ $ this ->loggerService ->debug ('Authorization::requireAdminOrUserWithPermission ' );
69+ $ this ->loggerService ->debug ('Authorization: For permission: ' . $ permission );
70+
5371 if ($ this ->isAdmin ()) {
72+ $ this ->loggerService ->debug ('Authorization: User is admin, returning. ' );
5473 return ;
74+ } else {
75+ $ this ->loggerService ->debug ('Authorization: User is not (authenticated as) admin. ' );
5576 }
5677
5778 try {
79+ $ this ->loggerService ->debug ('Authorization: Checking for user permission. ' );
5880 $ this ->authContextService ->requirePermission ($ permission );
59- } catch (\Exception ) {
60- // TODO mivanci v7 log this exception
81+ $ this ->loggerService ->debug ('Authorization: User has permission, returning. ' );
82+ return ;
83+ } catch (\Exception $ exception ) {
84+ $ this ->loggerService ->warning (
85+ 'Authorization: User permission check failed: ' . $ exception ->getMessage (),
86+ );
6187 }
6288
89+ $ this ->loggerService ->debug ('Authorization: Falling back to admin authentication. ' );
90+
6391 // If we get here, the user does not have the required permission, or permissions are not enabled.
6492 // Fallback to admin authentication.
6593 $ this ->requireAdmin (true );
0 commit comments