|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mrluke\Privileges\Extentions; |
|
4
|
|
|
|
|
5
|
|
|
use Mrluke\Privileges\Facades\Manager; |
|
6
|
|
|
use Mrluke\Privileges\Models\Permission; |
|
7
|
|
|
use Mrluke\Privileges\Models\Role; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Authorizable trait provides base functions that |
|
11
|
|
|
* turns Model into Authorizable. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Łukasz Sitnicki (mr-luke) |
|
14
|
|
|
* @link http://github.com/mr-luke/privileges |
|
15
|
|
|
* |
|
16
|
|
|
* @category Laravel |
|
17
|
|
|
* @package mr-luke/privileges |
|
18
|
|
|
* @license MIT |
|
19
|
|
|
*/ |
|
20
|
|
|
trait Authorizable |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Returns users personal aditional privileges. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
public function permissions() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->morphMany(Permission::class, 'grantable'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Return related Roles. |
|
33
|
|
|
* |
|
34
|
|
|
*/ |
|
35
|
|
|
public function roles() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->belongsToMany(Role::class, 'priv_auth_role', 'auth_id', 'role_id'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Assign a role to given Authorizable. |
|
42
|
|
|
* |
|
43
|
|
|
* @param mixed $role |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
public function assignRole($role): void |
|
47
|
|
|
{ |
|
48
|
|
|
Manager::assignRole($this, $role); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Return permission level based on personal's & role's permission. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string|array $scopes |
|
55
|
|
|
* @return int |
|
56
|
|
|
*/ |
|
57
|
|
|
public function considerPermission($scopes): int |
|
58
|
|
|
{ |
|
59
|
|
|
return Manager::considerPermission($this, $scopes); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Grant or update premission for a Permitable. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $scope |
|
66
|
|
|
* @param int $level |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
public function grantPermission(string $scope, int $level): void |
|
70
|
|
|
{ |
|
71
|
|
|
Manager::grantPermission($this, $scope, $level); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Regain permission for a Permitable. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $scope |
|
78
|
|
|
* @return void |
|
79
|
|
|
*/ |
|
80
|
|
|
public function regainPermission(string $scope): void |
|
81
|
|
|
{ |
|
82
|
|
|
Manager::regainPermission($this, $scope); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Remove a role from given Authorizable. |
|
87
|
|
|
* |
|
88
|
|
|
* @param mixed $role |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
public function removeRole($role): void |
|
92
|
|
|
{ |
|
93
|
|
|
Manager::removeRole($this, $role); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|