Test Failed
Pull Request — master (#22)
by Maximo
04:37
created

RolesAccesListController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Test Coverage

Coverage 56.92%

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 191
ccs 37
cts 65
cp 0.5692
rs 10
c 0
b 0
f 0
wmc 19

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 38 5
A getById() 0 18 3
A delete() 0 15 3
B edit() 0 47 7
A onConstruct() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Gewaer\Models\AccessList;
8
use Phalcon\Http\Response;
9
use Phalcon\Acl\Role;
10
use Phalcon\Validation;
11
use Phalcon\Validation\Validator\PresenceOf;
12
use Gewaer\Models\Apps;
13
use Gewaer\Exception\NotFoundHttpException;
14
use Gewaer\Exception\ServerErrorHttpException;
15
use Gewaer\Models\Roles;
16
17
/**
18
 * Class RolesController
19
 *
20
 * @package Gewaer\Api\Controllers
21
 *
22
 * @property Users $userData
23
 * @property Request $request
24
 * @property Config $config
25
 * @property \Baka\Mail\Message $mail
26
 * @property Apps $app
27
 *
28
 */
29
class RolesAccesListController extends BaseController
30
{
31
    /*
32
     * fields we accept to create
33
     *
34
     * @var array
35
     */
36
    protected $createFields = [];
37
38
    /*
39
     * fields we accept to create
40
     *
41
     * @var array
42
     */
43
    protected $updateFields = [];
44
45
    /**
46
     * set objects
47
     *
48
     * @return void
49
     */
50 5
    public function onConstruct()
51
    {
52 5
        $this->model = new AccessList();
53
54
        //get the list of roes for the systema + my company
55 5
        $this->additionalSearchFields = [
56 5
            ['is_deleted', ':', 0],
57 5
            ['apps_id', ':', $this->app->getId()],
58
        ];
59 5
    }
60
61
    /**
62
     * Add a new item
63
     *
64
     * @method POST
65
     * @url /v1/roles-acceslist
66
     *
67
     * @return Phalcon\Http\Response
0 ignored issues
show
Bug introduced by
The type Gewaer\Api\Controllers\Phalcon\Http\Response was not found. Did you mean Phalcon\Http\Response? If so, make sure to prefix the type with \.
Loading history...
68
     */
69 1
    public function create() : Response
70
    {
71 1
        $request = $this->request->getPost();
72
73 1
        if (empty($request)) {
74
            $request = $this->request->getJsonRawBody(true);
75
        }
76
77
        //Ok let validate user password
78 1
        $validation = new Validation();
79 1
        $validation->add('roles', new PresenceOf(['message' => _('Role information is required.')]));
80 1
        $validation->add('access', new PresenceOf(['message' => _('Access list is required.')]));
81
82
        //validate this form for password
83 1
        $messages = $validation->validate($request);
84 1
        if (count($messages)) {
85
            foreach ($messages as $message) {
86
                throw new ServerErrorHttpException((string)$message);
87
            }
88
        }
89
90
        //set the company and app
91 1
        $this->acl->setCompany($this->userData->DefaultCompany);
0 ignored issues
show
Bug Best Practice introduced by
The property acl does not exist on Gewaer\Api\Controllers\RolesAccesListController. Since you implemented __get, consider adding a @property annotation.
Loading history...
92 1
        $this->acl->setApp($this->app);
93
94 1
        $scope = 1;
95
        //create the role , the scope is level 1 , that means user
96 1
        $this->acl->addRole(new Role($request['roles']['name'], $request['roles']['description']), $scope);
97
98
        /**
99
         * we always deny permision, by default the canvas set allow to all
100
         * so we only have to take away permissions
101
         */
102 1
        foreach ($request['access'] as $access) {
103 1
            $this->acl->deny($request['roles']['name'], $access['resources_name'], $access['access_name']);
104
        }
105
106
        return $this->response($request['roles']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($request['roles']) returns the type Phalcon\Http\Response which is incompatible with the documented return type Gewaer\Api\Controllers\Phalcon\Http\Response.
Loading history...
107
    }
108
109
    /**
110
     * get item
111
     *
112
     * @param mixed $id
113
     *
114
     * @method GET
115
     * @url /v1/roles-acceslist/{id}
116
     *
117
     * @return Phalcon\Http\Response
118
     */
119
    public function getById($id) : Response
120
    {
121
        $objectInfo = $this->model->findFirst([
122
            'roles_id = ?0 AND is_deleted = 0 AND apps_id in (?1, ?2)',
123
            'bind' => [$id, $this->app->getId(), Apps::GEWAER_DEFAULT_APP_ID],
124
        ]);
125
126
        //get relationship
127
        if ($this->request->hasQuery('relationships')) {
128
            $relationships = $this->request->getQuery('relationships', 'string');
129
130
            $objectInfo = QueryParser::parseRelationShips($relationships, $objectInfo);
0 ignored issues
show
Bug introduced by
The type Gewaer\Api\Controllers\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...
131
        }
132
133
        if ($objectInfo) {
134
            return $this->response($objectInfo);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($objectInfo) returns the type Phalcon\Http\Response which is incompatible with the documented return type Gewaer\Api\Controllers\Phalcon\Http\Response.
Loading history...
135
        } else {
136
            throw new NotFoundHttpException('Record not found');
137
        }
138
    }
139
140
    /**
141
     * Update a new Entry
142
     *
143
     * @method PUT
144
     * @url /v1/roles-acceslist/{id}
145
     *
146
     * @return Phalcon\Http\Response
147
     */
148 1
    public function edit($id) : Response
149
    {
150 1
        if (!$role = Roles::findFirst($id)) {
151
            throw new NotFoundHttpException('Record not found');
152
        }
153
154 1
        $request = $this->request->getPut();
155
156 1
        if (empty($request)) {
157
            $request = $this->request->getJsonRawBody(true);
158
        }
159
160
        //Ok let validate user password
161 1
        $validation = new Validation();
162 1
        $validation->add('roles', new PresenceOf(['message' => _('Role information is required.')]));
163 1
        $validation->add('access', new PresenceOf(['message' => _('Access list is required.')]));
164
165
        //validate this form for password
166 1
        $messages = $validation->validate($request);
167 1
        if (count($messages)) {
168
            foreach ($messages as $message) {
169
                throw new ServerErrorHttpException((string)$message);
170
            }
171
        }
172
173
        //set the company and app
174 1
        $this->acl->setCompany($this->userData->DefaultCompany);
0 ignored issues
show
Bug Best Practice introduced by
The property acl does not exist on Gewaer\Api\Controllers\RolesAccesListController. Since you implemented __get, consider adding a @property annotation.
Loading history...
175 1
        $this->acl->setApp($this->app);
176
177 1
        $role->name = $request['roles']['name'];
178 1
        $role->description = $request['roles']['description'];
179 1
        if (!$role->update()) {
180
            throw new ServerErrorHttpException((string) current($role->getMessages()));
181
        }
182
183
        //delete the acces list before hand
184 1
        AccessList::deleteAllByRole($role);
185
186
        /**
187
         * we always deny permision, by default the canvas set allow to all
188
         * so we only have to take away permissions
189
         */
190 1
        foreach ($request['access'] as $access) {
191 1
            $this->acl->deny($request['roles']['name'], $access['resources_name'], $access['access_name']);
192
        }
193
194
        return $this->response($role);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($role) returns the type Phalcon\Http\Response which is incompatible with the documented return type Gewaer\Api\Controllers\Phalcon\Http\Response.
Loading history...
195
    }
196
197
    /**
198
     * delete a new Entry
199
     *
200
     * @method DELETE
201
     * @url /v1/roles-acceslist/{id}
202
     *
203
     * @return Phalcon\Http\Response
204
     */
205
    public function delete($id) : Response
206
    {
207
        if ($role = Roles::findFirst($id)) {
208
            if ($this->softDelete == 1) {
209
                $role->softDelete();
210
            } else {
211
                //delete the acces list before hand
212
                AccessList::deleteAllByRole($role);
213
214
                $role->delete();
215
            }
216
217
            return $this->response(['Delete Successfully']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response(a...'Delete Successfully')) returns the type Phalcon\Http\Response which is incompatible with the documented return type Gewaer\Api\Controllers\Phalcon\Http\Response.
Loading history...
218
        } else {
219
            throw new NotFoundHttpException('Record not found');
220
        }
221
    }
222
}
223