Completed
Pull Request — master (#32655)
by Sujith
11:05
created

UserStoragesService::deleteAllMountsForUser()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 2
dl 0
loc 22
rs 8.9457
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\External\PersonalMount;
28
use OC\Files\Filesystem;
29
30
use OCP\Files\Config\IMountProviderCollection;
31
use OCP\Files\Config\IUserMountCache;
32
use OCP\IUser;
33
use OCP\IUserSession;
34
35
use OCP\Files\External\IStorageConfig;
36
use OCP\Files\External\NotFoundException;
37
use OCP\Files\External\IStoragesBackendService;
38
use OCP\Files\External\Service\IUserStoragesService;
39
40
/**
41
 * Service class to manage user external storages
42
 * (aka personal storages)
43
 */
44
class UserStoragesService extends StoragesService implements IUserStoragesService {
45
	use UserTrait;
46
47
	/**
48
	 * Create a user storages service
49
	 *
50
	 * @param IStoragesBackendService $backendService
51
	 * @param DBConfigService $dbConfig
52
	 * @param IUserSession $userSession user session
53
	 * @param IUserMountCache $userMountCache
54
	 */
55 View Code Duplication
	public function __construct(
56
		IStoragesBackendService $backendService,
57
		DBConfigService $dbConfig,
58
		IUserSession $userSession,
59
		IUserMountCache $userMountCache
60
	) {
61
		$this->userSession = $userSession;
62
		$this->userMountCache = $userMountCache;
63
		parent::__construct($backendService, $dbConfig, $userMountCache);
64
	}
65
66
	protected function readDBConfig() {
67
		return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID());
68
	}
69
70
	/**
71
	 * Triggers $signal for all applicable users of the given
72
	 * storage
73
	 *
74
	 * @param IStorageConfig $storage storage data
75
	 * @param string $signal signal to trigger
76
	 */
77
	protected function triggerHooks(IStorageConfig $storage, $signal) {
78
		$user = $this->getUser()->getUID();
79
80
		// trigger hook for the current user
81
		$this->triggerApplicableHooks(
82
			$signal,
83
			$storage->getMountPoint(),
84
			IStorageConfig::MOUNT_TYPE_USER,
85
			[$user]
86
		);
87
	}
88
89
	/**
90
	 * Triggers signal_create_mount or signal_delete_mount to
91
	 * accommodate for additions/deletions in applicableUsers
92
	 * and applicableGroups fields.
93
	 *
94
	 * @param IStorageConfig $oldStorage old storage data
95
	 * @param IStorageConfig $newStorage new storage data
96
	 */
97
	protected function triggerChangeHooks(IStorageConfig $oldStorage, IStorageConfig $newStorage) {
98
		// if mount point changed, it's like a deletion + creation
99 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...
100
			$this->triggerHooks($oldStorage, Filesystem::signal_delete_mount);
101
			$this->triggerHooks($newStorage, Filesystem::signal_create_mount);
102
		}
103
	}
104
105
	protected function getType() {
106
		return DBConfigService::MOUNT_TYPE_PERSONAl;
107
	}
108
109
	/**
110
	 * Add new storage to the configuration
111
	 *
112
	 * @param IStorageConfig $newStorage storage attributes
113
	 *
114
	 * @return IStorageConfig storage config, with added id
115
	 */
116
	public function addStorage(IStorageConfig $newStorage) {
117
		$newStorage->setApplicableUsers([$this->getUser()->getUID()]);
118
		$config = parent::addStorage($newStorage);
119
		return $config;
120
	}
121
122
	/**
123
	 * Update storage to the configuration
124
	 *
125
	 * @param IStorageConfig $updatedStorage storage attributes
126
	 *
127
	 * @return IStorageConfig storage config
128
	 * @throws NotFoundException if the given storage does not exist in the config
129
	 */
130
	public function updateStorage(IStorageConfig $updatedStorage) {
131
		$updatedStorage->setApplicableUsers([$this->getUser()->getUID()]);
132
		return parent::updateStorage($updatedStorage);
133
	}
134
135
	/**
136
	 * Get the visibility type for this controller, used in validation
137
	 *
138
	 * @return string IStoragesBackendService::VISIBILITY_* constants
139
	 */
140
	public function getVisibilityType() {
141
		return IStoragesBackendService::VISIBILITY_PERSONAL;
142
	}
143
144
	protected function isApplicable(IStorageConfig $config) {
145
		return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === IStorageConfig::MOUNT_TYPE_PERSONAl;
146
	}
147
148
	/**
149
	 * Deletes the storages mounted to a user
150
	 * @param IUser $user
151
	 * @param IMountProviderCollection $mountProviderCollection
152
	 * @return bool
153
	 */
154
	public function deleteAllMountsForUser(IUser $user, IMountProviderCollection $mountProviderCollection) {
155
		$getUserMounts = $this->userMountCache->getMountsForUser($user);
156
		$getMountCollection = $mountProviderCollection->getMountsForUser($user);
157
		if (\count($getMountCollection) > 0) {
158
			foreach ($getMountCollection as $userMount) {
159
				if ($userMount instanceof PersonalMount) {
160
					$storageId = $userMount->getNumbericStorage();
161
					$this->removeStorage($storageId);
162
				}
163
			}
164
		}
165
166
		$result = false;
167
		if (\count($getUserMounts) > 0) {
168
			foreach ($getUserMounts as $userMount) {
169
				$id = $userMount->getStorageId();
170
				$this->userMountCache->removeUserStorageMount($id, $user->getUID());
171
				$result = true;
172
			}
173
		}
174
		return $result;
175
	}
176
}
177