Passed
Push — master ( e17684...b6c034 )
by Blizzz
35:05 queued 17:14
created

SystemTagNode::getNumberOfFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 * @author Vincent Petry <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
namespace OCA\DAV\SystemTag;
26
27
use OCP\IUser;
28
use OCP\SystemTag\ISystemTag;
29
use OCP\SystemTag\ISystemTagManager;
30
use OCP\SystemTag\TagAlreadyExistsException;
31
32
use OCP\SystemTag\TagNotFoundException;
33
use Sabre\DAV\Exception\Conflict;
34
use Sabre\DAV\Exception\Forbidden;
35
use Sabre\DAV\Exception\MethodNotAllowed;
36
use Sabre\DAV\Exception\NotFound;
37
38
/**
39
 * DAV node representing a system tag, with the name being the tag id.
40
 */
41
class SystemTagNode implements \Sabre\DAV\INode {
42
43
	/**
44
	 * @var ISystemTag
45
	 */
46
	protected $tag;
47
48
	/**
49
	 * @var ISystemTagManager
50
	 */
51
	protected $tagManager;
52
53
	/**
54
	 * User
55
	 *
56
	 * @var IUser
57
	 */
58
	protected $user;
59
60
	/**
61
	 * Whether to allow permissions for admins
62
	 *
63
	 * @var bool
64
	 */
65
	protected $isAdmin;
66
67
	protected int $numberOfFiles = -1;
68
	protected int $referenceFileId = -1;
69
70
	/**
71
	 * Sets up the node, expects a full path name
72
	 *
73
	 * @param ISystemTag $tag system tag
74
	 * @param IUser $user user
75
	 * @param bool $isAdmin whether to allow operations for admins
76
	 * @param ISystemTagManager $tagManager tag manager
77
	 */
78
	public function __construct(ISystemTag $tag, IUser $user, $isAdmin, ISystemTagManager $tagManager) {
79
		$this->tag = $tag;
80
		$this->user = $user;
81
		$this->isAdmin = $isAdmin;
82
		$this->tagManager = $tagManager;
83
	}
84
85
	/**
86
	 *  Returns the id of the tag
87
	 *
88
	 * @return string
89
	 */
90
	public function getName() {
91
		return $this->tag->getId();
92
	}
93
94
	/**
95
	 * Returns the system tag represented by this node
96
	 *
97
	 * @return ISystemTag system tag
98
	 */
99
	public function getSystemTag() {
100
		return $this->tag;
101
	}
102
103
	/**
104
	 * Renames the node
105
	 *
106
	 * @param string $name The new name
107
	 *
108
	 * @throws MethodNotAllowed not allowed to rename node
109
	 *
110
	 * @return never
111
	 */
112
	public function setName($name) {
113
		throw new MethodNotAllowed();
114
	}
115
116
	/**
117
	 * Update tag
118
	 *
119
	 * @param string $name new tag name
120
	 * @param bool $userVisible user visible
121
	 * @param bool $userAssignable user assignable
122
	 *
123
	 * @throws NotFound whenever the given tag id does not exist
124
	 * @throws Forbidden whenever there is no permission to update said tag
125
	 * @throws Conflict whenever a tag already exists with the given attributes
126
	 */
127
	public function update($name, $userVisible, $userAssignable): void {
128
		try {
129
			if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) {
130
				throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist');
131
			}
132
			if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) {
133
				throw new Forbidden('No permission to update tag ' . $this->tag->getId());
134
			}
135
136
			// only admin is able to change permissions, regular users can only rename
137
			if (!$this->isAdmin) {
138
				// only renaming is allowed for regular users
139
				if ($userVisible !== $this->tag->isUserVisible()
140
					|| $userAssignable !== $this->tag->isUserAssignable()
141
				) {
142
					throw new Forbidden('No permission to update permissions for tag ' . $this->tag->getId());
143
				}
144
			}
145
146
			$this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable);
147
		} catch (TagNotFoundException $e) {
148
			throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist');
149
		} catch (TagAlreadyExistsException $e) {
150
			throw new Conflict(
151
				'Tag with the properties "' . $name . '", ' .
152
				$userVisible . ', ' . $userAssignable . ' already exists'
153
			);
154
		}
155
	}
156
157
	/**
158
	 * Returns null, not supported
159
	 *
160
	 * @return null
161
	 */
162
	public function getLastModified() {
163
		return null;
164
	}
165
166
	/**
167
	 * @return void
168
	 */
169
	public function delete() {
170
		try {
171
			if (!$this->isAdmin) {
172
				throw new Forbidden('No permission to delete tag ' . $this->tag->getId());
173
			}
174
175
			if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) {
176
				throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found');
177
			}
178
179
			$this->tagManager->deleteTags($this->tag->getId());
180
		} catch (TagNotFoundException $e) {
181
			// can happen if concurrent deletion occurred
182
			throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e);
183
		}
184
	}
185
186
	public function getNumberOfFiles(): int {
187
		return $this->numberOfFiles;
188
	}
189
190
	public function setNumberOfFiles(int $numberOfFiles): void {
191
		$this->numberOfFiles = $numberOfFiles;
192
	}
193
194
	public function getReferenceFileId(): int {
195
		return $this->referenceFileId;
196
	}
197
198
	public function setReferenceFileId(int $referenceFileId): void {
199
		$this->referenceFileId = $referenceFileId;
200
	}
201
}
202