1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class ApprovalsDispatcher extends Dispatcher { |
|
|
|
|
4
|
|
|
|
5
|
|
|
const ACTION_APPROVALS = 'approvals'; |
6
|
|
|
|
7
|
|
|
const ALLOW_APPROVAL = 'ALLOW_APPROVAL'; |
8
|
|
|
|
9
|
|
|
const ALLOW_APPROVAL_BYPASS = 'ALLOW_APPROVAL_BYPASS'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private static $allowed_actions = [ |
|
|
|
|
15
|
|
|
'approvers', |
16
|
|
|
'submit', |
17
|
|
|
'cancel', |
18
|
|
|
'approve', |
19
|
|
|
'reject' |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
private static $dependencies = [ |
23
|
|
|
'formatter' => '%$DeploynautAPIFormatter' |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \DNProject |
28
|
|
|
*/ |
29
|
|
|
protected $project = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \DNEnvironment |
33
|
|
|
*/ |
34
|
|
|
protected $environment = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private static $action_types = [ |
|
|
|
|
40
|
|
|
self::ACTION_APPROVALS |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* This is a per request cache of $this->project()->listMembers() |
45
|
|
|
* @var null|array |
46
|
|
|
*/ |
47
|
|
|
private static $_cache_project_members = null; |
48
|
|
|
|
49
|
|
View Code Duplication |
public function init() { |
|
|
|
|
50
|
|
|
parent::init(); |
51
|
|
|
|
52
|
|
|
$this->project = $this->getCurrentProject(); |
53
|
|
|
if (!$this->project) { |
54
|
|
|
return $this->project404Response(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Performs canView permission check by limiting visible projects |
58
|
|
|
$this->environment = $this->getCurrentEnvironment($this->project); |
59
|
|
|
if (!$this->environment) { |
60
|
|
|
return $this->environment404Response(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param \SS_HTTPRequest $request |
66
|
|
|
* @return \SS_HTTPResponse |
67
|
|
|
*/ |
68
|
|
|
public function approvers(SS_HTTPRequest $request) { |
69
|
|
|
$list = []; |
70
|
|
|
|
71
|
|
|
if (self::$_cache_project_members === null) { |
72
|
|
|
self::$_cache_project_members = $this->project->listMembers(); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
foreach (self::$_cache_project_members as $data) { |
76
|
|
|
if ($this->project->allowed(self::ALLOW_APPROVAL, Member::get()->byId($data['MemberID']))) { |
|
|
|
|
77
|
|
|
$list[] = [ |
78
|
|
|
'id' => $data['MemberID'], |
79
|
|
|
'email' => $data['Email'], |
80
|
|
|
'role' => $data['RoleTitle'], |
81
|
|
|
'name' => $data['FullName'] |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->getAPIResponse([ |
87
|
|
|
'approvers' => $list |
88
|
|
|
], 200); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \SS_HTTPRequest $request |
93
|
|
|
* @return \SS_HTTPResponse |
94
|
|
|
*/ |
95
|
|
|
public function submit(SS_HTTPRequest $request) { |
96
|
|
|
if ($request->httpMethod() !== 'POST') { |
97
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$deployment = DNDeployment::get()->byId($request->postVar('id')); |
101
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
102
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
103
|
|
|
return $errorResponse; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$approver = Member::get()->byId($request->postVar('approver_id')); |
107
|
|
|
if ($approver && $approver->exists()) { |
108
|
|
|
if (!$this->project->allowed(ApprovalsDispatcher::ALLOW_APPROVAL, $approver)) { |
|
|
|
|
109
|
|
|
return $this->getAPIResponse(['message' => 'The given approver does not have permissions to approve'], 403); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$deployment->ApproverID = $approver->ID; |
113
|
|
|
$deployment->write(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
try { |
117
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_SUBMIT); |
118
|
|
|
} catch (\Exception $e) { |
119
|
|
|
return $this->getAPIResponse([ |
120
|
|
|
'message' => $e->getMessage() |
121
|
|
|
], 400); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->getAPIResponse([ |
125
|
|
|
'message' => 'Deployment request has been submitted', |
126
|
|
|
'deployment' => $this->formatter->getDeploymentData($deployment) |
|
|
|
|
127
|
|
|
], 200); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param \SS_HTTPRequest $request |
132
|
|
|
* @return \SS_HTTPResponse |
133
|
|
|
*/ |
134
|
|
|
public function cancel(SS_HTTPRequest $request) { |
135
|
|
|
if ($request->httpMethod() !== 'POST') { |
136
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$deployment = DNDeployment::get()->byId($request->postVar('id')); |
140
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
141
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
142
|
|
|
return $errorResponse; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// @todo permission checking for cancelling an approval request |
146
|
|
|
try { |
147
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_NEW); |
148
|
|
|
} catch (\Exception $e) { |
149
|
|
|
return $this->getAPIResponse([ |
150
|
|
|
'message' => $e->getMessage() |
151
|
|
|
], 400); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->getAPIResponse([ |
155
|
|
|
'message' => 'Deployment request has been cancelled', |
156
|
|
|
'deployment' => $this->formatter->getDeploymentData($deployment) |
|
|
|
|
157
|
|
|
], 200); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param \SS_HTTPRequest $request |
162
|
|
|
* @return \SS_HTTPResponse |
163
|
|
|
*/ |
164
|
|
|
public function approve(SS_HTTPRequest $request) { |
165
|
|
|
if ($request->httpMethod() !== 'POST') { |
166
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$deployment = DNDeployment::get()->byId($request->postVar('id')); |
170
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
171
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
172
|
|
|
return $errorResponse; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// ensure we have either bypass or approval permission of the logged in user |
176
|
|
|
if ( |
177
|
|
|
!$this->project->allowed(self::ALLOW_APPROVAL_BYPASS, Member::currentUser()) |
|
|
|
|
178
|
|
|
|| !$this->project->allowed(self::ALLOW_APPROVAL, Member::currentUser()) |
|
|
|
|
179
|
|
|
) { |
180
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to approve or bypass this deployment'], 403); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// check for specific permission depending on the current state of the deployment: |
184
|
|
|
// submitted => approved requires approval permissions |
185
|
|
|
// new => approved requires bypass permissions. |
186
|
|
View Code Duplication |
if ( |
|
|
|
|
187
|
|
|
$deployment->State === DNDeployment::STATE_SUBMITTED |
188
|
|
|
&& !$this->project->allowed(self::ALLOW_APPROVAL, Member::currentUser()) |
|
|
|
|
189
|
|
|
) { |
190
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to approve this deployment'], 403); |
191
|
|
|
} |
192
|
|
View Code Duplication |
if ( |
|
|
|
|
193
|
|
|
$deployment->State === DNDeployment::STATE_NEW |
194
|
|
|
&& !$this->project->allowed(self::ALLOW_APPROVAL_BYPASS, Member::currentUser()) |
|
|
|
|
195
|
|
|
) { |
196
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to bypass approval of this deployment'], 403); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// if the current user is not the person who was marked for approval but they got |
200
|
|
|
// here because they still have permission, then change the approver to the current user |
201
|
|
|
if (Member::currentUserID() !== $deployment->ApproverID) { |
202
|
|
|
$deployment->ApproverID = Member::currentUserID(); |
203
|
|
|
$deployment->write(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
try { |
207
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_APPROVE); |
208
|
|
|
} catch (\Exception $e) { |
209
|
|
|
return $this->getAPIResponse([ |
210
|
|
|
'message' => $e->getMessage() |
211
|
|
|
], 400); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $this->getAPIResponse([ |
215
|
|
|
'message' => 'Deployment request has been approved', |
216
|
|
|
'deployment' => $this->formatter->getDeploymentData($deployment) |
|
|
|
|
217
|
|
|
], 200); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param \SS_HTTPRequest $request |
222
|
|
|
* @return \SS_HTTPResponse |
223
|
|
|
*/ |
224
|
|
|
public function reject(SS_HTTPRequest $request) { |
225
|
|
|
if ($request->httpMethod() !== 'POST') { |
226
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$deployment = DNDeployment::get()->byId($request->postVar('id')); |
230
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
231
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
232
|
|
|
return $errorResponse; |
233
|
|
|
} |
234
|
|
|
// reject permissions are the same as can approve |
235
|
|
View Code Duplication |
if (!$this->project->allowed(self::ALLOW_APPROVAL, Member::currentUser())) { |
|
|
|
|
236
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to reject this deployment'], 403); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
// if the current user is not the person who was marked for approval but they got |
240
|
|
|
// here because they still have permission, then change the approver to the current user |
241
|
|
|
if (Member::currentUserID() !== $deployment->ApproverID) { |
242
|
|
|
$deployment->ApproverID = Member::currentUserID(); |
243
|
|
|
$deployment->write(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
try { |
247
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_REJECT); |
248
|
|
|
} catch (\Exception $e) { |
249
|
|
|
return $this->getAPIResponse([ |
250
|
|
|
'message' => $e->getMessage() |
251
|
|
|
], 400); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $this->getAPIResponse([ |
255
|
|
|
'message' => 'Deployment request has been rejected', |
256
|
|
|
'deployment' => $this->formatter->getDeploymentData($deployment) |
|
|
|
|
257
|
|
|
], 200); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
262
|
|
|
* an APIResponse with the error, otherwise null. |
263
|
|
|
* |
264
|
|
|
* @param \DNDeployment $deployment |
265
|
|
|
* |
266
|
|
|
* @return null|SS_HTTPResponse |
267
|
|
|
*/ |
268
|
|
View Code Duplication |
protected function validateDeployment(\DNDeployment $deployment) { |
|
|
|
|
269
|
|
|
if (!$deployment || !$deployment->exists()) { |
270
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
271
|
|
|
} |
272
|
|
|
if ($deployment->EnvironmentID != $this->environment->ID) { |
273
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not belong to the environment'], 403); |
274
|
|
|
} |
275
|
|
|
if (!$deployment->canView()) { |
276
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to view this deployment'], 403); |
277
|
|
|
} |
278
|
|
|
return null; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param string $name |
283
|
|
|
* @return array |
284
|
|
|
*/ |
285
|
|
|
public function getModel($name = '') { |
286
|
|
|
return []; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param string $action |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
public function Link($action = '') { |
294
|
|
|
return \Controller::join_links($this->environment->Link(), self::ACTION_APPROVALS, $action); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
} |
298
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.