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