在我的应用程序中,用户可以随着时间的推移在免费用户和高级用户之间切换,当他们的订阅到期时,他们不再拥有高级权限。
我想我可以走捷径,不将高级用户角色存储在数据库中,只存储他们付款的日期,从而消除了添加或删除用户的高级角色的 cron 作业的需要。
我想到的解决方案是在用户实体上执行此操作:
public function __construct()
{
if ( $this->hasPlus() )
{
$this->addRole('ROLE_PLUSUSER');
}
}
其中 hasPlus 是一个函数,用于将当前日期与付款日期进行比较,如果用户仍然付款则返回 true。
现在,这不起作用,所以我想也许有人可以为我阐明这一点 - 我知道角色是在登录时添加的,如果我在登录后添加角色,我需要注销并重新登录才能使用效果,但在这里我试图在构造我的用户对象时添加角色,但它仍然不起作用......
按照下面的优秀答案添加了 eventListener,仍然无法将角色添加到用户:
<?php
namespace Hemekonomi\UserBundle\EventListener;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Session;
class SecurityListener
{
protected $security;
protected $session;
/**
* Constructs a new instance of SecurityListener.
*
* @param SecurityContext $security The security context
* @param Session $session The session
*/
public function __construct(SecurityContext $security, Session $session)
{
//You can bring whatever you need here, but for a start this should be useful to you
$this->security = $security;
$this->session = $session;
}
/**
* Invoked after a successful login.
*
* @param InteractiveLoginEvent $event The event
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
//Your logic needs to go here
//You can addRole
//Even persist if you want but bring the right tools to your constructor
$security = $this->security;
if ($security->getToken()->getUser()->hasPlus()) {
$security->getToken()->getUser()->addRole('ROLE_PLUSUSER');
}
}
}
你的逻辑不会在user
实体...
如果您想在登录时实现,请使用Event Listeners
,这就是为什么它们如此有用:-)
您需要创建一个对事件做出反应的监听器InteractiveLoginEvent
,像这样:
1/ 创建监听器
<?php
namespace Acme\YourBundle\EventListener;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Session\Session;
class SecurityListener
{
protected $security;
protected $session;
/**
* Constructs a new instance of SecurityListener.
*
* @param SecurityContext $security The security context
* @param Session $session The session
*/
public function __construct(SecurityContext $security, Session $session)
{
//You can bring whatever you need here, but for a start this should be useful to you
$this->security = $security;
$this->session = $session;
}
/**
* Invoked after a successful login.
*
* @param InteractiveLoginEvent $event The event
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
//Your logic needs to go here
//You can addRole
//Even persist if you want but bring the right tools to your constructor
}
}
请记住交互式登录事件已经在 Symfony 中默认创建了(正如你可以在use
声明)所以现在没什么可做的:
2/ 将此侦听器声明为服务:
services:
acme_your_bundle.listener.login:
class: Acme\YourBundle\EventListener\SecurityListener
arguments: [@security.context, @session]
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }
3/如果需要,请检查文档
事件调度程序组件
如何创建事件监听器
登录重定向,作者:Dustin Dobervich:这篇文章将为您提供一个很好的示例,说明侦听器如何工作以及如何在登录时简单地实现它们。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)