|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CakePHP permission handling library |
|
4
|
|
|
* @author Tao <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Slince\CakePermission\Model\Table; |
|
7
|
|
|
|
|
8
|
|
|
use Cake\Validation\Validator; |
|
9
|
|
|
use Slince\CakePermission\Constants; |
|
10
|
|
|
use Cake\Cache\Cache; |
|
11
|
|
|
use Cake\Core\Configure; |
|
12
|
|
|
use Slince\CakePermission\TableFactory; |
|
13
|
|
|
|
|
14
|
|
|
trait RolesTableTrait |
|
15
|
|
|
{ |
|
16
|
|
|
public function buildPermissionRelationship() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->belongsToMany('Permissions', [ |
|
|
|
|
|
|
19
|
|
|
'className' => TableFactory::getModelClass('Permissions'), |
|
20
|
|
|
'foreignKey' => 'role_id', |
|
21
|
|
|
'targetForeignKey' => 'permission_id', |
|
22
|
|
|
'joinTable' => Configure::read('Permission.tableNameMap.roles_permissions') ?: 'roles_permissions', |
|
23
|
|
|
'saveStrategy' => 'append' |
|
24
|
|
|
]); |
|
25
|
|
|
$this->belongsToMany('Users', [ |
|
|
|
|
|
|
26
|
|
|
'className' => TableFactory::getModelClass('Users'), |
|
27
|
|
|
'foreignKey' => 'role_id', |
|
28
|
|
|
'targetForeignKey' => 'user_id', |
|
29
|
|
|
'joinTable' => Configure::read('Permission.tableNameMap.users_roles') ?: 'users_roles', |
|
30
|
|
|
'saveStrategy' => 'append' |
|
31
|
|
|
]); |
|
32
|
|
|
$this->addBehavior('Timestamp'); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Validator $validator |
|
37
|
|
|
* @return Validator |
|
38
|
|
|
*/ |
|
39
|
|
|
public function validationPermission(Validator $validator) |
|
40
|
|
|
{ |
|
41
|
|
|
$validator->add('name', 'unique', [ |
|
42
|
|
|
'rule' => 'validateUnique', |
|
43
|
|
|
'message' => 'The role already exists', |
|
44
|
|
|
'provider' => 'table' |
|
45
|
|
|
]); |
|
46
|
|
|
return $validator; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Refreshes the cache |
|
51
|
|
|
* @param int $userId |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function refreshCache($userId) |
|
54
|
|
|
{ |
|
55
|
|
|
Cache::delete(sprintf(Constants::CACHE_ROLES, $userId)); |
|
56
|
|
|
} |
|
57
|
|
|
} |
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
Idableprovides a methodequalsIdthat 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.