|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace z1haze\Acl\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use z1haze\Acl\Models\Permission; |
|
6
|
|
|
|
|
7
|
|
|
trait UserOnly |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* USER |
|
11
|
|
|
* A User can have permissions explicitly assigned to it |
|
12
|
|
|
* |
|
13
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
14
|
|
|
*/ |
|
15
|
15 |
|
public function permissions() |
|
16
|
|
|
{ |
|
17
|
15 |
|
return $this->belongsToMany(config('laravel-acl.permission', Permission::class))->withPivot('negated')->withTimestamps(); |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* USER |
|
22
|
|
|
* Negate a permission from a User |
|
23
|
|
|
* |
|
24
|
|
|
* @param $permission |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function negatePermission($permission) |
|
27
|
|
|
{ |
|
28
|
1 |
|
$this->modifyPermissions([$permission], 'negate'); |
|
|
|
|
|
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Negate an array of permissions from a User |
|
33
|
|
|
* |
|
34
|
|
|
* @param $permissions |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function negatePermissions(array $permissions) |
|
37
|
|
|
{ |
|
38
|
1 |
|
$this->modifyPermissions($permissions, 'negate'); |
|
|
|
|
|
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* First try the cache to return the collection, |
|
43
|
|
|
* then fetch it from the database. |
|
44
|
|
|
* |
|
45
|
|
|
* See @cacheGetNegatedPermissions() |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Illuminate\Support\Collection |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function getNegatedPermissions() |
|
50
|
|
|
{ |
|
51
|
3 |
|
return \Cache::remember( |
|
52
|
3 |
|
'laravel-acl.getNegatedPermissionsForUser_' . $this->id, |
|
|
|
|
|
|
53
|
3 |
|
config('laravel-acl.cacheMinutes'), |
|
54
|
3 |
|
function() { |
|
55
|
3 |
|
return $this->cacheGetNegatedPermissions(); |
|
56
|
3 |
|
} |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Return a collection of permissions that |
|
62
|
|
|
* are negated for a User |
|
63
|
|
|
* |
|
64
|
|
|
* @return \Illuminate\Support\Collection |
|
65
|
|
|
*/ |
|
66
|
3 |
|
protected function cacheGetNegatedPermissions() |
|
67
|
|
|
{ |
|
68
|
3 |
|
return $this->permissions()->wherePivot('negated', true)->get(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
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.