src/BundleExtensions/Oauth2/OAuthStorage.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\BundleExtensions\Oauth2;
  3. use App\Entity\User;
  4. use FOS\OAuthServerBundle\Storage\OAuthStorage as OAuthStorageBase;
  5. use OAuth2\Model\IOAuth2Client;
  6. use OAuth2\OAuth2;
  7. use OAuth2\OAuth2ServerException;
  8. use Symfony\Component\HttpFoundation\Response;
  9. class OAuthStorage extends OAuthStorageBase
  10. {
  11.     public function checkUserCredentials(IOAuth2Client $client$username$password) {
  12.         $stored parent::checkUserCredentials($client$username$password);
  13.         if ($stored == false) return $stored;
  14.         /** @var User $user */
  15.         $user $stored["data"];
  16.         $allowedRoles explode(" "$client->getAllowedRoles());
  17.         $roles array_map(function($role){ return strtolower(str_replace("ROLE_"""$role)); }, $user->getRoles());
  18.         if(!count(array_intersect($allowedRoles$roles)))
  19.             throw new OAuth2ServerException(Response::HTTP_FORBIDDENOAuth2::ERROR_USER_DENIED);
  20.         return $stored;
  21.     }
  22. }