File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1313use Vizir \KeycloakWebGuard \Auth \KeycloakWebUserProvider ;
1414use Vizir \KeycloakWebGuard \Middleware \KeycloakAuthenticated ;
1515use Vizir \KeycloakWebGuard \Middleware \KeycloakCan ;
16+ use Vizir \KeycloakWebGuard \Middleware \KeycloakCanOne ;
1617use Vizir \KeycloakWebGuard \Models \KeycloakUser ;
1718use Vizir \KeycloakWebGuard \Services \KeycloakService ;
1819
@@ -72,6 +73,9 @@ public function register()
7273 // Add Middleware "keycloak-web-can"
7374 $ this ->app ['router ' ]->aliasMiddleware ('keycloak-web-can ' , KeycloakCan::class);
7475
76+ // Add Middleware "keycloak-web-can-one
77+ $ this ->app ['router ' ]->aliasMiddleware ('keycloak-web-can-one ' , KeycloakCanOne::class);
78+
7579 // Bind for client data
7680 $ this ->app ->when (KeycloakService::class)->needs (ClientInterface::class)->give (function () {
7781 return new Client (Config::get ('keycloak-web.guzzle_options ' , []));
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Vizir \KeycloakWebGuard \Middleware ;
4+
5+ use Closure ;
6+ use Illuminate \Auth \Access \AuthorizationException ;
7+ use Illuminate \Support \Facades \Auth ;
8+ use Vizir \KeycloakWebGuard \Exceptions \KeycloakCanException ;
9+
10+ class KeycloakCanOne extends KeycloakAuthenticated
11+ {
12+ /**
13+ * Handle an incoming request.
14+ *
15+ * @param \Illuminate\Http\Request $request
16+ * @param \Closure $next
17+ * @param string|null $guard
18+ * @return mixed
19+ */
20+ public function handle ($ request , Closure $ next , ...$ guards )
21+ {
22+ if (empty ($ guards ) && Auth::check ()) {
23+ return $ next ($ request );
24+ }
25+
26+ $ guards = explode ('| ' , ($ guards [0 ] ?? '' ));
27+ foreach ($ guards as $ guard ) {
28+ if (Auth::hasRole ($ guard )) {
29+ return $ next ($ request );
30+ }
31+ }
32+
33+ throw new AuthorizationException ('Forbidden ' , 403 );
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments