Forms Symfony2 FOSUserBundle&x2013;根据“确认”;“用户活动”;登录时标记
我的用户上有一个“活动”的标志,如果设置为零或null,我将不允许登录 我试过几种方法,但都失败了 如果我执行注销路由,则不会保留闪存消息,因此用户看不到任何内容 我研究了在登录表单上添加一个验证,这样如果该标志没有设置为true,它就会抛出一个正常的表单错误,但是在该文件夹(vendor/Bundles/FOS/UserBundle/form/Type)中,我找不到登录表单的任何内容,只有注册之类的内容,所以我不知道将其放在哪里或从哪里继承以覆盖 我也按照这里的建议尝试手动注销,但这给我留下了一个白色的死亡屏幕 有什么建议可以轻松做到这一点吗 **************更新************** 我意识到我可能想在登录表单上添加一个验证器。目前,我已将其编码到用户发送到的第一条路由的控制器中,但如果用户在登录前键入路由,则不会提供太多安全性,因为在成功登录尝试中,登录后我的默认“登录页”将不是用户被带到的路由,但他将在他选择的路由上登录 ***再次更新*** 所以服务配置文件有以下内容Forms Symfony2 FOSUserBundle&x2013;根据“确认”;“用户活动”;登录时标记,forms,flash,symfony,logout,fosuserbundle,Forms,Flash,Symfony,Logout,Fosuserbundle,我的用户上有一个“活动”的标志,如果设置为零或null,我将不允许登录 我试过几种方法,但都失败了 如果我执行注销路由,则不会保留闪存消息,因此用户看不到任何内容 我研究了在登录表单上添加一个验证,这样如果该标志没有设置为true,它就会抛出一个正常的表单错误,但是在该文件夹(vendor/Bundles/FOS/UserBundle/form/Type)中,我找不到登录表单的任何内容,只有注册之类的内容,所以我不知道将其放在哪里或从哪里继承以覆盖 我也按照这里的建议尝试手动注销,但这给我留下了
<service id="security.user_checker" class="%security.user_checker.class%" public="false" />
现在,我在symfony app/config中的我自己的parameters.ini中覆盖了上面的参数,如下所示
security.user_checker.class = BizTV\UserBundle\Controller\UserChecker
。。并将此检查添加到我的userChecker重写器
//Test for companylock...
if ( !$user->getCompany()->getActive() ) {
throw new LockedException('The company of this user is locked.', $user);
}
以下是整个文件:
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//Override by Mattias
namespace BizTV\UserBundle\Controller;
//namespace Symfony\Component\Security\Core\User;
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
use Symfony\Component\Security\Core\Exception\LockedException;
use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserChecker as OriginalUserChecker;
/**
* UserChecker checks the user account flags.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class UserChecker extends OriginalUserChecker
{
/**
* {@inheritdoc}
*/
public function checkPreAuth(UserInterface $user)
{
//Test for companylock...
if ( !$user->getCompany()->getActive() ) {
throw new LockedException('The company of this user is locked.', $user);
}
if (!$user instanceof AdvancedUserInterface) {
return;
}
if (!$user->isCredentialsNonExpired()) {
throw new CredentialsExpiredException('User credentials have expired.', $user);
}
}
/**
* {@inheritdoc}
*/
public function checkPostAuth(UserInterface $user)
{
//Test for companylock...
if ( !$user->getCompany()->getActive() ) {
throw new LockedException('The company of this user is locked.', $user);
}
if (!$user instanceof AdvancedUserInterface) {
return;
}
if (!$user->isAccountNonLocked()) {
throw new LockedException('User account is locked.', $user);
}
if (!$user->isEnabled()) {
throw new DisabledException('User account is disabled.', $user);
}
if (!$user->isAccountNonExpired()) {
throw new AccountExpiredException('User account has expired.', $user);
}
}
}
…我得到这个错误:
Parse error: syntax error, unexpected T_EXTENDS, expecting '{' in /var/www/cloudsign/src/BizTV/UserBundle/Entity/User.php on line 18
FOSUserBundle/Symfony已经集成了某种“活动”标志 已经提供了基本上用于此目的的属性“锁定”和“启用”。这两个属性之间的区别如下(引用@stof的评论) 从安全组件的角度来看,没有真正的 区别:两者都禁止登录。区别在于 语义一:禁用用户通常是需要 激活他们的帐户(例如,当您激活 确认FOSUserBundle中的电子邮件,用户在创建时被禁用 并在确认时启用)。另一方面,锁定用户是非常困难的 通常是由站点管理员执行的禁止用户的操作。 在数据库中使用相同的字段没有意义 允许被禁止的用户只需通过 确认过程 对锁定/禁用用户的检查由FOSUserBundle中的一个(symfony提供此项作为@security.user\u checker)执行,它实现了 现在,为了将非活动用户重定向到不同的路由,您需要:
Symfony\Component\Security\Core\Exception\DisabledException
(可选)将重定向移动到新创建的EventListener/-Subscriber,该订阅服务器将在扩展AuthenticationListener中调度。通过这种方式,您可以稍后创建其他侦听器,即出于日志记录目的,只需将其订阅到非活动用户登录尝试事件。那么fos用户包是否使用symfony2 core中的userChecker?我这样问是因为我正在考虑向checkPostAuth函数添加一个检查(我的用户是可以锁定的组的一部分,而该组又应该锁定组中的所有用户,而无需对用户实体设置单独的锁定)。所以$user->getGroup()->getActive()也是我想要放进去的。但是我需要找到如何覆盖该用户检查器…是的,FOSUserBundle使用symfony的用户检查器-如果我没记错的话,您可以通过覆盖服务
@security.user\u checker
来使用您自己的用户检查器。只需在供应商目录中对“UserChecker”进行文件内文本搜索,就会找到一个包含相应服务定义的xml文件。然后覆盖这项服务。我马上就到了。。。你能看看我更新过的问题吗,也许你知道我错过了什么来绕过那些错误信息=)好了,没有错误信息了,但我认为我没有做正确的覆盖,因为它不会咬人。在它当前的形式中,它不应该允许anyonw登录,因为我有一个if 1==1抛出错误。。。我想。你的用户
实体必须实现AdvancedUserInterface
,否则你将永远无法到达1==1
,因为检查程序中有一个提前返回。在if语句之前尝试使用一个简单的die('whatever')
,或者(更好)。。。设置断点并使用xdebug或类似工具进行调试。
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//Override by Mattias
namespace BizTV\UserBundle\Controller;
//namespace Symfony\Component\Security\Core\User;
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
use Symfony\Component\Security\Core\Exception\LockedException;
use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserChecker as OriginalUserChecker;
/**
* UserChecker checks the user account flags.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class UserChecker extends OriginalUserChecker
{
/**
* {@inheritdoc}
*/
public function checkPreAuth(UserInterface $user)
{
//Test for companylock...
if ( !$user->getCompany()->getActive() ) {
throw new LockedException('The company of this user is locked.', $user);
}
if (!$user instanceof AdvancedUserInterface) {
return;
}
if (!$user->isCredentialsNonExpired()) {
throw new CredentialsExpiredException('User credentials have expired.', $user);
}
}
/**
* {@inheritdoc}
*/
public function checkPostAuth(UserInterface $user)
{
//Test for companylock...
if ( !$user->getCompany()->getActive() ) {
throw new LockedException('The company of this user is locked.', $user);
}
if (!$user instanceof AdvancedUserInterface) {
return;
}
if (!$user->isAccountNonLocked()) {
throw new LockedException('User account is locked.', $user);
}
if (!$user->isEnabled()) {
throw new DisabledException('User account is disabled.', $user);
}
if (!$user->isAccountNonExpired()) {
throw new AccountExpiredException('User account has expired.', $user);
}
}
}
<?php
// src/BizTV/UserBundle/Entity/User.php
namespace BizTV\UserBundle\Entity;
use BizTV\UserBundle\Validator\Constraints as BizTVAssert;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use BizTV\BackendBundle\Entity\company as company;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser implements AdvancedUserInterface
{
class User implements AdvancedUserInterface extends BaseUser
Parse error: syntax error, unexpected T_EXTENDS, expecting '{' in /var/www/cloudsign/src/BizTV/UserBundle/Entity/User.php on line 18