Passed
Push — devel-3.0 ( c503ba...513ab0 )
by Rubén
03:26
created

UserGroupController::viewAction()   A

Complexity

Conditions 2
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 6
nop 0
dl 0
loc 20
rs 9.8333
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\Modules\Api\Controllers;
26
27
use SP\Core\Acl\ActionsInterface;
28
use SP\Core\Events\Event;
29
use SP\Core\Events\EventMessage;
30
use SP\DataModel\ItemSearchData;
31
use SP\DataModel\UserGroupData;
32
use SP\Modules\Api\Controllers\Help\TagHelp;
33
use SP\Services\Api\ApiResponse;
34
use SP\Services\UserGroup\UserGroupService;
35
36
/**
37
 * Class UserGroupController
38
 *
39
 * @package SP\Modules\Api\Controllers
40
 */
41
final class UserGroupController extends ControllerBase
42
{
43
    /**
44
     * @var UserGroupService
45
     */
46
    private $userGroupService;
47
48
    /**
49
     * viewAction
50
     */
51
    public function viewAction()
52
    {
53
        try {
54
            $this->setupApi(ActionsInterface::GROUP_VIEW);
55
56
            $id = $this->apiService->getParamInt('id', true);
57
            $userGroupData = $this->userGroupService->getById($id);
58
59
            $this->eventDispatcher->notifyEvent('show.userGroup',
60
                new Event($this, EventMessage::factory()
61
                    ->addDescription(__u('Grupo visualizado'))
62
                    ->addDetail(__u('Nombre'), $userGroupData->getName())
63
                    ->addDetail('ID', $id))
64
            );
65
66
            $this->returnResponse(ApiResponse::makeSuccess($userGroupData, $id));
67
        } catch (\Exception $e) {
68
            processException($e);
69
70
            $this->returnResponseException($e);
71
        }
72
    }
73
74
    /**
75
     * createAction
76
     */
77
    public function createAction()
78
    {
79
        try {
80
            $this->setupApi(ActionsInterface::GROUP_CREATE);
81
82
            $userGroupData = new UserGroupData();
83
            $userGroupData->setName($this->apiService->getParamString('name', true));
84
            $userGroupData->setDescription($this->apiService->getParamString('description'));
85
            $userGroupData->setUsers($this->apiService->getParamArray('usersId'));
86
87
            $id = $this->userGroupService->create($userGroupData);
88
89
            $this->eventDispatcher->notifyEvent('create.userGroup',
90
                new Event($this, EventMessage::factory()
91
                    ->addDescription(__u('Grupo creado'))
92
                    ->addDetail(__u('Nombre'), $userGroupData->getName())
93
                    ->addDetail('ID', $id))
94
            );
95
96
            $this->returnResponse(ApiResponse::makeSuccess($userGroupData, $id, __('Grupo creado')));
97
        } catch (\Exception $e) {
98
            processException($e);
99
100
            $this->returnResponseException($e);
101
        }
102
    }
103
104
    /**
105
     * editAction
106
     */
107
    public function editAction()
108
    {
109
        try {
110
            $this->setupApi(ActionsInterface::GROUP_EDIT);
111
112
            $userGroupData = new UserGroupData();
113
            $userGroupData->setId($this->apiService->getParamInt('id', true));
114
            $userGroupData->setName($this->apiService->getParamString('name', true));
115
            $userGroupData->setDescription($this->apiService->getParamString('description'));
116
            $userGroupData->setUsers($this->apiService->getParamArray('usersId'));
117
118
            $this->userGroupService->update($userGroupData);
119
120
            $this->eventDispatcher->notifyEvent('edit.userGroup',
121
                new Event($this, EventMessage::factory()
122
                    ->addDescription(__u('Grupo actualizado'))
123
                    ->addDetail(__u('Nombre'), $userGroupData->getName())
124
                    ->addDetail('ID', $userGroupData->getId()))
125
            );
126
127
            $this->returnResponse(ApiResponse::makeSuccess($userGroupData, $userGroupData->getId(), __('Grupo actualizado')));
128
        } catch (\Exception $e) {
129
            processException($e);
130
131
            $this->returnResponseException($e);
132
        }
133
    }
134
135
    /**
136
     * deleteAction
137
     */
138
    public function deleteAction()
139
    {
140
        try {
141
            $this->setupApi(ActionsInterface::GROUP_DELETE);
142
143
            $id = $this->apiService->getParamInt('id', true);
144
145
            $userGroupData = $this->userGroupService->getById($id);
146
147
            $this->userGroupService->delete($id);
148
149
            $this->eventDispatcher->notifyEvent('delete.userGroup',
150
                new Event($this, EventMessage::factory()
151
                    ->addDescription(__u('Grupo eliminado'))
152
                    ->addDetail(__u('Nombre'), $userGroupData->getName())
153
                    ->addDetail('ID', $id))
154
            );
155
156
            $this->returnResponse(ApiResponse::makeSuccess($userGroupData, $id, __('Grupo eliminado')));
157
        } catch (\Exception $e) {
158
            processException($e);
159
160
            $this->returnResponseException($e);
161
        }
162
    }
163
164
    /**
165
     * searchAction
166
     */
167
    public function searchAction()
168
    {
169
        try {
170
            $this->setupApi(ActionsInterface::GROUP_SEARCH);
171
172
            $itemSearchData = new ItemSearchData();
173
            $itemSearchData->setSeachString($this->apiService->getParamString('text'));
174
            $itemSearchData->setLimitCount($this->apiService->getParamInt('count', false, self::SEARCH_COUNT_ITEMS));
175
176
            $this->eventDispatcher->notifyEvent('search.userGroup', new Event($this));
177
178
            $this->returnResponse(ApiResponse::makeSuccess($this->userGroupService->search($itemSearchData)->getDataAsArray()));
179
        } catch (\Exception $e) {
180
            processException($e);
181
182
            $this->returnResponseException($e);
183
        }
184
    }
185
186
    /**
187
     * @throws \DI\DependencyException
188
     * @throws \DI\NotFoundException
189
     * @throws \SP\Core\Exceptions\InvalidClassException
190
     */
191
    protected function initialize()
192
    {
193
        $this->userGroupService = $this->dic->get(UserGroupService::class);
194
        $this->apiService->setHelpClass(TagHelp::class);
195
    }
196
}