Test Failed
Pull Request — master (#160)
by Maximo
06:15
created

RolesAccesListController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 180
ccs 0
cts 75
cp 0
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A edit() 0 34 2
A delete() 0 11 2
A onConstruct() 0 8 1
A copy() 0 7 2
A create() 0 29 2
A getById() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Api\Controllers;
6
7
use Canvas\Models\AccessList;
8
use Phalcon\Http\Response;
9
use Phalcon\Acl\Role;
0 ignored issues
show
Bug introduced by
The type Phalcon\Acl\Role was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Phalcon\Validation;
11
use Phalcon\Validation\Validator\PresenceOf;
0 ignored issues
show
Bug introduced by
The type Phalcon\Validation\Validator\PresenceOf was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Canvas\Models\Apps;
13
use Canvas\Exception\NotFoundHttpException;
14
use Canvas\Exception\ServerErrorHttpException;
15
use Canvas\Models\Roles;
16
use Baka\Http\QueryParser;
0 ignored issues
show
Bug introduced by
The type Baka\Http\QueryParser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Canvas\Validation as CanvasValidation;
18
19
/**
20
 * Class RolesController.
21
 *
22
 * @package Canvas\Api\Controllers
23
 *
24
 * @property Users $userData
25
 * @property Request $request
26
 * @property Config $config
27
 * @property \Canvas\Acl\Manager  $acl
28
 * @property \Baka\Mail\Message $mail
29
 * @property Apps $app
30
 *
31
 */
32
class RolesAccesListController extends BaseController
33
{
34
    /*
35
     * fields we accept to create
36
     *
37
     * @var array
38
     */
39
    protected $createFields = [];
40
41
    /*
42
     * fields we accept to create
43
     *
44
     * @var array
45
     */
46
    protected $updateFields = [];
47
48
    /**
49
     * set objects.
50
     *
51
     * @return void
52
     */
53
    public function onConstruct()
54
    {
55
        $this->model = new AccessList();
56
57
        //get the list of roes for the systema + my company
58
        $this->additionalSearchFields = [
59
            ['is_deleted', ':', '0'],
60
            ['apps_id', ':', '0|' . $this->app->getId()],
61
        ];
62
    }
63
64
    /**
65
     * Add a new item.
66
     *
67
     * @method POST
68
     * @url /v1/roles-acceslist
69
     *
70
     * @return Response
71
     */
72
    public function create() : Response
73
    {
74
        $request = $this->request->getPostData();
75
76
        //Ok let validate user password
77
        $validation = new CanvasValidation();
78
        $validation->add('roles', new PresenceOf(['message' => _('Role information is required.')]));
79
        $validation->add('access', new PresenceOf(['message' => _('Access list is required.')]));
80
81
        //validate this form for password
82
        $validation->validate($request);
83
84
        //set the company and app
85
        $this->acl->setCompany($this->userData->getDefaultCompany());
86
        $this->acl->setApp($this->app);
87
88
        $scope = 1;
89
        //create the role , the scope is level 1 , that means user
90
        $this->acl->addRole(new Role($request['roles']['name'], $request['roles']['description']), $scope);
91
92
        /**
93
         * we always deny permision, by default the canvas set allow to all
94
         * so we only have to take away permissions.
95
         */
96
        foreach ($request['access'] as $access) {
97
            $this->acl->deny($request['roles']['name'], $access['resources_name'], $access['access_name']);
98
        }
99
100
        return $this->response($request['roles']);
101
    }
102
103
    /**
104
     * get item.
105
     *
106
     * @param mixed $id
107
     *
108
     * @method GET
109
     * @url /v1/roles-acceslist/{id}
110
     *
111
     * @return Response
112
     */
113
    public function getById($id) : Response
114
    {
115
        $objectInfo = $this->model->findFirst([
116
            'roles_id = ?0 AND is_deleted = 0 AND apps_id in (?1, ?2)',
117
            'bind' => [$id, $this->app->getId(), Apps::CANVAS_DEFAULT_APP_ID],
118
        ]);
119
120
        //get relationship
121
        if ($this->request->hasQuery('relationships')) {
122
            $relationships = $this->request->getQuery('relationships', 'string');
123
124
            $objectInfo = QueryParser::parseRelationShips($relationships, $objectInfo);
125
        }
126
127
        if ($objectInfo) {
128
            return $this->response($objectInfo);
129
        } else {
130
            throw new NotFoundHttpException('Record not found');
131
        }
132
    }
133
134
    /**
135
     * Update a new Entry.
136
     *
137
     * @method PUT
138
     * @url /v1/roles-acceslist/{id}
139
     *
140
     * @return Response
141
     */
142
    public function edit($id) : Response
143
    {
144
        $role = Roles::getById((int) $id);
145
146
        $request = $this->request->getPutData();
147
148
        //Ok let validate user password
149
        $validation = new CanvasValidation();
150
        $validation->add('roles', new PresenceOf(['message' => _('Role information is required.')]));
151
        $validation->add('access', new PresenceOf(['message' => _('Access list is required.')]));
152
153
        //validate this form for password
154
        $validation->validate($request);
155
156
        //set the company and app
157
        $this->acl->setCompany($this->userData->getDefaultCompany());
158
        $this->acl->setApp($this->app);
159
160
        $role->name = $request['roles']['name'];
161
        $role->description = $request['roles']['description'];
162
        $role->updateOrFail();
163
164
        //clean previous records
165
        $role->accesList->delete();
166
167
        /**
168
         * we always deny permision, by default the canvas set allow to all
169
         * so we only have to take away permissions.
170
         */
171
        foreach ($request['access'] as $access) {
172
            $this->acl->deny($request['roles']['name'], $access['resources_name'], $access['access_name']);
173
        }
174
175
        return $this->response($role);
176
    }
177
178
    /**
179
     * Copy a existen.
180
     *
181
     * @param int $id
182
     * @return Response
183
     */
184
    public function copy($id) : Response
185
    {
186
        if (!$role = Roles::getById((int) $id)) {
187
            throw new NotFoundHttpException('Record not found');
188
        }
189
190
        return $this->response($role->copy());
191
    }
192
193
    /**
194
     * delete a new Entry.
195
     *
196
     * @method DELETE
197
     * @url /v1/roles-acceslist/{id}
198
     *
199
     * @return Response
200
     */
201
    public function delete($id) : Response
202
    {
203
        $role = Roles::getById($id);
204
205
        if ($this->softDelete == 1) {
206
            $role->softDelete();
207
        } else {
208
            $role->delete();
209
        }
210
211
        return $this->response(['Delete Successfully']);
212
    }
213
}
214