|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Nextcloud - namespace OCA\Nextnote |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016, Sander Brand ([email protected]) |
|
6
|
|
|
* @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([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\OwnNote\Service; |
|
25
|
|
|
|
|
26
|
|
|
use OCA\OwnNote\Db\OwnNote; |
|
27
|
|
|
use OCA\OwnNote\Utility\Utils; |
|
28
|
|
|
use OCA\OwnNote\Db\OwnNoteMapper; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
class OwnNoteService { |
|
32
|
|
|
|
|
33
|
|
|
private $noteMapper; |
|
34
|
|
|
private $utils; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(OwnNoteMapper $noteMapper, Utils $utils) { |
|
37
|
|
|
$this->noteMapper = $noteMapper; |
|
38
|
|
|
$this->utils = $utils; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get vaults from a user. |
|
43
|
|
|
* |
|
44
|
|
|
* @param $userId |
|
45
|
|
|
* @param int $deleted |
|
46
|
|
|
* @return OwnNote[] |
|
47
|
|
|
*/ |
|
48
|
|
|
public function findNotesFromUser($userId, $deleted = 0) { |
|
49
|
|
|
// Get shares |
|
50
|
|
|
return $this->noteMapper->findNotesFromUser($userId, $deleted); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get a single vault |
|
55
|
|
|
* |
|
56
|
|
|
* @param $note_id |
|
57
|
|
|
* @param $user_id |
|
58
|
|
|
* @return OwnNote |
|
59
|
|
|
* @internal param $vault_id |
|
60
|
|
|
*/ |
|
61
|
|
|
public function find($note_id, $user_id = null) { |
|
62
|
|
|
$note = $this->noteMapper->find($note_id, $user_id); |
|
63
|
|
|
return $note; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Creates a note |
|
68
|
|
|
* |
|
69
|
|
|
* @param array|OwnNote $note |
|
70
|
|
|
* @param $userId |
|
71
|
|
|
* @return OwnNote |
|
72
|
|
|
* @throws \Exception |
|
73
|
|
|
*/ |
|
74
|
|
View Code Duplication |
public function create($note, $userId) { |
|
75
|
|
|
if (is_array($note)) { |
|
76
|
|
|
$entity = new OwnNote(); |
|
77
|
|
|
$entity->setName($note['title']); |
|
78
|
|
|
$entity->setUid($userId); |
|
79
|
|
|
$entity->setGrouping($note['group']); |
|
80
|
|
|
$entity->setNote($note['note'] ? $note['note'] : ''); |
|
81
|
|
|
$entity->setMtime(time()); |
|
82
|
|
|
$note = $entity; |
|
83
|
|
|
} |
|
84
|
|
|
if (!$note instanceof OwnNote) { |
|
85
|
|
|
throw new \Exception("Expected OwnNote object!"); |
|
86
|
|
|
} |
|
87
|
|
|
return $this->noteMapper->create($note); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Update vault |
|
92
|
|
|
* |
|
93
|
|
|
* @param $note array|OwnNote |
|
94
|
|
|
* @return OwnNote|bool |
|
95
|
|
|
* @throws \Exception |
|
96
|
|
|
* @internal param $userId |
|
97
|
|
|
* @internal param $vault |
|
98
|
|
|
*/ |
|
99
|
|
View Code Duplication |
public function update($note) { |
|
100
|
|
|
|
|
101
|
|
|
if (is_array($note)) { |
|
102
|
|
|
$entity = $this->find($note['id']); |
|
103
|
|
|
$entity->setName($note['name']); |
|
104
|
|
|
$entity->setGrouping($note['group']); |
|
105
|
|
|
$entity->setNote($note['note']); |
|
106
|
|
|
$entity->setMtime(time()); |
|
107
|
|
|
$note = $entity; |
|
108
|
|
|
} |
|
109
|
|
|
if (!$note instanceof OwnNote) { |
|
110
|
|
|
throw new \Exception("Expected OwnNote object!"); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// @TODO check if we can enable this without issues |
|
114
|
|
|
// if (!$this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE, $note->getId())) { |
|
115
|
|
|
// return false; |
|
116
|
|
|
// } |
|
117
|
|
|
|
|
118
|
|
|
return $this->noteMapper->updateNote($note); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function renameNote($FOLDER, $id, $in_newname, $in_newgroup, $uid = null) { |
|
|
|
|
|
|
122
|
|
|
$newname = str_replace("\\", "-", str_replace("/", "-", $in_newname)); |
|
123
|
|
|
$newgroup = str_replace("\\", "-", str_replace("/", "-", $in_newgroup)); |
|
124
|
|
|
|
|
125
|
|
|
$note = $this->find($id); |
|
126
|
|
|
$note->setName($newname); |
|
127
|
|
|
$note->setGrouping($newgroup); |
|
128
|
|
|
$this->update($note); |
|
129
|
|
|
|
|
130
|
|
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Delete a vault from user |
|
135
|
|
|
* |
|
136
|
|
|
* @param $note_id |
|
137
|
|
|
* @param string $user_id |
|
138
|
|
|
* @return bool |
|
139
|
|
|
* @internal param string $vault_guid |
|
140
|
|
|
*/ |
|
141
|
|
|
public function delete($note_id, $user_id = null) { |
|
142
|
|
|
if (!$this->checkPermissions(\OCP\Constants::PERMISSION_DELETE, $note_id)) { |
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$note = $this->noteMapper->find($note_id, $user_id); |
|
147
|
|
|
if ($note instanceof OwnNote) { |
|
148
|
|
|
$this->noteMapper->deleteNote($note); |
|
149
|
|
|
return true; |
|
150
|
|
|
} else { |
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param $FOLDER |
|
158
|
|
|
* @param boolean $showdel |
|
159
|
|
|
* @return array |
|
160
|
|
|
* @throws \Exception |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getListing($FOLDER, $showdel) { |
|
163
|
|
|
throw new \Exception('Calling a deprecated method! (Folder'. $FOLDER. '. Showdel: '. $showdel .')'); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
private function checkPermissions($permission, $nid) { |
|
167
|
|
|
// gather information |
|
168
|
|
|
$uid = \OC::$server->getUserSession()->getUser()->getUID(); |
|
169
|
|
|
$note = $this->find($nid); |
|
170
|
|
|
// owner is allowed to change everything |
|
171
|
|
|
if ($uid === $note->getUid()) { |
|
172
|
|
|
return true; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// check share permissions |
|
176
|
|
|
$shared_note = \OCP\Share::getItemSharedWith('ownnote', $nid, 'populated_shares')[0]; |
|
177
|
|
|
return $shared_note['permissions'] & $permission; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.