1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Acl\Models; |
4
|
|
|
|
5
|
|
|
use Yajra\Acl\Traits\HasRole; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Collection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property string resource |
11
|
|
|
* @property string name |
12
|
|
|
* @property string slug |
13
|
|
|
* @property bool system |
14
|
|
|
*/ |
15
|
|
|
class Permission extends Model |
16
|
|
|
{ |
17
|
|
|
use HasRole; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The database table used by the model. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $table = 'permissions'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $fillable = ['name', 'slug', 'resource', 'system']; |
30
|
|
|
|
31
|
|
|
protected static function boot() |
32
|
|
|
{ |
33
|
|
|
parent::boot(); |
34
|
|
|
|
35
|
|
|
static::saved(function () { |
36
|
|
|
app('cache.store')->forget(config('acl.cache.key', 'permissions.policies')); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
static::deleted(function () { |
40
|
|
|
app('cache.store')->forget(config('acl.cache.key', 'permissions.policies')); |
41
|
|
|
}); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a permissions for a resource. |
46
|
|
|
* |
47
|
|
|
* @param $resource |
48
|
|
|
* @param bool $system |
49
|
|
|
* @return \Illuminate\Support\Collection |
50
|
|
|
*/ |
51
|
|
|
public static function createResource($resource, $system = false) |
52
|
|
|
{ |
53
|
|
|
$group = ucfirst($resource); |
54
|
|
|
$slug = strtolower($group); |
55
|
|
|
$permissions = [ |
56
|
|
|
[ |
57
|
|
|
'slug' => $slug . '.view', |
58
|
|
|
'resource' => $group, |
59
|
|
|
'name' => 'View ' . $group, |
60
|
|
|
'system' => $system, |
61
|
|
|
], |
62
|
|
|
[ |
63
|
|
|
'slug' => $slug . '.create', |
64
|
|
|
'resource' => $group, |
65
|
|
|
'name' => 'Create ' . $group, |
66
|
|
|
'system' => $system, |
67
|
|
|
], |
68
|
|
|
[ |
69
|
|
|
'slug' => $slug . '.update', |
70
|
|
|
'resource' => $group, |
71
|
|
|
'name' => 'Update ' . $group, |
72
|
|
|
'system' => $system, |
73
|
|
|
], |
74
|
|
|
[ |
75
|
|
|
'slug' => $slug . '.delete', |
76
|
|
|
'resource' => $group, |
77
|
|
|
'name' => 'Delete ' . $group, |
78
|
|
|
'system' => $system, |
79
|
|
|
], |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$collection = new Collection; |
83
|
|
|
foreach ($permissions as $permission) { |
84
|
|
|
try { |
85
|
|
|
$collection->push(static::create($permission)); |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
// permission already exists. |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $collection; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|