Failed Conditions
Pull Request — master (#55)
by Sander
03:54
created

lib/Service/NextNoteService.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\ShareBackend\NextNoteShareBackend;
28
use OCA\NextNote\Utility\Utils;
29
use OCA\NextNote\Db\NextNoteMapper;
30
31
32
33
class NextNoteService {
34
35
	private $noteMapper;
36
	private $utils;
37
38
	public function __construct(NextNoteMapper $noteMapper, Utils $utils, NextNoteShareBackend $shareBackend) {
39
		$this->noteMapper = $noteMapper;
40
		$this->utils = $utils;
41
		$this->sharing = $shareBackend;
0 ignored issues
show
The property sharing does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
	}
43
44
	/**
45
	 * Get vaults from a user.
46
	 *
47
	 * @param $userId
48
	 * @param int|bool $deleted
49
	 * @param string|bool $grouping
50
	 * @return NextNote[]
51
	 */
52
	public function findNotesFromUser($userId, $deleted = false, $grouping = false) {
53
		// Get shares
54
55
		$dbNotes = $this->noteMapper->findNotesFromUser($userId, $deleted, $grouping);
56
		$sharedNotes = $this->sharing->getSharedNotes();
57
		$notes = array_merge($dbNotes, $sharedNotes);
58
		return $notes;
59
	}
60
61
	/**
62
	 * Get a single vault
63
	 *
64
	 * @param $note_id
65
	 * @param $user_id
66
	 * @param bool|int $deleted
67
	 * @return NextNote
68
	 * @internal param $vault_id
69
	 */
70
	public function find($note_id, $user_id = null, $deleted = false) {
71
		$note = $this->noteMapper->find($note_id, $user_id, $deleted);
72
		return $note;
73
	}
74
75
	/**
76
	 * Creates a note
77
	 *
78
	 * @param array|NextNote $note
79
	 * @param $userId
80
	 * @return NextNote
81
	 * @throws \Exception
82
	 */
83 View Code Duplication
	public function create($note, $userId) {
84
		if (is_array($note)) {
85
			$entity = new NextNote();
86
			$entity->setName($note['title']);
87
			$entity->setUid($userId);
88
			$entity->setGrouping($note['grouping']);
89
			$entity->setNote($note['note'] ? $note['note'] : '');
90
			$entity->setMtime(time());
91
			$note = $entity;
92
		}
93
		if (!$note instanceof NextNote) {
94
			throw new \Exception("Expected NextNote object!");
95
		}
96
		return $this->noteMapper->create($note);
97
	}
98
99
	/**
100
	 * Update vault
101
	 *
102
	 * @param $note array|NextNote
103
	 * @return NextNote|bool
104
	 * @throws \Exception
105
	 * @internal param $userId
106
	 * @internal param $vault
107
	 */
108 View Code Duplication
	public function update($note) {
109
110
		if (is_array($note)) {
111
			$entity = $this->find($note['id']);
112
			$entity->setName($note['title']);
113
			$entity->setGrouping($note['grouping']);
114
			$entity->setNote($note['note']);
115
			$entity->setDeleted($note['deleted']);
116
			$entity->setMtime(time());
117
			$note = $entity;
118
		}
119
		if (!$note instanceof NextNote) {
120
			throw new \Exception("Expected NextNote 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) {
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 NextNote) {
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('nextnote', $nid, 'populated_shares')[0];
187
		return $shared_note['permissions'] & $permission;
188
	}
189
}
190