1 | <?php |
||||
2 | declare(strict_types=1); |
||||
3 | |||||
4 | namespace Gewaer\Models; |
||||
5 | |||||
6 | use Gewaer\Exception\ServerErrorHttpException; |
||||
7 | use Baka\Auth\Models\Companies as BakaCompanies; |
||||
8 | use Phalcon\Di; |
||||
9 | use Phalcon\Validation; |
||||
10 | use Phalcon\Validation\Validator\PresenceOf; |
||||
11 | use Phalcon\Validation\Validator\StringLength; |
||||
12 | |||||
13 | class Roles extends AbstractModel |
||||
14 | { |
||||
15 | /** |
||||
16 | * |
||||
17 | * @var integer |
||||
18 | */ |
||||
19 | public $id; |
||||
20 | |||||
21 | /** |
||||
22 | * |
||||
23 | * @var string |
||||
24 | */ |
||||
25 | public $name; |
||||
26 | |||||
27 | /** |
||||
28 | * |
||||
29 | * @var string |
||||
30 | */ |
||||
31 | public $description; |
||||
32 | |||||
33 | /** |
||||
34 | * |
||||
35 | * @var integer |
||||
36 | */ |
||||
37 | public $scope; |
||||
38 | |||||
39 | /** |
||||
40 | * |
||||
41 | * @var integer |
||||
42 | */ |
||||
43 | public $company_id; |
||||
44 | |||||
45 | /** |
||||
46 | * |
||||
47 | * @var int |
||||
48 | */ |
||||
49 | public $apps_id; |
||||
50 | |||||
51 | /** |
||||
52 | * |
||||
53 | * @var string |
||||
54 | */ |
||||
55 | public $created_at; |
||||
56 | |||||
57 | /** |
||||
58 | * |
||||
59 | * @var string |
||||
60 | */ |
||||
61 | public $updated_at; |
||||
62 | |||||
63 | /** |
||||
64 | * |
||||
65 | * @var integer |
||||
66 | */ |
||||
67 | public $is_deleted; |
||||
68 | |||||
69 | /** |
||||
70 | * Default ACL company |
||||
71 | * |
||||
72 | */ |
||||
73 | const DEFAULT_ACL_COMPANY_ID = 0; |
||||
74 | const DEFAULT_ACL_APP_ID = 0; |
||||
75 | |||||
76 | /** |
||||
77 | * Initialize method for model. |
||||
78 | */ |
||||
79 | 18 | public function initialize() |
|||
80 | { |
||||
81 | 18 | $this->setSource('roles'); |
|||
82 | |||||
83 | 18 | $this->hasMany( |
|||
84 | 18 | 'id', |
|||
85 | 18 | 'Gewaer\Models\AccessList', |
|||
86 | 18 | 'roles_id', |
|||
87 | 18 | ['alias' => 'accesList'] |
|||
88 | ); |
||||
89 | 18 | } |
|||
90 | |||||
91 | /** |
||||
92 | * Validations and business logic |
||||
93 | */ |
||||
94 | 2 | public function validation() |
|||
95 | { |
||||
96 | 2 | $validator = new Validation(); |
|||
97 | |||||
98 | 2 | $validator->add( |
|||
99 | 2 | 'name', |
|||
100 | 2 | new PresenceOf([ |
|||
101 | 2 | 'field' => 'name', |
|||
102 | 'required' => true, |
||||
103 | ]) |
||||
104 | ); |
||||
105 | |||||
106 | 2 | $validator->add( |
|||
107 | 2 | 'description', |
|||
108 | 2 | new PresenceOf([ |
|||
109 | 2 | 'field' => 'description', |
|||
110 | 'required' => true, |
||||
111 | ]) |
||||
112 | ); |
||||
113 | |||||
114 | 2 | $validator->add( |
|||
115 | 2 | 'name', |
|||
116 | 2 | new StringLength([ |
|||
117 | 2 | 'max' => 32, |
|||
118 | 2 | 'messageMinimum' => _('Role Name. Maxium 32 characters.'), |
|||
119 | ]) |
||||
120 | ); |
||||
121 | |||||
122 | 2 | return $this->validate($validator); |
|||
123 | } |
||||
124 | |||||
125 | /** |
||||
126 | * Returns table name mapped in the model. |
||||
127 | * |
||||
128 | * @return string |
||||
129 | */ |
||||
130 | 17 | public function getSource(): string |
|||
131 | { |
||||
132 | 17 | return 'roles'; |
|||
133 | } |
||||
134 | |||||
135 | /** |
||||
136 | * Get the entity by its name |
||||
137 | * |
||||
138 | * @param string $name |
||||
139 | * @return void |
||||
140 | */ |
||||
141 | public static function getByName(string $name) |
||||
142 | { |
||||
143 | return self::findFirst([ |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
144 | 'conditions' => 'name = ?0 AND company_id = ?1 AND apps_id = ?2 AND is_deleted = 0', |
||||
145 | 'bind' => [$name, Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId()] |
||||
146 | ]); |
||||
147 | } |
||||
148 | |||||
149 | /** |
||||
150 | * Get the entity by its name |
||||
151 | * |
||||
152 | * @param string $name |
||||
153 | * @return void |
||||
154 | */ |
||||
155 | 1 | public static function getById(int $id) |
|||
156 | { |
||||
157 | 1 | return self::findFirst([ |
|||
0 ignored issues
–
show
|
|||||
158 | 1 | 'conditions' => 'id = ?0 AND company_id in (?1, ?2) AND apps_id in (?3, ?4) AND is_deleted = 0', |
|||
159 | 1 | 'bind' => [$id, Di::getDefault()->getUserData()->default_company, Apps::GEWAER_DEFAULT_APP_ID, Di::getDefault()->getApp()->getId(), Apps::GEWAER_DEFAULT_APP_ID] |
|||
160 | ]); |
||||
161 | } |
||||
162 | |||||
163 | /** |
||||
164 | * Get the Role by it app name |
||||
165 | * |
||||
166 | * @param string $role |
||||
167 | * @return Roles |
||||
168 | */ |
||||
169 | 6 | public static function getByAppName(string $role, BakaCompanies $company): Roles |
|||
170 | { |
||||
171 | //echeck if we have a dot , taht means we are sending the specific app to use |
||||
172 | 6 | if (strpos($role, '.') === false) { |
|||
173 | throw new ServerErrorHttpException('ACL - We are expecting the app for this role'); |
||||
174 | } |
||||
175 | |||||
176 | 6 | $appRole = explode('.', $role); |
|||
177 | 6 | $role = $appRole[1]; |
|||
178 | 6 | $appName = $appRole[0]; |
|||
179 | |||||
180 | //look for the app and set it |
||||
181 | 6 | if (!$app = Apps::getACLApp($appName)) { |
|||
182 | throw new ServerErrorHttpException('ACL - No app found for this role'); |
||||
183 | } |
||||
184 | |||||
185 | 6 | return self::findFirst([ |
|||
186 | 6 | 'conditions' => 'apps_id in (?0, ?1) AND company_id in (?2 , ?3)', |
|||
187 | 6 | 'bind' => [$app->getId(), self::DEFAULT_ACL_APP_ID, $company->getId(), self::DEFAULT_ACL_COMPANY_ID] |
|||
188 | ]); |
||||
189 | } |
||||
190 | |||||
191 | /** |
||||
192 | * Duplicate a role with it access list |
||||
193 | * |
||||
194 | * @return bool |
||||
195 | */ |
||||
196 | public function copy(): Roles |
||||
197 | { |
||||
198 | $accesList = $this->accesList; |
||||
0 ignored issues
–
show
The property
accesList does not exist on Gewaer\Models\Roles . Since you implemented __get , consider adding a @property annotation.
![]() |
|||||
199 | |||||
200 | //remove id to create new record |
||||
201 | $this->name .= 'Copie'; |
||||
202 | $this->scope = 1; |
||||
203 | $this->id = null; |
||||
204 | $this->company_id = $this->di->getUserData()->default_company; |
||||
0 ignored issues
–
show
The property
di does not exist on Gewaer\Models\Roles . Since you implemented __get , consider adding a @property annotation.
![]() The method
getUserData() does not exist on Phalcon\Mvc\Model\Resultset .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||
205 | $this->apps_id = $this->di->getApp()->getId(); |
||||
0 ignored issues
–
show
The method
getApp() does not exist on Phalcon\Mvc\Model\Resultset .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||
206 | $this->save(); |
||||
207 | |||||
208 | foreach ($accesList as $access) { |
||||
209 | $copyAccessList = new AccessList(); |
||||
210 | $copyAccessList->apps_id = $this->apps_id; |
||||
211 | $copyAccessList->roles_id = $this->getId(); |
||||
212 | $copyAccessList->roles_name = $this->name; |
||||
213 | $copyAccessList->resources_name = $access->resources_name; |
||||
214 | $copyAccessList->access_name = $access->access_name; |
||||
215 | $copyAccessList->allowed = $access->allowed; |
||||
216 | $copyAccessList->create(); |
||||
217 | } |
||||
218 | |||||
219 | return $this; |
||||
0 ignored issues
–
show
|
|||||
220 | } |
||||
221 | } |
||||
222 |