使用Symfony 2.1的最新版本,应该可以使用MongoDB作为SecurityBundle的Userprovider而不使用FOSUserBundle(如此处介绍:mongodb symfony user authentication?) .
可以't figure out, where is actually the problem in the code, but I can' t使用预定义的用户 test:test
登录 .
我的 security.yml
看起来像这样:
security:
encoders:
test\TestBundle\Document\User: plaintext
providers:
document_members:
mongodb: { class: testTestBundle:User, property: username }
firewalls:
secured_area:
pattern: ^/
http_basic:
realm: "Login to TEST"
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
role_hierarchy:
ROLE_ADMIN: ROLE_USER
test/TestBundle/Document/User.php
-Document:
namespace test\TestBundle\Document;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* @ODM\Document(collection="user", repositoryClass="test\TestBundle\Document\UserRepository")
*/
class User implements UserInterface, EquatableInterface
{
/**
* @ODM\Id
*/
protected $id;
/**
* @ODM\String
*/
protected $username;
/**
* @ODM\String
*/
protected $password;
/**
* @ODM\Collection
*/
protected $roles = array();
/**
* @ODM\String
*/
protected $salt;
/**
* @ODM\Boolean
*/
protected $isActive;
// Setter
/**
* @param String
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* @param String
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* @param String
*/
public function setRole($role)
{
$this->roles[] = $role;
}
/**
* @param array
*/
public function setRoles(array $roles)
{
$this->roles = (array) $roles;
}
/**
* @param String
*/
public function setSalt($salt)
{
$this->salt = $salt;
}
// Getter
/**
* @return String
*/
public function getUsername()
{
return $this->username;
}
/**
* @return String
*/
public function getPassword()
{
return $this->password;
}
/**
* @return array
*/
public function getRoles()
{
return $this->roles;
}
/**
* @return String
*/
public function getSalt()
{
return $this->salt;
}
// General
public function __construct()
{
$this->isActive = true;
$this->salt = '';
}
public function isEqualTo(UserInterface $user)
{
return $user->getUsername() === $this->username;
}
public function eraseCredentials()
{
}
}
test/TestBundle/Document/UserRepository.php
:
namespace test\TestBundle\Document;
use Doctrine\ODM\MongoDB\DocumentRepository;
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;
class UserRepository extends DocumentRepository implements UserProviderInterface
{
public function loadUserByUsername($username)
{
$q = $this->createQueryBuilder()
->field('username')->equals((string) $username)
->getQuery();
try
{
$user = $q->getSingleResult();
}
catch (NoResultException $e)
{
throw new UsernameNotFoundException(sprintf('Can\'t find Username "%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 $class === 'test\TestBundle\Document\User';
}
}
具体 route
:
Admin:
pattern: /admin
defaults: { _controller: testTestBundle:Test:index }
(将导致现有的控制器和视图)
预定义的 user
-Document如下所示:
Array
(
[_id] => 4f59b5731c911ab41e001234
[username] => test
[password] => test
[roles] => Array
(
[0] => ROLE_ADMIN
)
[salt] =>
[isActive] => 1
)
但我无法使用用户名 test
和密码 test
登录 /admin
.
1 回答
问题与在apache fastCGI上使用symfony的问题有关(https://github.com/symfony/symfony/pull/3551) .
上面的代码按预期工作 .