1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Fort Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Fort Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Models; |
17
|
|
|
|
18
|
|
|
use Illuminate\Database\Eloquent\Model; |
19
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
20
|
|
|
|
21
|
|
|
class Ability extends Model |
22
|
|
|
{ |
23
|
|
|
use SoftDeletes; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
protected $dates = ['deleted_at']; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected $fillable = [ |
34
|
|
|
'action', |
35
|
|
|
'resource', |
36
|
|
|
'policy', |
37
|
|
|
'title', |
38
|
|
|
'description', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a new Eloquent model instance. |
43
|
|
|
* |
44
|
|
|
* @param array $attributes |
45
|
|
|
* |
46
|
|
|
* @return void |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function __construct(array $attributes = []) |
49
|
|
|
{ |
50
|
|
|
parent::__construct($attributes); |
51
|
|
|
|
52
|
|
|
$this->setTable(config('rinvex.fort.tables.abilities')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* An ability can be applied to roles. |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
59
|
|
|
*/ |
60
|
|
|
public function roles() |
61
|
|
|
{ |
62
|
|
|
return $this->belongsToMany(config('rinvex.fort.models.role'), config('rinvex.fort.tables.ability_role')) |
63
|
|
|
->withTimestamps(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* An ability can be applied to users. |
68
|
|
|
* |
69
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
70
|
|
|
*/ |
71
|
|
|
public function users() |
72
|
|
|
{ |
73
|
|
|
return $this->belongsToMany(config('rinvex.fort.models.user'), config('rinvex.fort.tables.ability_user')) |
74
|
|
|
->withTimestamps(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Determine if the ability is super admin. |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function isSuperadmin() |
83
|
|
|
{ |
84
|
|
|
return ! $this->policy && $this->resource === 'global' && $this->action === 'superadmin'; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Determine if the ability is protected. |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function isProtected() |
93
|
|
|
{ |
94
|
|
|
return in_array($this->id, config('rinvex.fort.protected.abilities')); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get slug attribute out of ability's action & resource. |
99
|
|
|
* |
100
|
|
|
* @return bool |
|
|
|
|
101
|
|
|
*/ |
102
|
|
|
public function getSlugAttribute() |
103
|
|
|
{ |
104
|
|
|
return $this->action.'-'.$this->resource; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.