Completed
Pull Request — master (#26700)
by Philipp
08:19
created

lib/Service/GlobalStoragesService.php (2 issues)

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
 * @author Joas Schilling <[email protected]>
4
 * @author Lukas Reschke <[email protected]>
5
 * @author Robin Appelman <[email protected]>
6
 * @author Robin McCorkell <[email protected]>
7
 * @author Stefan Weil <[email protected]>
8
 * @author Vincent Petry <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2016, ownCloud GmbH.
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OCA\Files_External\Service;
28
29
30
use \OC\Files\Filesystem;
31
use OCA\Files_External\Lib\StorageConfig;
32
33
/**
34
 * Service class to manage global external storages
35
 */
36
class GlobalStoragesService extends StoragesService {
37
	/**
38
	 * Triggers $signal for all applicable users of the given
39
	 * storage
40
	 *
41
	 * @param StorageConfig $storage storage data
42
	 * @param string $signal signal to trigger
43
	 */
44
	protected function triggerHooks(StorageConfig $storage, $signal) {
45
		// FIXME: Use as expression in empty once PHP 5.4 support is dropped
46
		$applicableUsers = $storage->getApplicableUsers();
47
		$applicableGroups = $storage->getApplicableGroups();
48
		if (empty($applicableUsers) && empty($applicableGroups)) {
49
			// raise for user "all"
50
			$this->triggerApplicableHooks(
51
				$signal,
52
				$storage->getMountPoint(),
53
				\OC_Mount_Config::MOUNT_TYPE_USER,
54
				['all']
55
			);
56
			return;
57
		}
58
59
		$this->triggerApplicableHooks(
60
			$signal,
61
			$storage->getMountPoint(),
62
			\OC_Mount_Config::MOUNT_TYPE_USER,
63
			$applicableUsers
64
		);
65
		$this->triggerApplicableHooks(
66
			$signal,
67
			$storage->getMountPoint(),
68
			\OC_Mount_Config::MOUNT_TYPE_GROUP,
69
			$applicableGroups
70
		);
71
	}
72
73
	/**
74
	 * Triggers signal_create_mount or signal_delete_mount to
75
	 * accommodate for additions/deletions in applicableUsers
76
	 * and applicableGroups fields.
77
	 *
78
	 * @param StorageConfig $oldStorage old storage config
79
	 * @param StorageConfig $newStorage new storage config
80
	 */
81
	protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) {
82
		// if mount point changed, it's like a deletion + creation
83 View Code Duplication
		if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) {
84
			$this->triggerHooks($oldStorage, Filesystem::signal_delete_mount);
85
			$this->triggerHooks($newStorage, Filesystem::signal_create_mount);
86
			return;
87
		}
88
89
		$userAdditions = array_diff($newStorage->getApplicableUsers(), $oldStorage->getApplicableUsers());
90
		$userDeletions = array_diff($oldStorage->getApplicableUsers(), $newStorage->getApplicableUsers());
91
		$groupAdditions = array_diff($newStorage->getApplicableGroups(), $oldStorage->getApplicableGroups());
92
		$groupDeletions = array_diff($oldStorage->getApplicableGroups(), $newStorage->getApplicableGroups());
93
94
		// FIXME: Use as expression in empty once PHP 5.4 support is dropped
95
		// if no applicable were set, raise a signal for "all"
96
		$oldApplicableUsers = $oldStorage->getApplicableUsers();
97
		$oldApplicableGroups = $oldStorage->getApplicableGroups();
98 View Code Duplication
		if (empty($oldApplicableUsers) && empty($oldApplicableGroups)) {
0 ignored issues
show
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...
99
			$this->triggerApplicableHooks(
100
				Filesystem::signal_delete_mount,
101
				$oldStorage->getMountPoint(),
102
				\OC_Mount_Config::MOUNT_TYPE_USER,
103
				['all']
104
			);
105
		}
106
107
		// trigger delete for removed users
108
		$this->triggerApplicableHooks(
109
			Filesystem::signal_delete_mount,
110
			$oldStorage->getMountPoint(),
111
			\OC_Mount_Config::MOUNT_TYPE_USER,
112
			$userDeletions
113
		);
114
115
		// trigger delete for removed groups
116
		$this->triggerApplicableHooks(
117
			Filesystem::signal_delete_mount,
118
			$oldStorage->getMountPoint(),
119
			\OC_Mount_Config::MOUNT_TYPE_GROUP,
120
			$groupDeletions
121
		);
122
123
		// and now add the new users
124
		$this->triggerApplicableHooks(
125
			Filesystem::signal_create_mount,
126
			$newStorage->getMountPoint(),
127
			\OC_Mount_Config::MOUNT_TYPE_USER,
128
			$userAdditions
129
		);
130
131
		// and now add the new groups
132
		$this->triggerApplicableHooks(
133
			Filesystem::signal_create_mount,
134
			$newStorage->getMountPoint(),
135
			\OC_Mount_Config::MOUNT_TYPE_GROUP,
136
			$groupAdditions
137
		);
138
139
		// FIXME: Use as expression in empty once PHP 5.4 support is dropped
140
		// if no applicable, raise a signal for "all"
141
		$newApplicableUsers = $newStorage->getApplicableUsers();
142
		$newApplicableGroups = $newStorage->getApplicableGroups();
143 View Code Duplication
		if (empty($newApplicableUsers) && empty($newApplicableGroups)) {
0 ignored issues
show
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...
144
			$this->triggerApplicableHooks(
145
				Filesystem::signal_create_mount,
146
				$newStorage->getMountPoint(),
147
				\OC_Mount_Config::MOUNT_TYPE_USER,
148
				['all']
149
			);
150
		}
151
	}
152
153
	/**
154
	 * Get the visibility type for this controller, used in validation
155
	 *
156
	 * @return string BackendService::VISIBILITY_* constants
157
	 */
158
	public function getVisibilityType() {
159
		return BackendService::VISIBILITY_ADMIN;
160
	}
161
162
	protected function isApplicable(StorageConfig $config) {
163
		return true;
164
	}
165
166
	/**
167
	 * Get all configured admin and personal mounts
168
	 *
169
	 * @return array map of storage id to storage config
170
	 */
171 View Code Duplication
	public function getStorageForAllUsers() {
172
		$mounts = $this->dbConfig->getAllMounts();
173
		$configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts);
174
		$configs = array_filter($configs, function ($config) {
175
			return $config instanceof StorageConfig;
176
		});
177
178
		$keys = array_map(function (StorageConfig $config) {
179
			return $config->getId();
180
		}, $configs);
181
182
		return array_combine($keys, $configs);
183
	}
184
}
185