我正在将一个相当大的社区迁移到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();
}
}