1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakePHP permission handling library |
4
|
|
|
* @author Tao <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Slince\CakePermission\Model\Entity; |
7
|
|
|
|
8
|
|
|
use Cake\Cache\Cache; |
9
|
|
|
use Cake\Collection\Collection; |
10
|
|
|
use Cake\ORM\Query; |
11
|
|
|
use Slince\CakePermission\Constants; |
12
|
|
|
use Slince\CakePermission\Model\Table\RolesTableTrait; |
13
|
|
|
use Slince\CakePermission\TableFactory; |
14
|
|
|
|
15
|
|
|
trait UserTrait |
16
|
|
|
{ |
17
|
|
|
use HasPermissionTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Alias of "hasAnyPermission" |
21
|
|
|
* @param $permission |
22
|
|
|
* @return boolean |
23
|
|
|
*/ |
24
|
|
|
public function can($permission) |
25
|
|
|
{ |
26
|
|
|
return $this->hasAnyPermission((array)$permission); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Assigns a role or an array of roles for the user |
31
|
|
|
* @param string|Role|array $role |
32
|
|
|
* @return boolean |
33
|
|
|
*/ |
34
|
|
|
public function assignRole($role) |
35
|
|
|
{ |
36
|
|
|
$roles = is_array($role) ? $role : [$role]; |
37
|
|
|
$roles = array_map(function($role){ |
38
|
|
|
return is_string($role) ? Role::find($role) : $role; |
39
|
|
|
}, $roles); |
40
|
|
|
$this->set('roles', $roles); |
|
|
|
|
41
|
|
|
$result = TableFactory::getUserModel()->save($this) !== false; |
42
|
|
|
RolesTableTrait::refreshCache($this->id); |
|
|
|
|
43
|
|
|
return $result; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Revokes the specified role from the user |
48
|
|
|
* @param string|Role $role |
49
|
|
|
* @return boolean |
50
|
|
|
*/ |
51
|
|
|
public function removeRole($role) |
52
|
|
|
{ |
53
|
|
|
$role = is_string($role) ? Role::find($role) : $role; |
54
|
|
|
$result = TableFactory::getUserModel()->association('Roles')->unlink($this, [$role]); |
55
|
|
|
RolesTableTrait::refreshCache($this->id); |
56
|
|
|
return $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Removes all roles |
61
|
|
|
*/ |
62
|
|
|
public function removeAllRoles() |
63
|
|
|
{ |
64
|
|
|
$result = TableFactory::getUserModel()->association('Roles')->unlink($this, $this->getAllRoles() ); |
65
|
|
|
RolesTableTrait::refreshCache($this->id); |
66
|
|
|
return $result; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Checks whether the user has thr role |
71
|
|
|
* @param string|Role $role |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function hasRole($role) |
75
|
|
|
{ |
76
|
|
|
$roleName = is_string($role) ? $role : $role->get('name'); |
77
|
|
|
foreach ($this->getAllRoles() as $role) { |
78
|
|
|
if ($role->get('name') == $roleName) { |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets all the permissions for the user |
87
|
|
|
* @return Role[] |
88
|
|
|
*/ |
89
|
|
|
public function getAllPermissions() |
90
|
|
|
{ |
91
|
|
|
$roles = $this->getAllRoles(); |
92
|
|
|
$permissions = $roles ? call_user_func_array('array_merge', array_map(function(Role $role){ |
93
|
|
|
return $role->getAllPermissions(); |
94
|
|
|
}, $roles)) : []; |
95
|
|
|
return (new Collection($permissions))->combine('slug', function($permission){ |
96
|
|
|
return $permission; |
97
|
|
|
})->toArray(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Gets all roles of the user |
102
|
|
|
* @return Role[] |
103
|
|
|
*/ |
104
|
|
|
public function getAllRoles() |
105
|
|
|
{ |
106
|
|
|
$id = $this->get(TableFactory::getUserModel()->getPrimaryKey()); |
|
|
|
|
107
|
|
|
return Cache::remember(sprintf(Constants::CACHE_ROLES, $id), function() use($id){ |
108
|
|
|
return TableFactory::getRoleModel()->find()->matching('Users', function(Query $query) use ($id){ |
109
|
|
|
return $query->where(['Users.id' => $id]); |
110
|
|
|
})->toArray(); |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.