src/App/src/Middleware/AuthorizationMiddleware.php
/**
     * @param ServerRequestInterface $request
     * @param RequestHandlerInterface $handler
     * @return ResponseInterface
     * @throws Throwable
     */
@param ServerRequestInterface $request
@param RequestHandlerInterface $handler
@return ResponseInterface
@throws Throwable
@param ServerRequestInterface $request @param RequestHandlerInterface $handler @return ResponseInterface @throws Throwable
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $defaultUser = $request->getAttribute(UserInterface::class);
        switch ($defaultUser->getDetail('oauth_client_id')) {
            case 'admin':
                $user = $this->adminService->findByIdentity($defaultUser->getIdentity());
                if ($user->getStatus() !== Admin::STATUS_ACTIVE) {
                    return new JsonResponse([
                        'error' => [
                            'messages' => [
                                Message::ADMIN_NOT_ACTIVATED
                            ]
                        ]
                    ], Response::STATUS_CODE_403);
                }
                $request = $request->withAttribute(Admin::class, $user);
                break;
            case 'frontend':
                $user = $this->userService->findByIdentity($defaultUser->getIdentity());
                if (!($user instanceof User) || $user->isDeleted()) {
                    return new JsonResponse([
                        'error' => [
                            'messages' => [
                                sprintf(Message::USER_NOT_FOUND_BY_IDENTITY, $defaultUser->getIdentity())
                            ]
                        ]
                    ], Response::STATUS_CODE_403);
                }
                if ($user->getStatus() !== User::STATUS_ACTIVE) {
                    return new JsonResponse([
                        'error' => [
                            'messages' => [
                                Message::USER_NOT_ACTIVATED
                            ]
                        ]
                    ], Response::STATUS_CODE_403);
                }
                $request = $request->withAttribute(User::class, $user);
                break;
            case 'guest':
                $user = new Guest();
                $request = $request->withAttribute(Guest::class, $user);
                break;
            default:
                return new JsonResponse([
                    'error' => [
                        'messages' => [
                            Message::INVALID_CLIENT_ID
                        ]
                    ]
                ], Response::STATUS_CODE_403);
        }
        $defaultUser->setRoles(array_map(function (RoleInterface $role) {
            return $role->getName();
        }, $user->getRoles()->getIterator()->getArrayCopy()));
        $request = $request->withAttribute(UserInterface::class, $defaultUser);
        $isGranted = false;
        foreach ($defaultUser->getRoles() as $role) {
            if ($this->authorization->isGranted($role, $request)) {
                $isGranted = true;
                break;
            }
        }
        if (!$isGranted) {
            return new JsonResponse([
                'error' => [
                    'messages' => [
                        Message::RESOURCE_NOT_ALLOWED
                    ]
                ]
            ], Response::STATUS_CODE_403);
        }
        return $handler->handle($request);
    }
public function process ( ServerRequestInterface $ request , RequestHandlerInterface $ handler ) : ResponseInterface { $ defaultUser = $ request -> getAttribute ( UserInterface :: class ) ; switch ( $ defaultUser -> getDetail ( 'oauth_client_id' ) ) { case 'admin' : $ user = $ this -> adminService -> findByIdentity ( $ defaultUser -> getIdentity ( ) ) ; if ( $ user -> getStatus ( ) !== Admin :: STATUS_ACTIVE ) { return new JsonResponse ( [ 'error' => [ 'messages' => [ Message :: ADMIN_NOT_ACTIVATED ] ] ] , Response :: STATUS_CODE_403 ) ; } $ request = $ request -> withAttribute ( Admin :: class , $ user ) ; break ; case 'frontend' : $ user = $ this -> userService -> findByIdentity ( $ defaultUser -> getIdentity ( ) ) ; if ( ! ( $ user instanceof User ) || $ user -> isDeleted ( ) ) { return new JsonResponse ( [ 'error' => [ 'messages' => [ sprintf ( Message :: USER_NOT_FOUND_BY_IDENTITY , $ defaultUser -> getIdentity ( ) ) ] ] ] , Response :: STATUS_CODE_403 ) ; } if ( $ user -> getStatus ( ) !== User :: STATUS_ACTIVE ) { return new JsonResponse ( [ 'error' => [ 'messages' => [ Message :: USER_NOT_ACTIVATED ] ] ] , Response :: STATUS_CODE_403 ) ; } $ request = $ request -> withAttribute ( User :: class , $ user ) ; break ; case 'guest' : $ user = new Guest ( ) ; $ request = $ request -> withAttribute ( Guest :: class , $ user ) ; break ; default : return new JsonResponse ( [ 'error' => [ 'messages' => [ Message :: INVALID_CLIENT_ID ] ] ] , Response :: STATUS_CODE_403 ) ; } $ defaultUser -> setRoles ( array_map ( function ( RoleInterface $ role ) { return $ role -> getName ( ) ; } , $ user -> getRoles ( ) -> getIterator ( ) -> getArrayCopy ( ) ) ) ; $ request = $ request -> withAttribute ( UserInterface :: class , $ defaultUser ) ; $ isGranted = false ; foreach ( $ defaultUser -> getRoles ( ) as $ role ) { if ( $ this -> authorization -> isGranted ( $ role , $ request ) ) { $ isGranted = true ; break ; } } if ( ! $ isGranted ) { return new JsonResponse ( [ 'error' => [ 'messages' => [ Message :: RESOURCE_NOT_ALLOWED ] ] ] , Response :: STATUS_CODE_403 ) ; } return $ handler -> handle ( $ request ) ; }
@param ServerRequestInterface $request
 @param RequestHandlerInterface $handler
 @return ResponseInterface
 @throws Throwable
@param ServerRequestInterface $request @param RequestHandlerInterface $handler @return ResponseInterface @throws Throwable
/** @var UserIdentity $defaultUser */