Failed Conditions
Pull Request — master (#6)
by Sander
01:54
created

OwnNoteService::update()   D

Complexity

Conditions 9
Paths 34

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 16
c 0
b 0
f 0
nc 34
nop 1
dl 0
loc 31
rs 4.909
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
	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
	public function update($note) {
100
101
		if (is_array($note)) {
102
			$entity = $this->find($note['id']);
103
			if(!$entity){
104
				$entity = new OwnNote();
105
			}
106
			if (isset($note['name'])) {
107
				$entity->setName($note['name']);
108
			}
109
			if(isset($note['group']) || $note['group']) {
110
				$entity->setGrouping($note['group']);
111
			}
112
113
			if (isset($note['note']) || $note['note'] == '') {
114
				$entity->setNote($note['note']);
115
			}
116
			$entity->setMtime(time());
117
			$note = $entity;
118
		}
119
		if (!$note instanceof OwnNote) {
120
			throw new \Exception("Expected OwnNote object!");
121
		}
122
123
		// @TODO check if we can enable this without issues
124
//		if (!$this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE, $note->getId())) {
125
//			return false;
126
//		}
127
128
		return $this->noteMapper->updateNote($note);
129
	}
130
131
	public function renameNote($FOLDER, $id, $in_newname, $in_newgroup, $uid = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $FOLDER is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $uid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
		$newname = str_replace("\\", "-", str_replace("/", "-", $in_newname));
133
		$newgroup = str_replace("\\", "-", str_replace("/", "-", $in_newgroup));
134
135
		$note = $this->find($id);
136
		$note->setName($newname);
137
		$note->setGrouping($newgroup);
138
		$this->update($note);
139
140
		return true;
141
	}
142
143
	/**
144
	 * Delete a vault from user
145
	 *
146
	 * @param $note_id
147
	 * @param string $user_id
148
	 * @return bool
149
	 * @internal param string $vault_guid
150
	 */
151
	public function delete($note_id, $user_id = null) {
152
		if (!$this->checkPermissions(\OCP\Constants::PERMISSION_DELETE, $note_id)) {
153
			return false;
154
		}
155
156
		$note = $this->noteMapper->find($note_id, $user_id);
157
		if ($note instanceof OwnNote) {
158
			$this->noteMapper->deleteNote($note);
159
			return true;
160
		} else {
161
			return false;
162
		}
163
	}
164
165
166
	/**
167
	 * @param $FOLDER
168
	 * @param boolean $showdel
169
	 * @return array
170
	 * @throws \Exception
171
	 */
172
	public function getListing($FOLDER, $showdel) {
173
		throw new \Exception('Calling a deprecated method! (Folder'. $FOLDER. '. Showdel: '. $showdel .')');
174
	}
175
176
	private function checkPermissions($permission, $nid) {
177
		// gather information
178
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
179
		$note = $this->find($nid);
180
		// owner is allowed to change everything
181
		if ($uid === $note->getUid()) {
182
			return true;
183
		}
184
185
		// check share permissions
186
		$shared_note = \OCP\Share::getItemSharedWith('ownnote', $nid, 'populated_shares')[0];
187
		return $shared_note['permissions'] & $permission;
188
	}
189
}
190