forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForUserScope.php
More file actions
98 lines (82 loc) · 3.08 KB
/
ForUserScope.php
File metadata and controls
98 lines (82 loc) · 3.08 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
<?php
namespace ProcessMaker\Traits;
use DB;
use ProcessMaker\Models\Group;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\User;
trait ForUserScope
{
public function scopeForUser($query, $user)
{
if ($user->is_administrator) {
// Allow all
return $query;
}
if ($user->canAny('edit-request_data|view-all_requests')) {
// Allow all
return $query;
}
return $query->userStarted($user)
->orWhere(fn ($q) => $q->userParticipated($user))
->orWhere(fn ($q) => $q->userHasSelfServiceTasks($user))
->orWhere(fn ($q) => $q->userHasEditProcessDataPermission($user));
}
public function scopeUserStarted($query, $user)
{
return $query->where('user_id', $user->id);
}
public function scopeUserParticipated($query, $user)
{
return $query->whereIn('id', function ($query) use ($user) {
$query->select('process_request_id')
->from((new ProcessRequestToken)->getTable())
->where('user_id', $user->id);
});
}
public function scopeUserHasSelfServiceTasks($query, $user)
{
$query->whereIn('id', function ($query) use ($user) {
$stringUserId = (string) $user->id;
return $query->select('process_request_id')
->from((new ProcessRequestToken)->getTable())
->where('is_self_service', true)
->whereJsonContains('self_service_groups->users', $stringUserId);
});
$stringGroupIds = $user->groups()
->pluck('groups.id')
->map(fn ($id) => (string) $id);
if ($stringGroupIds->isNotEmpty()) {
$query->orWhereIn('id', function ($query) use ($stringGroupIds) {
return $query->select('process_request_id')
->from((new ProcessRequestToken)->getTable())
->where('is_self_service', true)
->whereRaw(
'JSON_OVERLAPS(JSON_EXTRACT(`self_service_groups`, \'$."groups"\'), ?)',
[
$stringGroupIds->toJson(),
]
);
});
}
return $query;
}
public function scopeUserHasEditProcessDataPermission($query, $user)
{
$query->whereIn('process_id', DB::table('processables')->select('process_id')->where([
'processable_type' => User::class,
'processable_id' => $user->id,
'method' => 'EDIT_DATA',
]));
$stringGroupIds = $user->groups()
->pluck('groups.id')
->map(fn ($id) => (string) $id);
if ($stringGroupIds->isNotEmpty()) {
$processables = DB::table('processables')->select('process_id')->where([
'processable_type' => Group::class,
'method' => 'EDIT_DATA',
])->whereIn('processable_id', $stringGroupIds);
$query->orWhereIn('process_id', $processables);
}
return $query;
}
}