-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathApprovalController.php
More file actions
134 lines (123 loc) · 3.89 KB
/
Copy pathApprovalController.php
File metadata and controls
134 lines (123 loc) · 3.89 KB
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
<?php
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Approval\Controller;
use OCA\Approval\AppInfo\Application;
use OCA\Approval\Exceptions\OutdatedEtagException;
use OCA\Approval\Service\ApprovalService;
use OCA\Approval\Service\RuleService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\IL10N;
use OCP\IRequest;
class ApprovalController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private ApprovalService $approvalService,
private RuleService $ruleService,
private IL10N $l10n,
private ?string $userId,
) {
parent::__construct($appName, $request);
}
/**
* @param int|null $fileId
* @return DataResponse
*/
#[NoAdminRequired]
public function getUserRequesterRules(?int $fileId = null): DataResponse {
$rules = $this->approvalService->getUserRules($this->userId, 'requesters', $fileId);
return new DataResponse($rules);
}
/**
* get file tags
*
* @param int $fileId
* @return DataResponse
*/
#[NoAdminRequired]
public function getApprovalState(int $fileId): DataResponse {
$state = $this->approvalService->getApprovalState($fileId, $this->userId);
if ($state['state'] !== Application::STATE_NOTHING) {
// who did that?
$stateToLookFor = $state['state'] === Application::STATE_APPROVABLE ? Application::STATE_PENDING : $state['state'];
$activity = $this->ruleService->getLastAction($fileId, $state['rule']['id'], $stateToLookFor);
if (!is_null($activity)) {
$state['userId'] = $activity['userId'];
$state['userName'] = $activity['userName'];
$state['timestamp'] = $activity['timestamp'];
$state['message'] = $activity['message'];
}
}
return new DataResponse($state);
}
/**
* @param int|null $since
* @return DataResponse
*/
#[NoAdminRequired]
public function getPendingNodes(?int $since = null): DataResponse {
$nodes = $this->approvalService->getPendingNodes($this->userId, $since);
return new DataResponse($nodes);
}
/**
* Approve a file
*
* @param int $fileId
* @param string $etag
* @param string|null $message
* @return DataResponse
*/
#[NoAdminRequired]
public function approve(int $fileId, string $etag, ?string $message = ''): DataResponse {
try {
if ($this->approvalService->approve($fileId, $this->userId, $message, $etag)) {
return new DataResponse([]);
}
return new DataResponse([], Http::STATUS_BAD_REQUEST);
} catch (OutdatedEtagException) {
return new DataResponse(['error' => $this->l10n->t('The file/folder you tried to approve has an outdated content, please reload and review it again')], Http::STATUS_BAD_REQUEST);
}
}
/**
* Reject a file
*
* @param int $fileId
* @param string $etag
* @param string|null $message
* @return DataResponse
*/
#[NoAdminRequired]
public function reject(int $fileId, string $etag, ?string $message = ''): DataResponse {
try {
if ($this->approvalService->reject($fileId, $this->userId, $message, $etag)) {
return new DataResponse([]);
}
return new DataResponse([], Http::STATUS_BAD_REQUEST);
} catch (OutdatedEtagException) {
return new DataResponse(['error' => $this->l10n->t('The file/folder you tried to reject has an outdated content, please reload and review it again')], Http::STATUS_BAD_REQUEST);
}
}
/**
* Request approval for a file
*
* @param int $fileId
* @param int $ruleId
* @param bool $createShares
* @return DataResponse
*/
#[NoAdminRequired]
public function request(int $fileId, int $ruleId, bool $createShares): DataResponse {
$result = $this->approvalService->request($fileId, $ruleId, $this->userId, $createShares);
if (isset($result['error'])) {
return new DataResponse($result, 400);
} else {
return new DataResponse($result);
}
}
}