src/Platform/SecurityBundle/Controller/Dashboard/ProfileController.php line 54

Open in your IDE?
  1. <?php
  2. namespace Platform\SecurityBundle\Controller\Dashboard;
  3. use Cms\CoreBundle\Model\Scenes\DashboardScenes\DocumentScene;
  4. use Cms\CoreBundle\Service\Aws\S3Wrapper;
  5. use Cms\CoreBundle\Util\Controller;
  6. use Cms\FileBundle\Service\BlitlineWrapper;
  7. use Cms\ModuleBundle\Controller\ContentController;
  8. use Cms\Modules\PeopleBundle\Entity\Profile\ProfileProxy;
  9. use Platform\SecurityBundle\Entity\Identity\Account;
  10. use Platform\SecurityBundle\Form\Type\AccountType;
  11. use Platform\SecurityBundle\Form\Type\ChangePasswordType;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Form\Extension\Core\Type\FileType;
  14. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  15. use Symfony\Component\Form\FormFactoryInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. /**
  20.  * Class ProfileController
  21.  * @package Platform\SecurityBundle\Controller\Dashboard
  22.  */
  23. class ProfileController extends Controller
  24. {
  25.     const ROUTES__LANDING 'platform.security.dashboard.profile.landing';
  26.     const ROUTES__VIEW 'platform.security.dashboard.profile.view';
  27.     const ROUTES__EDIT 'platform.security.dashboard.profile.edit';
  28.     const ROUTES__CREDENTIALS 'platform.security.dashboard.profile.credentials';
  29.     const ROUTES__DELETE_CREDENTIAL 'platform.security.dashboard.profile.credential_delete';
  30.     const ROUTES__CREATE_CREDENTIAL 'platform.security.dashboard.profile.credential_create';
  31.     const ROUTES__REGISTER_CREDENTIAL 'platform.security.dashboard.profile.credential_register';
  32.     const ROUTES__CHANGE_PASSWORD 'platform.security.dashboard.profile.change.password';
  33.     const ROUTES__CHANGE_PASSWORD_FORCED 'platform.security.dashboard.profile.change.password.forced';
  34.     const ROUTES__AVATAR 'platform.security.dashboard.profile.avatar';
  35.     const ROUTES__AVATAR_CROP 'platform.security.dashboard.profile.avatar_crop';
  36.     const ROUTES__LIST 'platform.security.dashboard.profile.list';
  37.     const ROUTES__LIST_LAZY 'platform.security.dashboard.profile.list_lazy';
  38.     /**
  39.      * TODO: this needs moved to a more proper controller...
  40.      *
  41.      * Quick controller to handle Settings tab redirection based on permissions.
  42.      *
  43.      * @return RedirectResponse
  44.      *
  45.      * @Route(
  46.      *     "/redir",
  47.      *     name = ProfileController::ROUTES__LANDING
  48.      * )
  49.      */
  50.     public function landingAction()
  51.     {
  52.         // check special cases
  53.         switch (true) {
  54.             // see if they have permission to manage accounts and security stuff
  55.             case $this->isGranted('campussuite.platform.security.accounts.manage'):
  56.                 return $this->redirectToRoute('campussuite.platform.security.dashboard.accounts.list');
  57.         }
  58.         // by default, redirect to the "my accounts" view as everyone has this
  59.         return $this->redirectToRoute(self::ROUTES__VIEW);
  60.     }
  61.     /**
  62.      * Renders 'My Account' page.
  63.      *
  64.      * @return DocumentScene
  65.      *
  66.      * @Route("/", name = ProfileController::ROUTES__VIEW)
  67.      */
  68.     public function viewAction()
  69.     {
  70.         return $this->view(
  71.             array(
  72.                 'account' => $this->getGlobalContext()->getEffectiveAccount(),
  73.                 'profile' => $this->getGlobalContext()->getEffectiveAccount()->getSystemProfile(),
  74.             )
  75.         );
  76.     }
  77.     /**
  78.      * @return DocumentScene
  79.      * @throws \Exception
  80.      *
  81.      * @Route(
  82.      *     "/profiles",
  83.      *     name = ProfileController::ROUTES__LIST
  84.      * )
  85.      */
  86.     public function listAction()
  87.     {
  88.         /** @var ProfileProxy[] $proxies */
  89.         $proxies $this->getEntityManager()->getRepository(ProfileProxy::class)->findByAccount(
  90.             $this->getUser()
  91.         );
  92.         return $this->view(array(
  93.             'proxies' => $proxies,
  94.             'redirectTo' => $this->generateUrl(self::ROUTES__LIST),
  95.             'routes' => array(
  96.                 'content' => ContentController::routing(),
  97.             ),
  98.         ));
  99.     }
  100.     /**
  101.      * Renders 'Edit Account' page.
  102.      *
  103.      * @param Request $request
  104.      * @return DocumentScene
  105.      *
  106.      * @Route(
  107.      *  "/edit",
  108.      *  name = ProfileController::ROUTES__EDIT,
  109.      * )
  110.      */
  111.     public function editAction(Request $request)
  112.     {
  113.         $account $this->getGlobalContext()->getEffectiveAccount();
  114.         $form $this->createForm(
  115.             AccountType::class,
  116.             $account,
  117.             []
  118.         );
  119.         if ($request->isMethod('POST')) {
  120.             /** @var Account $account */
  121.             $account $form->handleRequest($request)->getData();
  122.             $this->getEntityManager()->save($account);
  123.             // record log
  124.             $this->getActivityLogger()->createLog($account);
  125.             return $this->redirectToRoute(self::ROUTES__VIEW);
  126.         }
  127.         return $this->view(
  128.             array(
  129.                 'form' => $form->createView(),
  130.                 'account' => $account,
  131.                 'profile' => $account->getSystemProfile(),
  132.             )
  133.         );
  134.     }
  135.     /**
  136.      * Allows user to change password.
  137.      *
  138.      * @param Request $request
  139.      * @return DocumentScene|RedirectResponse
  140.      *
  141.      * @Route(
  142.      *   "/change-password",
  143.      *   name = ProfileController::ROUTES__CHANGE_PASSWORD,
  144.      * )
  145.      *
  146.      * @Route(
  147.      *   "/change-password-forced",
  148.      *   name = ProfileController::ROUTES__CHANGE_PASSWORD_FORCED,
  149.      * )
  150.      */
  151.     public function changePasswordAction(Request $request)
  152.     {
  153.         $account $this->getGlobalContext()->getEffectiveAccount();
  154.         $form $this->createForm(
  155.             ChangePasswordType::class,
  156.             [],
  157.             array(
  158.                 'account' => $account,
  159.             )
  160.         );
  161.         if ($request->isMethod('POST')) {
  162.             $data $form->handleRequest($request)->getData();
  163.             if ($form->isValid()) {
  164.                 $this->getEntityManager()->save($account->setPasswordRaw($data['password']));
  165.                 // record log
  166.                 $this->getActivityLogger()->createLog($account);
  167.                 return $this->redirectToRoute(self::ROUTES__VIEW);
  168.             }
  169.         }
  170.         return $this->view(
  171.             array(
  172.                 'form' => $form->createView(),
  173.                 'account' => $account,
  174.             )
  175.         );
  176.     }
  177.     /**
  178.      * @return S3Wrapper|object
  179.      */
  180.     private function getS3Wrapper(): S3Wrapper
  181.     {
  182.         return $this->get(__METHOD__);
  183.     }
  184.     /**
  185.      * @return BlitlineWrapper|object
  186.      */
  187.     private function getBlitlineWrapper(): BlitlineWrapper
  188.     {
  189.         return $this->get(__METHOD__);
  190.     }
  191.     /**
  192.      * @param Request $request
  193.      * @return DocumentScene
  194.      * @throws \Exception
  195.      *
  196.      * @Route(
  197.      *  "/profile/avatar/crop",
  198.      *  name = ProfileController::ROUTES__AVATAR_CROP
  199.      * )
  200.      */
  201.     public function avatarCropAction(Request $request)
  202.     {
  203.         if ($request->query->count() > 0) {
  204.             return $this->redirectToRoute(self::ROUTES__AVATAR_CROP);
  205.         }
  206.         $account $this->getGlobalContext()->getEffectiveAccount();
  207.         $key $this->getS3Wrapper()->entityKey($account'/avatar/original');
  208.         if ($this->getS3Wrapper()->exists(S3Wrapper::BUCKETS__STORAGE$key) === false) {
  209.             throw new \Exception();
  210.         }
  211.         if ($request->isMethod('POST')) {
  212.             $cropping $request->request->all();
  213.             $blitline $this->getBlitlineWrapper();
  214.             $funcs = [];
  215.             $funcs[] = $blitline->cropResize(
  216.                 $this->getS3Wrapper()->entityKey($account'/avatar/feature'),
  217.                 $cropping['x'],
  218.                 $cropping['y'],
  219.                 $cropping['width'],
  220.                 $cropping['height'],
  221.                 164,
  222.                 164,
  223.             );
  224.             $funcs[] = $blitline->cropResize(
  225.                 $this->getS3Wrapper()->entityKey($account'/avatar/thumb'),
  226.                 $cropping['x'],
  227.                 $cropping['y'],
  228.                 $cropping['width'],
  229.                 $cropping['height'],
  230.                 36,
  231.                 36,
  232.             );
  233.             $blitline->job(
  234.                 $this->getS3Wrapper()->url(S3Wrapper::BUCKETS__STORAGE$key),
  235.                 $funcs
  236.             );
  237.             $account->getSystemProfile()->setAvatar(true);
  238.             $this->getEntityManager()->save($account);
  239.             // record log
  240.             $this->getActivityLogger()->createLog($account);
  241.             return $this->redirectToRoute(self::ROUTES__VIEW);
  242.         }
  243.         return $this->view(
  244.             array(
  245.                 'account' => $account,
  246.                 'image' => $this->getS3Wrapper()->url(S3Wrapper::BUCKETS__STORAGE$key),
  247.             )
  248.         );
  249.     }
  250.     /**
  251.      * @return DocumentScene
  252.      * @throws \Exception
  253.      * @Route("/profile/avatar",name = ProfileController::ROUTES__AVATAR)
  254.      */
  255.     public function avatarAction()
  256.     {
  257.         $account $this->getGlobalContext()->getEffectiveAccount();
  258.         $fields $this->getS3Wrapper()->formInputs(S3Wrapper::BUCKETS__STORAGE, array(
  259.             'success_action_redirect' => $this->generateUrl(
  260.                 self::ROUTES__AVATAR_CROP,
  261.                 [],
  262.                 UrlGeneratorInterface::ABSOLUTE_URL
  263.             ),
  264.             'key' => ltrim($this->getS3Wrapper()->entityKey($account'/avatar/original'), '/'),
  265.             'Content-Type' => '^',
  266.         ));
  267.         $action $this->getS3Wrapper()->formAction(S3Wrapper::BUCKETS__STORAGE);
  268.         $form $this->getFormFactory()->createNamedBuilder('');
  269.         foreach ($fields as $name => $value) {
  270.             $form->add($nameHiddenType::class, array(
  271.                 'data' => $value,
  272.             ));
  273.         }
  274.         $form
  275.             ->add('file'FileType::class, [])
  276.             ->setAction($action);
  277.         $form $form
  278.             ->getForm();
  279.         return $this->view(
  280.             array(
  281.                 'form' => $form->createView(),
  282.             )
  283.         );
  284.     }
  285. }