尝试从命名空间“ AppBundle \ Security \ Handler”中加载类“ OAuthFailureHandler”

时间:2019-02-06 21:43:43

标签: symfony oauth symfony-3.4

我在Symfony 3.4应用程序中使用的是hwi / oauth-bundle 0.6,我刚刚使用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;
    }
}

0 个答案:

没有答案