Issues (3627)

app/bundles/UserBundle/Model/RoleModel.php (2 issues)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\UserBundle\Model;
13
14
use Mautic\CoreBundle\Model\FormModel;
15
use Mautic\UserBundle\Entity\Role;
16
use Mautic\UserBundle\Event\RoleEvent;
17
use Mautic\UserBundle\Form\Type\RoleType;
18
use Mautic\UserBundle\UserEvents;
19
use Symfony\Component\EventDispatcher\Event;
20
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
21
use Symfony\Component\HttpKernel\Exception\PreconditionRequiredHttpException;
22
23
/**
24
 * Class RoleModel.
25
 */
26
class RoleModel extends FormModel
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getRepository()
32
    {
33
        return $this->em->getRepository('MauticUserBundle:Role');
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getPermissionBase()
40
    {
41
        return 'user:roles';
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     *
47
     * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
48
     */
49
    public function saveEntity($entity, $unlock = true)
50
    {
51
        if (!$entity instanceof Role) {
52
            throw new MethodNotAllowedHttpException(['Role'], 'Entity must be of class Role()');
53
        }
54
55
        $isNew = ($entity->getId()) ? 0 : 1;
56
57
        if (!$isNew) {
58
            //delete all existing
59
            $this->em->getRepository('MauticUserBundle:Permission')->purgeRolePermissions($entity);
60
        }
61
62
        parent::saveEntity($entity, $unlock);
63
    }
64
65
    /**
66
     * Generate the role's permissions.
67
     *
68
     * @param array $rawPermissions (i.e. from request)
69
     */
70
    public function setRolePermissions(Role &$entity, $rawPermissions)
71
    {
72
        if (!is_array($rawPermissions)) {
0 ignored issues
show
The condition is_array($rawPermissions) is always true.
Loading history...
73
            return;
74
        }
75
76
        //set permissions if applicable and if the user is not an admin
77
        $permissions = (!$entity->isAdmin() && !empty($rawPermissions)) ?
78
            $this->security->generatePermissions($rawPermissions) :
79
            [];
80
81
        foreach ($permissions as $permissionEntity) {
82
            $entity->addPermission($permissionEntity);
83
        }
84
85
        $entity->setRawPermissions($rawPermissions);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     *
91
     * @throws PreconditionRequiredHttpException
92
     */
93
    public function deleteEntity($entity)
94
    {
95
        if (!$entity instanceof Role) {
96
            throw new MethodNotAllowedHttpException(['Role'], 'Entity must be of class Role()');
97
        }
98
99
        $users = $this->em->getRepository('MauticUserBundle:User')->findByRole($entity);
0 ignored issues
show
The method findByRole() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findBy()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        $users = $this->em->getRepository('MauticUserBundle:User')->/** @scrutinizer ignore-call */ findByRole($entity);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
        if (count($users)) {
101
            throw new PreconditionRequiredHttpException($this->translator->trans('mautic.user.role.error.deletenotallowed', ['%name%' => $entity->getName()], 'flashes'));
102
        }
103
104
        parent::deleteEntity($entity);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     *
110
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
111
     */
112
    public function createForm($entity, $formFactory, $action = null, $options = [])
113
    {
114
        if (!$entity instanceof Role) {
115
            throw new MethodNotAllowedHttpException(['Role']);
116
        }
117
118
        if (!empty($action)) {
119
            $options['action'] = $action;
120
        }
121
122
        return $formFactory->create(RoleType::class, $entity, $options);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getEntity($id = null)
129
    {
130
        if (null === $id) {
131
            return new Role();
132
        }
133
134
        return parent::getEntity($id);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     *
140
     * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
141
     */
142
    protected function dispatchEvent($action, &$entity, $isNew = false, Event $event = null)
143
    {
144
        if (!$entity instanceof Role) {
145
            throw new MethodNotAllowedHttpException(['Role'], 'Entity must be of class Role()');
146
        }
147
148
        switch ($action) {
149
            case 'pre_save':
150
                $name = UserEvents::ROLE_PRE_SAVE;
151
                break;
152
            case 'post_save':
153
                $name = UserEvents::ROLE_POST_SAVE;
154
                break;
155
            case 'pre_delete':
156
                $name = UserEvents::ROLE_PRE_DELETE;
157
                break;
158
            case 'post_delete':
159
                $name = UserEvents::ROLE_POST_DELETE;
160
                break;
161
            default:
162
                return null;
163
        }
164
165
        if ($this->dispatcher->hasListeners($name)) {
166
            if (empty($event)) {
167
                $event = new RoleEvent($entity, $isNew);
168
                $event->setEntityManager($this->em);
169
            }
170
            $this->dispatcher->dispatch($name, $event);
171
172
            return $event;
173
        }
174
175
        return null;
176
    }
177
}
178