2012-11-19 25 views
1

我正在將一個相當大的社區遷移到symfony2。當前用戶表中包含許多用戶名和非字母數字字符。在新版本中,我只允許[a-zA-Z0-9-]爲每個用戶提供語義URL等好處。如何捕捉沒有用戶名的用戶?

是否有可能通過電子郵件/密碼登錄並且沒有設置用戶名?我希望他們重定向到可以重新選擇用戶名的頁面。棘手的部分:除非他們有正確的用戶名,否則他們不應該觸摸網站上的任何內容。

我想過一個事件,從fosuserbundle,但我找不到合適的。

回答

0

只是一個想法。 AOP範例(JMSAopBundle)如何?定義你的控制器一個切入點(除了登錄一個):

class PrivateEntityInformationPointcut implements PointcutInterface 
{ 

    public function matchesClass(\ReflectionClass $class) 
    { 
     return $class->isSubclassOf('Your\Controller\Superclass') 
      && $class->name !== 'Your\Controller\Access'; 
    } 

    public function matchesMethod(\ReflectionMethod $method) 
    { 
     return true; // Any method 
    } 

} 

然後攔截器應該重定向到頁面設置的用戶名:

class DenyEntityAccessInterceptor implements MethodInterceptorInterface 
{ 

    private $securityContext; 

    private $logger; 

    /** 
    * @DI\InjectParams({ 
    *  "securityContext" = @DI\Inject("security.context"), 
    *  "logger"   = @DI\Inject("logger"), 
    * }) 
    */ 
    public function __construct(SecurityContext $securityContext, 
     Logger $logger) 
    { 
     $this->securityContext = $securityContext; 
     $this->logger   = $logger; 
    } 

    public function intercept(MethodInvocation $invocation) 
    { 
     // Check username, redirect using the router, log what's happening 

     // It's OK 
     return $invocation->proceed(); 
    } 

} 
相關問題