<?php
namespace Cms\TenantBundle\Controller\Dashboard;
use Cms\CoreBundle\Model\Scenes\DashboardScenes\DocumentScene;
use Cms\CoreBundle\Service\Aws\S3Wrapper;
use Cms\CoreBundle\Util\Controller;
use Cms\TenantBundle\Entity\Tenant;
use Cms\TenantBundle\Form\PolicyType;
use Michelf\Markdown;
use Platform\SecurityBundle\Entity\Identity\Account;
use Symfony\Component\Routing\Annotation\Route;
use DateTime;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Class PolicyController
* @package Cms\TenantBundle\Controller\Dashboard
*/
class PolicyController extends Controller
{
const ROUTES__VIEW = 'cms.policy.dashboard.view';
const ROUTES__TERMS = 'cms.policy.dashboard.terms';
const ROUTES__ACCEPT = 'cms.policy.dashboard.accepted';
const ROUTES__TERMS_ACCEPT = 'cms.policy.dashboard.terms_accept';
const ROUTES___POLICY = 'cms.tenant.dashboard.policy';
/**
* @param Request $request
* @return DocumentScene|RedirectResponse
*
* @Route(
* "/tenant/policy",
* name = PolicyController::ROUTES___POLICY,
* )
*/
public function editAction(Request $request)
{
// AUDIT
$this->denyAccessUnlessGranted('campussuite.cms.tenant.manage');
$tenant = $this->getTenant();
$currentUser = $this->getCurrentUser();
$policy = $this->getPolicy($tenant);
$form = $this->createForm(PolicyType::class);
$form->get('policy')->setData($policy);
if ($request->isMethod(Request::METHOD_POST)) {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$policyContent = $form->get('policy')->getData();
$now = new DateTime();
$this
->getS3Wrapper()
->writeMemory($policyContent, S3Wrapper::BUCKETS__STORAGE, $this->getPolicyKey($tenant));
$tenant->setPolicy($now);
$currentUser->setPolicyAcceptedOn($now);
$this->getEntityManager()->saveAll(array($tenant, $currentUser));
$this->getSession()->getFlashBag()->add('success', 'Policy saved successfully');
// record log
$this->getActivityLogger()->createLog($tenant);
return $this->redirectToRoute(self::ROUTES___POLICY);
} catch (\Exception $e) {
$this->getSession()->getFlashBag()->add('danger', 'Policy was not saved. ' . $e->getMessage());
}
}
}
return $this->view(
array(
'form' => $form->createView()
)
);
}
/**
* @return DocumentScene
*
* @Route(
* "/policy",
* name = PolicyController::ROUTES__VIEW,
* )
*/
public function viewAction(): DocumentScene
{
return $this->view(
[
'tenant' => $this->getTenant(),
'content' => $this->getPolicy($this->getTenant()),
]
);
}
/**
* @return RedirectResponse
*
* @Route(
* "/policy/accepted",
* name = PolicyController::ROUTES__ACCEPT,
* )
*/
public function acceptedAction(): RedirectResponse
{
$this->getEntityManager()->save(
$this->getCurrentUser()
->setPolicyAcceptedOn(new DateTime())
);
return $this->redirect('/_dashboard');
}
/**
* @return DocumentScene
*
* @Route(
* "/policy/terms",
* name = PolicyController::ROUTES__TERMS,
* )
*/
public function termsAction(): DocumentScene
{
return $this->view(
[
'content' => $this->getTerms(),
]
);
}
/**
* @return RedirectResponse
*
* @Route(
* "/policy/terms/accept",
* name = PolicyController::ROUTES__TERMS_ACCEPT,
* )
*/
public function termsAcceptAction(): RedirectResponse
{
$this->getEntityManager()->save(
$this->getCurrentUser()
->setTermsAcceptedOn(new DateTime())
);
return $this->redirect('/_dashboard');
}
/**
* @return Account
*/
public function getCurrentUser(): Account
{
return $this->getGlobalContext()->getAuthenticatedAccount();
}
/**
* @param Tenant $tenant
* @return string|null
*/
protected function getPolicy(Tenant $tenant): ?string
{
try {
return $this->getS3Wrapper()->readMemory(
S3Wrapper::BUCKETS__STORAGE,
$this->getPolicyKey($tenant)
);
} catch (\Exception $e) {
return null;
}
}
/**
* @return string|null
*/
protected function getTerms(): ?string
{
try {
return Markdown::defaultTransform(file_get_contents(
$this->locateResource('@CmsCoreBundle/Resources/policies/ferpa.md')
));
} catch (\Exception $e) {
return null;
}
}
/**
* @param Tenant $tenant
* @return string
*/
protected function getPolicyKey(Tenant $tenant): string
{
return ltrim($this->getS3Wrapper()->manualKey(
$tenant->getId(),
$tenant->getUid()->toString(),
$tenant->getId(),
$tenant->getUid()->toString(),
'/policy'
));
}
/**
* @return S3Wrapper|object
*/
private function getS3Wrapper(): S3Wrapper
{
return $this->get(__METHOD__);
}
}