trinity-cms-bundles/src/Trinity/Bundle/UserBundle/Controller/ProfileAdminController.php

110 lines
3.6 KiB
PHP

<?php
namespace Trinity\Bundle\UserBundle\Controller;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Model\UserInterface;
use \Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use \FOS\UserBundle\Controller\ProfileController as BaseProfileController;
class ProfileAdminController extends BaseProfileController
{
/**
* Show the user
*/
public function showAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
return $this->container->get('templating')->renderResponse('TrinityUserBundle:Profile\Admin:show.html.'.$this->container->getParameter('fos_user.template.engine'), array('user' => $user));
}
/**
* Edit the user
*/
public function editAction(Request $request)
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->container->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->container->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
if ('POST' === $request->getMethod()) {
$form->bind($request);
$is_valid = $form->isValid();
$is_posted = true;
if ($is_valid) {
// @var $userManager \FOS\UserBundle\Model\UserManagerInterface
$userManager = $this->container->get('fos_user.user_manager');
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
$userManager->updateUser($user);
$this->container->get('trinity.user_log')->updated($user, $this->getUser());
}
}
return $this->container->get('templating')->renderResponse(
'TrinityUserBundle:Profile\Admin:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
array(
'form' => $form->createView(),
'is_posted' => !empty($is_posted),
'is_valid' => !empty($is_valid),
)
);
}
protected function getSessionUser()
{
return $this->container->get('trinity.session_user');
}
public function getUser()
{
if (!$this->container->has('security.context')) {
throw new \LogicException('The SecurityBundle is not registered in your application.');
}
if (null === $token = $this->container->get('security.context')->getToken()) {
return null;
}
if (!is_object($user = $token->getUser())) {
return null;
}
if (method_exists($user, 'setStorage')) {
$user->setStorage($this->container->get('trinity.storage'));
}
return $user;
}
}