2015-03-02 21:57:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Trinity\Bundle\UserBundle\Controller;
|
|
|
|
|
2015-05-04 19:34:46 +02:00
|
|
|
use FOS\UserBundle\Propel\User;
|
2015-03-02 21:57:49 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
use Trinity\Bundle\UserBundle\Configuration\UserCrudConfiguration as CrudConfiguration;
|
|
|
|
use Trinity\Bundle\AdminBundle\Controller\BaseAdminController;
|
|
|
|
use Trinity\Bundle\UserBundle\Flash\FlashError;
|
|
|
|
use Trinity\Bundle\UserBundle\Flash\FlashSuccess;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/admin")
|
|
|
|
*/
|
|
|
|
class UserAdminController extends BaseAdminController
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->configuration = new CrudConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/user/{page}", name="FOSUserBundleUserAdmin_index", defaults={"page" = "1"}, requirements={"page" = "\d+"})
|
|
|
|
* @Template()
|
|
|
|
*/
|
|
|
|
public function indexAction($page, Request $request)
|
|
|
|
{
|
|
|
|
return parent::indexAction($page, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/user/new", name="FOSUserBundleUserAdmin_new")
|
|
|
|
* @Template()
|
|
|
|
*/
|
|
|
|
public function newAction(Request $request)
|
|
|
|
{
|
2015-03-09 23:29:12 +01:00
|
|
|
$this->getConfiguration()->setFormNewOptions(array('roles' => $this->container->getParameter('trinity_user.roles')));
|
|
|
|
$this->getConfiguration()->setFormFilterOptions(array('roles' => $this->container->getParameter('trinity_user.roles')));
|
2015-03-02 21:57:49 +01:00
|
|
|
|
|
|
|
return parent::newAction($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/user/edit/{id}", name="FOSUserBundleUserAdmin_edit")
|
|
|
|
* @Template()
|
|
|
|
* @ParamConverter("object", class="FOS\UserBundle\Propel\User")
|
|
|
|
*/
|
|
|
|
public function editAction($object, Request $request)
|
|
|
|
{
|
2015-03-09 23:29:12 +01:00
|
|
|
$this->getConfiguration()->setFormEditOptions(array('roles' => $this->container->getParameter('trinity_user.roles')));
|
|
|
|
$this->getConfiguration()->setFormFilterOptions(array('roles' => $this->container->getParameter('trinity_user.roles')));
|
2015-03-02 21:57:49 +01:00
|
|
|
|
|
|
|
return parent::editAction($object, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/user/remove/{id}/{token}", name="FOSUserBundleUserAdmin_remove")
|
|
|
|
* @Template()
|
|
|
|
* @ParamConverter("object", class="FOS\UserBundle\Propel\User")
|
|
|
|
*/
|
|
|
|
public function removeAction($object, $token, Request $request)
|
|
|
|
{
|
|
|
|
return parent::removeAction($object, $token, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/user/batch", name="FOSUserBundleUserAdmin_batch")
|
|
|
|
* @Template()
|
|
|
|
* @Method({"POST"})
|
|
|
|
*/
|
|
|
|
public function batchAction(Request $request)
|
|
|
|
{
|
|
|
|
return parent::batchAction($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/user/filter/clear", name="FOSUserBundleUserAdmin_filter_clear")
|
|
|
|
* @Template()
|
|
|
|
*/
|
|
|
|
public function clearFilterAction(Request $request)
|
|
|
|
{
|
|
|
|
return parent::clearFilterAction($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/user/generate_password/{id}/{token}", name="TrinityDashBoardBundle_generate_password")
|
|
|
|
* @Template()
|
|
|
|
* @ParamConverter("object", class="FOS\UserBundle\Propel\User", options={"id" = "id"})
|
|
|
|
*/
|
|
|
|
public function generatePasswordAction($object, $token, Request $request)
|
|
|
|
{
|
2015-05-04 19:34:46 +02:00
|
|
|
/** @var User $object */
|
2015-03-02 21:57:49 +01:00
|
|
|
$redirect = $this->redirect($this->generateUrl('FOSUserBundleUserAdmin_index'));
|
|
|
|
|
|
|
|
if ($this->getRemoveToken() !== $token) {
|
|
|
|
$this->getSessionUser()->setFlash(
|
|
|
|
'message',
|
|
|
|
new FlashError('crud.flash.token'), false,
|
|
|
|
$this->getConfiguration()->getStorageNamespace()
|
|
|
|
);
|
|
|
|
|
|
|
|
return $redirect;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_password = substr(sha1(uniqid() + mt_rand() + time()), 0, 15);
|
|
|
|
|
2015-05-04 19:34:46 +02:00
|
|
|
$object->setPlainPassword($new_password);
|
|
|
|
|
|
|
|
if($this->container->getParameter('trinity_user.active_when_generate') === true){
|
|
|
|
$object->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->get('fos_user.user_manager')->updateUser($object);
|
2015-03-02 21:57:49 +01:00
|
|
|
|
|
|
|
$this->sendGeneratedPassword($object, $new_password);
|
|
|
|
|
|
|
|
$this->getSessionUser()->setFlash(
|
|
|
|
'message',
|
|
|
|
new FlashSuccess('crud.user.new_password'),
|
|
|
|
false,
|
|
|
|
$this->getConfiguration()->getStorageNamespace()
|
|
|
|
);
|
|
|
|
|
|
|
|
return $redirect;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function sendGeneratedPassword($user, $password)
|
|
|
|
{
|
|
|
|
$body = $this->get('templating')->render(
|
|
|
|
'TrinityUserBundle:UserAdmin:generatePasswordEmail.html.twig',
|
|
|
|
array(
|
|
|
|
'user' => $user,
|
|
|
|
'password' => $password,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$message =
|
|
|
|
\Swift_Message::newInstance()
|
2015-05-04 19:34:46 +02:00
|
|
|
->setSubject($this->get('translator')->trans('New password'))
|
|
|
|
->setFrom(
|
|
|
|
$this->container->getParameter('trinity_user.sender_mail'),
|
|
|
|
$this->container->getParameter('trinity_user.sender_name')
|
|
|
|
)
|
|
|
|
->setTo(array($user->getEmail()))
|
|
|
|
->setBody($body, 'text/html')
|
2015-03-02 21:57:49 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
return $this->get('mailer')->send($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postSave($object, $was_new = false)
|
|
|
|
{
|
|
|
|
$this->get('fos_user.user_manager')->updateUser($object);
|
|
|
|
}
|
2015-03-09 23:29:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @throws \LogicException
|
|
|
|
*/
|
|
|
|
public function getConfiguration()
|
|
|
|
{
|
|
|
|
// DI first
|
|
|
|
if($this->has('trinity.user.crud.configuration')){
|
|
|
|
$this->configuration = $this->get('trinity.user.crud.configuration');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->configuration)) {
|
|
|
|
throw new \LogicException('You must specify a crud configuration.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $this->configuration->getStorageNamespace()) {
|
|
|
|
$this->configuration->setStorageNamespace(get_class($this));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->configuration;
|
|
|
|
}
|
2015-03-02 21:57:49 +01:00
|
|
|
}
|