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
|
|
|
public function assignRole(string $role): bool
|
27
|
|
|
{
|
28
|
|
|
$role = Roles::getByAppName($role, $this->defaultCompany);
|
|
|
|
|
29
|
|
|
|
30
|
|
|
if (!$role) {
|
|
|
|
|
31
|
|
|
throw new ServerErrorHttpException('Role not found in DB');
|
32
|
|
|
}
|
33
|
|
|
|
34
|
|
|
$userRole = UserRoles::findFirst([
|
35
|
|
|
'conditions' => 'users_id = ?0 and roles_id = ?1 and apps_id = ?2 and company_id = ?3',
|
36
|
|
|
'bind' => [$this->getId(), $role->getId(), $role->apps_id, $this->default_company]
|
|
|
|
|
37
|
|
|
]);
|
38
|
|
|
|
39
|
|
|
if (!$userRole) {
|
|
|
|
|
40
|
|
|
$userRole = new UserRoles();
|
41
|
|
|
$userRole->users_id = $this->getid();
|
42
|
|
|
$userRole->roles_id = $role->getId();
|
43
|
|
|
$userRole->apps_id = $role->apps_id;
|
44
|
|
|
$userRole->company_id = $this->default_company;
|
45
|
|
|
if (!$userRole->save()) {
|
46
|
|
|
throw new ModelException((string) current($userRole->getMessages()));
|
47
|
|
|
}
|
48
|
|
|
}
|
49
|
|
|
|
50
|
|
|
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
|
|
|
public function removeRole(string $role): bool
|
61
|
|
|
{
|
62
|
|
|
$role = Roles::getByAppName($role, $this->defaultCompany);
|
|
|
|
|
63
|
|
|
|
64
|
|
|
if (!$role) {
|
|
|
|
|
65
|
|
|
throw new ServerErrorHttpException('Role not found in DB');
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
$userRole = UserRoles::findFirst([
|
69
|
|
|
'conditions' => 'users_id = ?0 and roles_id = ?1 and apps_id = ?2 and company_id = ?3',
|
70
|
|
|
'bind' => [$this->getId(), $role->getId(), $role->apps_id, $this->default_company]
|
71
|
|
|
]);
|
72
|
|
|
|
73
|
|
|
if ($userRole) {
|
|
|
|
|
74
|
|
|
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 (!$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 and company_id = ?3',
|
96
|
|
|
'bind' => [$this->getId(), $role->getId(), $role->apps_id, $this->default_company]
|
97
|
|
|
]);
|
98
|
|
|
|
99
|
|
|
if ($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
|
|
|
public function can(string $action): bool
|
116
|
|
|
{
|
117
|
|
|
//if we find the . then les
|
118
|
|
|
if (strpos($action, '.') == false) {
|
|
|
|
|
119
|
|
|
throw new ServerErrorHttpException('ACL - We are expecting the resource for this action');
|
120
|
|
|
}
|
121
|
|
|
|
122
|
|
|
$action = explode('.', $action);
|
123
|
|
|
$resource = $action[0];
|
124
|
|
|
$action = $action[1];
|
125
|
|
|
//get your user account role for this app or the canvas ecosystem
|
126
|
|
|
$role = $this->getPermission('apps_id in (' . \Phalcon\DI::getDefault()->getConfig()->app->id . ',' . Roles::DEFAULT_ACL_APP_ID . ')')->roles->name;
|
|
|
|
|
127
|
|
|
|
128
|
|
|
return $this->di->getAcl()->isAllowed($role, $resource, $action);
|
129
|
|
|
}
|
130
|
|
|
}
|
131
|
|
|
|