|
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 \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
16
|
|
|
*/ |
|
17
|
16 |
|
public function level() |
|
18
|
|
|
{ |
|
19
|
16 |
|
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
|
20 |
|
public function assignLevel($level) |
|
30
|
|
|
{ |
|
31
|
20 |
|
$level = aclGetALevel($level); |
|
32
|
|
|
|
|
33
|
19 |
|
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
|
2 |
|
public function getLevel() |
|
57
|
|
|
{ |
|
58
|
2 |
|
$model = is_a($this, config('laravel-acl.level'), true) ? 'Level' : 'User'; |
|
59
|
|
|
|
|
60
|
2 |
|
return Cache::remember( |
|
61
|
2 |
|
'laravel-acl.getLevelFor' . $model . '_' . $this->id, |
|
|
|
|
|
|
62
|
2 |
|
config('laravel-acl.cacheMinutes'), |
|
63
|
2 |
|
function() { |
|
64
|
2 |
|
return $this->cacheGetLevel(); |
|
65
|
2 |
|
} |
|
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
|
2 |
|
protected function cacheGetLevel() |
|
82
|
|
|
{ |
|
83
|
2 |
|
return $this->level; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
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.