Я использую hwi / oauth-bundle 0.6 в моем приложении Symfony 3.4, я только что обновил свои пакеты, используя composer, потому что проект все еще на 3.2.
Теперь я получил эту ошибку: Attempted to load class "OAuthFailureHandler" from namespace "AppBundle\Security\Handler".
Did you forget a "use" statement for another namespace?
Вот мой security.yaml
security:
providers:
user_db:
entity: { class: AppBundle:User, property: username }
administrators:
entity: { class: AppBundle:User, property: username }
encoders:
Symfony\Component\Security\Core\User\User:
algorithm: bcrypt
cost: 12
AppBundle\Entity\User: bcrypt
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
http_basic: ~
anonymous: ~
form_login:
login_path: login
check_path: login
logout:
csrf_parameter: _csrf_token
csrf_token_generator: security.csrf.token_manager
csrf_token_id: logout
path: /logout
target: /
success_handler: ~
invalidate_session: true
delete_cookies:
name:
path: null
domain: null
handlers: []
provider: user_db
remember_me:
secret: "%secret%"
lifetime: 31536000
path: /
domain: ~
oauth:
resource_owners:
google: "/connect/check-google"
facebook: "/connect/check-facebook"
login_path: /login
failure_path: /login
oauth_user_provider:
service: app.fos_user.oauth_provider
failure_handler: app.oauthfailurehandler
admin:
pattern: ^/admin
http_basic: ~
role_hierarchy:
ROLE_ADMIN: [ROLE_MODERATOR, ROLE_SUPPORT, ROLE_USER]
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_MODERATOR, ROLE_SUPPORT, ROLE_ALLOWED_TO_SWITCH]
ROLE_MODERATOR: [ROLE_SUPPORT, ROLE_USER]
ROLE_SUPPORT: [ROLE_USER]
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_SUPPORT }
- { path: ^/profilo, roles: [ROLE_USER, ROLE_ADMIN] }
А вот services.yaml
services:
app.fos_user.oauth_provider:
class: AppBundle\Security\Core\User\FOSUBUserProvider
arguments: ['@fos_user.user_manager',{facebook: 'facebook_id', google: 'google_id'},'@mailer','@service_container']
app.oauthfailurehandler:
class: AppBundle\Security\Handler\OAuthFailureHandler
arguments: ["@security.http_utils", {}, "@service_container"]
_defaults:
autowire: true
autoconfigure: true
public: false
Я все еще получаю эту ошибку, даже если я пытался обновить все пакеты ископируйте и вставьте параметры из документации HWIOAuth.
Вот OAuthFailureHandler.php
<?php
namespace AppBundle\Security\Handler;
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
class OAuthFailureHandler implements AuthenticationFailureHandlerInterface {
public function onAuthenticationFailure( Request $request, AuthenticationException $exception) {
if ( !$exception instanceof AccountNotLinkedException ) {
throw $exception;
}
return new RedirectResponse( '/registrati' );
}
}
Вот FOSUBUserProvider.php
<?php
namespace AppBundle\Security\Core\User;
use AppBundle\Entity\User;
use FOS\UserBundle\Model\UserManagerInterface;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\User\UserInterface;
class FOSUBUserProvider extends BaseClass {
private $session;
public function __construct(Session $session, UserManagerInterface $userManager, array $properties)
{
$this->session = $session;
parent::__construct($userManager, $properties);
}
public function connect(UserInterface $user, UserResponseInterface $response) {
$property = $this->getProperty($response);
$username = $response->getUsername();
// On connect, retrieve the access token and the user id
$service = $response->getResourceOwner()->getName();
$setter = 'set' . ucfirst($service);
$setter_id = $setter . 'Id';
$setter_token = $setter . 'AccessToken';
// Disconnect previously connected users
if (null !== $previousUser = $this->userManager->findUserBy(array($property => $username))) {
$previousUser->$setter_id(null);
$previousUser->$setter_token(null);
$this->userManager->updateUser($previousUser);
}
// Connect using the current user
$user->$setter_id($username);
$user->$setter_token($response->getAccessToken());
$this->userManager->updateUser($user);
}
public function loadUserByOAuthUserResponse(UserResponseInterface $response) {
$username = $response->getUsername();
$email = $response->getEmail() ? $response->getEmail() : $username;
$realname = $response->getRealName();
/** @var User $user */
$user = $this->userManager->findUserByUsernameOrEmail($email);
$service = $response->getResourceOwner()->getName();
$setter = 'set' . ucfirst($service);
$setter_id = $setter . 'Id';
$setter_token = $setter . 'AccessToken';
// If the user is new
if (null === $user) {
$this->session->set( 'oauth.resource', $response->getResourceOwner()->getName() );
$this->session->set( 'oauth.email', $email );
$this->session->set( 'oauth.realname', $realname );
throw new AccountNotLinkedException();
}
$user->$setter_id($username);
$user->$setter_token($response->getAccessToken());
return $user;
}
/**
* Generates a random username with the given
* e.g 12345_github, 12345_facebook
*
* @param string $username
* @param type $serviceName
* @return type
*/
private function generateRandomUsername($username, $serviceName){
if(!$username){
$username = "user". uniqid((rand()), true) . $serviceName;
}
return $username. "_" . $serviceName;
}
}