Spaces:
No application file
No application file
File size: 4,534 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
<?php
namespace Mautic\UserBundle\Model;
use Mautic\CoreBundle\Model\FormModel;
use Mautic\UserBundle\Entity\Role;
use Mautic\UserBundle\Entity\RoleRepository;
use Mautic\UserBundle\Event\RoleEvent;
use Mautic\UserBundle\Form\Type\RoleType;
use Mautic\UserBundle\UserEvents;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\PreconditionRequiredHttpException;
use Symfony\Contracts\EventDispatcher\Event;
/**
* @extends FormModel<Role>
*/
class RoleModel extends FormModel
{
public function getRepository(): RoleRepository
{
return $this->em->getRepository(Role::class);
}
public function getPermissionBase(): string
{
return 'user:roles';
}
/**
* @throws MethodNotAllowedHttpException
*/
public function saveEntity($entity, $unlock = true): void
{
if (!$entity instanceof Role) {
throw new MethodNotAllowedHttpException(['Role'], 'Entity must be of class Role()');
}
$isNew = ($entity->getId()) ? 0 : 1;
if (!$isNew) {
// delete all existing
$this->em->getRepository(\Mautic\UserBundle\Entity\Permission::class)->purgeRolePermissions($entity);
}
parent::saveEntity($entity, $unlock);
}
/**
* Generate the role's permissions.
*
* @param array $rawPermissions (i.e. from request)
*/
public function setRolePermissions(Role &$entity, $rawPermissions): void
{
if (!is_array($rawPermissions)) {
return;
}
// set permissions if applicable and if the user is not an admin
$permissions = (!$entity->isAdmin() && !empty($rawPermissions)) ?
$this->security->generatePermissions($rawPermissions) :
[];
foreach ($permissions as $permissionEntity) {
$entity->addPermission($permissionEntity);
}
$entity->setRawPermissions($rawPermissions);
}
/**
* @throws PreconditionRequiredHttpException
*/
public function deleteEntity($entity): void
{
if (!$entity instanceof Role) {
throw new MethodNotAllowedHttpException(['Role'], 'Entity must be of class Role()');
}
$users = $this->em->getRepository(\Mautic\UserBundle\Entity\User::class)->findByRole($entity);
if (count($users)) {
throw new PreconditionRequiredHttpException($this->translator->trans('mautic.user.role.error.deletenotallowed', ['%name%' => $entity->getName()], 'flashes'));
}
parent::deleteEntity($entity);
}
/**
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): \Symfony\Component\Form\FormInterface
{
if (!$entity instanceof Role) {
throw new MethodNotAllowedHttpException(['Role']);
}
if (!empty($action)) {
$options['action'] = $action;
}
return $formFactory->create(RoleType::class, $entity, $options);
}
public function getEntity($id = null): ?Role
{
if (null === $id) {
return new Role();
}
return parent::getEntity($id);
}
/**
* @throws MethodNotAllowedHttpException
*/
protected function dispatchEvent($action, &$entity, $isNew = false, Event $event = null): ?Event
{
if (!$entity instanceof Role) {
throw new MethodNotAllowedHttpException(['Role'], 'Entity must be of class Role()');
}
switch ($action) {
case 'pre_save':
$name = UserEvents::ROLE_PRE_SAVE;
break;
case 'post_save':
$name = UserEvents::ROLE_POST_SAVE;
break;
case 'pre_delete':
$name = UserEvents::ROLE_PRE_DELETE;
break;
case 'post_delete':
$name = UserEvents::ROLE_POST_DELETE;
break;
default:
return null;
}
if ($this->dispatcher->hasListeners($name)) {
if (empty($event)) {
$event = new RoleEvent($entity, $isNew);
$event->setEntityManager($this->em);
}
$this->dispatcher->dispatch($event, $name);
return $event;
}
return null;
}
}
|