1
有關堆棧的問題很多,但說實話 - 沒有人爲我工作(我正在使用Symfony 3.3和FOSUserBundle 2.x)。註冊後禁用用戶,並允許管理員確認
我需要什麼才達到:
- 用戶註冊成功,在我的網頁
- 他的賬戶被自動設置爲默認值(不發送電子郵件)
- 管理員可以激活/啓用該帳戶從他管理面板
我已經創建了一個事件監聽,但它不會工作..我甚至無法處理用戶對象改變enabled
狀態...
#config.yml
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: AppBundle\Entity\User
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
registration:
form:
type: AppBundle\Form\RegistrationType
和事件監聽器:
class RegistrationListener implements EventSubscriberInterface
{
/**
* @var RouterInterface
*/
private $router;
private $em;
public function __construct(RouterInterface $route, UserManagerInterface $em)
{
$this->router = $route;
$this->em = $em;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
FOSUserEvents::REGISTRATION_CONFIRMED=> ['onRegistrationConfirmed', 999],
FOSUserEvents::REGISTRATION_FAILURE => 'onRegistrationFailure'
);
}
public function onRegistrationSuccess(FormEvent $event)
{
$url = $this->router->generate('app.registration.succes');
$response = new JsonResponse(['redirectUrl' => $url]);
$event->setResponse($response);
}
public function onRegistrationFailure(FormEvent $event)
{
$array = array('success' => false, 'message' => (string)$event->getForm()->getErrors(true, true));
$response = new JsonResponse($array, Response::HTTP_BAD_REQUEST);
$event->setResponse($response);
}
public function onRegistrationConfirmed(FilterUserResponseEvent $event)
{
/**
* this one is no working.. after registration account is enabled anyway...
*/
$user = $event->getUser();
$user->setEnabled(false);
$user->setSalt('test');
$this->em->updateUser($user);
}
}