<?php
namespace Platform\SecurityBundle\Controller\Dashboard;
use App\Controller\Dashboard\Settings\AccountController;
use Cms\BulletinBundle\Model\Bulletins\AccountCreationBulletin;
use Cms\BulletinBundle\Model\Bulletins\PasswordResetBulletin;
use Cms\BulletinBundle\Service\BulletinService;
use Cms\ContainerBundle\Doctrine\ContainerRepository;
use Cms\ContainerBundle\Doctrine\Containers\GenericContainerRepository;
use Cms\ContainerBundle\Doctrine\Containers\IntranetContainerRepository;
use Cms\ContainerBundle\Entity\Container;
use Cms\ContainerBundle\Entity\Containers\GenericContainer;
use Cms\ContainerBundle\Entity\Containers\IntranetContainer;
use Cms\CoreBundle\Model\Scenes\DashboardScenes\AjaxScene;
use Cms\CoreBundle\Model\Scenes\DashboardScenes\DocumentScene;
use Cms\CoreBundle\Service\ContextManager;
use Cms\CoreBundle\Util\Controller;
use Cms\CoreBundle\Util\DateTimeUtils;
use Cms\CoreBundle\Util\Doctrine\EntityRepository;
use Cms\FileBundle\Service\FileManager;
use Cms\TenantBundle\Model\ProductsBitwise;
use Platform\SecurityBundle\Doctrine\Access\GroupAccountRepository;
use Platform\SecurityBundle\Doctrine\Access\RoleAssociation\AccountRoleAssociationRepository;
use Platform\SecurityBundle\Doctrine\Access\RoleRepository;
use Platform\SecurityBundle\Doctrine\Identity\AccountRepository;
use Platform\SecurityBundle\Doctrine\Identity\GroupRepository;
use Platform\SecurityBundle\Entity\Access\GroupAccount;
use Platform\SecurityBundle\Entity\Access\Role;
use Platform\SecurityBundle\Entity\Access\RoleAssociation;
use Platform\SecurityBundle\Entity\Access\RoleAssociation\AccountRoleAssociation;
use Platform\SecurityBundle\Entity\Identity\Account;
use Platform\SecurityBundle\Entity\Identity\Group;
use Platform\SecurityBundle\Form\Type\Access\PermissionsAuditType;
use Platform\SecurityBundle\Form\Type\Identity\AccountLocaleType;
use Platform\SecurityBundle\Form\Type\Identity\AccountType;
use Platform\SecurityBundle\Form\Type\Profiles\GroupsType;
use Platform\SecurityBundle\Form\Type\Profiles\RolesType;
use Platform\SecurityBundle\Form\Type\Profiles\SpecialPermissionsType;
use Platform\SecurityBundle\Model\Permission;
use Platform\SecurityBundle\Model\Permissions;
use Platform\SecurityBundle\Service\PasswordService;
use Platform\SecurityBundle\Service\Reporters\AccountReporter;
use Platform\SecurityBundle\Service\Search\AccountSearcher;
use Platform\SecurityBundle\Service\Sentry;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
/**
* Class AccountsController
* @package Platform\SecurityBundle\Controller\Dashboard
*/
final class AccountsController extends Controller
{
const ROUTES__LIST = 'campussuite.platform.security.dashboard.accounts.list';
const ROUTES__LIST_LAZY = 'campussuite.platform.security.dashboard.accounts.list_lazy';
const ROUTES__CREATE = 'campussuite.platform.security.dashboard.accounts.create';
const ROUTES__VIEW = 'campussuite.platform.security.dashboard.accounts.view';
const ROUTES__EDIT = 'campussuite.platform.security.dashboard.accounts.edit';
const ROUTES__EDIT_LOCALE = 'campussuite.platform.security.dashboard.accounts.edit_locale';
const ROUTES__REACTIVATE = 'campussuite.platform.security.dashboard.accounts.reactivate';
const ROUTES__DEACTIVATE = 'campussuite.platform.security.dashboard.accounts.deactivate';
const ROUTES__ASSIGN_ROLES = 'campussuite.platform.security.dashboard.accounts.assign_roles';
const ROUTES__REMOVE_ROLES = 'campussuite.platform.security.dashboard.accounts.remove_roles';
const ROUTES__ASSIGN_GROUPS = 'campussuite.platform.security.dashboard.accounts.assign_groups';
const ROUTES__REMOVE_GROUPS = 'campussuite.platform.security.dashboard.accounts.remove_groups';
const ROUTES__MANAGE_PASSWORD = 'campussuite.platform.security.dashboard.accounts.manage_password';
const ROUTES__RESET_PASSWORD = 'campussuite.platform.security.dashboard.accounts.reset_password';
const ROUTES__CLEAR_PASSWORD = 'campussuite.platform.security.dashboard.accounts.clear_password';
const ROUTES__CREDENTIALS = 'campussuite.platform.security.dashboard.accounts.credentials';
const ROUTES__SPECIAL_PERMISSIONS = 'campussuite.platform.security.dashboard.accounts.special_permissions';
const ROUTES__AUDIT = 'campussuite.platform.security.dashboard.accounts.audit';
const ROUTES__EXPORT_XLSX = 'campussuite.platform.security.dashboard.accounts.export_xlsx';
const DEFAULT_COUNT = 200;
/**
* @return AccountRepository|EntityRepository
*/
private function getAccountRepository()
{
return $this->getRepository(Account::class);
}
/**
* @return RoleRepository|EntityRepository
*/
private function getRoleRepository()
{
return $this->getRepository(Role\CmsRole::class);
}
/**
* @return AccountRoleAssociationRepository|EntityRepository
*/
private function getAccountRoleAssociationRepository()
{
return $this->getRepository(AccountRoleAssociation::class);
}
/**
* @return GroupRepository|EntityRepository
*/
private function getGroupRepository()
{
return $this->getRepository(Group::class);
}
/**
* @return GroupAccountRepository|EntityRepository
*/
private function getGroupAccountRepository()
{
return $this->getRepository(GroupAccount::class);
}
/**
* @return ContainerRepository|EntityRepository
*/
private function getContainerRepository()
{
return $this->getRepository(Container::class);
}
/**
* @return GenericContainerRepository|EntityRepository
*/
private function getGenericContainerRepository()
{
return $this->getRepository(GenericContainer::class);
}
/**
* @return IntranetContainerRepository|EntityRepository
*/
private function getIntranetContainerRepository()
{
return $this->getRepository(IntranetContainer::class);
}
/**
* @return BulletinService|object
*/
private function getBulletinService(): BulletinService
{
return $this->get(__METHOD__);
}
/**
* @return Sentry|object
*/
private function getSentry(): Sentry
{
return $this->get(__METHOD__);
}
/**
* @return FileManager|object
*/
private function getFileManager(): FileManager
{
return $this->get(__METHOD__);
}
/**
* @return AccountReporter|object
*/
private function getAccountReporter(): AccountReporter
{
return $this->get(__METHOD__);
}
/**
* @return AccountSearcher|object
*/
private function getAccountSearcher(): AccountSearcher
{
return $this->get(__METHOD__);
}
/**
* @return PasswordService|object
*/
private function getPasswordService(): PasswordService
{
return $this->get(__METHOD__);
}
/**
* @param Account $account
* @param string|null $password
* @return array|bool
*/
private function sendCreateEmail(Account $account, ?string $password = null)
{
return $this->getBulletinService()->send(
(new AccountCreationBulletin())
->setTo($account)
->setAccount($account)
->setPassword($password)
->setDashboard($this->getGlobalContext()->getDashboardUrl())
);
}
/**
* @param Account $account
* @param string $password
* @return array|bool
*/
private function sendResetPasswordEmail(Account $account, string $password)
{
return $this->getBulletinService()->send(
(new PasswordResetBulletin())
->setTo($account)
->setAccount($account)
->setPassword($password)
->setDashboard($this->getGlobalContext()->getDashboardUrl())
);
}
/**
* @param Request $request
* @return DocumentScene|AjaxScene|RedirectResponse
*
* @Route("/", name = AccountsController::ROUTES__LIST)
* @Route(
* "/lazyload",
* name = AccountsController::ROUTES__LIST_LAZY
* )
*/
public function listAction(Request $request)
{
// AUDIT
$this->denyAccessUnlessGranted('campussuite.platform.security.accounts.manage');
if ($request->get('_route') == self::ROUTES__LIST_LAZY && ! $request->isXmlHttpRequest()) {
throw new NotFoundHttpException();
}
// if we are in sn migrated mode, jump to the newer areas
if ($this->getContextManager()->getGlobalContext()->getTenant()->getProducts()->hasFlag(ProductsBitwise::SCHOOLNOW__MIGRATED)) {
return $this->redirectToRoute(AccountController::ROUTES__MAIN);
}
$search = $this->getAccountSearcher()->handleRequest($request);
if ($search instanceof RedirectResponse) {
return $search;
}
$accounts = $this->getAccountRepository()->search($search);
if ($request->isXmlHttpRequest()) {
return $this->viewAjax(
'@PlatformSecurity/Dashboard/Accounts/listLazy.html.twig',
array(
'accounts' => $accounts,
'search' => $search,
)
);
}
return $this->view(
array(
'accounts' => $accounts,
'search' => $search,
)
);
}
/**
* @param Request $request
* @return DocumentScene|RedirectResponse
*
* @Route("/create", name = AccountsController::ROUTES__CREATE)
*/
public function createAction(Request $request)
{
// AUDIT
$this->denyAccessUnlessGranted('campussuite.platform.security.accounts.manage');
// create new object
$account = new Account();
// create form for creation
$form = $this->createForm(
AccountType::class,
$account,
array(
'password' => false,
'notify' => ($this->getGlobalContext()->getTenant()->getProducts()->getMask() !== ProductsBitwise::SMM__BASE),
)
);
// check for submission
if ($this->handleForm($form, $request)) {
// merge form data to object
/** @var Account $account */
$account = $form->getData();
try {
// attempt to get a password and set it if given
$password = $form->get('password')->getData();
if ( ! empty($password)) {
$account->setPasswordRaw($password);
}
// save to db
$this->getEntityManager()->save($account);
if ($form->get('notify')->getData() === true) {
$this->sendCreateEmail($account, $password);
}
// record log
$this->getActivityLogger()->createLog($account);
$this->getSession()->getFlashBag()->add('success', 'Account was successfully saved.');
// go to view page
return $this->redirectToRoute(
self::ROUTES__VIEW,
array(
'accountId' => $account->getId(),
)
);
} catch (\Exception $e) {
$this->getSession()->getFlashBag()->add('danger', $e->getMessage());
}
}
return $this->view(
array(
'form' => $form->createView(),
)
);
}
/**
* @param $accountId
* @return DocumentScene
* @throws \Exception
*
* @Route(
* "/{accountId}",
* name = AccountsController::ROUTES__VIEW,
* requirements = {
* "accountId" = "[1-9]\d*"
* }
* )
*/
public function viewAction($accountId)
{
// obtain the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.accounts.manage',
array($account, null)
);
return $this->view(
array(
'account' => $account,
)
);
}
/**
* @param $accountId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/edit",
* name = AccountsController::ROUTES__EDIT,
* requirements = {
* "accountId" = "[1-9]\d*"
* }
* )
*/
public function editAction($accountId)
{
// load the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.accounts.manage',
array($account, null)
);
// check for oneroster
if ($account->isOneRoster()) {
throw new \Exception();
}
// form for edit
$form = $this->createForm(AccountType::class, $account, array(
'password' => false,
));
// check for submission
if ($this->handleForm($form)) {
// merge the form data
/** @var Account $account */
$account = $form->getData();
try {
// save to db
$this->getEntityManager()->save($account);
// it is, make the new credential
$password = $form->get('password')->getData();
if ( ! empty($password)) {
$this->getEntityManager()->save($account->setPasswordRaw($password));
if ($form->get('notify')->getData() === true) {
$this->sendResetPasswordEmail($account, $password);
}
}
// record log
$this->getActivityLogger()->createLog($account);
// go to viewing this one
return $this->redirectToRoute(
self::ROUTES__VIEW,
array(
'accountId' => $accountId,
)
);
} catch (\Exception $e) {
$this->getSession()->getFlashBag()->add('danger', $e->getMessage());
}
}
return $this->view(
array(
'account' => $account,
'form' => $form->createView(),
)
);
}
/**
* @param Request $request
* @param $accountId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/edit-locale",
* name = AccountsController::ROUTES__EDIT_LOCALE,
* requirements = {
* "accountId" = "[1-9]\d*"
* }
* )
*/
public function editLocaleAction(Request $request, $accountId)
{
// load the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.accounts.manage',
array($account, null)
);
// form for edit
$form = $this->createForm(
AccountLocaleType::class,
$account
);
// check for submission
if ($request->isMethod('POST')) {
// merge the form data
/** @var Account $account */
$account = $form->handleRequest($request)->getData();
try {
// save to db
$this->getEntityManager()->save($account);
// record log
$this->getActivityLogger()->createLog($account);
// go to viewing this one
return $this->redirectToRoute(
self::ROUTES__VIEW,
array(
'accountId' => $accountId,
)
);
} catch (\Exception $e) {
$this->getSession()->getFlashBag()->add('danger', $e->getMessage());
}
}
return $this->view(
array(
'account' => $account,
'form' => $form->createView(),
)
);
}
/**
* @param Request $request
* @param $accountId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/reactivate",
* name = AccountsController::ROUTES__REACTIVATE,
* requirements = {
* "accountId" = "[1-9]\d*"
* }
* )
*/
public function reactivateAction(Request $request, $accountId)
{
// obtain the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.accounts.manage',
array($account, null)
);
// if the account is already active, we want to get away from here
if ($account->isActive()) {
return $this->redirectToRoute(self::ROUTES__LIST);
}
// check for submission
if ($request->isMethod('POST')) {
// force active state
$account->setActive(true);
// save to db
$this->getEntityManager()->save($account);
// log the change
$this->getActivityLogger()->createLog($account, $accountId);
// go back
return $this->redirectToRoute(self::ROUTES__LIST);
}
return $this->view(
array(
'account' => $account,
)
);
}
/**
* @param Request $request
* @param $accountId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/deactivate",
* name = AccountsController::ROUTES__DEACTIVATE,
* requirements = {
* "accountId" = "[1-9]\d*"
* }
* )
*/
public function deactivateAction(Request $request, $accountId)
{
// obtain the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.accounts.manage',
array($account, null)
);
// if we are not active, we just want to get away from here as nothing to do
if ($account->isActive() === false) {
return $this->redirectToRoute(self::ROUTES__LIST);
}
// check for submission
if ($request->isMethod('POST')) {
// unset the active state
$account->setActive(false);
// save
$this->getEntityManager()->save($account);
// log the action
$this->getActivityLogger()->createLog($account, $accountId);
// go back
return $this->redirectToRoute(self::ROUTES__LIST);
}
return $this->view(
array(
'account' => $account,
)
);
}
/**
* @param $accountId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/manage-password",
* name = AccountsController::ROUTES__MANAGE_PASSWORD,
* requirements = {
* "accountId" = "[1-9]\d*"
* }
* )
*/
public function managePasswordAction($accountId)
{
// load up the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.accounts.manage',
array($account, null)
);
// if no password, disallow
if ( ! $account->canManagePassword()) {
throw new \Exception();
}
return $this->view(
array(
'account' => $account,
)
);
}
/**
* @param Request $request
* @param $accountId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/reset-password",
* name = AccountsController::ROUTES__RESET_PASSWORD,
* requirements = {
* "accountId" = "[1-9]\d*"
* }
* )
*/
public function resetPasswordAction(Request $request, $accountId)
{
// load up the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.accounts.manage',
array($account, null)
);
// if no password, disallow
if ( ! $account->canManagePassword()) {
throw new \Exception();
}
// handle the submission
if ($request->isMethod('POST')) {
// setup and send reset email
$this->getPasswordService()->sendResetPasswordInitEmail(
$request,
$account
);
// log this
$this->getActivityLogger()->createLog($account, $accountId);
// go back to where we came
return $this->redirectToRoute(self::ROUTES__LIST);
}
throw new \Exception();
}
/**
* @param Request $request
* @param $accountId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/clear-password",
* name = AccountsController::ROUTES__CLEAR_PASSWORD,
* requirements = {
* "accountId" = "[1-9]\d*"
* }
* )
*/
public function clearPasswordAction(Request $request, $accountId)
{
// load up the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.accounts.manage',
array($account, null)
);
// if no password, disallow
if ( ! $account->canManagePassword()) {
throw new \Exception();
}
// handle the submission
if ($request->isMethod('POST')) {
// wipe the password
$this->getEntityManager()->save(
$account->setPassword(null)
);
// log this
$this->getActivityLogger()->createLog($account, $accountId);
// go back to where we came
return $this->redirectToRoute(self::ROUTES__LIST);
}
throw new \Exception();
}
/**
* @param Request $request
* @param int $accountId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/assign-roles",
* name = AccountsController::ROUTES__ASSIGN_ROLES,
* requirements = {
* "accountId" = "[1-9]\d*"
* }
* )
*/
public function assignRolesAction(Request $request, $accountId)
{
// get the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.accounts.manage',
array($account, null)
);
// grab all of the groups in the system
$roles = $this->getRoleRepository()->findBy(array(), array(
'name' => 'ASC',
));
// grab all of the current associations
$associations = $this->getAccountRoleAssociationRepository()->findByAccount($account);
$form = $this->createForm(RolesType::class, [], array(
'roles' => $roles,
'containers' => array_merge(
$this->getGenericContainerRepository()->findAllHierarchy(),
$this->getIntranetContainerRepository()->findAllHierarchy()
),
));
$form->handleRequest($request);
// check for submission
if ($request->isMethod('POST') && $data = $form->getData()) {
$data['container'] = (isset($data['container']) && ! empty($data['container'])) ? $data['container'] : null;
$data['inheritance'] =
(isset($data['inheritance']) && ! empty($data['inheritance']))
? $data['inheritance']
: null;
if (empty($data['role'])) {
$this->getSession()->getFlashBag()->add('danger', 'Please, choose role for account.');
} else {
if (empty(
$this->getAccountRoleAssociationRepository()
->findBy(
array('account' => $account, 'role' => $data['role'], 'container' => $data['container'])
)
)) {
$association = (new AccountRoleAssociation())
->setAccount($account)
->setRole($data['role']);
if ($data['container']) {
$association
->setContainer($data['container'])
->setInheritance($data['inheritance']);
} else {
$association
->setContainer(null)
->setInheritance(RoleAssociation::INHERITANCES__ME_ONLY);
}
$this->getEntityManager()->save($association);
// record log
$this->getActivityLogger()->createLog($association);
$this->getSession()->getFlashBag()->add('success', 'Role has been added.');
// go back to listing
return $this->redirectToRoute(self::ROUTES__ASSIGN_ROLES, array('accountId' => $accountId));
} else {
$this->getSession()->getFlashBag()->add('danger', 'Role with this parameters already exist.');
}
}
}
return $this->view(
array(
'form' => $form->createView(),
'account' => $account,
'roles' => $roles,
'associations' => $associations,
)
);
}
/**
* @param Request $request
* @param int $accountId
* @param int $associationId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/remove-roles/{associationId}",
* name = AccountsController::ROUTES__REMOVE_ROLES,
* requirements = {
* "accountId" = "[1-9]\d*",
* "associationId" = "[1-9]\d*"
* }
* )
*/
public function removeRolesAction(Request $request, $accountId, $associationId)
{
// get the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.accounts.manage',
array($account, null)
);
// get association
$roleAssociation = $this->getAccountRoleAssociationRepository()->findExact($associationId);
// check for submission
if ($request->isMethod('POST')) {
// do the deletion
$this->getEntityManager()->delete($roleAssociation);
// record log
$this->getActivityLogger()->createLog($roleAssociation, $associationId);
$this->getSession()->getFlashBag()->add('danger', 'Role has been removed.');
// go back to list
return $this->redirectToRoute(self::ROUTES__ASSIGN_ROLES, array('accountId' => $accountId));
}
return $this->view(
array(
'account' => $account,
'roleAssociation' => $roleAssociation,
)
);
}
/**
* @param Request $request
* @param $accountId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/assign-groups",
* name = AccountsController::ROUTES__ASSIGN_GROUPS,
* requirements = {
* "accountId" = "[1-9]\d*"
* }
* )
*/
public function assignGroupsAction(Request $request, $accountId)
{
// get the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.groups.manage',
array($account, null)
);
// grab all of the groups in the system
$groups = $this->getGroupRepository()->findBy(array(), array(
'name' => 'ASC',
));
// grab all of the current associations
$associations = $this->getGroupAccountRepository()->findByAccount($account);
$form = $this->createForm(
GroupsType::class,
null,
array(
'groups' => $groups,
)
);
$form->handleRequest($request);
// check for submission
if ($request->isMethod('POST') && $data = $form->getData()) {
if (empty($data['group'])) {
$this->getSession()->getFlashBag()->add('danger', 'Please, choose group for account.');
} else {
if (empty(
$this->getGroupAccountRepository()->findBy(
array('account' => $account, 'group' => $data['group'])
)
)) {
$association = (new GroupAccount())
->setAccount($account)
->setGroup($data['group']);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.groups.manage',
array($data['group'], null)
);
$this->getEntityManager()->save($association);
// record log
$this->getActivityLogger()->createLog($association);
$this->getSession()->getFlashBag()->add('success', 'Group has been added.');
// go back to listing
return $this->redirectToRoute(self::ROUTES__ASSIGN_GROUPS, array('accountId' => $accountId));
} else {
$this->getSession()->getFlashBag()->add('danger', 'Group with this parameters already exist.');
}
}
}
return $this->view(
array(
'form' => $form->createView(),
'account' => $account,
'groups' => $groups,
'associations' => $associations,
)
);
}
/**
* @param Request $request
* @param int $accountId
* @param int $associationId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/remove-groups/{associationId}",
* name = AccountsController::ROUTES__REMOVE_GROUPS,
* requirements = {
* "accountId" = "[1-9]\d*",
* "associationId" = "[1-9]\d*"
* }
* )
*/
public function removeGroupsAction(Request $request, $accountId, $associationId)
{
// get the account
$account = $this->getAccountRepository()->findExact($accountId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.groups.manage',
array($account, null)
);
// get association
$groupAssociation = $this->getGroupAccountRepository()->findExact($associationId);
// AUDIT
$this->denyAccessUnlessGranted(
'campussuite.platform.security.groups.manage',
array($groupAssociation, null)
);
// check for submission
if ($request->isMethod('POST')) {
// do the deletion
$this->getEntityManager()->delete($groupAssociation);
// record log
$this->getActivityLogger()->createLog($groupAssociation, $associationId);
$this->getSession()->getFlashBag()->add('danger', 'Group has been removed.');
// go back to list
return $this->redirectToRoute(self::ROUTES__ASSIGN_GROUPS, array('accountId' => $accountId));
}
return $this->view(
array(
'account' => $account,
'groupAssociation' => $groupAssociation,
)
);
}
/**
* @param Request $request
* @param int $accountId
* @return DocumentScene|RedirectResponse
* @throws \Exception
*
* @Route(
* "/{accountId}/special-permissions",
* name = AccountsController::ROUTES__SPECIAL_PERMISSIONS,
* requirements = {
* "accountId" = "[1-9]\d*",
* }
* )
*/
public function specialPermissionsAction(Request $request, $accountId)
{
// only SuperAdmin can perform this action!
if( ! $this->getSentry()->isSuperAdmin($this->getContextManager()->getGlobalContext()->getEffectiveAccount())) {
throw new AccessDeniedException();
}
$account = $this->getAccountRepository()->findExact($accountId);
$form = $this->createForm(SpecialPermissionsType::class, $account);
if ($this->handleForm($form, $request)) {
$this->getEntityManager()->save($account);
// record log
$this->getActivityLogger()->createLog($account);
return $this->redirectToRoute(
self::ROUTES__VIEW,
array(
'accountId' => $accountId,
)
);
}
return $this->view(
array(
'form' => $form->createView(),
'account' => $account,
)
);
}
/**
* @param int $accountId
* @param int $containerId
* @return DocumentScene
* @throws \Exception
*
* @Route(
* "/{accountId}/audit/{containerId}",
* name = AccountsController::ROUTES__AUDIT,
* requirements = {
* "accountId" = "[1-9]\d*",
* "containerId" = "[1-9]\d*",
* },
* defaults = {
* "containerId" = null
* }
* )
*/
public function auditAction($accountId, $containerId)
{
$container = $containerId ? $this->getContainerRepository()->find($containerId) : null;
// obtain all of the permissions available in the system
$permissionsArray = $this->getParameter('platform.security.permissions');
// load the account
$account = $this->getAccountRepository()->findExact($accountId);
$permissions = new Permissions();
foreach ($permissionsArray as $permission) {
$permissions->addPermission(
(new Permission())
->setValue(
$this->getSentry()->check(
$account,
$permission['key'],
$container,
)
)
->setLabel($permission['name'])
);
}
$form = $this->createForm(PermissionsAuditType::class, $permissions, array('container' => $container));
return $this->view(
array(
'account' => $account,
'form' => $form->createView(),
)
);
}
/**
* @return Response
*
* @Route("/export/xlsx", name = AccountsController::ROUTES__EXPORT_XLSX)
*/
public function exportAction()
{
// AUDIT
$this->denyAccessUnlessGranted('campussuite.platform.security.accounts.manage');
/** @var Account[] $accounts */
$accounts = $this->getAccountRepository()->findAll();
$data = $this->getAccountReporter()->generateAccountList($accounts);
$response = new Response($data);
$contentDisposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
sprintf(
'accounts-%s.xlsx',
DateTimeUtils::format(
DateTimeUtils::today(),
'Y-m-d'
)
)
);
$response->headers->set('Content-Disposition', $contentDisposition);
$response->headers->set(
'Content-type',
$this->getFileManager()->mimeByExtension('xlsx')
);
return $response;
}
}