0

我一直在尝试让数据库登录系统与 Symfony2 一起工作。我已经阅读了文档,但我现在完全不知道该怎么做......

每次我尝试登录时,我都会得到“坏凭据”。

安全.yml

security:
    encoders:
        D\UserBundle\Entity\User:
            algorithm:       sha512
            encode_as_base64: false
            iterations:      1

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

providers:
    user_db:
        entity: { class: DUserBundle:User}

firewalls:
    secured_area:
        pattern:   ^/
        anonymous: ~
        form_login:
            login_path: /login
            check_path: /login_check
        logout:
            path:  /logout
            target:

access_control:
    - { path: ^/admin, roles: ROLE_ADMIN }
    - { path: ^/user, roles: ROLE_USER }

用户存储库.php

<?php

namespace D\UserBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;

class UserRepository extends EntityRepository implements UserProviderInterface
{
    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) {
            throw new UsernameNotFoundException(sprintf('Unable to find an active admin AcmeUserBundle:User object identified by "%s".', $username), null, 0, $e);
        }

        return $user;
    }

    public function refreshUser(UserInterface $user)
    {
        $class = get_class($user);
        if (!$this->supportsClass($class)) {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class));
        }

        return $this->loadUserByUsername($user->getUsername());
    }

    public function supportsClass($class)
    {
        return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
    }
}

用户.php

用户控制器.php

那是我的数据库,表(用户)

id:5
username:Pawel
salt:5633267d072cd3119af5270d64b4b45d
password:test
email:test@test.pl
is_active1
4

2 回答 2

0

你的 security.yml 应该是这样的

providers:
        user_db:
            entity: { class: DUserBundle:User, property: username }

哟根本不需要 UserRepository 类。

于 2012-11-20T21:51:23.393 回答
0

真的很晚很抱歉,但由于问题还没有回答......

我不确定您是否可以将实体的别名用作提供者的“类”值。

也许尝试使用完整的命名空间实体名称进行设置:

volunteers:
    entity:
        class: D\UserBundle\Entity\User

但我认为主要问题是您的密码应该 - 至少,并且可能必须 - 在您的数据库中加密。如果您想使用您在 security.yml 配置文件中定义的安全编码器,正确的方法(根据文档)是在您的注册表单验证后对其进行管理。

假设您有一个用户注册表单:

# Any routing file
d_userbundle_register:
    pattern: /register
    defaults:
        _controller: DUserBundle:Users:register
    requirements:
        _method: get

d_userbundle_register_check:
    pattern: /register_check
    defaults:
        _controller: DUserBundle:Users:registerCheck
    requirements:
        _method: post

和你的控制器的 registerCheckAction :

// UsersController.php
public function registerCheckAction()
{
    $request = $this->getRequest();

    $user = new D\UserBundle\Entity\User();
    $registrationForm = $this->createForm(new D\UserBundle\Form\UserType(), $user);

    $registrationForm->bind($request);

    if ($registrationForm->isValid()) {
        $em = $this->getDoctrine()->getManager(); // use getEntityManager() instead of getManager() for old symfony2 versions

        // Get the factory encoder service
        $encoderFactory = $this->get('security_encoder.factory');
        // Retrieve the algorithms, iterations and everything defined in your security.yml file
        $encoder = $encoderFactory->getEncoder($user);
        // Encrypt the password with the user's plaintext password AND the user salt
        $encryptedPassword = $encoder->encodePassword($user->getPassword(), $user->getSalt());
        // Then set the user's encrypted password :)
        $user->setPassword($encryptedPassword);

        $em->persist($user);
        $em->flush();

        // Thanks the user
        $request->getSession()->getFlashBag()->add('success', 'Heyoh, welcome on board !');
        return $this->redirect($this->generateUrl('d_userbundle_login'));
    }

    return $this->render('DUserBundle:User:registration.html.twig', array(
        'registration_form' => $registrationForm->createView()
    ));
}
于 2013-08-27T22:10:52.927 回答