我正在將一個相當大的社區遷移到symfony2。當前用戶表中包含許多用戶名和非字母數字字符。在新版本中,我只允許[a-zA-Z0-9-]爲每個用戶提供語義URL等好處。如何捕捉沒有用戶名的用戶?
是否有可能通過電子郵件/密碼登錄並且沒有設置用戶名?我希望他們重定向到可以重新選擇用戶名的頁面。棘手的部分:除非他們有正確的用戶名,否則他們不應該觸摸網站上的任何內容。
我想過一個事件,從fosuserbundle,但我找不到合適的。
我正在將一個相當大的社區遷移到symfony2。當前用戶表中包含許多用戶名和非字母數字字符。在新版本中,我只允許[a-zA-Z0-9-]爲每個用戶提供語義URL等好處。如何捕捉沒有用戶名的用戶?
是否有可能通過電子郵件/密碼登錄並且沒有設置用戶名?我希望他們重定向到可以重新選擇用戶名的頁面。棘手的部分:除非他們有正確的用戶名,否則他們不應該觸摸網站上的任何內容。
我想過一個事件,從fosuserbundle,但我找不到合適的。
你可以使用事件。在此處看到一個示例:http://symfony.com/doc/2.0/cookbook/event_dispatcher/before_after_filters.html
當然,更改用戶名的操作應該被事件偵聽器忽略。就像登錄和其他匿名行爲一樣。
您可以通過設置事件響應來返回任何響應,包括重定向。
只是一個想法。 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();
}
}