-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathProcessPolicy.php
More file actions
156 lines (131 loc) · 4.06 KB
/
ProcessPolicy.php
File metadata and controls
156 lines (131 loc) · 4.06 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace ProcessMaker\Policies;
use Illuminate\Auth\Access\HandlesAuthorization;
use ProcessMaker\Models\AnonymousUser;
use ProcessMaker\Models\Group;
use ProcessMaker\Models\GroupMember;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\User;
class ProcessPolicy
{
use HandlesAuthorization;
/**
* Run before all methods to determine if the
* user is an admin and can do everything.
*
* @param User $user
* @return mixed
*/
public function before(User $user)
{
if ($user->is_administrator) {
return true;
}
}
public function edit(User $user, Process $process)
{
if ($this->isPmBlockImportedLocked($process)) {
return false;
}
return $user->can('edit-processes');
}
/**
* Determine whether the user can start the process.
*
* @param User $user
* @param Process $process
* @return mixed
*/
public function start(User $user, Process $process)
{
$userGroupIds = $user->groups->pluck('id')->all();
$nestedGroupIds = GroupMember::where('member_type', Group::class)->whereIn('member_id', $userGroupIds)->pluck('group_id')->all();
$groupIds = array_merge($userGroupIds, $nestedGroupIds);
if ($process->groupsCanStart(request()->query('event'))->whereIn('id', $groupIds)->count()) {
return true;
}
$usersCanStart = $process->usersCanStart(
request()->query('event')
)->pluck('id');
$userCanStartAsProcessManager = array_reduce($process->getStartEvents(),
function ($carry, $item) use ($process, $user) {
if (array_key_exists('assignment', $item)) {
$carry = $carry || ($item['assignment'] === 'process_manager' && in_array($user->id, $process->manager_id ?? []));
}
return $carry;
},
false);
if (
$usersCanStart->contains($user->id) ||
$usersCanStart->contains(app(AnonymousUser::class)->id) ||
$userCanStartAsProcessManager
) {
return true;
}
return false;
}
/**
* Determine whether the user can cancel the process.
*
* @param User $user
* @param Process $process
*
* @return bool
*/
public function cancel(User $user, Process $process)
{
$groupIds = $user->groups->pluck('id');
if ($process->groupsCanCancel()->whereIn('id', $groupIds)->exists()) {
return true;
}
if ($process->usersCanCancel()->where('id', $user->id)->exists()) {
return true;
}
if (
in_array($user->id, $process->manager_id ?? []) &&
$process->getProperty('manager_can_cancel_request') === true
) {
return true;
}
return false;
}
/**
* Determine whether the user can edit data.
*
* @param User $user
* @param Process $process
*
* @return bool
*/
public function editData(User $user, Process $process)
{
$groupIds = $user->groups->pluck('id');
if ($process->groupsCanEditData()->whereIn('id', $groupIds)->exists()) {
return true;
}
if ($process->usersCanEditData()->where('id', $user->id)->exists()) {
return true;
}
return false;
}
/**
* Check if the PM Block package is installed and if so,
* it checks if the related pmBlock of the given Process is imported and locked.
*/
private function isPmBlockImportedLocked(Process $process): bool
{
$className = 'ProcessMaker\Package\PackagePmBlocks\Models\PmBlock';
return class_exists($className) && $process->pmBlock?->is_imported_locked;
}
/**
* This determine if user can view processes.
*
* @param User $user
* @param Process $process
* @return bool
*/
public function view(User $user, Process $process)
{
return $user->can('view-processes');
}
}