Completed
Push — master ( 2a37c3...4dad52 )
by
unknown
11:17 queued 10s
created

UserStoragesService::deleteAllMountsForUser()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 4
nop 1
dl 0
loc 28
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Robin Appelman <[email protected]>
4
 * @author Robin McCorkell <[email protected]>
5
 * @author Stefan Weil <[email protected]>
6
 * @author Vincent Petry <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2018, ownCloud GmbH
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\Files\External\Service;
26
27
use OC\Files\Filesystem;
28
29
use OCP\Files\Config\IUserMountCache;
30
use OCP\IUser;
31
use OCP\IUserSession;
32
33
use OCP\Files\External\IStorageConfig;
34
use OCP\Files\External\NotFoundException;
35
use OCP\Files\External\IStoragesBackendService;
36
use OCP\Files\External\Service\IUserStoragesService;
37
38
/**
39
 * Service class to manage user external storages
40
 * (aka personal storages)
41
 */
42
class UserStoragesService extends StoragesService implements IUserStoragesService {
43
	use UserTrait;
44
45
	/**
46
	 * Create a user storages service
47
	 *
48
	 * @param IStoragesBackendService $backendService
49
	 * @param DBConfigService $dbConfig
50
	 * @param IUserSession $userSession user session
51
	 * @param IUserMountCache $userMountCache
52
	 */
53
	public function __construct(
54
		IStoragesBackendService $backendService,
55
		DBConfigService $dbConfig,
56
		IUserSession $userSession,
57
		IUserMountCache $userMountCache
58
	) {
59
		$this->userSession = $userSession;
60
		$this->userMountCache = $userMountCache;
61
		parent::__construct($backendService, $dbConfig, $userMountCache);
62
	}
63
64
	protected function readDBConfig() {
65
		return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID());
66
	}
67
68
	/**
69
	 * Triggers $signal for all applicable users of the given
70
	 * storage
71
	 *
72
	 * @param IStorageConfig $storage storage data
73
	 * @param string $signal signal to trigger
74
	 */
75
	protected function triggerHooks(IStorageConfig $storage, $signal) {
76
		$user = $this->getUser()->getUID();
77
78
		// trigger hook for the current user
79
		$this->triggerApplicableHooks(
80
			$signal,
81
			$storage->getMountPoint(),
82
			IStorageConfig::MOUNT_TYPE_USER,
83
			[$user]
84
		);
85
	}
86
87
	/**
88
	 * Triggers signal_create_mount or signal_delete_mount to
89
	 * accommodate for additions/deletions in applicableUsers
90
	 * and applicableGroups fields.
91
	 *
92
	 * @param IStorageConfig $oldStorage old storage data
93
	 * @param IStorageConfig $newStorage new storage data
94
	 */
95
	protected function triggerChangeHooks(IStorageConfig $oldStorage, IStorageConfig $newStorage) {
96
		// if mount point changed, it's like a deletion + creation
97 View Code Duplication
		if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
			$this->triggerHooks($oldStorage, Filesystem::signal_delete_mount);
99
			$this->triggerHooks($newStorage, Filesystem::signal_create_mount);
100
		}
101
	}
102
103
	protected function getType() {
104
		return DBConfigService::MOUNT_TYPE_PERSONAl;
105
	}
106
107
	/**
108
	 * Add new storage to the configuration
109
	 *
110
	 * @param IStorageConfig $newStorage storage attributes
111
	 *
112
	 * @return IStorageConfig storage config, with added id
113
	 */
114
	public function addStorage(IStorageConfig $newStorage) {
115
		$newStorage->setApplicableUsers([$this->getUser()->getUID()]);
116
		$config = parent::addStorage($newStorage);
117
		return $config;
118
	}
119
120
	/**
121
	 * Update storage to the configuration
122
	 *
123
	 * @param IStorageConfig $updatedStorage storage attributes
124
	 *
125
	 * @return IStorageConfig storage config
126
	 * @throws NotFoundException if the given storage does not exist in the config
127
	 */
128
	public function updateStorage(IStorageConfig $updatedStorage) {
129
		$updatedStorage->setApplicableUsers([$this->getUser()->getUID()]);
130
		return parent::updateStorage($updatedStorage);
131
	}
132
133
	/**
134
	 * Get the visibility type for this controller, used in validation
135
	 *
136
	 * @return string IStoragesBackendService::VISIBILITY_* constants
137
	 */
138
	public function getVisibilityType() {
139
		return IStoragesBackendService::VISIBILITY_PERSONAL;
140
	}
141
142
	protected function isApplicable(IStorageConfig $config) {
143
		return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === IStorageConfig::MOUNT_TYPE_PERSONAl;
144
	}
145
146
	/**
147
	 * Deletes the storages mounted to a user
148
	 * @param IUser $user
149
	 * @return bool
150
	 */
151
	public function deleteAllMountsForUser(IUser $user) {
152
		$getUserMounts = $this->userMountCache->getMountsForUser($user);
153
		$allMounts = $this->dbConfig->getAllMounts();
154
		$result = false;
155
		if (\count($getUserMounts) > 0) {
156
			foreach ($getUserMounts as $userMount) {
157
				$id = $userMount->getStorageId();
158
				$this->userMountCache->removeUserStorageMount($id, $user->getUID());
159
				$result = true;
160
			}
161
		}
162
		if (\count($allMounts)) {
163
			foreach ($allMounts as $userMount) {
164
				/**
165
				 * Remove any mounts which are applicable to only this user
166
				 * Specifically targeted to, mounts created by the user.
167
				 */
168
				if (\count($userMount['applicable']) === 1) {
169
					foreach ($userMount['applicable'] as $applicableUser) {
170
						if ($applicableUser['value'] === $user->getUID()) {
171
							$this->dbConfig->removeMount($applicableUser['mount_id']);
172
						}
173
					}
174
				}
175
			}
176
		}
177
		return $result;
178
	}
179
}
180