从用户提供商处获取错误

时间:2014-06-26 11:16:03

标签: php symfony doctrine

我正在使用symfony框架。我创建了两个登录表单,并希望根据他们访问的表单进行身份验证。这两种形式与两个不同的数据库交互。但是,我收到错误“Unknown Entity namespace alias'ESSUserBundle'”。当我将ESSUserBundle添加到默认实体管理器时,它可以工作。但是,此捆绑包不应位于默认实体管理器中。如何解决这个问题?

Security.yml

security:
    encoders:
        ESS\UserBundle\Entity\User: 
            algorithm: sha512
            encode-as-base64: true
            iterations: 1

        ESS\AdminBundle\Entity\AdminUser:
            algorithm: sha512
            encode-as-base64: true
            iterations: 1

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        admin_area:
            name: admin_security
            entity: {class: ESSAdminBundle:AdminUser, property: username }

        company_area:
            name: company_security
            entity: { class: ESSUserBundle:User, property: username }

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        admin_area:
            pattern:    ^/admin
            provider: admin_security
            anonymous: ~
            form_login:
                login_path:  /admin
                check_path:  /admin/login_check
                default_target_path: /admin/home
                failure_path: /admin

            logout:
                path:   /admin/logout
                target: /admin  

        company_area:
            pattern:    ^/login
            anonymous: ~
            provider: company_security
            form_login:
                login_path:  /login
                check_path: login_check
                default_target_path: /main/home
                failure_path: /login

            logout:
                path:   /main/logout
                target: /login  

Config.yml

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   "%database_driver%"
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
            customer:
                driver:   "%database_driver2%"
                host:     "%database_host2%"
                port:     "%database_port2%"
                dbname:   "%database_name2%"
                user:     "%database_user2%"
                password: "%database_password2%"
                charset:  UTF8

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    ESSAdminBundle:  ~
                    #ESSRoleBundle: ~
            customer:
                connection: customer
                mappings:
                    ESSUserBundle: ~

更新

UserRepository.php

public function loadUserByUsername($username)
    {
        $q = $this
            ->createQueryBuilder('u')
            ->where('u.username = :username OR u.email = :email')
            ->setParameter('username', $username)
            ->setParameter('email', $username)
            ->getQuery();

        try {
            // The Query::getSingleResult() method throws an exception
            // if there is no record matching the criteria.
            $user = $q->getSingleResult();
        } catch (NoResultException $e) {
            $message = sprintf(
                'Unable to find an active admin AcmeUserBundle:User object identified by "%s".',
                $username
            );
            throw new UsernameNotFoundException($message, 0, $e);
        }

        return $user;
    }

1 个答案:

答案 0 :(得分:0)

您正在使用两个提供商。我认为您必须将您的提供商链接起来:

security:
    providers:
        chain_provider:
            chain:
                providers: [admin_area, company_area]

请阅读使用多个用户提供商的部分下的文档: http://symfony.com/doc/current/book/security.html