|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace z1haze\Acl\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use z1haze\Acl\Exceptions\LevelNotFoundException; |
|
6
|
|
|
use Illuminate\Support\Facades\Cache; |
|
7
|
|
|
use z1haze\Acl\Models\Level; |
|
8
|
|
|
|
|
9
|
|
|
trait UserAndPermission |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* USER & PERMISSION |
|
13
|
|
|
* A model belongs to a Level |
|
14
|
|
|
* |
|
15
|
|
|
* @return mixed |
|
16
|
|
|
*/ |
|
17
|
15 |
|
public function level() |
|
18
|
|
|
{ |
|
19
|
15 |
|
return $this->belongsTo(config('laravel-acl.level', Level::class)); |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* USER & PERMISSION |
|
24
|
|
|
* Assign a model to a level |
|
25
|
|
|
* |
|
26
|
|
|
* @param $level |
|
27
|
|
|
* @return mixed |
|
28
|
|
|
*/ |
|
29
|
19 |
|
public function assignLevel($level) |
|
30
|
|
|
{ |
|
31
|
19 |
|
$level = $this->getALevel($level); |
|
32
|
|
|
|
|
33
|
18 |
|
return $this->level()->associate($level)->save(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* USER & PERMISSION |
|
38
|
|
|
* Clear a model's level |
|
39
|
|
|
* |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public function clearLevel() |
|
43
|
|
|
{ |
|
44
|
2 |
|
return $this->level()->dissociate()->save(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* USER & PERMISSION |
|
49
|
|
|
* First try the cache to return the collection, |
|
50
|
|
|
* then fetch it from the database. |
|
51
|
|
|
* |
|
52
|
|
|
* See @cacheGetLevel() |
|
53
|
|
|
* |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function getLevel() |
|
57
|
|
|
{ |
|
58
|
3 |
|
$model = is_a($this, config('laravel-acl.level'), true) ? 'Level' : 'User'; |
|
59
|
|
|
|
|
60
|
3 |
|
return Cache::remember( |
|
61
|
3 |
|
'laravel-acl.getLevelFor' . $model . '_' . $this->id, |
|
|
|
|
|
|
62
|
3 |
|
config('laravel-acl.cacheMinutes'), |
|
63
|
3 |
|
function () { |
|
64
|
3 |
|
return $this->cacheGetLevel(); |
|
65
|
3 |
|
} |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
71
|
|
|
| Other Functions |
|
72
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
73
|
|
|
*/ |
|
74
|
|
|
/** |
|
75
|
|
|
* USER & PERMISSION |
|
76
|
|
|
* Return a relationship of the level on a user/permission |
|
77
|
|
|
* associated to a user by direct or inheritance |
|
78
|
|
|
* |
|
79
|
|
|
* @return mixed |
|
80
|
|
|
*/ |
|
81
|
3 |
|
protected function cacheGetLevel() |
|
82
|
|
|
{ |
|
83
|
3 |
|
return $this->level; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* USER & PERMISSION |
|
88
|
|
|
* Helper function to get the level whether it is the ID |
|
89
|
|
|
* or the name of the level, or the level object itself. |
|
90
|
|
|
* |
|
91
|
|
|
* @param $level |
|
92
|
|
|
* @return mixed |
|
93
|
|
|
* @throws LevelNotFoundException |
|
94
|
|
|
*/ |
|
95
|
19 |
View Code Duplication |
protected function getALevel($level) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
19 |
|
if (is_string($level)) |
|
98
|
1 |
|
$level = config('laravel-acl.level', Level::class)::whereName($level)->first(); |
|
99
|
|
|
|
|
100
|
19 |
|
if (is_int($level)) |
|
101
|
1 |
|
$level = config('laravel-acl.level', Level::class)::find($level); |
|
102
|
|
|
|
|
103
|
19 |
|
if (!$level) |
|
104
|
1 |
|
throw new LevelNotFoundException(); |
|
105
|
|
|
|
|
106
|
18 |
|
return $level; |
|
107
|
|
|
} |
|
108
|
|
|
} |
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.