1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SlFomin\Roles\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
7
|
|
|
use Ultraware\Roles\Models\Permission; |
8
|
|
|
|
9
|
|
|
trait RoleHasRelations |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Role belongs to many permissions. |
13
|
|
|
* |
14
|
|
|
* @return BelongsToMany |
15
|
|
|
*/ |
16
|
|
|
public function permissions() |
17
|
|
|
{ |
18
|
|
|
return $this->belongsToMany(config('roles.models.permission'))->withTimestamps(); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Role belongs to many users. |
23
|
|
|
* |
24
|
|
|
* @return BelongsToMany |
25
|
|
|
*/ |
26
|
|
|
public function users() |
27
|
|
|
{ |
28
|
|
|
return $this->belongsToMany(config('auth.providers.users.model'))->withTimestamps(); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Role belongs to parent role. |
33
|
|
|
* |
34
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
35
|
|
|
*/ |
36
|
|
|
public function parent() |
37
|
|
|
{ |
38
|
|
|
return $this->belongsTo(config('roles.models.role'),'parent_id'); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function ancestors() |
42
|
|
|
{ |
43
|
|
|
$ancestors = $this->where('id', '=', $this->parent_id)->get(); |
|
|
|
|
44
|
|
|
while ($ancestors->last() && $ancestors->last()->parent_id !== null) |
45
|
|
|
{ |
46
|
|
|
$parent = $this->where('id', '=', $ancestors->last()->parent_id)->get(); |
|
|
|
|
47
|
|
|
$ancestors = $ancestors->merge($parent); |
48
|
|
|
} |
49
|
|
|
return $ancestors; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Role has many children roles |
54
|
|
|
* |
55
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
56
|
|
|
*/ |
57
|
|
|
public function children() |
58
|
|
|
{ |
59
|
|
|
return $this->hasMany(config('roles.models.role'),'parent_id'); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function descendants() |
63
|
|
|
{ |
64
|
|
|
$descendants = $this->where('parent_id', '=', $this->id)->get(); |
|
|
|
|
65
|
|
|
foreach($descendants as $descendant) |
66
|
|
|
$descendants = $descendants->merge($descendant->descendants()); |
67
|
|
|
return $descendants; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Attach permission to a role. |
72
|
|
|
* |
73
|
|
|
* @param int|Permission $permission |
74
|
|
|
* @return int|bool |
75
|
|
|
*/ |
76
|
|
|
public function attachPermission($permission) |
77
|
|
|
{ |
78
|
|
|
return (!$this->permissions()->get()->contains($permission)) ? $this->permissions()->attach($permission) : true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Detach permission from a role. |
83
|
|
|
* |
84
|
|
|
* @param int|Permission $permission |
85
|
|
|
* @return int |
86
|
|
|
*/ |
87
|
|
|
public function detachPermission($permission) |
88
|
|
|
{ |
89
|
|
|
return $this->permissions()->detach($permission); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Detach all permissions. |
94
|
|
|
* |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
public function detachAllPermissions() |
98
|
|
|
{ |
99
|
|
|
return $this->permissions()->detach(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Sync permissions for a role. |
104
|
|
|
* |
105
|
|
|
* @param array|Permission[]|Collection $permissions |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function syncPermissions($permissions) |
109
|
|
|
{ |
110
|
|
|
return $this->permissions()->sync($permissions); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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
Idable
provides a methodequalsId
that 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.