src/Cms/TenantBundle/Controller/Dashboard/PolicyController.php line 110

Open in your IDE?
  1. <?php
  2. namespace Cms\TenantBundle\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\TenantBundle\Entity\Tenant;
  7. use Cms\TenantBundle\Form\PolicyType;
  8. use Michelf\Markdown;
  9. use Platform\SecurityBundle\Entity\Identity\Account;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use DateTime;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. /**
  15.  * Class PolicyController
  16.  * @package Cms\TenantBundle\Controller\Dashboard
  17.  */
  18. class PolicyController extends Controller
  19. {
  20.     const ROUTES__VIEW 'cms.policy.dashboard.view';
  21.     const ROUTES__TERMS 'cms.policy.dashboard.terms';
  22.     const ROUTES__ACCEPT 'cms.policy.dashboard.accepted';
  23.     const ROUTES__TERMS_ACCEPT 'cms.policy.dashboard.terms_accept';
  24.     const ROUTES___POLICY 'cms.tenant.dashboard.policy';
  25.     /**
  26.      * @param Request $request
  27.      * @return DocumentScene|RedirectResponse
  28.      *
  29.      * @Route(
  30.      *     "/tenant/policy",
  31.      *     name = PolicyController::ROUTES___POLICY,
  32.      * )
  33.      */
  34.     public function editAction(Request $request)
  35.     {
  36.         // AUDIT
  37.         $this->denyAccessUnlessGranted('campussuite.cms.tenant.manage');
  38.         $tenant $this->getTenant();
  39.         $currentUser $this->getCurrentUser();
  40.         $policy $this->getPolicy($tenant);
  41.         $form $this->createForm(PolicyType::class);
  42.         $form->get('policy')->setData($policy);
  43.         if ($request->isMethod(Request::METHOD_POST)) {
  44.             $form->handleRequest($request);
  45.             if ($form->isSubmitted() && $form->isValid()) {
  46.                 try {
  47.                     $policyContent $form->get('policy')->getData();
  48.                     $now = new DateTime();
  49.                     $this
  50.                         ->getS3Wrapper()
  51.                         ->writeMemory($policyContentS3Wrapper::BUCKETS__STORAGE$this->getPolicyKey($tenant));
  52.                     $tenant->setPolicy($now);
  53.                     $currentUser->setPolicyAcceptedOn($now);
  54.                     $this->getEntityManager()->saveAll(array($tenant$currentUser));
  55.                     $this->getSession()->getFlashBag()->add('success''Policy saved successfully');
  56.                     // record log
  57.                     $this->getActivityLogger()->createLog($tenant);
  58.                     return $this->redirectToRoute(self::ROUTES___POLICY);
  59.                 } catch (\Exception $e) {
  60.                     $this->getSession()->getFlashBag()->add('danger''Policy was not saved. ' $e->getMessage());
  61.                 }
  62.             }
  63.         }
  64.         return $this->view(
  65.             array(
  66.                 'form' => $form->createView()
  67.             )
  68.         );
  69.     }
  70.     /**
  71.      * @return DocumentScene
  72.      *
  73.      * @Route(
  74.      *     "/policy",
  75.      *     name = PolicyController::ROUTES__VIEW,
  76.      * )
  77.      */
  78.     public function viewAction(): DocumentScene
  79.     {
  80.         return $this->view(
  81.             [
  82.                 'tenant' => $this->getTenant(),
  83.                 'content' => $this->getPolicy($this->getTenant()),
  84.             ]
  85.         );
  86.     }
  87.     /**
  88.      * @return RedirectResponse
  89.      *
  90.      * @Route(
  91.      *     "/policy/accepted",
  92.      *     name = PolicyController::ROUTES__ACCEPT,
  93.      * )
  94.      */
  95.     public function acceptedAction(): RedirectResponse
  96.     {
  97.         $this->getEntityManager()->save(
  98.             $this->getCurrentUser()
  99.                 ->setPolicyAcceptedOn(new DateTime())
  100.         );
  101.         return $this->redirect('/_dashboard');
  102.     }
  103.     /**
  104.      * @return DocumentScene
  105.      *
  106.      * @Route(
  107.      *     "/policy/terms",
  108.      *     name = PolicyController::ROUTES__TERMS,
  109.      * )
  110.      */
  111.     public function termsAction(): DocumentScene
  112.     {
  113.         return $this->view(
  114.             [
  115.                 'content' => $this->getTerms(),
  116.             ]
  117.         );
  118.     }
  119.     /**
  120.      * @return RedirectResponse
  121.      *
  122.      * @Route(
  123.      *     "/policy/terms/accept",
  124.      *     name = PolicyController::ROUTES__TERMS_ACCEPT,
  125.      * )
  126.      */
  127.     public function termsAcceptAction(): RedirectResponse
  128.     {
  129.         $this->getEntityManager()->save(
  130.             $this->getCurrentUser()
  131.                 ->setTermsAcceptedOn(new DateTime())
  132.         );
  133.         return $this->redirect('/_dashboard');
  134.     }
  135.     /**
  136.      * @return Account
  137.      */
  138.     public function getCurrentUser(): Account
  139.     {
  140.         return $this->getGlobalContext()->getAuthenticatedAccount();
  141.     }
  142.     /**
  143.      * @param Tenant $tenant
  144.      * @return string|null
  145.      */
  146.     protected function getPolicy(Tenant $tenant): ?string
  147.     {
  148.         try {
  149.             return $this->getS3Wrapper()->readMemory(
  150.                 S3Wrapper::BUCKETS__STORAGE,
  151.                 $this->getPolicyKey($tenant)
  152.             );
  153.         } catch (\Exception $e) {
  154.             return null;
  155.         }
  156.     }
  157.     /**
  158.      * @return string|null
  159.      */
  160.     protected function getTerms(): ?string
  161.     {
  162.         try {
  163.             return Markdown::defaultTransform(file_get_contents(
  164.                 $this->locateResource('@CmsCoreBundle/Resources/policies/ferpa.md')
  165.             ));
  166.         } catch (\Exception $e) {
  167.             return null;
  168.         }
  169.     }
  170.     /**
  171.      * @param Tenant $tenant
  172.      * @return string
  173.      */
  174.     protected function getPolicyKey(Tenant $tenant): string
  175.     {
  176.         return ltrim($this->getS3Wrapper()->manualKey(
  177.             $tenant->getId(),
  178.             $tenant->getUid()->toString(),
  179.             $tenant->getId(),
  180.             $tenant->getUid()->toString(),
  181.             '/policy'
  182.         ));
  183.     }
  184.     /**
  185.      * @return S3Wrapper|object
  186.      */
  187.     private function getS3Wrapper(): S3Wrapper
  188.     {
  189.         return $this->get(__METHOD__);
  190.     }
  191. }