Completed
Push — master ( 011d1d...4bc32b )
by Sander
01:28
created

NextNoteApiController::formatApiResponse()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 17
nc 12
nop 1
1
<?php
2
/**
3
 * Nextcloud - NextNote
4
 *
5
 * @copyright Copyright (c) 2015, Ben Curtis <[email protected]>
6
 * @copyright Copyright (c) 2017, Sander Brand ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\NextNote\Controller;
25
26
use OCA\NextNote\Fixtures\ShareFix;
27
use OCA\NextNote\Service\NextNoteService;
28
use OCA\NextNote\ShareBackend\NextNoteShareBackend;
29
use OCA\NextNote\Utility\NotFoundJSONResponse;
30
use OCA\NextNote\Utility\UnauthorizedJSONResponse;
31
use OCA\NextNote\Utility\Utils;
32
use \OCP\AppFramework\ApiController;
33
use OCP\AppFramework\Http\JSONResponse;
34
use OCP\Constants;
35
use OCP\IConfig;
36
use OCP\ILogger;
37
use \OCP\IRequest;
38
use OCP\IUserManager;
39
use OCP\Share;
40
41
42
class NextNoteApiController extends ApiController {
43
44
	private $config;
45
	private $noteService;
46
	private $shareBackend;
47
	private $userManager;
48
	private $shareManager;
49
50
	public function __construct($appName, IRequest $request,
51
								ILogger $logger, IConfig $config, NextNoteService $noteService, NextNoteShareBackend $shareBackend, IUserManager $userManager, Share\IManager $shareManager) {
52
		parent::__construct($appName, $request);
53
		$this->config = $config;
54
		$this->noteService = $noteService;
55
		$this->shareBackend = $shareBackend;
56
		$this->userManager = $userManager;
57
		$this->shareManager = $shareManager;
58
	}
59
60
	/**
61
	 * @NoAdminRequired
62
	 * @NoCSRFRequired
63
	 * @TODO Add etag / lastmodified
64
	 * @param int|bool $deleted
65
	 * @param string|bool $group
66
	 * @return JSONResponse
67
	 */
68
	public function index($deleted = false, $group = false) {
69
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
70
		$results = $this->noteService->findNotesFromUser($uid, $deleted, $group);
71
		foreach ($results as &$note) {
72
			if (is_array($note)) {
73
				$note = $this->noteService->find($note['id']);
74
			}
75
			$note = $note->jsonSerialize();
76
			$note = $this->formatApiResponse($note);
77
78
		}
79
		return new JSONResponse($results);
80
	}
81
82
	/**
83
	 * @NoAdminRequired
84
	 * @NoCSRFRequired
85
	 * @TODO Add etag / lastmodified
86
	 */
87
	public function get($id) {
88
		$result = $this->noteService->find($id);
89
		if (!$result) {
90
			return new NotFoundJSONResponse();
91
		}
92
		//@todo Check access
93
		$result = $result->jsonSerialize();
94
		return new JSONResponse($this->formatApiResponse($result));
95
	}
96
97
98
	/**
99
	 * @NoAdminRequired
100
	 * @NoCSRFRequired
101
	 */
102
	public function create($title, $grouping, $content) {
103
		if ($title == "" || !$title) {
104
			return new JSONResponse(['error' => 'title is missing']);
105
		}
106
		$note = [
107
			'title' => $title,
108
			'name' => $title,
109
			'grouping' => $grouping,
110
			'note' => $content
111
		];
112
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
113
		$result = $this->noteService->create($note, $uid)->jsonSerialize();
114
		\OC_Hook::emit('OCA\NextNote', 'post_create_note', ['note' => $note]);
115
		return new JSONResponse($this->formatApiResponse($result));
116
	}
117
118
	/**
119
	 * @NoAdminRequired
120
	 * @NoCSRFRequired
121
	 */
122
	public function update($id, $title, $grouping, $content, $deleted) {
123
		if ($title == "" || !$title) {
124
			return new JSONResponse(['error' => 'title is missing']);
125
		}
126
127
128
		$note = [
129
			'id' => $id,
130
			'title' => $title,
131
			'name' => $title,
132
			'grouping' => $grouping,
133
			'note' => $content,
134
			'deleted' => $deleted
135
		];
136
		//@TODO for sharing add access check
137
		$entity = $this->noteService->find($id);
138
		if (!$entity) {
139
			return new NotFoundJSONResponse();
140
		}
141
142
143
		if (!$this->shareBackend->checkPermissions(Constants::PERMISSION_UPDATE, $entity)) {
144
			return new UnauthorizedJSONResponse();
145
		}
146
147
		$results = $this->noteService->update($note)->jsonSerialize();
148
		\OC_Hook::emit('OCA\NextNote', 'post_update_note', ['note' => $note]);
149
		return new JSONResponse($this->formatApiResponse($results));
150
	}
151
152
	/**
153
	 * @NoAdminRequired
154
	 * @NoCSRFRequired
155
	 */
156
	public function delete($id) {
157
		$entity = $this->noteService->find($id);
158
		if (!$entity) {
159
			return new NotFoundJSONResponse();
160
		}
161
162
		if (!$this->shareBackend->checkPermissions(Constants::PERMISSION_DELETE, $entity)) {
163
			return new UnauthorizedJSONResponse();
164
		}
165
166
		$this->noteService->delete($id);
167
		$result = (object)['success' => true];
168
		\OC_Hook::emit('OCA\NextNote', 'post_delete_note', ['note_id' => $id]);
169
		return new JSONResponse($result);
170
	}
171
172
	/**
173
	 * @param $note array
174
	 * @return array
175
	 */
176
	private function formatApiResponse($note) {
177
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
178
		$acl = [
179
			'permissions' => Constants::PERMISSION_ALL
180
		];
181
		if ($uid !== $note['uid']) {
182
			$aclRoles = ShareFix::getItemSharedWith('nextnote', $note['id'], 'populated_shares');
183
			$acl['permissions'] = $aclRoles['permissions'];
184
		}
185
		$note['owner'] = Utils::getUserInfo($note['uid']);
186
		$note['permissions'] = $acl['permissions'];
187
188
		$shared_with = ShareFix::getUsersItemShared('nextnote', $note['id'], $note['uid']);
189
		foreach ($shared_with as &$u) {
190
			$info = Utils::getUserInfo($u);
191
			if($info) {
192
				$u = $info;
193
			}
194
		}
195
196
		$note['shared_with'] = ($note['uid'] == $uid) ? $shared_with : [$uid];
197
		unset($note['uid']);
198
		return $note;
199
	}
200
}
201