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
|
|
|
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
|
|
|
$this->checkSecurityToken(); |
101
|
|
|
|
102
|
|
|
$deployment = DNDeployment::get()->byId($request->postVar('id')); |
103
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
104
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
105
|
|
|
return $errorResponse; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$approver = Member::get()->byId($request->postVar('approver_id')); |
109
|
|
|
if ($approver && $approver->exists()) { |
110
|
|
|
if (!$this->project->allowed(ApprovalsDispatcher::ALLOW_APPROVAL, $approver)) { |
|
|
|
|
111
|
|
|
return $this->getAPIResponse(['message' => 'The given approver does not have permissions to approve'], 403); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$deployment->ApproverID = $approver->ID; |
115
|
|
|
$deployment->write(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// title and summary may have changed, ensure they are saved |
119
|
|
|
if ($request->postVar('title')) { |
120
|
|
|
$deployment->Title = $request->postVar('title'); |
121
|
|
|
} |
122
|
|
|
if ($request->postVar('summary')) { |
123
|
|
|
$deployment->Summary = $request->postVar('summary'); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
try { |
127
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_SUBMIT); |
128
|
|
|
} catch (\Exception $e) { |
129
|
|
|
return $this->getAPIResponse([ |
130
|
|
|
'message' => $e->getMessage() |
131
|
|
|
], 400); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->getAPIResponse([ |
135
|
|
|
'message' => 'Deployment request has been submitted', |
136
|
|
|
'deployment' => $this->formatter->getDeploymentData($deployment) |
|
|
|
|
137
|
|
|
], 200); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param \SS_HTTPRequest $request |
142
|
|
|
* @return \SS_HTTPResponse |
143
|
|
|
*/ |
144
|
|
|
public function cancel(\SS_HTTPRequest $request) { |
145
|
|
|
if ($request->httpMethod() !== 'POST') { |
146
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->checkSecurityToken(); |
150
|
|
|
|
151
|
|
|
$deployment = DNDeployment::get()->byId($request->postVar('id')); |
152
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
153
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
154
|
|
|
return $errorResponse; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// if the person cancelling is not the one who created the deployment, update the deployer |
158
|
|
|
if (Member::currentUserID() !== $deployment->DeployerID) { |
159
|
|
|
$deployment->DeployerID = Member::currentUserID(); |
160
|
|
|
$deployment->write(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
try { |
164
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_NEW); |
165
|
|
|
} catch (\Exception $e) { |
166
|
|
|
return $this->getAPIResponse([ |
167
|
|
|
'message' => $e->getMessage() |
168
|
|
|
], 400); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->getAPIResponse([ |
172
|
|
|
'message' => 'Deployment request has been cancelled', |
173
|
|
|
'deployment' => $this->formatter->getDeploymentData($deployment) |
|
|
|
|
174
|
|
|
], 200); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param \SS_HTTPRequest $request |
179
|
|
|
* @return \SS_HTTPResponse |
180
|
|
|
*/ |
181
|
|
|
public function approve(\SS_HTTPRequest $request) { |
182
|
|
|
if ($request->httpMethod() !== 'POST') { |
183
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$this->checkSecurityToken(); |
187
|
|
|
|
188
|
|
|
$deployment = DNDeployment::get()->byId($request->postVar('id')); |
189
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
190
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
191
|
|
|
return $errorResponse; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// ensure we have either bypass or approval permission of the logged in user |
195
|
|
|
if ( |
196
|
|
|
!$this->project->allowed(self::ALLOW_APPROVAL_BYPASS, Member::currentUser()) |
|
|
|
|
197
|
|
|
|| !$this->project->allowed(self::ALLOW_APPROVAL, Member::currentUser()) |
|
|
|
|
198
|
|
|
) { |
199
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to approve or bypass this deployment'], 403); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// check for specific permission depending on the current state of the deployment: |
203
|
|
|
// submitted => approved requires approval permissions |
204
|
|
|
// new => approved requires bypass permissions. |
205
|
|
View Code Duplication |
if ( |
|
|
|
|
206
|
|
|
$deployment->State === DNDeployment::STATE_SUBMITTED |
207
|
|
|
&& !$this->project->allowed(self::ALLOW_APPROVAL, Member::currentUser()) |
|
|
|
|
208
|
|
|
) { |
209
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to approve this deployment'], 403); |
210
|
|
|
} |
211
|
|
View Code Duplication |
if ( |
|
|
|
|
212
|
|
|
$deployment->State === DNDeployment::STATE_NEW |
213
|
|
|
&& !$this->project->allowed(self::ALLOW_APPROVAL_BYPASS, Member::currentUser()) |
|
|
|
|
214
|
|
|
) { |
215
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to bypass approval of this deployment'], 403); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if ($deployment->State === DNDeployment::STATE_NEW) { |
219
|
|
|
// Bypassing approval: Ensure that approver is not set. This may happen when someone has requested approval, |
220
|
|
|
// cancelled approval, then bypassed. |
221
|
|
|
$deployment->ApproverID = 0; |
222
|
|
|
$deployment->write(); |
223
|
|
|
} else { |
224
|
|
|
// if the current user is not the person who was selected for approval on submit, but they got |
225
|
|
|
// here because they still have permission, then change the approver to the current user |
226
|
|
|
if (Member::currentUserID() !== $deployment->ApproverID) { |
227
|
|
|
$deployment->ApproverID = Member::currentUserID(); |
228
|
|
|
$deployment->write(); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// title and summary may have changed, ensure they are saved |
233
|
|
|
if ($request->postVar('title')) { |
234
|
|
|
$deployment->Title = $request->postVar('title'); |
235
|
|
|
} |
236
|
|
|
if ($request->postVar('summary')) { |
237
|
|
|
$deployment->Summary = $request->postVar('summary'); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
try { |
241
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_APPROVE); |
242
|
|
|
} catch (\Exception $e) { |
243
|
|
|
return $this->getAPIResponse([ |
244
|
|
|
'message' => $e->getMessage() |
245
|
|
|
], 400); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $this->getAPIResponse([ |
249
|
|
|
'message' => 'Deployment request has been approved', |
250
|
|
|
'deployment' => $this->formatter->getDeploymentData($deployment) |
|
|
|
|
251
|
|
|
], 200); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param \SS_HTTPRequest $request |
256
|
|
|
* @return \SS_HTTPResponse |
257
|
|
|
*/ |
258
|
|
|
public function reject(\SS_HTTPRequest $request) { |
259
|
|
|
if ($request->httpMethod() !== 'POST') { |
260
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$this->checkSecurityToken(); |
264
|
|
|
|
265
|
|
|
$deployment = DNDeployment::get()->byId($request->postVar('id')); |
266
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
267
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
268
|
|
|
return $errorResponse; |
269
|
|
|
} |
270
|
|
|
// reject permissions are the same as can approve |
271
|
|
View Code Duplication |
if (!$this->project->allowed(self::ALLOW_APPROVAL, Member::currentUser())) { |
|
|
|
|
272
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to reject this deployment'], 403); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
// if the current user is not the person who was selected for approval on submit, but they got |
276
|
|
|
// here because they still have permission, then change the approver to the current user |
277
|
|
|
if (Member::currentUserID() !== $deployment->ApproverID) { |
278
|
|
|
$deployment->ApproverID = Member::currentUserID(); |
279
|
|
|
$deployment->write(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
try { |
283
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_REJECT); |
284
|
|
|
} catch (\Exception $e) { |
285
|
|
|
return $this->getAPIResponse([ |
286
|
|
|
'message' => $e->getMessage() |
287
|
|
|
], 400); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
return $this->getAPIResponse([ |
291
|
|
|
'message' => 'Deployment request has been rejected', |
292
|
|
|
'deployment' => $this->formatter->getDeploymentData($deployment) |
|
|
|
|
293
|
|
|
], 200); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
298
|
|
|
* an APIResponse with the error, otherwise null. |
299
|
|
|
* |
300
|
|
|
* @param \DNDeployment $deployment |
301
|
|
|
* |
302
|
|
|
* @return null|SS_HTTPResponse |
303
|
|
|
*/ |
304
|
|
View Code Duplication |
protected function validateDeployment($deployment) { |
|
|
|
|
305
|
|
|
if (!$deployment || !$deployment->exists()) { |
306
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
307
|
|
|
} |
308
|
|
|
if ($deployment->EnvironmentID != $this->environment->ID) { |
309
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not belong to the environment'], 403); |
310
|
|
|
} |
311
|
|
|
if (!$deployment->canView()) { |
312
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to view this deployment'], 403); |
313
|
|
|
} |
314
|
|
|
return null; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param string $name |
319
|
|
|
* @return array |
320
|
|
|
*/ |
321
|
|
|
public function getModel($name = '') { |
322
|
|
|
return []; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param string $action |
327
|
|
|
* @return string |
328
|
|
|
*/ |
329
|
|
|
public function Link($action = '') { |
330
|
|
|
return \Controller::join_links($this->environment->Link(), self::ACTION_APPROVALS, $action); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
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
.