1 | <?php namespace Arcanedev\LaravelAuth\Models; |
||
32 | class Permission extends AbstractModel implements PermissionContract |
||
33 | { |
||
34 | /* ----------------------------------------------------------------- |
||
35 | | Traits |
||
36 | | ----------------------------------------------------------------- |
||
37 | */ |
||
38 | |||
39 | use Roleable; |
||
40 | |||
41 | /* ----------------------------------------------------------------- |
||
42 | | Properties |
||
43 | | ----------------------------------------------------------------- |
||
44 | */ |
||
45 | |||
46 | /** |
||
47 | * The attributes that are mass assignable. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $fillable = [ |
||
52 | 'group_id', |
||
53 | 'name', |
||
54 | 'slug', |
||
55 | 'description', |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * The event map for the model. |
||
60 | * |
||
61 | * Allows for object-based events for native Eloquent events. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $dispatchesEvents = [ |
||
66 | 'creating' => CreatingPermission::class, |
||
67 | 'created' => CreatedPermission::class, |
||
68 | 'updating' => UpdatingPermission::class, |
||
69 | 'updated' => UpdatedPermission::class, |
||
70 | 'saving' => SavingPermission::class, |
||
71 | 'saved' => SavedPermission::class, |
||
72 | 'deleting' => DeletingPermission::class, |
||
73 | 'deleted' => DeletedPermission::class, |
||
74 | ]; |
||
75 | |||
76 | /** |
||
77 | * The attributes that should be cast to native types. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $casts = [ |
||
82 | 'id' => 'integer', |
||
83 | 'group_id' => 'integer', |
||
84 | ]; |
||
85 | |||
86 | /* ----------------------------------------------------------------- |
||
87 | | Constructor |
||
88 | | ----------------------------------------------------------------- |
||
89 | */ |
||
90 | |||
91 | /** |
||
92 | * Create a new Eloquent model instance. |
||
93 | * |
||
94 | * @param array $attributes |
||
95 | */ |
||
96 | 108 | public function __construct(array $attributes = []) |
|
102 | |||
103 | /* ----------------------------------------------------------------- |
||
104 | | Relationships |
||
105 | | ----------------------------------------------------------------- |
||
106 | */ |
||
107 | |||
108 | /** |
||
109 | * Permission belongs to one group. |
||
110 | * |
||
111 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
112 | */ |
||
113 | 3 | public function group() |
|
120 | |||
121 | /** |
||
122 | * Permission belongs to many roles. |
||
123 | * |
||
124 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
125 | */ |
||
126 | 27 | public function roles() |
|
135 | |||
136 | /* ----------------------------------------------------------------- |
||
137 | | Setters & Getters |
||
138 | | ----------------------------------------------------------------- |
||
139 | */ |
||
140 | |||
141 | /** |
||
142 | * Set the slug attribute. |
||
143 | * |
||
144 | * @param string $slug |
||
145 | */ |
||
146 | 90 | public function setSlugAttribute($slug) |
|
150 | |||
151 | /* ----------------------------------------------------------------- |
||
152 | | Main Methods |
||
153 | | ----------------------------------------------------------------- |
||
154 | */ |
||
155 | |||
156 | /** |
||
157 | * Attach a role to a user. |
||
158 | * |
||
159 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
160 | * @param bool $reload |
||
161 | */ |
||
162 | 21 | public function attachRole($role, $reload = true) |
|
172 | |||
173 | /** |
||
174 | * Sync the roles by its slugs. |
||
175 | * |
||
176 | * @param array|\Illuminate\Support\Collection $slugs |
||
177 | * @param bool $reload |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | 3 | public function syncRoles($slugs, $reload = true) |
|
194 | |||
195 | /** |
||
196 | * Detach a role from a user. |
||
197 | * |
||
198 | * @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
||
199 | * @param bool $reload |
||
200 | * |
||
201 | * @return int |
||
202 | */ |
||
203 | 3 | public function detachRole($role, $reload = true) |
|
213 | |||
214 | /** |
||
215 | * Detach all roles from a user. |
||
216 | * |
||
217 | * @param bool $reload |
||
218 | * |
||
219 | * @return int |
||
220 | */ |
||
221 | 3 | public function detachAllRoles($reload = true) |
|
231 | |||
232 | /* ----------------------------------------------------------------- |
||
233 | | Check Methods |
||
234 | | ----------------------------------------------------------------- |
||
235 | */ |
||
236 | |||
237 | /** |
||
238 | * Check if the permission has the same slug. |
||
239 | * |
||
240 | * @param string $slug |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | 18 | public function hasSlug($slug) |
|
248 | |||
249 | /* ----------------------------------------------------------------- |
||
250 | | Other Functions |
||
251 | | ----------------------------------------------------------------- |
||
252 | */ |
||
253 | |||
254 | /** |
||
255 | * Slugify the value. |
||
256 | * |
||
257 | * @param string $value |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | 90 | protected function slugify($value) |
|
265 | } |
||
266 |