簡體   English   中英

如何在使用symfony2(和fosUserBundle)登錄時動態添加用戶角色?

[英]how to add user roles dynamically upon login with symfony2 (and fosUserBundle)?

在我的應用程序中,用戶可以隨着時間的推移在免費用戶和高級用戶之間切換,當他們的訂閱到期時,他們不再具有高級優先權。

我以為我可以削減一個角落,而不是將高級用戶角色存儲在數據庫中,只存儲他們已經支付的日期,從而消除了從我的用戶那里添加och刪除角色溢價的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
    }
}

請記住,默認情況下Sykfony中已經創建了InteractiveLoginEvent (正如您在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的登錄重定向 :這篇文章將為您提供一個關於聽眾如何工作以及如何在登錄時簡單實現它們的良好示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM