1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: sheldon |
5
|
|
|
* Date: 18-3-27 |
6
|
|
|
* Time: 下午4:12. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Yeelight\Traits; |
10
|
|
|
|
11
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
use Illuminate\Support\Facades\Storage; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait HasPermissions |
17
|
|
|
* |
18
|
|
|
* @category Yeelight |
19
|
|
|
* |
20
|
|
|
* @package Yeelight\Traits |
21
|
|
|
* |
22
|
|
|
* @author Sheldon Lee <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
25
|
|
|
* |
26
|
|
|
* @link https://www.yeelight.com |
27
|
|
|
*/ |
28
|
|
|
trait HasPermissions |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Get avatar attribute. |
32
|
|
|
* |
33
|
|
|
* @param string $avatar |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function getAvatarAttribute($avatar) |
38
|
|
|
{ |
39
|
|
|
if ($avatar) { |
40
|
|
|
return Storage::disk(config('yeelight.backend.upload.disk'))->url($avatar); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return backend_asset('/img/user2-160x160.jpg'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* A user has and belongs to many roles. |
48
|
|
|
* |
49
|
|
|
* @return BelongsToMany |
50
|
|
|
*/ |
51
|
|
|
public function roles() : BelongsToMany |
52
|
|
|
{ |
53
|
|
|
$pivotTable = config('yeelight.backend.database.admin_role_users_table'); |
54
|
|
|
|
55
|
|
|
$relatedModel = config('yeelight.backend.database.admin_roles_model'); |
56
|
|
|
|
57
|
|
|
return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'role_id')->withTimestamps(); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* A User has and belongs to many permissions. |
62
|
|
|
* |
63
|
|
|
* @return BelongsToMany |
64
|
|
|
*/ |
65
|
|
|
public function permissions() : BelongsToMany |
66
|
|
|
{ |
67
|
|
|
$pivotTable = config('yeelight.backend.database.admin_user_permissions_table'); |
68
|
|
|
|
69
|
|
|
$relatedModel = config('yeelight.backend.database.admin_permissions_model'); |
70
|
|
|
|
71
|
|
|
return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'permission_id')->withTimestamps(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get all permissions of user. |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function allPermissions() : Collection |
80
|
|
|
{ |
81
|
|
|
return $this->roles()->with('permissions')->get()->pluck('permissions')->flatten()->merge($this->permissions); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check if user has permission. |
86
|
|
|
* |
87
|
|
|
* @param $permission |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function can(string $permission) : bool |
92
|
|
|
{ |
93
|
|
|
if ($this->isAdministrator()) { |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($this->permissions->pluck('slug')->contains($permission)) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->roles->pluck('permissions')->flatten()->pluck('slug')->contains($permission); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check if user has no permission. |
106
|
|
|
* |
107
|
|
|
* @param $permission |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function cannot(string $permission) : bool |
112
|
|
|
{ |
113
|
|
|
return !$this->can($permission); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check if user is administrator. |
118
|
|
|
* |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
|
|
public function isAdministrator() : bool |
122
|
|
|
{ |
123
|
|
|
return $this->isRole(config('yeelight.backend.auth.super_role')); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Check if user is $role. |
128
|
|
|
* |
129
|
|
|
* @param string $role |
130
|
|
|
* |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function isRole(string $role) : bool |
134
|
|
|
{ |
135
|
|
|
return $this->roles->pluck('slug')->contains($role); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Check if user in $roles. |
140
|
|
|
* |
141
|
|
|
* @param array $roles |
142
|
|
|
* |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
|
|
public function inRoles(array $roles = []) : bool |
146
|
|
|
{ |
147
|
|
|
return $this->roles->pluck('slug')->intersect($roles)->isNotEmpty(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* If visible for roles. |
152
|
|
|
* |
153
|
|
|
* @param $roles |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function visible(array $roles = []) : bool |
158
|
|
|
{ |
159
|
|
|
if (empty($roles)) { |
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$roles = array_column($roles, 'slug'); |
164
|
|
|
|
165
|
|
|
return $this->inRoles($roles) || $this->isAdministrator(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Detach models from the relationship. |
170
|
|
|
* |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
|
|
protected static function boot() |
174
|
|
|
{ |
175
|
|
|
parent::boot(); |
176
|
|
|
|
177
|
|
|
static::deleting(function ($model) { |
178
|
|
|
$model->roles()->detach(); |
179
|
|
|
|
180
|
|
|
$model->permissions()->detach(); |
181
|
|
|
}); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|