1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Gewaer\Traits; |
||
6 | |||
7 | use Gewaer\Models\Roles; |
||
8 | use Gewaer\Models\UserRoles; |
||
9 | use Gewaer\Exception\ServerErrorHttpException; |
||
10 | use Gewaer\Exception\ModelException; |
||
11 | |||
12 | /** |
||
13 | * Trait FractalTrait |
||
14 | * |
||
15 | * @package Gewaer\Traits |
||
16 | */ |
||
17 | trait PermissionsTrait |
||
18 | { |
||
19 | /** |
||
20 | * Assigne a user this role |
||
21 | * Example: App.Role |
||
22 | * |
||
23 | * @param string $role |
||
24 | * @return boolean |
||
25 | */ |
||
26 | 1 | public function assignRole(string $role): bool |
|
27 | { |
||
28 | 1 | $role = Roles::getByAppName($role, $this->defaultCompany); |
|
29 | |||
30 | 1 | if (!is_object($role)) { |
|
31 | throw new ServerErrorHttpException('Role not found in DB'); |
||
32 | } |
||
33 | |||
34 | 1 | $userRole = UserRoles::findFirst([ |
|
35 | 1 | 'conditions' => 'users_id = ?0 and roles_id = ?1 and apps_id = ?2 and companies_id = ?3', |
|
36 | 1 | 'bind' => [$this->getId(), $role->getId(), $role->apps_id, $this->currentCompanyId()] |
|
1 ignored issue
–
show
Bug
introduced
by
![]() |
|||
37 | ]); |
||
38 | |||
39 | 1 | if (!is_object($userRole)) { |
|
40 | 1 | $userRole = new UserRoles(); |
|
41 | 1 | $userRole->users_id = $this->getId(); |
|
42 | 1 | $userRole->roles_id = $role->getId(); |
|
43 | 1 | $userRole->apps_id = $role->apps_id; |
|
44 | 1 | $userRole->companies_id = $this->currentCompanyId(); |
|
45 | 1 | if (!$userRole->save()) { |
|
46 | throw new ModelException((string) current($userRole->getMessages())); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | 1 | return true; |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * Remove a role for the current user |
||
55 | * Example: App.Role |
||
56 | * |
||
57 | * @param string $role |
||
58 | * @return boolean |
||
59 | */ |
||
60 | 1 | public function removeRole(string $role): bool |
|
61 | { |
||
62 | 1 | $role = Roles::getByAppName($role, $this->defaultCompany); |
|
63 | |||
64 | 1 | if (!is_object($role)) { |
|
65 | throw new ServerErrorHttpException('Role not found in DB'); |
||
66 | } |
||
67 | |||
68 | 1 | $userRole = UserRoles::findFirst([ |
|
69 | 1 | 'conditions' => 'users_id = ?0 and roles_id = ?1 and apps_id = ?2 and companies_id = ?3', |
|
70 | 1 | 'bind' => [$this->getId(), $role->getId(), $role->apps_id, $this->currentCompanyId()] |
|
71 | ]); |
||
72 | |||
73 | 1 | if (is_object($userRole)) { |
|
74 | 1 | return $userRole->delete(); |
|
75 | } |
||
76 | |||
77 | return false; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Check if the user has this role |
||
82 | * |
||
83 | * @param string $role |
||
84 | * @return boolean |
||
85 | */ |
||
86 | public function hasRole(string $role): bool |
||
87 | { |
||
88 | $role = Roles::getByAppName($role, $this->defaultCompany); |
||
89 | |||
90 | if (!is_object($role)) { |
||
91 | throw new ServerErrorHttpException('Role not found in DB'); |
||
92 | } |
||
93 | |||
94 | $userRole = UserRoles::findFirst([ |
||
95 | 'conditions' => 'users_id = ?0 and roles_id = ?1 and (apps_id = ?2 or apps_id = ?4) and companies_id = ?3', |
||
96 | 'bind' => [$this->getId(), $role->getId(), $role->apps_id, $this->currentCompanyId(), $this->di->getApp()->getId()] |
||
97 | ]); |
||
98 | |||
99 | if (is_object($userRole)) { |
||
100 | return true; |
||
101 | } |
||
102 | |||
103 | return false; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * At this current system / app can you do this? |
||
108 | * |
||
109 | * Example: resource.action |
||
110 | * Leads.add || leads.updates || lead.delete |
||
111 | * |
||
112 | * @param string $action |
||
113 | * @return boolean |
||
114 | */ |
||
115 | 2 | public function can(string $action): bool |
|
116 | { |
||
117 | //if we find the . then les |
||
118 | 2 | if (strpos($action, '.') === false) { |
|
119 | throw new ServerErrorHttpException('ACL - We are expecting the resource for this action'); |
||
120 | } |
||
121 | |||
122 | 2 | $action = explode('.', $action); |
|
123 | 2 | $resource = $action[0]; |
|
124 | 2 | $action = $action[1]; |
|
125 | //get your user account role for this app or the canvas ecosystem |
||
126 | 2 | $role = $this->getPermission('apps_id in (' . \Phalcon\DI::getDefault()->getConfig()->app->id . ',' . Roles::DEFAULT_ACL_APP_ID . ')')->roles->name; |
|
127 | |||
128 | 2 | return $this->di->getAcl()->isAllowed($role, $resource, $action); |
|
129 | } |
||
130 | } |
||
131 |