Completed
Push — master ( 7fe423...ae12d5 )
by Sander
12s queued 10s
created

NoteApiController::index()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.439
cc 5
eloc 15
nc 12
nop 2
1
<?php
2
/**
3
 * Nextcloud - NextNote
4
 *
5
 *
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\Db\Notebook;
27
use OCA\NextNote\Db\Note;
28
use OCA\NextNote\Fixtures\ShareFix;
29
use OCA\NextNote\Service\NotebookService;
30
use OCA\NextNote\Service\NoteService;
31
use OCA\NextNote\ShareBackend\NextNoteShareBackend;
32
use OCA\NextNote\Utility\NotFoundJSONResponse;
33
use OCA\NextNote\Utility\UnauthorizedJSONResponse;
34
use OCA\NextNote\Utility\Utils;
35
use \OCP\AppFramework\ApiController;
36
use OCP\AppFramework\Http\JSONResponse;
37
use OCP\Constants;
38
use OCP\IConfig;
39
use OCP\ILogger;
40
use \OCP\IRequest;
41
use OCP\IUserManager;
42
use OCP\Share;
43
44
45
class NoteApiController extends ApiController {
46
47
	private $config;
48
	private $noteService;
49
	private $shareBackend;
50
	private $userManager;
51
	private $shareManager;
52
	private $notebookService;
53
54
	public function __construct($appName, IRequest $request,
55
								ILogger $logger, IConfig $config, NoteService $noteService, NotebookService $groupService,
56
								NextNoteShareBackend $shareBackend, IUserManager $userManager, Share\IManager $shareManager) {
57
		parent::__construct($appName, $request);
58
		$this->config = $config;
59
		$this->noteService = $noteService;
60
		$this->notebookService = $groupService;
61
		$this->shareBackend = $shareBackend;
62
		$this->userManager = $userManager;
63
		$this->shareManager = $shareManager;
64
	}
65
66
	/**
67
	 * @NoAdminRequired
68
	 * @NoCSRFRequired
69
	 * @TODO Add etag / lastmodified
70
	 * @param int|bool $deleted
71
	 * @param string|bool $notebook_id
72
	 * @return JSONResponse
73
	 */
74
	public function index($deleted = false, $notebook_id = false) {
75
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
76
77
		if(!empty($notebook_id)){
78
			$notebook_id = $this->notebookService->find($notebook_id)->getId();
0 ignored issues
show
Documentation introduced by
$notebook_id is of type string|boolean, but the function expects a null|integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
		}
80
		$result = $this->noteService->findNotesFromUser($uid, $deleted, $notebook_id);
81
		foreach ($result as &$note) {
82
			if (is_array($note)) {
83
				$note = $this->noteService->find($note['id']);
84
			}
85
			$note = $note->jsonSerialize();
86
			$note = $this->formatApiResponse($note);
87
88
		}
89
90
		$results = $result;
91
		if($results instanceof Note){
92
			$results = [];
93
			/**
94
			 * @var $result Note
95
			 */
96
			$results[$result->getId()] = $result;
97
		}
98
		return new JSONResponse($results);
99
	}
100
101
	/**
102
	 * @NoAdminRequired
103
	 * @NoCSRFRequired
104
	 * @TODO Add etag / lastmodified
105
	 */
106
	public function get($id) {
107
		$result = $this->noteService->find($id);
108
		if (!$result) {
109
			return new NotFoundJSONResponse();
110
		}
111
		//@todo Check access
112
		$result = $result->jsonSerialize();
113
		return new JSONResponse($this->formatApiResponse($result));
114
	}
115
116
117
	/**
118
	 * @NoAdminRequired
119
	 * @NoCSRFRequired
120
	 */
