forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermission.php
More file actions
106 lines (86 loc) · 2.5 KB
/
Permission.php
File metadata and controls
106 lines (86 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class Permission extends ProcessMakerModel
{
protected $connection = 'processmaker';
protected $fillable = [
'title',
'name',
'group',
];
public function getResourceTitleAttribute()
{
$match = preg_match('/(.+)-(.+)/', $this->name, $matches);
if ($match === 1) {
return ucwords(preg_replace('/(\-|_)/', ' ', $matches[2]));
}
}
public function getResourceNameAttribute()
{
$match = preg_match('/(.+)-(.+)/', $this->name, $matches);
if ($match === 1) {
return $matches[2];
}
}
public function getActionAttribute()
{
$match = preg_match('/(.+)-(.+)/', $this->name, $matches);
if ($match === 1) {
return $matches[1];
}
}
public static function resourceTitleList()
{
//Grab all of our permissions
$all = self::all();
$grouped = $all->groupBy('resource_title')->sort();
return $grouped;
}
public static function resourceNameList()
{
//Grab all of our permissions
$all = self::all();
$grouped = $all->groupBy('resource_name');
return $grouped;
}
public static function for($resource)
{
return self::byResource($resource)->pluck('name');
}
public static function byResource($resource)
{
//Grab all of our permissions
$all = self::all();
//Filter them by the name of the resource
$filtered = $all->filter(function ($value, $key) use ($resource) {
$match = preg_match("/(.+)-{$resource}/", $value->name);
if ($match === 1) {
return true;
} else {
return false;
}
});
return $filtered;
}
public static function byName($name)
{
try {
if (is_array($name)) {
return self::whereIn('name', $name)->get();
} else {
return self::where('name', $name)->firstOrFail();
}
} catch (ModelNotFoundException $e) {
throw new ModelNotFoundException($name . ' permission does not exist');
}
}
public function users()
{
return $this->morphedByMany('ProcessMaker\Models\User', 'assignable');
}
public function groups()
{
return $this->morphedByMany('ProcessMaker\Models\Group', 'assignable');
}
}