我正在开发一个基于 Symfony 2 PHP 框架的网络应用程序。
它有一个注册用户的登录页面。我想为每个登录系统的用户执行一些自定义逻辑。
基本上,我想在任何用户登录系统时记录,但我不想在主页的 Controller 上执行此操作,因为每次用户重新加载主页时它都会记录。
我还想实现一个在用户登录系统时调用的函数,这样我就可以决定是否授予任何用户访问权限(基于存储在用户数据库中的全套信息)。
我怎样才能做到这一点?
最佳答案
对于您问题的第一部分,我有类似的东西(例如,存储用户登录的最后日期和时间)。我沿着事件触发的服务路线走下去。在您的服务配置中(此处为 XML 示例):
<services>
<service id="my.login.listener" class="My\OwnBundle\Event\LoginEventListener">
<tag name="kernel.event_listener" event="security.interactive_login" />
</service>
</services>
然后在您的包中的适当位置创建上述类:
namespace My\OwnBundle\Event;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use My\OwnBundle\User\User as MyUser;
class LoginEventListener
{
/**
* Catches the login of a user and does something with it
*
* @param \Symfony\Component\Security\Http\Event\InteractiveLoginEvent $event
* @return void
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$token = $event->getAuthenticationToken();
if ($token && $token->getUser() instanceof MyUser)
{
// You can do something here eg
// record the date & time of the user's login
}
}
}
我想您可以将其扩展到问题的第二部分,但是我还没有这样做 :-)
关于php - 基于 Symfony 2 的项目的安全性和登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8144446/