1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class ApprovalsDispatcher extends Dispatcher { |
|
|
|
|
4
|
|
|
|
5
|
|
|
const ACTION_APPROVALS = 'approvals'; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
public static $allowed_actions = [ |
11
|
|
|
'approvers', |
12
|
|
|
'submit', |
13
|
|
|
'cancel', |
14
|
|
|
'approve', |
15
|
|
|
'reject' |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \DNProject |
20
|
|
|
*/ |
21
|
|
|
protected $project = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \DNEnvironment |
25
|
|
|
*/ |
26
|
|
|
protected $environment = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private static $action_types = [ |
|
|
|
|
32
|
|
|
self::ACTION_APPROVALS |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
public function init() { |
36
|
|
|
parent::init(); |
37
|
|
|
|
38
|
|
|
$this->project = $this->getCurrentProject(); |
39
|
|
|
if (!$this->project) { |
40
|
|
|
return $this->project404Response(); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \SS_HTTPRequest $request |
46
|
|
|
* @return \SS_HTTPResponse |
47
|
|
|
*/ |
48
|
|
|
public function approvers(SS_HTTPRequest $request) { |
49
|
|
|
$list = []; |
50
|
|
|
foreach ($this->project->listMembers() as $data) { |
|
|
|
|
51
|
|
|
if ($this->canApprove(Member::get()->byId($data['MemberID']))) { |
|
|
|
|
52
|
|
|
$list[] = [ |
53
|
|
|
'id' => $data['MemberID'], |
54
|
|
|
'email' => $data['Email'], |
55
|
|
|
'role' => $data['RoleTitle'], |
56
|
|
|
'name' => $data['FullName'] |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->getAPIResponse([ |
62
|
|
|
'approvers' => $list |
63
|
|
|
], 200); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param \SS_HTTPRequest $request |
68
|
|
|
* @return \SS_HTTPResponse |
69
|
|
|
*/ |
70
|
|
|
public function submit(SS_HTTPRequest $request) { |
71
|
|
|
if ($request->httpMethod() !== 'POST') { |
72
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// @todo |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param \SS_HTTPRequest $request |
80
|
|
|
* @return \SS_HTTPResponse |
81
|
|
|
*/ |
82
|
|
|
public function cancel(SS_HTTPRequest $request) { |
83
|
|
|
if ($request->httpMethod() !== 'POST') { |
84
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$deployment = DNDeployment::get()->byId($request->param('ID')); |
88
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
89
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
90
|
|
|
return $errorResponse; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// @todo permission checking for cancelling an approval request |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_NEW); |
97
|
|
|
} catch (\Exception $e) { |
98
|
|
|
return $this->getAPIResponse([ |
99
|
|
|
'status' => 'FAILED', |
100
|
|
|
'message' => $e->getMessage() |
101
|
|
|
], 400); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->getAPIResponse([ |
105
|
|
|
'status' => 'OK', |
106
|
|
|
'id' => $deployment->ID |
107
|
|
|
], 200); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param \SS_HTTPRequest $request |
112
|
|
|
* @return \SS_HTTPResponse |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function approve(SS_HTTPRequest $request) { |
|
|
|
|
115
|
|
|
if ($request->httpMethod() !== 'POST') { |
116
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$deployment = DNDeployment::get()->byId($request->param('ID')); |
120
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
121
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
122
|
|
|
return $errorResponse; |
123
|
|
|
} |
124
|
|
|
if (!$this->canApprove()) { |
125
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to reject this deployment'], 403); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
try { |
129
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_APPROVE); |
130
|
|
|
} catch (\Exception $e) { |
131
|
|
|
return $this->getAPIResponse([ |
132
|
|
|
'status' => 'FAILED', |
133
|
|
|
'message' => $e->getMessage() |
134
|
|
|
], 400); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->getAPIResponse([ |
138
|
|
|
'status' => 'OK', |
139
|
|
|
'id' => $deployment->ID |
140
|
|
|
], 200); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param \SS_HTTPRequest $request |
145
|
|
|
* @return \SS_HTTPResponse |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function reject(SS_HTTPRequest $request) { |
|
|
|
|
148
|
|
|
if ($request->httpMethod() !== 'POST') { |
149
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$deployment = DNDeployment::get()->byId($request->param('ID')); |
153
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
154
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
155
|
|
|
return $errorResponse; |
156
|
|
|
} |
157
|
|
|
// can reject permissions are the same as can approve |
158
|
|
|
if (!$this->canApprove()) { |
159
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to reject this deployment'], 403); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
try { |
163
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_REJECT); |
164
|
|
|
} catch (\Exception $e) { |
165
|
|
|
return $this->getAPIResponse([ |
166
|
|
|
'status' => 'FAILED', |
167
|
|
|
'message' => $e->getMessage() |
168
|
|
|
], 400); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->getAPIResponse([ |
172
|
|
|
'status' => 'OK', |
173
|
|
|
'message' => 'Deployment has been rejected', |
174
|
|
|
'id' => $deployment->ID, |
175
|
|
|
], 200); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
180
|
|
|
* an APIResponse with the error, otherwise null. |
181
|
|
|
* |
182
|
|
|
* @param \DNDeployment $deployment |
183
|
|
|
* |
184
|
|
|
* @return null|SS_HTTPResponse |
185
|
|
|
*/ |
186
|
|
View Code Duplication |
protected function validateDeployment(\DNDeployment $deployment) { |
|
|
|
|
187
|
|
|
if (!$deployment || !$deployment->exists()) { |
188
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
189
|
|
|
} |
190
|
|
|
if (!$deployment->canView()) { |
191
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to view this deployment'], 403); |
192
|
|
|
} |
193
|
|
|
return null; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
protected function canApprove(Member $member = null) { |
197
|
|
|
if (!$member) { |
198
|
|
|
$member = Member::currentUser(); |
199
|
|
|
} |
200
|
|
|
if (!$member) { |
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
if (Permission::checkMember($member, 'ADMIN')) { |
204
|
|
|
return true; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
foreach ($this->project->listMembers() as $data) { |
|
|
|
|
208
|
|
|
if ($data['MemberID'] == $member->ID && in_array($data['RoleTitle'], [ |
209
|
|
|
GroupExtension::STACK_MANAGER, |
210
|
|
|
GroupExtension::RELEASE_MANAGER |
211
|
|
|
])) { |
212
|
|
|
return true; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return false; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $name |
221
|
|
|
* |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
public function getModel($name = '') { |
225
|
|
|
return []; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param string $action |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function Link($action = '') { |
233
|
|
|
return \Controller::join_links($this->project->Link(), self::ACTION_APPROVALS, $action); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
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
.