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
140 lines (117 loc) · 4.92 KB
/
ForUserScope.php
File metadata and controls
140 lines (117 loc) · 4.92 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
135
136
137
138
139
140
<?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;
}
$userHasSelfServiceTasks = ProcessRequestToken::where('is_self_service', true)
->whereJsonContains('self_service_groups->users', (string) $user->id)
->pluck('process_request_id')->toArray();
$stringGroupIds = $user->groups()
->pluck('groups.id')
->map(fn ($id) => (string) $id);
$userHasSelfServiceTasksGroups = ProcessRequestToken::where('is_self_service', true)
->whereRaw(
'JSON_OVERLAPS(JSON_EXTRACT(`self_service_groups`, \'$."groups"\'), ?)',
[
$stringGroupIds->toJson(),
]
)
->pluck('process_request_id')->toArray();
$processableUser = DB::table('processables')->where([
'processable_type' => User::class,
'processable_id' => $user->id,
'method' => 'EDIT_DATA',
])->pluck('process_id')->toArray();
$processableGroups = DB::table('processables')->where([
'processable_type' => Group::class,
'method' => 'EDIT_DATA',
])->whereIn('processable_id', $stringGroupIds->toArray())->pluck('process_id')->toArray();
$filteredQuery = $query->userStarted($user)
->orWhereIn('id',
array_unique(array_merge($userHasSelfServiceTasks, $userHasSelfServiceTasksGroups)))
->orWhereIn('process_id', array_unique(array_merge($processableUser, $processableGroups)));
// If the user has participated in a request, the request is added to the results.
// For the anonymous user this condition is not needed as it is a "placeholder" system account
if ($user->username !== '_pm4_anon_user') {
$mainTable = $query->getQuery()->from;
$filteredQuery->orWhereExists(function ($query) use ($user, $mainTable) {
$query->selectRaw('1')
->from('process_request_tokens')
->where('user_id', $user->id)
->whereRaw('process_request_id = ' . $mainTable . '.id');
});
}
return $filteredQuery;
}
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;
}
}