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