Passed
Push — 3.0 ( b611c9...572608 )
by Rubén
03:38
created

AclHandler::updateEvent()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 6
nop 2
dl 0
loc 14
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Providers\Acl;
26
27
use DI\Container;
28
use Psr\Container\ContainerInterface;
29
use SP\Core\Events\Event;
30
use SP\Core\Events\EventReceiver;
31
use SP\Providers\EventsTrait;
32
use SP\Providers\Provider;
33
use SP\Services\Account\AccountAclService;
34
use SP\Services\UserGroup\UserGroupService;
35
use SP\Services\UserProfile\UserProfileService;
36
use SplSubject;
37
38
/**
39
 * Class AclHandler
40
 *
41
 * @package SP\Providers\Acl
42
 */
43
final class AclHandler extends Provider implements EventReceiver
44
{
45
    use EventsTrait;
46
47
    const EVENTS = [
48
        'edit.userProfile',
49
        'edit.user',
50
        'edit.userGroup',
51
        'delete.user',
52
        'delete.user.selection'
53
    ];
54
55
    /**
56
     * @var string
57
     */
58
    private $events;
59
    /**
60
     * @var ContainerInterface
61
     */
62
    private $dic;
63
64
    /**
65
     * Devuelve los eventos que implementa el observador
66
     *
67
     * @return array
68
     */
69
    public function getEvents()
70
    {
71
        return self::EVENTS;
72
    }
73
74
    /**
75
     * Devuelve los eventos que implementa el observador en formato cadena
76
     *
77
     * @return string
78
     */
79
    public function getEventsString()
80
    {
81
        return $this->events;
82
    }
83
84
    /**
85
     * Receive update from subject
86
     *
87
     * @link  https://php.net/manual/en/splobserver.update.php
88
     *
89
     * @param SplSubject $subject <p>
90
     *                            The <b>SplSubject</b> notifying the observer of an update.
91
     *                            </p>
92
     *
93
     * @return void
94
     * @since 5.1.0
95
     */
96
    public function update(SplSubject $subject)
97
    {
98
        $this->updateEvent('update', new Event($subject));
99
    }
100
101
    /**
102
     * Evento de actualización
103
     *
104
     * @param string $eventType Nombre del evento
105
     * @param Event  $event     Objeto del evento
106
     */
107
    public function updateEvent($eventType, Event $event)
108
    {
109
        switch ($eventType) {
110
            case 'edit.userProfile':
111
                $this->processUserProfile($event);
112
                break;
113
            case 'edit.user':
114
            case 'delete.user':
115
            case 'delete.user.selection':
116
                $this->processUser($event);
117
                break;
118
            case 'edit.userGroup':
119
                $this->processUserGroup($event);
120
                break;
121
        }
122
    }
123
124
    /**
125
     * @param Event $event
126
     */
127
    private function processUserProfile(Event $event)
128
    {
129
        try {
130
            $eventMessage = $event->getEventMessage();
131
            $extra = $eventMessage->getExtra();
132
133
            if (isset($extra['userProfileId'])) {
134
                $userProfileService = $this->dic->get(UserProfileService::class);
135
136
                foreach ($userProfileService->getUsersForProfile($extra['userProfileId'][0]) as $user) {
137
                    AccountAclService::clearAcl($user->id);
138
                }
139
            }
140
        } catch (\Exception $e) {
141
            processException($e);
142
        }
143
    }
144
145
    /**
146
     * @param Event $event
147
     */
148
    private function processUser(Event $event)
149
    {
150
        $eventMessage = $event->getEventMessage();
151
        $extra = $eventMessage->getExtra();
152
153
        if (isset($extra['userId'])) {
154
            foreach ($extra['userId'] as $id) {
155
                AccountAclService::clearAcl($id);
156
            }
157
        }
158
    }
159
160
    /**
161
     * @param Event $event
162
     */
163
    private function processUserGroup(Event $event)
164
    {
165
        try {
166
            $eventMessage = $event->getEventMessage();
167
            $extra = $eventMessage->getExtra();
168
169
            if (isset($extra['userGroupId'])) {
170
                $userGroupService = $this->dic->get(UserGroupService::class);
171
172
                foreach ($userGroupService->getUsageByUsers($extra['userGroupId'][0]) as $user) {
173
                    AccountAclService::clearAcl($user->id);
174
                }
175
            }
176
        } catch (\Exception $e) {
177
            processException($e);
178
        }
179
    }
180
181
    /**
182
     * @param Container $dic
183
     */
184
    protected function initialize(Container $dic)
185
    {
186
        $this->dic = $dic;
187
        $this->events = $this->parseEventsToRegex(self::EVENTS);
188
    }
189
}