-
Notifications
You must be signed in to change notification settings - Fork 17
/
OpenProjectAPIController.php
224 lines (210 loc) · 6.11 KB
/
OpenProjectAPIController.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
<?php
/**
* Nextcloud - openproject
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Julien Veyssier <eneiluj@posteo.net>
* @copyright Julien Veyssier 2020
*/
namespace OCA\OpenProject\Controller;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\OpenProject\Service\OpenProjectAPIService;
use OCA\OpenProject\AppInfo\Application;
class OpenProjectAPIController extends Controller {
/**
* @var OpenProjectAPIService
*/
private $openprojectAPIService;
/**
* @var string|null
*/
private $userId;
/**
* @var string
*/
private $accessToken;
/**
* @var string
*/
private $refreshToken;
/**
* @var string
*/
private $clientID;
/**
* @var string
*/
private $clientSecret;
/**
* @var string
*/
private $openprojectUrl;
public function __construct(string $appName,
IRequest $request,
IConfig $config,
OpenProjectAPIService $openprojectAPIService,
?string $userId) {
parent::__construct($appName, $request);
$this->openprojectAPIService = $openprojectAPIService;
$this->userId = $userId;
$this->accessToken = $config->getUserValue($userId, Application::APP_ID, 'token');
$this->refreshToken = $config->getUserValue($userId, Application::APP_ID, 'refresh_token');
$this->clientID = $config->getAppValue(Application::APP_ID, 'client_id');
$this->clientSecret = $config->getAppValue(Application::APP_ID, 'client_secret');
$this->openprojectUrl = $config->getAppValue(Application::APP_ID, 'oauth_instance_url');
}
/**
* get openproject instance URL
* @NoAdminRequired
*
* @return DataResponse
*/
public function getOpenProjectUrl(): DataResponse {
return new DataResponse($this->openprojectUrl);
}
/**
* get openproject user avatar
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $userId
* @param string $userName
* @return DataDisplayResponse|DataDownloadResponse
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\Lock\LockedException
*/
public function getOpenProjectAvatar(string $userId = '', string $userName = '') {
$result = $this->openprojectAPIService->getOpenProjectAvatar(
$this->openprojectUrl, $this->accessToken, $this->refreshToken,
$this->clientID, $this->clientSecret, $userId, $userName
);
$response = new DataDownloadResponse(
$result['avatar'], 'avatar', $result['type'] ?? ''
);
$response->cacheFor(60 * 60 * 24);
return $response;
}
/**
* get notifications list
* @NoAdminRequired
*
* @param ?string $since
* @return DataResponse
*/
public function getNotifications(?string $since = null): DataResponse {
if ($this->accessToken === '' || !OpenProjectAPIService::validateOpenProjectURL($this->openprojectUrl)) {
return new DataResponse('', Http::STATUS_BAD_REQUEST);
}
$result = $this->openprojectAPIService->getNotifications(
$this->openprojectUrl, $this->accessToken, $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $since, 7
);
if (!isset($result['error'])) {
$response = new DataResponse($result);
} else {
$response = new DataResponse($result, Http::STATUS_UNAUTHORIZED);
}
return $response;
}
/**
* get searched work packages
*
* @NoAdminRequired
*
* @param ?string $searchQuery
* @param ?int $fileId
*
* @return DataResponse
*/
public function getSearchedWorkPackages(?string $searchQuery = null, ?int $fileId = null): DataResponse {
if ($this->accessToken === '' || !OpenProjectAPIService::validateOpenProjectURL($this->openprojectUrl)) {
return new DataResponse(
'invalid open project configuration', Http::STATUS_UNAUTHORIZED
);
}
$result = $this->openprojectAPIService->searchWorkPackage(
$this->openprojectUrl,
$this->accessToken,
$this->refreshToken,
$this->clientID,
$this->clientSecret,
$this->userId,
$searchQuery,
$fileId
);
if (!isset($result['error'])) {
$response = new DataResponse($result);
} else {
if (isset($result['statusCode'])) {
$statusCode = $result['statusCode'];
} else {
$statusCode = Http::STATUS_BAD_REQUEST;
}
$response = new DataResponse($result, $statusCode);
}
return $response;
}
/**
* @NoAdminRequired
* @param int $workpackageId
* @param int $fileId
* @return DataResponse
*/
public function linkWorkPackageToFile(int $workpackageId = 0, int $fileId = 0): DataResponse {
return new DataResponse("Fake result, to make UI happy");
}
/**
* get status of work packages
*
* @NoAdminRequired
*
* @param string $id
*
* @return DataResponse
*/
public function getOpenProjectWorkPackageStatus(string $id): DataResponse {
if ($this->accessToken === '' || !OpenProjectAPIService::validateOpenProjectURL($this->openprojectUrl)) {
return new DataResponse('', Http::STATUS_BAD_REQUEST);
}
$result = $this->openprojectAPIService->getOpenProjectWorkPackageStatus(
$this->openprojectUrl, $this->accessToken, $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $id
);
if (!isset($result['error'])) {
$response = new DataResponse($result);
} else {
$response = new DataResponse($result, Http::STATUS_UNAUTHORIZED);
}
return $response;
}
/**
* get type work packages
*
* @NoAdminRequired
*
* @param string $id
*
* @return DataResponse
*/
public function getOpenProjectWorkPackageType(string $id): DataResponse {
if ($this->accessToken === '' || !OpenProjectAPIService::validateOpenProjectURL($this->openprojectUrl)) {
return new DataResponse('', Http::STATUS_BAD_REQUEST);
}
$result = $this->openprojectAPIService->getOpenProjectWorkPackageType(
$this->openprojectUrl, $this->accessToken, $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $id
);
if (!isset($result['error'])) {
$response = new DataResponse($result);
} else {
$response = new DataResponse($result, Http::STATUS_UNAUTHORIZED);
}
return $response;
}
}