|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\Acl\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
use Yajra\Acl\Traits\HasRole; |
|
11
|
|
|
use Yajra\Acl\Traits\RefreshCache; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @property string resource |
|
15
|
|
|
* @property string name |
|
16
|
|
|
* @property string slug |
|
17
|
|
|
* @property bool system |
|
18
|
|
|
*/ |
|
19
|
|
|
class Permission extends Model |
|
20
|
|
|
{ |
|
21
|
|
|
use HasRole, RefreshCache; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $table = 'permissions'; |
|
25
|
|
|
|
|
26
|
|
|
/** @var array */ |
|
27
|
|
|
protected $fillable = ['name', 'slug', 'resource', 'system']; |
|
28
|
|
|
|
|
29
|
|
|
/** @var array */ |
|
30
|
|
|
protected $casts = ['system' => 'bool']; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Find a permission by slug. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $slug |
|
36
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function findBySlug(string $slug) |
|
39
|
|
|
{ |
|
40
|
|
|
return static::query()->where('slug', $slug)->firstOrFail(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a permissions for a resource. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $resource |
|
47
|
|
|
* @param bool $system |
|
48
|
|
|
* @return \Illuminate\Support\Collection |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function createResource(string $resource, $system = false) |
|
51
|
|
|
{ |
|
52
|
|
|
$group = Str::title($resource); |
|
53
|
|
|
$slug = Str::slug($group); |
|
54
|
|
|
$permissions = [ |
|
55
|
|
|
[ |
|
56
|
|
|
'slug' => $slug.'.viewAny', |
|
57
|
|
|
'resource' => $group, |
|
58
|
|
|
'name' => 'View Any '.$group, |
|
59
|
|
|
'system' => $system, |
|
60
|
|
|
], |
|
61
|
|
|
[ |
|
62
|
|
|
'slug' => $slug.'.view', |
|
63
|
|
|
'resource' => $group, |
|
64
|
|
|
'name' => 'View '.$group, |
|
65
|
|
|
'system' => $system, |
|
66
|
|
|
], |
|
67
|
|
|
[ |
|
68
|
|
|
'slug' => $slug.'.create', |
|
69
|
|
|
'resource' => $group, |
|
70
|
|
|
'name' => 'Create '.$group, |
|
71
|
|
|
'system' => $system, |
|
72
|
|
|
], |
|
73
|
|
|
[ |
|
74
|
|
|
'slug' => $slug.'.update', |
|
75
|
|
|
'resource' => $group, |
|
76
|
|
|
'name' => 'Update '.$group, |
|
77
|
|
|
'system' => $system, |
|
78
|
|
|
], |
|
79
|
|
|
[ |
|
80
|
|
|
'slug' => $slug.'.delete', |
|
81
|
|
|
'resource' => $group, |
|
82
|
|
|
'name' => 'Delete '.$group, |
|
83
|
|
|
'system' => $system, |
|
84
|
|
|
], |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
$collection = new Collection; |
|
88
|
|
|
foreach ($permissions as $permission) { |
|
89
|
|
|
try { |
|
90
|
|
|
$collection->push(static::create($permission)); |
|
|
|
|
|
|
91
|
|
|
} catch (Exception $e) { |
|
92
|
|
|
// permission already exists. |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $collection; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Permission can belong to many users. |
|
101
|
|
|
* |
|
102
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
103
|
|
|
*/ |
|
104
|
|
|
public function users(): BelongsToMany |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->belongsToMany(config('acl.user', config('auth.providers.users.model'))) |
|
107
|
|
|
->withTimestamps(); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.