121
	public function create($title, $notebook_id, $content) {
122
		if ($title == "" || !$title) {
123
			return new JSONResponse(['error' => 'title is missing']);
124
		}
125
126
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
127
		$note = new Note();
128
		$note->setName($title);
129
		$note->setUid($uid);
130
		$note->setGuid(Utils::GUID());
131
		$note->setNote($content);
132
		$note->setMtime(time());
133
		$note->setDeleted(0);
134
135 View Code Duplication
		if(!empty($notebook_id)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
			$notebook = $this->notebookService->find($notebook_id);
137
			if($notebook instanceof Notebook) {
138
				$note->setNotebook($notebook->getId());
139
			} else {
140
				return new JSONResponse(['error' => 'Notebook not found']);
141
			}
142
		}
143
144
		$result = $this->noteService->create($note)->jsonSerialize();
145
		\OC_Hook::emit('OCA\NextNote', 'post_create_note', ['note' => $note]);
146
		return new JSONResponse($this->formatApiResponse($result));
147
	}
148
149
	/**
150
	 * @NoAdminRequired
151
	 * @NoCSRFRequired
152
	 */
153
	public function update($id, $title, $content, $deleted, $notebook_id) {
154
		if ($title == "" || !$title) {
155
			return new JSONResponse(['error' => 'title is missing']);
156
		}
157
158
		$note = $this->noteService->find($id);
159
		if (!$note) {
160
			return new NotFoundJSONResponse();
161
		}
162
163
		if(!$note->getGuid()){
164
			$note->setGuid(Utils::GUID());
165
		}
166
167
		if (!$this->shareBackend->checkPermissions(Constants::PERMISSION_UPDATE, $note)) {
168
			return new UnauthorizedJSONResponse();
169
		}
170 View Code Duplication
		if(!empty($notebook_id)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
			$notebook = $this->notebookService->find($notebook_id);
172
			if($notebook instanceof Notebook) {
173
				$note->setNotebook($notebook->getId());
174
			} else {
175
				return new JSONResponse(['error' => 'Notebook not found']);
176
			}
177
		}
178
		$note->setName($title);
179
		$note->setNote($content);
180
		$note->setDeleted($deleted);
181
182
		$results = $this->noteService->update($note)->jsonSerialize();
183
		\OC_Hook::emit('OCA\NextNote', 'post_update_note', ['note' => $note]);
184
		return new JSONResponse($this->formatApiResponse($results));
185
	}
186
187
	/**
188
	 * @NoAdminRequired
189
	 * @NoCSRFRequired
190
	 */
191
	public function delete($id) {
192
		$entity = $this->noteService->find($id);
193
		if (!$entity) {
194
			return new NotFoundJSONResponse();
195
		}
196
197
		if (!$this->shareBackend->checkPermissions(Constants::PERMISSION_DELETE, $entity)) {
198
			return new UnauthorizedJSONResponse();
199
		}
200
201
		$this->noteService->delete($id);
202
		$result = (object)['success' => true];
203
		\OC_Hook::emit('OCA\NextNote', 'post_delete_note', ['note_id' => $id]);
204
		return new JSONResponse($result);
205
	}
206
207
	/**
208
	 * @param $note array
209
	 * @return array
210
	 */
211
	private function formatApiResponse($note) {
212
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
213
		$acl = [
214
			'permissions' => Constants::PERMISSION_ALL
215
		];
216
		if ($uid !== $note['uid']) {
217
			$aclRoles = ShareFix::getItemSharedWith('nextnote', $note['id'], 'populated_shares');
218
			$acl['permissions'] = $aclRoles['permissions'];
219
		}
220
		$note['owner'] = Utils::getUserInfo($note['uid']);
221
		$note['permissions'] = $acl['permissions'];
222
223
		$shared_with = ShareFix::getUsersItemShared('nextnote', $note['id'], $note['uid']);
224
		foreach ($shared_with as &$u) {
225
			$info = Utils::getUserInfo($u);
226
			if($info) {
227
				$u = $info;
228
			}
229
		}
230
231
		$note['shared_with'] = ($note['uid'] == $uid) ? $shared_with : [$uid];
232
		unset($note['uid']);
233
		return $note;
234
	}
235
}
236