Skip to content

Instantly share code, notes, and snippets.

@andrechavesg
Last active February 4, 2019 21:15
Show Gist options
  • Save andrechavesg/c8803378868f80d58f0586dd7284b8bf to your computer and use it in GitHub Desktop.
Save andrechavesg/c8803378868f80d58f0586dd7284b8bf to your computer and use it in GitHub Desktop.
<?php
namespace App\Security;
use App\Entity\Usuario;
use App\Repository\UserRepository;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
/**
* OAuthUserProvider.
*
*/
class OAuthUserProvider implements UserProviderInterface, OAuthAwareUserProviderInterface
{
/**
* @var UserRepository
*/
private $usuarioRepository;
/**
* @var AuthenticationManagerInterface
*/
private $authenticationManager;
public function __construct(UserRepository $usuarioRepository, AuthenticationManagerInterface $authenticationManager)
{
$this->usuarioRepository = $usuarioRepository;
$this->authenticationManager = $authenticationManager;
}
/**
* {@inheritdoc}
*/
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
try {
$email = $response->getEmail();
$user = $this->loadUserByUsername($email);
if (empty($user)) {
$user = (new Usuario());
if (!empty($email))
$user->setLogin($email);
$user->setSenha($response->getLastName());
$user->setPermissoes(["ROLE_ATENDENTE"]);
$this->usuarioRepository->persist($user);
}
return $user;
} catch (\Throwable $throwable) {
return null;
}
}
/**
* {@inheritdoc}
*/
public function loadUserByUsername($username)
{
return $this->usuarioRepository->findOneBy(["login" => $username]);
}
/**
* {@inheritdoc}
*/
public function refreshUser(UserInterface $user)
{
if (!$this->supportsClass(get_class($user))) {
throw new UnsupportedUserException(sprintf('Unsupported user class "%s"', get_class($user)));
}
return $this->loadUserByUsername($user->getUsername());
}
/**
* {@inheritdoc}
*/
public function supportsClass($class)
{
return $class === Usuario::class;
}
}
security:
encoders:
App\Entity\User:
algorithm: bcrypt
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\Usuario
property: login
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
guard:
authenticators:
- App\Security\LoginFormAuthenticator
oauth:
resource_owners:
facebook: "/connect/service/facebook"
login_path: /oauth/login
use_forward: false
failure_path: /login
oauth_user_provider:
service: oauth_user_provider
# activate different ways to authenticate
# http_basic: true
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: true
# https://symfony.com/doc/current/security/form_login_setup.html
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment