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
134 lines (111 loc) · 3.54 KB
/
Permission.php
File metadata and controls
134 lines (111 loc) · 3.54 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
class Permission extends ProcessMakerModel
{
protected $connection = 'processmaker';
protected $fillable = [
'title',
'name',
'group',
];
const DEFAULT_PERMISSIONS = ['Projects', 'Process Catalog'];
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');
}
/**
* This retrieves the users who have permissions in a list of groups.
*
* @param array $groups
* @return Collection
*/
public static function getUsersByGroup(array $groups)
{
$users = DB::table('assignables')
->join('permissions', function ($join) use ($groups) {
$join->on('assignables.permission_id', '=', 'permissions.id')
->whereIn('permissions.group', $groups);
})
->join('users', function ($join) {
$join->on('assignables.assignable_type', '=', DB::raw("'ProcessMaker\\\Models\\\User'"))
->on('assignables.assignable_id', '=', 'users.id');
})
->select('users.*')
->union(\ProcessMaker\Models\User::where('is_administrator', '=', true))
->groupBy('users.id')
->get();
return $users;
}
}