-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapprove.php
496 lines (441 loc) · 14.3 KB
/
approve.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<?php
/**
* Copyright (C) 2013-2024 Combodo SAS
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
/**
* Approval page
*
* @author Erwan Taloc <[email protected]>
* @author Romain Quetiez <[email protected]>
* @author Denis Flaven <[email protected]>
* @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
*/
use Combodo\iTop\ApprovalBase\Renderer\UnauthenticatedRenderer;
require_once('../../approot.inc.php');
//remove require itopdesignformat at the same time as version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') < 0
if (! defined("ITOP_DESIGN_LATEST_VERSION")) {
require_once APPROOT.'setup/itopdesignformat.class.inc.php';
}
require_once(APPROOT.'/application/application.inc.php');
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') < 0) {
require_once(APPROOT."/application/nicewebpage.class.inc.php");
}
require_once(APPROOT.'/application/wizardhelper.class.inc.php');
class QuitException extends Exception
{}
function ReadMandatoryParam($sParam)
{
$value = utils::ReadParam($sParam, null);
if (is_null($value))
{
throw new Exception("Missing argument '$sParam'");
}
return $value;
}
/**
* Compare ITOP_DESIGN_LATEST_VERSION with iTop version without Unauthenticated page and returns true if the former is <= to the later.
*
* @return bool
*
* @since 3.1.0
*/
function UseApprovalLegacyWebpage(){
return _ApprovalScheme_::UseLegacy();
}
/**
* @return bool
*
* @since 3.1.0
*/
function UseUnauthenticatedWebPage(){
$bIsFromObjectDetails = utils::ReadParam('from', '') === 'object_details';
return !$bIsFromObjectDetails && !UseApprovalLegacyWebpage();
}
/**
* Set the stage and that the approval is ongoing
*/
function CheckApprovalSchemeAndShowTitle($oP, $iSchemeId)
{
/** @var \_ApprovalScheme_ $oScheme */
$oScheme = MetaModel::GetObject('ApprovalScheme', $iSchemeId, false);
if (!$oScheme)
{
$oP->p(Dict::S('Approval:Form:ObjectDeleted'));
throw new QuitException();
}
if (UseUnauthenticatedWebPage()){
$oScheme->SetRenderer(new UnauthenticatedRenderer());
}
$oObject = MetaModel::GetObject($oScheme->Get('obj_class'), $oScheme->Get('obj_key'));
$sTitle = Dict::Format('Approval:Form:Ref', (UseUnauthenticatedWebPage() ? $oObject->Get('friendlyname') : $oObject->GetHyperLink()));
$oScheme->GetRenderer()->RenderTitle($oP, $sTitle);
if ($oScheme->Get('status') == 'accepted')
{
$oP->p(Dict::S('Approval:Form:AlreadyApproved'));
throw new QuitException();
}
elseif ($oScheme->Get('status') == 'rejected')
{
$oP->p(Dict::S('Approval:Form:AlreadyRejected'));
throw new QuitException();
}
return array($oScheme, $oObject);
}
/**
* Interpret the token to build the page arguments
* Validate that the arguments are consistent altogether
*/
function GetContext($oP, $sToken)
{
// For the moment, the token is made of <scheme_id>-<step>-<contact_class>-<contact_id>-<passcode>
$aToken = explode('-', $sToken);
if (count($aToken) != 5)
{
throw new CoreException("Unexpected value for token: '$sToken' does not have the required format");
}
$iSchemeId = $aToken[0];
$iStep = $aToken[1];
$sApproverClass = $aToken[2];
$iApproverId = $aToken[3];
$sPassCode = $aToken[4];
$oApprover = MetaModel::GetObject($sApproverClass, $iApproverId, false);
if (!$oApprover)
{
$oP->p(Dict::S('Approval:Form:ApproverDeleted'));
throw new QuitException();
}
list($oScheme, $oObject) = CheckApprovalSchemeAndShowTitle($oP, $iSchemeId);
$aSteps = $oScheme->GetSteps();
if ($iStep < $oScheme->Get('current_step'))
{
if ($aSteps[$iStep]['approved'])
{
$oP->p(Dict::S('Approval:Form:StepApproved'));
throw new QuitException();
}
else
{
$oP->p(Dict::S('Approval:Form:StepRejected'));
throw new QuitException();
}
}
if ($iStep > $oScheme->Get('current_step'))
{
throw new CoreException("Unexpected value for step: '$iStep' is not started or exceeds allowed values");
}
// Given the passcode, find the approver amongst the existing approvers for the given step...
//
$bFoundRecord = false;
$oSubstitute = null;
foreach($aSteps[$iStep]['approvers'] as $aApproverData)
{
if ($bFoundRecord)
{
break;
}
if ( ($aApproverData['class'] == get_class($oApprover))
&& ($aApproverData['id'] == $oApprover->GetKey())
&& ($aApproverData['passcode'] == $sPassCode) )
{
if (array_key_exists('answer_time', $aApproverData)
&& array_key_exists('replier_index', $aApproverData))
{
// The replier is one of the substitutes (not the main approver)
$iReplier = $aApproverData['replier_index'];
$aReplierData = $aApproverData['forward'][$iReplier];
$oReplier = MetaModel::GetObject($aReplierData['class'], $aReplierData['id'], false);
$sReplierName = is_null($oReplier) ? $aReplierData['class'].'::'.$aReplierData['id'].' (deleted)'
: $oReplier->GetName();
$oP->p(Dict::Format('Approval:Form:AnswerGivenBy', $sReplierName));
throw new QuitException();
}
$bFoundRecord = true;
}
elseif (array_key_exists('forward', $aApproverData))
{
foreach($aApproverData['forward'] as $iSubstitue => $aSubstituteData)
{
if ( ($aSubstituteData['class'] == get_class($oApprover))
&& ($aSubstituteData['id'] == $oApprover->GetKey())
&& ($aSubstituteData['passcode'] == $sPassCode) )
{
// Ultimate check: either nobody has answered (for this approver), or the existing answer is from the current replier
if (array_key_exists('answer_time', $aApproverData))
{
if (array_key_exists('replier_index', $aApproverData))
{
$iReplier = $aApproverData['replier_index'];
if ($iReplier != $iSubstitue)
{
// The replier is not the current subtitute
$aReplierData = $aApproverData['forward'][$iReplier];
$oReplier = MetaModel::GetObject($aReplierData['class'], $aReplierData['id'], false);
$sReplierName = is_null($oReplier) ? $aReplierData['class'].'::'.$aReplierData['id'].' (deleted)'
: $oReplier->GetName();
$oP->p(Dict::Format('Approval:Form:AnswerGivenBy', $sReplierName));
throw new QuitException();
}
}
else
{
// The replier is the main approver (not the current substitute)
$oReplier = MetaModel::GetObject($aApproverData['class'], $aApproverData['id'], false);
$sReplierName = is_null($oReplier) ? $aApproverData['class'].'::'.$aApproverData['id'].' (deleted)'
: $oReplier->GetName();
$oP->p(Dict::Format('Approval:Form:AnswerGivenBy', $sReplierName));
throw new QuitException();
}
}
$oApprover = MetaModel::GetObject($aApproverData['class'], $aApproverData['id'], false);
$oSubstitute = MetaModel::GetObject($aSubstituteData['class'], $aSubstituteData['id'], false);
if (!$oApprover || !$oSubstitute)
{
$oP->p(Dict::S('Approval:Form:ApproverDeleted'));
throw new QuitException();
}
$bFoundRecord = true;
break;
}
}
}
}
if (!$bFoundRecord)
{
throw new CoreException("Unexpected approver for this step");
}
return array($oScheme, $iStep, $oApprover, $oObject, $oSubstitute);
}
function ShowApprovalForm($sFrom, $oP, $sToken)
{
list($oScheme, $iStep, $oApprover, $oObject, $oSubstitute) = GetContext($oP, $sToken);
$oScheme->DisplayApprovalForm($sFrom, $oP, $oApprover, $oObject, $sToken, $oSubstitute);
}
function AfterSubmit($oP, $oScheme, $bReturnToObjectDetails)
{
if ($oScheme->Get('status') == 'accepted')
{
$sOutcome = Dict::S('Approval:Form:AnswerRecorded-Approved');
}
elseif ($oScheme->Get('status') == 'rejected')
{
$sOutcome = Dict::S('Approval:Form:AnswerRecorded-Rejected');
}
else
{
$sOutcome = Dict::S('Approval:Form:AnswerRecorded-Continue');
}
$oP->p($sOutcome);
if ($bReturnToObjectDetails)
{
cmdbAbstractObject::SetSessionMessage($oScheme->Get('obj_class'), $oScheme->Get('obj_key'), 'approval-result', $sOutcome, 'info', 0, true /* must not exist */);
$oAppContext = new ApplicationContext();
$oP->add_header('Location: '.utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=details&class='.$oScheme->Get('obj_class').'&id='.$oScheme->Get('obj_key').'&'.$oAppContext->GetForLink());
}
}
function SubmitAnswer($sFrom, $oP, $sToken, $bApprove, $sComment)
{
list($oScheme, $iStep, $oApprover, $oObject, $oSubstitute) = GetContext($oP, $sToken);
// As the current user is not necessarily logged in, keep track of her name
$sTrackInfo = $oScheme->GetIssuerInfo($bApprove, $oApprover, $oSubstitute);
CMDBObject::SetTrackInfo($sTrackInfo);
// Record the approval/rejection
//
$oScheme->OnAnswer($iStep, $oApprover, $bApprove, $oSubstitute, $sComment);
$bReturnToObjectDetails = ($sFrom == 'object_details');
AfterSubmit($oP, $oScheme, $bReturnToObjectDetails);
}
function ShowAbortForm($sFrom, $oP, $iApprovalId)
{
list($oScheme, $oObject) = CheckApprovalSchemeAndShowTitle($oP, $iApprovalId);
if (!$oScheme->IsAllowedToAbort())
{
throw new Exception("Only administrators are allowed to abort an approval process");
}
$oScheme->DisplayAbortForm($sFrom, $oP);
}
function SubmitAbort($sFrom, $oP, $iApprovalId, $bApprove, $sComment)
{
list($oScheme, $oObject) = CheckApprovalSchemeAndShowTitle($oP, $iApprovalId);
if (!$oScheme->IsAllowedToAbort())
{
throw new Exception("Only administrators are allowed to abort an approval process");
}
$oScheme->OnAbort($bApprove, $sComment);
$bReturnToObjectDetails = ($sFrom == 'object_details');
AfterSubmit($oP, $oScheme, $bReturnToObjectDetails);
}
/////////////////////////////
//
// Main
//
try
{
require_once(APPROOT.'/application/startup.inc.php');
$sOperation = utils::ReadParam('operation', '');
$sFrom = utils::ReadParam('from', '');
$bAbort = (utils::ReadParam('abort', '') == 1);
if ($sFrom == 'object_details')
{
require_once(APPROOT.'/application/loginwebpage.class.inc.php');
LoginWebPage::DoLoginEx(); // Check user rights and prompt if needed
if (version_compare(ITOP_DESIGN_LATEST_VERSION, '3.0') < 0) {
require_once(APPROOT.'application/itopwebpage.class.inc.php');
}
$oP = new iTopWebPage(Dict::S('Approval:Form:Title'));
$sModule = utils::GetAbsoluteUrlModulesRoot().'approval-base/asset/img';
if(UseApprovalLegacyWebpage()){
$oP->add_style(
<<<EOF
#comment{
display: block;
margin-bottom: 15px;
min-height: 150px;
}
#approval-button {
background: url("$sModule/approve.png") no-repeat scroll 10px center rgba(0, 0, 0, 0);
}
#rejection-button {
background: url("$sModule/reject.png") no-repeat scroll 10px center rgba(0, 0, 0, 0);
}
#approval-button, #rejection-button {
margin: 0 10px 10px;
padding: 5px 10px 5px 35px;
}
EOF
);
}
else {
$oP->LinkStylesheetFromModule('approval-base/asset/css/approve.css');
}
}
else
{
if(UseUnauthenticatedWebPage()) {
$oP = new UnauthenticatedWebPage(Dict::S('Approval:Form:Title'));
}
else {
require_once(MODULESROOT.'approval-base/approvalwebpage.class.inc.php');
$oP = new ApprovalWebPage(Dict::S('Approval:Form:Title'));
}
}
$oP->set_base(utils::GetAbsoluteUrlAppRoot().'pages/');
switch ($sOperation)
{
case 'do_approve':
case 'do_reject':
$bApprove = ($sOperation == 'do_approve');
$sComment = trim(utils::ReadParam('comment', '', false, 'raw_data'));
if (!$bApprove && ($sComment == ''))
{
throw new Exception('Empty comment not authorized!'); // bug: should be protected in the form
}
if ($bAbort)
{
$iApprovalId = ReadMandatoryParam('approval_id');
SubmitAbort($sFrom, $oP, $iApprovalId, $bApprove, $sComment);
}
else
{
$sToken = ReadMandatoryParam('token');
SubmitAnswer($sFrom, $oP, $sToken, $bApprove, $sComment);
}
break;
case 'reject': // legacy: emails were created with such an option
case 'approve': // legacy: emails were created with such an option
default:
if ($bAbort)
{
$iApprovalId = ReadMandatoryParam('approval_id');
ShowAbortForm($sFrom, $oP, $iApprovalId);
}
else
{
$sToken = ReadMandatoryParam('token');
ShowApprovalForm($sFrom, $oP, $sToken);
}
}
$oP->output();
}
catch(QuitException $e)
{
$oP->output();
}
catch(CoreException $e)
{
require_once(APPROOT.'/setup/setuppage.class.inc.php');
$oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
$oP->set_base(utils::GetAbsoluteUrlAppRoot().'pages/');
$oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
$oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
$oP->output();
if (MetaModel::IsLogEnabledIssue())
{
if (MetaModel::IsValidClass('EventIssue'))
{
try
{
$oLog = new EventIssue();
$oLog->Set('message', $e->getMessage());
$oLog->Set('userinfo', '');
$oLog->Set('issue', $e->GetIssue());
$oLog->Set('impact', 'Page could not be displayed');
$oLog->Set('callstack', $e->getTrace());
$oLog->Set('data', $e->getContextData());
$oLog->DBInsertNoReload();
}
catch(Exception $e)
{
IssueLog::Error("Failed to log issue into the DB");
}
}
IssueLog::Error($e->getMessage());
}
// For debugging only
//throw $e;
}
catch(Exception $e)
{
require_once(APPROOT.'/setup/setuppage.class.inc.php');
$oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
$oP->set_base(utils::GetAbsoluteUrlAppRoot().'pages/');
$oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
$oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
$oP->output();
if (MetaModel::IsLogEnabledIssue())
{
if (MetaModel::IsValidClass('EventIssue'))
{
try
{
$oLog = new EventIssue();
$oLog->Set('message', $e->getMessage());
$oLog->Set('userinfo', '');
$oLog->Set('issue', 'PHP Exception');
$oLog->Set('impact', 'Page could not be displayed');
$oLog->Set('callstack', $e->getTrace());
$oLog->Set('data', array());
$oLog->DBInsertNoReload();
}
catch(Exception $e)
{
IssueLog::Error("Failed to log issue into the DB");
}
}
IssueLog::Error($e->getMessage());
}
}
?>