forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessPolicy.php
More file actions
125 lines (105 loc) · 3.36 KB
/
ProcessPolicy.php
File metadata and controls
125 lines (105 loc) · 3.36 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
<?php
namespace ProcessMaker\Policies;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Http\Request;
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 \ProcessMaker\Models\User $user
* @return mixed
*/
public function before(User $user)
{
if ($user->is_administrator) {
return true;
}
}
/**
* Determine whether the user can start the process.
*
* @param \ProcessMaker\Models\User $user
* @param \ProcessMaker\Models\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' && $process->manager_id === $user->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 \ProcessMaker\Models\User $user
* @param \ProcessMaker\Models\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 (
$process->manager_id === $user->id &&
$process->getProperty('manager_can_cancel_request') === true
) {
return true;
}
return false;
}
/**
* Determine whether the user can edit data.
*
* @param \ProcessMaker\Models\User $user
* @param \ProcessMaker\Models\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;
}
}