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\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(); |
79
|
|
|
} |
80
|
|
|
$results = $this->noteService->findNotesFromUser($uid, $deleted, $notebook_id); |
81
|
|
|
foreach ($results 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
|
|
|
return new JSONResponse($results); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @NoAdminRequired |
94
|
|
|
* @NoCSRFRequired |
95
|
|
|
* @TODO Add etag / lastmodified |
96
|
|
|
*/ |
97
|
|
|
public function get($id) { |
98
|
|
|
$result = $this->noteService->find($id); |
99
|
|
|
if (!$result) { |
100
|
|
|
return new NotFoundJSONResponse(); |
101
|
|
|
} |
102
|
|
|
//@todo Check access |
103
|
|
|
$result = $result->jsonSerialize(); |
104
|
|
|
return new JSONResponse($this->formatApiResponse($result)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @NoAdminRequired |
110
|
|
|
* @NoCSRFRequired |
111
|
|
|
*/ |
112
|
|
|
public function create($title, $notebook_id, $content) { |
113
|
|
|
if ($title == "" || !$title) { |
114
|
|
|
return new JSONResponse(['error' => 'title is missing']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$uid = \OC::$server->getUserSession()->getUser()->getUID(); |
118
|
|
|
$note = new Note(); |
119
|
|
|
$note->setName($title); |
120
|
|
|
$note->setUid($uid); |
121
|
|
|
$note->setGuid(Utils::GUID()); |
122
|
|
|
$note->setNote($content); |
123
|
|
|
$note->setMtime(time()); |
124
|
|
|
$note->setDeleted(0); |
125
|
|
|
|
126
|
|
View Code Duplication |
if(!empty($notebook_id)){ |
|
|
|
|
127
|
|
|
$notebook = $this->notebookService->find($notebook_id); |
128
|
|
|
if($notebook instanceof Notebook) { |
129
|
|
|
$note->setNotebook($notebook->getId()); |
130
|
|
|
} else { |
131
|
|
|
return new JSONResponse(['error' => 'Notebook not found']); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$result = $this->noteService->create($note)->jsonSerialize(); |
136
|
|
|
\OC_Hook::emit('OCA\NextNote', 'post_create_note', ['note' => $note]); |
137
|
|
|
return new JSONResponse($this->formatApiResponse($result)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @NoAdminRequired |
142
|
|
|
* @NoCSRFRequired |
143
|
|
|
*/ |
144
|
|
|
public function update($id, $title, $content, $deleted, $notebook_id) { |
145
|
|
|
if ($title == "" || !$title) { |
146
|
|
|
return new JSONResponse(['error' => 'title is missing']); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$note = $this->noteService->find($id); |
150
|
|
|
if (!$note) { |
151
|
|
|
return new NotFoundJSONResponse(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if(!$note->getGuid()){ |
155
|
|
|
$note->setGuid(Utils::GUID()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!$this->shareBackend->checkPermissions(Constants::PERMISSION_UPDATE, $note)) { |
159
|
|
|
return new UnauthorizedJSONResponse(); |
160
|
|
|
} |
161
|
|
View Code Duplication |
if(!empty($notebook_id)){ |
|
|
|
|
162
|
|
|
$notebook = $this->notebookService->find($notebook_id); |
163
|
|
|
if($notebook instanceof Notebook) { |
164
|
|
|
$note->setNotebook($notebook->getId()); |
165
|
|
|
} else { |
166
|
|
|
return new JSONResponse(['error' => 'Notebook not found']); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
$note->setName($title); |
170
|
|
|
$note->setNote($content); |
171
|
|
|
$note->setDeleted($deleted); |
172
|
|
|
|
173
|
|
|
$results = $this->noteService->update($note)->jsonSerialize(); |
174
|
|
|
\OC_Hook::emit('OCA\NextNote', 'post_update_note', ['note' => $note]); |
175
|
|
|
return new JSONResponse($this->formatApiResponse($results)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @NoAdminRequired |
180
|
|
|
* @NoCSRFRequired |
181
|
|
|
*/ |
182
|
|
View Code Duplication |
public function delete($id) { |
183
|
|
|
$entity = $this->noteService->find($id); |
184
|
|
|
if (!$entity) { |
185
|
|
|
return new NotFoundJSONResponse(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (!$this->shareBackend->checkPermissions(Constants::PERMISSION_DELETE, $entity)) { |
189
|
|
|
return new UnauthorizedJSONResponse(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$this->noteService->delete($id); |
193
|
|
|
$result = (object)['success' => true]; |
194
|
|
|
\OC_Hook::emit('OCA\NextNote', 'post_delete_note', ['note_id' => $id]); |
195
|
|
|
return new JSONResponse($result); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param $note array |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
private function formatApiResponse($note) { |
203
|
|
|
$uid = \OC::$server->getUserSession()->getUser()->getUID(); |
204
|
|
|
$acl = [ |
205
|
|
|
'permissions' => Constants::PERMISSION_ALL |
206
|
|
|
]; |
207
|
|
|
if ($uid !== $note['uid']) { |
208
|
|
|
$aclRoles = ShareFix::getItemSharedWith('nextnote', $note['id'], 'populated_shares'); |
209
|
|
|
$acl['permissions'] = $aclRoles['permissions']; |
210
|
|
|
} |
211
|
|
|
$note['owner'] = Utils::getUserInfo($note['uid']); |
212
|
|
|
$note['permissions'] = $acl['permissions']; |
213
|
|
|
|
214
|
|
|
$shared_with = ShareFix::getUsersItemShared('nextnote', $note['id'], $note['uid']); |
215
|
|
|
foreach ($shared_with as &$u) { |
216
|
|
|
$info = Utils::getUserInfo($u); |
217
|
|
|
if($info) { |
218
|
|
|
$u = $info; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$note['shared_with'] = ($note['uid'] == $uid) ? $shared_with : [$uid]; |
223
|
|
|
unset($note['uid']); |
224
|
|
|
return $note; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
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.