|
1
|
|
|
<?php namespace Arcanedev\LaravelAuth\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelAuth\Bases\Model; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class PermissionsGroup |
|
7
|
|
|
* |
|
8
|
|
|
* @package Arcanedev\LaravelAuth\Models |
|
9
|
|
|
* @author ARCANEDEV <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @property int id |
|
12
|
|
|
* @property string name |
|
13
|
|
|
* @property string slug |
|
14
|
|
|
* @property string description |
|
15
|
|
|
* @property \Carbon\Carbon created_at |
|
16
|
|
|
* @property \Carbon\Carbon updated_at |
|
17
|
|
|
* @property \Illuminate\Database\Eloquent\Collection permissions |
|
18
|
|
|
*/ |
|
19
|
|
|
class PermissionsGroup extends Model |
|
20
|
|
|
{ |
|
21
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
22
|
|
|
| Properties |
|
23
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
24
|
|
|
*/ |
|
25
|
|
|
/** |
|
26
|
|
|
* The attributes that are mass assignable. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $fillable = ['name', 'slug', 'description']; |
|
31
|
|
|
|
|
32
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
33
|
|
|
| Constructor |
|
34
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
35
|
|
|
*/ |
|
36
|
|
|
/** |
|
37
|
|
|
* Create a new Eloquent model instance. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $attributes |
|
40
|
|
|
*/ |
|
41
|
32 |
|
public function __construct(array $attributes = []) |
|
42
|
|
|
{ |
|
43
|
32 |
|
$this->setTable(config('laravel-auth.permissions-group.table', 'permissions_group')); |
|
44
|
|
|
|
|
45
|
32 |
|
parent::__construct($attributes); |
|
46
|
32 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
49
|
|
|
| Relationships |
|
50
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
51
|
|
|
*/ |
|
52
|
|
|
/** |
|
53
|
|
|
* Permissions Groups has many permissions. |
|
54
|
|
|
* |
|
55
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
56
|
|
|
*/ |
|
57
|
24 |
|
public function permissions() |
|
58
|
|
|
{ |
|
59
|
24 |
|
return $this->hasMany(Permission::class, 'group_id'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
63
|
|
|
| CRUD Functions |
|
64
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
65
|
|
|
*/ |
|
66
|
|
|
/** |
|
67
|
|
|
* Create and attach a permission. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $attributes |
|
70
|
|
|
* @param bool $reload |
|
71
|
|
|
*/ |
|
72
|
8 |
|
public function createPermission(array $attributes, $reload = true) |
|
73
|
|
|
{ |
|
74
|
8 |
|
$this->permissions()->create($attributes); |
|
75
|
|
|
|
|
76
|
8 |
|
if ($reload) { |
|
77
|
8 |
|
$this->load('permissions'); |
|
78
|
6 |
|
} |
|
79
|
8 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Attach the permission to a group. |
|
83
|
|
|
* |
|
84
|
|
|
* @param \Arcanedev\LaravelAuth\Models\Permission|int $permission |
|
85
|
|
|
* @param bool $reload |
|
86
|
|
|
*/ |
|
87
|
8 |
|
public function attachPermission($permission, $reload = true) |
|
88
|
|
|
{ |
|
89
|
8 |
|
if ($this->hasPermission($permission)) { |
|
90
|
4 |
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
8 |
|
$this->permissions()->save($permission); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
8 |
|
if ($reload) { |
|
96
|
8 |
|
$this->load('permissions'); |
|
97
|
6 |
|
} |
|
98
|
8 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Attach the permission from a group. |
|
102
|
|
|
* |
|
103
|
|
|
* @param \Arcanedev\LaravelAuth\Models\Permission|int $permission |
|
104
|
|
|
* @param bool $reload |
|
105
|
|
|
*/ |
|
106
|
4 |
|
public function detachPermission($permission, $reload = true) |
|
107
|
|
|
{ |
|
108
|
4 |
|
if ( ! $this->hasPermission($permission)) { |
|
109
|
4 |
|
return; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
4 |
|
$this->getPermission($permission)->update([ |
|
113
|
4 |
|
'group_id' => 0, |
|
114
|
3 |
|
]); |
|
115
|
|
|
|
|
116
|
4 |
|
if ($reload) { |
|
117
|
4 |
|
$this->load('permissions'); |
|
118
|
3 |
|
} |
|
119
|
4 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
122
|
|
|
| Check Functions |
|
123
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
124
|
|
|
*/ |
|
125
|
|
|
/** |
|
126
|
|
|
* Check if role has the given permission (Permission Model or Id). |
|
127
|
|
|
* |
|
128
|
|
|
* @param \Arcanedev\LaravelAuth\Models\Permission|int $id |
|
129
|
|
|
* |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
8 |
|
public function hasPermission($id) |
|
133
|
|
|
{ |
|
134
|
8 |
|
if ($id instanceof Permission) { |
|
135
|
8 |
|
$id = $id->getKey(); |
|
136
|
6 |
|
} |
|
137
|
|
|
|
|
138
|
8 |
|
return ! is_null($this->getPermission($id)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get a permission from the group. |
|
143
|
|
|
* |
|
144
|
|
|
* @param \Arcanedev\LaravelAuth\Models\Permission|int $id |
|
145
|
|
|
* |
|
146
|
|
|
* @return \Arcanedev\LaravelAuth\Models\Permission|null |
|
147
|
|
|
*/ |
|
148
|
8 |
|
private function getPermission($id) |
|
149
|
|
|
{ |
|
150
|
8 |
|
if ($id instanceof Permission) { |
|
151
|
4 |
|
$id = $id->getKey(); |
|
152
|
3 |
|
} |
|
153
|
|
|
|
|
154
|
8 |
|
return $this->permissions->filter(function (Permission $permission) use ($id) { |
|
155
|
8 |
|
return $permission->id == $id; |
|
156
|
8 |
|
})->first(); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.