|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace z1haze\Acl\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use z1haze\Acl\Exceptions\UserNotFoundException; |
|
7
|
|
|
use z1haze\Acl\Traits\UserAndPermission; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Permission |
|
11
|
|
|
* @package z1haze\Acl\Models |
|
12
|
|
|
* |
|
13
|
|
|
* @method levels() |
|
14
|
|
|
*/ |
|
15
|
|
|
class Permission extends Model |
|
16
|
|
|
{ |
|
17
|
|
|
use UserAndPermission; |
|
18
|
|
|
|
|
19
|
|
|
protected $guarded = ['id', 'created_id', 'updated_at']; |
|
20
|
|
|
protected $casts = ['id' => 'integer']; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* PERMISSION |
|
24
|
|
|
* A Permission belongs to a level |
|
25
|
|
|
* |
|
26
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
27
|
|
|
*/ |
|
28
|
|
|
public function level() |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->belongsTo(config('laravel-acl.level', Level::class)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* PERMISSION |
|
35
|
|
|
* A permission belongs to many users |
|
36
|
|
|
* |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
|
|
public function users() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->belongsToMany(config('laravel-acl.user')); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* PERMISSION |
|
46
|
|
|
* Assign a single user to a permission |
|
47
|
|
|
* |
|
48
|
|
|
* @param $user |
|
49
|
|
|
*/ |
|
50
|
|
|
public function addUser($user) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->addUsers([$user]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* PERMISSION |
|
57
|
|
|
* Assign an array of users to a permission |
|
58
|
|
|
* |
|
59
|
|
|
* @param $users |
|
60
|
|
|
*/ |
|
61
|
|
|
public function addUsers($users) |
|
62
|
|
|
{ |
|
63
|
|
|
$userObjects = $this->buildUserArray($users); |
|
64
|
|
|
|
|
65
|
|
|
$this->users()->attach($userObjects); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* PERMISSION |
|
70
|
|
|
* Remove a single user from a permission |
|
71
|
|
|
* |
|
72
|
|
|
* @param $user |
|
73
|
|
|
*/ |
|
74
|
|
|
public function removeUser($user) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->removeUsers([$user]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* PERMISSION |
|
81
|
|
|
* Remove an array of users from a permission |
|
82
|
|
|
* |
|
83
|
|
|
* @param $users |
|
84
|
|
|
*/ |
|
85
|
|
|
public function removeUsers($users) |
|
86
|
|
|
{ |
|
87
|
|
|
$userObjects = $this->buildUserArray($users); |
|
88
|
|
|
|
|
89
|
|
|
$this->users()->detach($userObjects); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
94
|
|
|
| Other Functions |
|
95
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
96
|
|
|
*/ |
|
97
|
|
|
/** |
|
98
|
|
|
* PERMISSION |
|
99
|
|
|
* Helper function to get the user whether it is the user ID |
|
100
|
|
|
* or the user object itself. |
|
101
|
|
|
* |
|
102
|
|
|
* @param $user |
|
103
|
|
|
* @return User |
|
104
|
|
|
* @throws UserNotFoundException |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getUser($user) |
|
107
|
|
|
{ |
|
108
|
|
|
if (is_int($user)) |
|
109
|
|
|
$user = config('laravel-acl.user')::find($user); |
|
110
|
|
|
|
|
111
|
|
|
if (!$user) |
|
112
|
|
|
throw new UserNotFoundException; |
|
113
|
|
|
|
|
114
|
|
|
return $user; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Helper function to process users and return an |
|
119
|
|
|
* array of user id's |
|
120
|
|
|
* |
|
121
|
|
|
* @param $users |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function buildUserArray($users) |
|
125
|
|
|
{ |
|
126
|
|
|
$userArray = []; |
|
127
|
|
|
|
|
128
|
|
|
foreach ($users as $user) { |
|
129
|
|
|
$user = $this->getUser($user); |
|
130
|
|
|
array_push($userArray, $user->id); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $userArray; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Handle model events |
|
138
|
|
|
*/ |
|
139
|
|
|
public static function boot() |
|
140
|
|
|
{ |
|
141
|
|
|
parent::boot(); |
|
142
|
|
|
|
|
143
|
|
|
static::deleting(function ($permission) { |
|
144
|
|
|
$permission->users()->detach(); |
|
145
|
|
|
$permission->level()->dissociate()->save(); |
|
146
|
|
|
}); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|