Completed
Pull Request — master (#21)
by Sander
01:21
created

NextNoteService::findNotesFromUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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