@@ -24,25 +24,25 @@ |
||
24 | 24 | namespace OCA\Files_External\Service; |
25 | 25 | |
26 | 26 | class ImportLegacyStoragesService extends LegacyStoragesService { |
27 | - private $data; |
|
27 | + private $data; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @param BackendService $backendService |
|
31 | - */ |
|
32 | - public function __construct(BackendService $backendService) { |
|
33 | - $this->backendService = $backendService; |
|
34 | - } |
|
29 | + /** |
|
30 | + * @param BackendService $backendService |
|
31 | + */ |
|
32 | + public function __construct(BackendService $backendService) { |
|
33 | + $this->backendService = $backendService; |
|
34 | + } |
|
35 | 35 | |
36 | - public function setData($data) { |
|
37 | - $this->data = $data; |
|
38 | - } |
|
36 | + public function setData($data) { |
|
37 | + $this->data = $data; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Read legacy config data |
|
42 | - * |
|
43 | - * @return array list of mount configs |
|
44 | - */ |
|
45 | - protected function readLegacyConfig() { |
|
46 | - return $this->data; |
|
47 | - } |
|
40 | + /** |
|
41 | + * Read legacy config data |
|
42 | + * |
|
43 | + * @return array list of mount configs |
|
44 | + */ |
|
45 | + protected function readLegacyConfig() { |
|
46 | + return $this->data; |
|
47 | + } |
|
48 | 48 | } |
@@ -34,167 +34,167 @@ |
||
34 | 34 | */ |
35 | 35 | class UserGlobalStoragesService extends GlobalStoragesService { |
36 | 36 | |
37 | - use UserTrait; |
|
38 | - |
|
39 | - /** @var IGroupManager */ |
|
40 | - protected $groupManager; |
|
41 | - |
|
42 | - /** |
|
43 | - * @param BackendService $backendService |
|
44 | - * @param DBConfigService $dbConfig |
|
45 | - * @param IUserSession $userSession |
|
46 | - * @param IGroupManager $groupManager |
|
47 | - * @param IUserMountCache $userMountCache |
|
48 | - */ |
|
49 | - public function __construct( |
|
50 | - BackendService $backendService, |
|
51 | - DBConfigService $dbConfig, |
|
52 | - IUserSession $userSession, |
|
53 | - IGroupManager $groupManager, |
|
54 | - IUserMountCache $userMountCache |
|
55 | - ) { |
|
56 | - parent::__construct($backendService, $dbConfig, $userMountCache); |
|
57 | - $this->userSession = $userSession; |
|
58 | - $this->groupManager = $groupManager; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Replace config hash ID with real IDs, for migrating legacy storages |
|
63 | - * |
|
64 | - * @param StorageConfig[] $storages Storages with real IDs |
|
65 | - * @param StorageConfig[] $storagesWithConfigHash Storages with config hash IDs |
|
66 | - */ |
|
67 | - protected function setRealStorageIds(array &$storages, array $storagesWithConfigHash) { |
|
68 | - // as a read-only view, storage IDs don't need to be real |
|
69 | - foreach ($storagesWithConfigHash as $storage) { |
|
70 | - $storages[$storage->getId()] = $storage; |
|
71 | - } |
|
72 | - } |
|
73 | - |
|
74 | - protected function readDBConfig() { |
|
75 | - $userMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); |
|
76 | - $globalMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
77 | - $groups = $this->groupManager->getUserGroupIds($this->getUser()); |
|
78 | - if (is_array($groups) && count($groups) !== 0) { |
|
79 | - $groupMounts = $this->dbConfig->getAdminMountsForMultiple(DBConfigService::APPLICABLE_TYPE_GROUP, $groups); |
|
80 | - } else { |
|
81 | - $groupMounts = []; |
|
82 | - } |
|
83 | - return array_merge($userMounts, $groupMounts, $globalMounts); |
|
84 | - } |
|
85 | - |
|
86 | - public function addStorage(StorageConfig $newStorage) { |
|
87 | - throw new \DomainException('UserGlobalStoragesService writing disallowed'); |
|
88 | - } |
|
89 | - |
|
90 | - public function updateStorage(StorageConfig $updatedStorage) { |
|
91 | - throw new \DomainException('UserGlobalStoragesService writing disallowed'); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param integer $id |
|
96 | - */ |
|
97 | - public function removeStorage($id) { |
|
98 | - throw new \DomainException('UserGlobalStoragesService writing disallowed'); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Get unique storages, in case two are defined with the same mountpoint |
|
103 | - * Higher priority storages take precedence |
|
104 | - * |
|
105 | - * @return StorageConfig[] |
|
106 | - */ |
|
107 | - public function getUniqueStorages() { |
|
108 | - $storages = $this->getStorages(); |
|
109 | - |
|
110 | - $storagesByMountpoint = []; |
|
111 | - foreach ($storages as $storage) { |
|
112 | - $storagesByMountpoint[$storage->getMountPoint()][] = $storage; |
|
113 | - } |
|
114 | - |
|
115 | - $result = []; |
|
116 | - foreach ($storagesByMountpoint as $storageList) { |
|
117 | - $storage = array_reduce($storageList, function ($carry, $item) { |
|
118 | - if (isset($carry)) { |
|
119 | - $carryPriorityType = $this->getPriorityType($carry); |
|
120 | - $itemPriorityType = $this->getPriorityType($item); |
|
121 | - if ($carryPriorityType > $itemPriorityType) { |
|
122 | - return $carry; |
|
123 | - } elseif ($carryPriorityType === $itemPriorityType) { |
|
124 | - if ($carry->getPriority() > $item->getPriority()) { |
|
125 | - return $carry; |
|
126 | - } |
|
127 | - } |
|
128 | - } |
|
129 | - return $item; |
|
130 | - }); |
|
131 | - $result[$storage->getID()] = $storage; |
|
132 | - } |
|
133 | - |
|
134 | - return $result; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Get a priority 'type', where a bigger number means higher priority |
|
139 | - * user applicable > group applicable > 'all' |
|
140 | - * |
|
141 | - * @param StorageConfig $storage |
|
142 | - * @return int |
|
143 | - */ |
|
144 | - protected function getPriorityType(StorageConfig $storage) { |
|
145 | - $applicableUsers = $storage->getApplicableUsers(); |
|
146 | - $applicableGroups = $storage->getApplicableGroups(); |
|
147 | - |
|
148 | - if ($applicableUsers && $applicableUsers[0] !== 'all') { |
|
149 | - return 2; |
|
150 | - } |
|
151 | - if ($applicableGroups) { |
|
152 | - return 1; |
|
153 | - } |
|
154 | - return 0; |
|
155 | - } |
|
156 | - |
|
157 | - protected function isApplicable(StorageConfig $config) { |
|
158 | - $applicableUsers = $config->getApplicableUsers(); |
|
159 | - $applicableGroups = $config->getApplicableGroups(); |
|
160 | - |
|
161 | - if (count($applicableUsers) === 0 && count($applicableGroups) === 0) { |
|
162 | - return true; |
|
163 | - } |
|
164 | - if (in_array($this->getUser()->getUID(), $applicableUsers, true)) { |
|
165 | - return true; |
|
166 | - } |
|
167 | - $groupIds = $this->groupManager->getUserGroupIds($this->getUser()); |
|
168 | - foreach ($groupIds as $groupId) { |
|
169 | - if (in_array($groupId, $applicableGroups, true)) { |
|
170 | - return true; |
|
171 | - } |
|
172 | - } |
|
173 | - return false; |
|
174 | - } |
|
175 | - |
|
176 | - |
|
177 | - /** |
|
178 | - * Gets all storages for the user, admin, personal, global, etc |
|
179 | - * |
|
180 | - * @return StorageConfig[] array of storage configs |
|
181 | - */ |
|
182 | - public function getAllStoragesForUser() { |
|
183 | - if (is_null($this->getUser())) { |
|
184 | - return []; |
|
185 | - } |
|
186 | - $groupIds = $this->groupManager->getUserGroupIds($this->getUser()); |
|
187 | - $mounts = $this->dbConfig->getMountsForUser($this->getUser()->getUID(), $groupIds); |
|
188 | - $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
189 | - $configs = array_filter($configs, function ($config) { |
|
190 | - return $config instanceof StorageConfig; |
|
191 | - }); |
|
192 | - |
|
193 | - $keys = array_map(function (StorageConfig $config) { |
|
194 | - return $config->getId(); |
|
195 | - }, $configs); |
|
196 | - |
|
197 | - $storages = array_combine($keys, $configs); |
|
198 | - return array_filter($storages, [$this, 'validateStorage']); |
|
199 | - } |
|
37 | + use UserTrait; |
|
38 | + |
|
39 | + /** @var IGroupManager */ |
|
40 | + protected $groupManager; |
|
41 | + |
|
42 | + /** |
|
43 | + * @param BackendService $backendService |
|
44 | + * @param DBConfigService $dbConfig |
|
45 | + * @param IUserSession $userSession |
|
46 | + * @param IGroupManager $groupManager |
|
47 | + * @param IUserMountCache $userMountCache |
|
48 | + */ |
|
49 | + public function __construct( |
|
50 | + BackendService $backendService, |
|
51 | + DBConfigService $dbConfig, |
|
52 | + IUserSession $userSession, |
|
53 | + IGroupManager $groupManager, |
|
54 | + IUserMountCache $userMountCache |
|
55 | + ) { |
|
56 | + parent::__construct($backendService, $dbConfig, $userMountCache); |
|
57 | + $this->userSession = $userSession; |
|
58 | + $this->groupManager = $groupManager; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Replace config hash ID with real IDs, for migrating legacy storages |
|
63 | + * |
|
64 | + * @param StorageConfig[] $storages Storages with real IDs |
|
65 | + * @param StorageConfig[] $storagesWithConfigHash Storages with config hash IDs |
|
66 | + */ |
|
67 | + protected function setRealStorageIds(array &$storages, array $storagesWithConfigHash) { |
|
68 | + // as a read-only view, storage IDs don't need to be real |
|
69 | + foreach ($storagesWithConfigHash as $storage) { |
|
70 | + $storages[$storage->getId()] = $storage; |
|
71 | + } |
|
72 | + } |
|
73 | + |
|
74 | + protected function readDBConfig() { |
|
75 | + $userMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); |
|
76 | + $globalMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
77 | + $groups = $this->groupManager->getUserGroupIds($this->getUser()); |
|
78 | + if (is_array($groups) && count($groups) !== 0) { |
|
79 | + $groupMounts = $this->dbConfig->getAdminMountsForMultiple(DBConfigService::APPLICABLE_TYPE_GROUP, $groups); |
|
80 | + } else { |
|
81 | + $groupMounts = []; |
|
82 | + } |
|
83 | + return array_merge($userMounts, $groupMounts, $globalMounts); |
|
84 | + } |
|
85 | + |
|
86 | + public function addStorage(StorageConfig $newStorage) { |
|
87 | + throw new \DomainException('UserGlobalStoragesService writing disallowed'); |
|
88 | + } |
|
89 | + |
|
90 | + public function updateStorage(StorageConfig $updatedStorage) { |
|
91 | + throw new \DomainException('UserGlobalStoragesService writing disallowed'); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param integer $id |
|
96 | + */ |
|
97 | + public function removeStorage($id) { |
|
98 | + throw new \DomainException('UserGlobalStoragesService writing disallowed'); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Get unique storages, in case two are defined with the same mountpoint |
|
103 | + * Higher priority storages take precedence |
|
104 | + * |
|
105 | + * @return StorageConfig[] |
|
106 | + */ |
|
107 | + public function getUniqueStorages() { |
|
108 | + $storages = $this->getStorages(); |
|
109 | + |
|
110 | + $storagesByMountpoint = []; |
|
111 | + foreach ($storages as $storage) { |
|
112 | + $storagesByMountpoint[$storage->getMountPoint()][] = $storage; |
|
113 | + } |
|
114 | + |
|
115 | + $result = []; |
|
116 | + foreach ($storagesByMountpoint as $storageList) { |
|
117 | + $storage = array_reduce($storageList, function ($carry, $item) { |
|
118 | + if (isset($carry)) { |
|
119 | + $carryPriorityType = $this->getPriorityType($carry); |
|
120 | + $itemPriorityType = $this->getPriorityType($item); |
|
121 | + if ($carryPriorityType > $itemPriorityType) { |
|
122 | + return $carry; |
|
123 | + } elseif ($carryPriorityType === $itemPriorityType) { |
|
124 | + if ($carry->getPriority() > $item->getPriority()) { |
|
125 | + return $carry; |
|
126 | + } |
|
127 | + } |
|
128 | + } |
|
129 | + return $item; |
|
130 | + }); |
|
131 | + $result[$storage->getID()] = $storage; |
|
132 | + } |
|
133 | + |
|
134 | + return $result; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Get a priority 'type', where a bigger number means higher priority |
|
139 | + * user applicable > group applicable > 'all' |
|
140 | + * |
|
141 | + * @param StorageConfig $storage |
|
142 | + * @return int |
|
143 | + */ |
|
144 | + protected function getPriorityType(StorageConfig $storage) { |
|
145 | + $applicableUsers = $storage->getApplicableUsers(); |
|
146 | + $applicableGroups = $storage->getApplicableGroups(); |
|
147 | + |
|
148 | + if ($applicableUsers && $applicableUsers[0] !== 'all') { |
|
149 | + return 2; |
|
150 | + } |
|
151 | + if ($applicableGroups) { |
|
152 | + return 1; |
|
153 | + } |
|
154 | + return 0; |
|
155 | + } |
|
156 | + |
|
157 | + protected function isApplicable(StorageConfig $config) { |
|
158 | + $applicableUsers = $config->getApplicableUsers(); |
|
159 | + $applicableGroups = $config->getApplicableGroups(); |
|
160 | + |
|
161 | + if (count($applicableUsers) === 0 && count($applicableGroups) === 0) { |
|
162 | + return true; |
|
163 | + } |
|
164 | + if (in_array($this->getUser()->getUID(), $applicableUsers, true)) { |
|
165 | + return true; |
|
166 | + } |
|
167 | + $groupIds = $this->groupManager->getUserGroupIds($this->getUser()); |
|
168 | + foreach ($groupIds as $groupId) { |
|
169 | + if (in_array($groupId, $applicableGroups, true)) { |
|
170 | + return true; |
|
171 | + } |
|
172 | + } |
|
173 | + return false; |
|
174 | + } |
|
175 | + |
|
176 | + |
|
177 | + /** |
|
178 | + * Gets all storages for the user, admin, personal, global, etc |
|
179 | + * |
|
180 | + * @return StorageConfig[] array of storage configs |
|
181 | + */ |
|
182 | + public function getAllStoragesForUser() { |
|
183 | + if (is_null($this->getUser())) { |
|
184 | + return []; |
|
185 | + } |
|
186 | + $groupIds = $this->groupManager->getUserGroupIds($this->getUser()); |
|
187 | + $mounts = $this->dbConfig->getMountsForUser($this->getUser()->getUID(), $groupIds); |
|
188 | + $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
189 | + $configs = array_filter($configs, function ($config) { |
|
190 | + return $config instanceof StorageConfig; |
|
191 | + }); |
|
192 | + |
|
193 | + $keys = array_map(function (StorageConfig $config) { |
|
194 | + return $config->getId(); |
|
195 | + }, $configs); |
|
196 | + |
|
197 | + $storages = array_combine($keys, $configs); |
|
198 | + return array_filter($storages, [$this, 'validateStorage']); |
|
199 | + } |
|
200 | 200 | } |
@@ -35,249 +35,249 @@ |
||
35 | 35 | */ |
36 | 36 | class BackendService { |
37 | 37 | |
38 | - /** Visibility constants for VisibilityTrait */ |
|
39 | - const VISIBILITY_NONE = 0; |
|
40 | - const VISIBILITY_PERSONAL = 1; |
|
41 | - const VISIBILITY_ADMIN = 2; |
|
42 | - //const VISIBILITY_ALIENS = 4; |
|
43 | - |
|
44 | - const VISIBILITY_DEFAULT = 3; // PERSONAL | ADMIN |
|
45 | - |
|
46 | - /** Priority constants for PriorityTrait */ |
|
47 | - const PRIORITY_DEFAULT = 100; |
|
48 | - |
|
49 | - /** @var IConfig */ |
|
50 | - protected $config; |
|
51 | - |
|
52 | - /** @var bool */ |
|
53 | - private $userMountingAllowed = true; |
|
54 | - |
|
55 | - /** @var string[] */ |
|
56 | - private $userMountingBackends = []; |
|
57 | - |
|
58 | - /** @var Backend[] */ |
|
59 | - private $backends = []; |
|
60 | - |
|
61 | - /** @var IBackendProvider[] */ |
|
62 | - private $backendProviders = []; |
|
63 | - |
|
64 | - /** @var AuthMechanism[] */ |
|
65 | - private $authMechanisms = []; |
|
66 | - |
|
67 | - /** @var IAuthMechanismProvider[] */ |
|
68 | - private $authMechanismProviders = []; |
|
69 | - |
|
70 | - /** |
|
71 | - * @param IConfig $config |
|
72 | - */ |
|
73 | - public function __construct( |
|
74 | - IConfig $config |
|
75 | - ) { |
|
76 | - $this->config = $config; |
|
77 | - |
|
78 | - // Load config values |
|
79 | - if ($this->config->getAppValue('files_external', 'allow_user_mounting', 'yes') !== 'yes') { |
|
80 | - $this->userMountingAllowed = false; |
|
81 | - } |
|
82 | - $this->userMountingBackends = explode(',', |
|
83 | - $this->config->getAppValue('files_external', 'user_mounting_backends', '') |
|
84 | - ); |
|
85 | - |
|
86 | - // if no backend is in the list an empty string is in the array and user mounting is disabled |
|
87 | - if ($this->userMountingBackends === ['']) { |
|
88 | - $this->userMountingAllowed = false; |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Register a backend provider |
|
94 | - * |
|
95 | - * @since 9.1.0 |
|
96 | - * @param IBackendProvider $provider |
|
97 | - */ |
|
98 | - public function registerBackendProvider(IBackendProvider $provider) { |
|
99 | - $this->backendProviders[] = $provider; |
|
100 | - } |
|
101 | - |
|
102 | - private function loadBackendProviders() { |
|
103 | - foreach ($this->backendProviders as $provider) { |
|
104 | - $this->registerBackends($provider->getBackends()); |
|
105 | - } |
|
106 | - $this->backendProviders = []; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Register an auth mechanism provider |
|
111 | - * |
|
112 | - * @since 9.1.0 |
|
113 | - * @param IAuthMechanismProvider $provider |
|
114 | - */ |
|
115 | - public function registerAuthMechanismProvider(IAuthMechanismProvider $provider) { |
|
116 | - $this->authMechanismProviders[] = $provider; |
|
117 | - } |
|
118 | - |
|
119 | - private function loadAuthMechanismProviders() { |
|
120 | - foreach ($this->authMechanismProviders as $provider) { |
|
121 | - $this->registerAuthMechanisms($provider->getAuthMechanisms()); |
|
122 | - } |
|
123 | - $this->authMechanismProviders = []; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Register a backend |
|
128 | - * |
|
129 | - * @deprecated 9.1.0 use registerBackendProvider() |
|
130 | - * @param Backend $backend |
|
131 | - */ |
|
132 | - public function registerBackend(Backend $backend) { |
|
133 | - if (!$this->isAllowedUserBackend($backend)) { |
|
134 | - $backend->removeVisibility(BackendService::VISIBILITY_PERSONAL); |
|
135 | - } |
|
136 | - foreach ($backend->getIdentifierAliases() as $alias) { |
|
137 | - $this->backends[$alias] = $backend; |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @deprecated 9.1.0 use registerBackendProvider() |
|
143 | - * @param Backend[] $backends |
|
144 | - */ |
|
145 | - public function registerBackends(array $backends) { |
|
146 | - foreach ($backends as $backend) { |
|
147 | - $this->registerBackend($backend); |
|
148 | - } |
|
149 | - } |
|
150 | - /** |
|
151 | - * Register an authentication mechanism |
|
152 | - * |
|
153 | - * @deprecated 9.1.0 use registerAuthMechanismProvider() |
|
154 | - * @param AuthMechanism $authMech |
|
155 | - */ |
|
156 | - public function registerAuthMechanism(AuthMechanism $authMech) { |
|
157 | - if (!$this->isAllowedAuthMechanism($authMech)) { |
|
158 | - $authMech->removeVisibility(BackendService::VISIBILITY_PERSONAL); |
|
159 | - } |
|
160 | - foreach ($authMech->getIdentifierAliases() as $alias) { |
|
161 | - $this->authMechanisms[$alias] = $authMech; |
|
162 | - } |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @deprecated 9.1.0 use registerAuthMechanismProvider() |
|
167 | - * @param AuthMechanism[] $mechanisms |
|
168 | - */ |
|
169 | - public function registerAuthMechanisms(array $mechanisms) { |
|
170 | - foreach ($mechanisms as $mechanism) { |
|
171 | - $this->registerAuthMechanism($mechanism); |
|
172 | - } |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Get all backends |
|
177 | - * |
|
178 | - * @return Backend[] |
|
179 | - */ |
|
180 | - public function getBackends() { |
|
181 | - $this->loadBackendProviders(); |
|
182 | - // only return real identifiers, no aliases |
|
183 | - $backends = []; |
|
184 | - foreach ($this->backends as $backend) { |
|
185 | - $backends[$backend->getIdentifier()] = $backend; |
|
186 | - } |
|
187 | - return $backends; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Get all available backends |
|
192 | - * |
|
193 | - * @return Backend[] |
|
194 | - */ |
|
195 | - public function getAvailableBackends() { |
|
196 | - return array_filter($this->getBackends(), function($backend) { |
|
197 | - return !($backend->checkDependencies()); |
|
198 | - }); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * @param string $identifier |
|
203 | - * @return Backend|null |
|
204 | - */ |
|
205 | - public function getBackend($identifier) { |
|
206 | - $this->loadBackendProviders(); |
|
207 | - if (isset($this->backends[$identifier])) { |
|
208 | - return $this->backends[$identifier]; |
|
209 | - } |
|
210 | - return null; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Get all authentication mechanisms |
|
215 | - * |
|
216 | - * @return AuthMechanism[] |
|
217 | - */ |
|
218 | - public function getAuthMechanisms() { |
|
219 | - $this->loadAuthMechanismProviders(); |
|
220 | - // only return real identifiers, no aliases |
|
221 | - $mechanisms = []; |
|
222 | - foreach ($this->authMechanisms as $mechanism) { |
|
223 | - $mechanisms[$mechanism->getIdentifier()] = $mechanism; |
|
224 | - } |
|
225 | - return $mechanisms; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Get all authentication mechanisms for schemes |
|
230 | - * |
|
231 | - * @param string[] $schemes |
|
232 | - * @return AuthMechanism[] |
|
233 | - */ |
|
234 | - public function getAuthMechanismsByScheme(array $schemes) { |
|
235 | - return array_filter($this->getAuthMechanisms(), function($authMech) use ($schemes) { |
|
236 | - return in_array($authMech->getScheme(), $schemes, true); |
|
237 | - }); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @param string $identifier |
|
242 | - * @return AuthMechanism|null |
|
243 | - */ |
|
244 | - public function getAuthMechanism($identifier) { |
|
245 | - $this->loadAuthMechanismProviders(); |
|
246 | - if (isset($this->authMechanisms[$identifier])) { |
|
247 | - return $this->authMechanisms[$identifier]; |
|
248 | - } |
|
249 | - return null; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * @return bool |
|
254 | - */ |
|
255 | - public function isUserMountingAllowed() { |
|
256 | - return $this->userMountingAllowed; |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * Check a backend if a user is allowed to mount it |
|
261 | - * |
|
262 | - * @param Backend $backend |
|
263 | - * @return bool |
|
264 | - */ |
|
265 | - protected function isAllowedUserBackend(Backend $backend) { |
|
266 | - if ($this->userMountingAllowed && |
|
267 | - array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends) |
|
268 | - ) { |
|
269 | - return true; |
|
270 | - } |
|
271 | - return false; |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Check an authentication mechanism if a user is allowed to use it |
|
276 | - * |
|
277 | - * @param AuthMechanism $authMechanism |
|
278 | - * @return bool |
|
279 | - */ |
|
280 | - protected function isAllowedAuthMechanism(AuthMechanism $authMechanism) { |
|
281 | - return true; // not implemented |
|
282 | - } |
|
38 | + /** Visibility constants for VisibilityTrait */ |
|
39 | + const VISIBILITY_NONE = 0; |
|
40 | + const VISIBILITY_PERSONAL = 1; |
|
41 | + const VISIBILITY_ADMIN = 2; |
|
42 | + //const VISIBILITY_ALIENS = 4; |
|
43 | + |
|
44 | + const VISIBILITY_DEFAULT = 3; // PERSONAL | ADMIN |
|
45 | + |
|
46 | + /** Priority constants for PriorityTrait */ |
|
47 | + const PRIORITY_DEFAULT = 100; |
|
48 | + |
|
49 | + /** @var IConfig */ |
|
50 | + protected $config; |
|
51 | + |
|
52 | + /** @var bool */ |
|
53 | + private $userMountingAllowed = true; |
|
54 | + |
|
55 | + /** @var string[] */ |
|
56 | + private $userMountingBackends = []; |
|
57 | + |
|
58 | + /** @var Backend[] */ |
|
59 | + private $backends = []; |
|
60 | + |
|
61 | + /** @var IBackendProvider[] */ |
|
62 | + private $backendProviders = []; |
|
63 | + |
|
64 | + /** @var AuthMechanism[] */ |
|
65 | + private $authMechanisms = []; |
|
66 | + |
|
67 | + /** @var IAuthMechanismProvider[] */ |
|
68 | + private $authMechanismProviders = []; |
|
69 | + |
|
70 | + /** |
|
71 | + * @param IConfig $config |
|
72 | + */ |
|
73 | + public function __construct( |
|
74 | + IConfig $config |
|
75 | + ) { |
|
76 | + $this->config = $config; |
|
77 | + |
|
78 | + // Load config values |
|
79 | + if ($this->config->getAppValue('files_external', 'allow_user_mounting', 'yes') !== 'yes') { |
|
80 | + $this->userMountingAllowed = false; |
|
81 | + } |
|
82 | + $this->userMountingBackends = explode(',', |
|
83 | + $this->config->getAppValue('files_external', 'user_mounting_backends', '') |
|
84 | + ); |
|
85 | + |
|
86 | + // if no backend is in the list an empty string is in the array and user mounting is disabled |
|
87 | + if ($this->userMountingBackends === ['']) { |
|
88 | + $this->userMountingAllowed = false; |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Register a backend provider |
|
94 | + * |
|
95 | + * @since 9.1.0 |
|
96 | + * @param IBackendProvider $provider |
|
97 | + */ |
|
98 | + public function registerBackendProvider(IBackendProvider $provider) { |
|
99 | + $this->backendProviders[] = $provider; |
|
100 | + } |
|
101 | + |
|
102 | + private function loadBackendProviders() { |
|
103 | + foreach ($this->backendProviders as $provider) { |
|
104 | + $this->registerBackends($provider->getBackends()); |
|
105 | + } |
|
106 | + $this->backendProviders = []; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Register an auth mechanism provider |
|
111 | + * |
|
112 | + * @since 9.1.0 |
|
113 | + * @param IAuthMechanismProvider $provider |
|
114 | + */ |
|
115 | + public function registerAuthMechanismProvider(IAuthMechanismProvider $provider) { |
|
116 | + $this->authMechanismProviders[] = $provider; |
|
117 | + } |
|
118 | + |
|
119 | + private function loadAuthMechanismProviders() { |
|
120 | + foreach ($this->authMechanismProviders as $provider) { |
|
121 | + $this->registerAuthMechanisms($provider->getAuthMechanisms()); |
|
122 | + } |
|
123 | + $this->authMechanismProviders = []; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Register a backend |
|
128 | + * |
|
129 | + * @deprecated 9.1.0 use registerBackendProvider() |
|
130 | + * @param Backend $backend |
|
131 | + */ |
|
132 | + public function registerBackend(Backend $backend) { |
|
133 | + if (!$this->isAllowedUserBackend($backend)) { |
|
134 | + $backend->removeVisibility(BackendService::VISIBILITY_PERSONAL); |
|
135 | + } |
|
136 | + foreach ($backend->getIdentifierAliases() as $alias) { |
|
137 | + $this->backends[$alias] = $backend; |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @deprecated 9.1.0 use registerBackendProvider() |
|
143 | + * @param Backend[] $backends |
|
144 | + */ |
|
145 | + public function registerBackends(array $backends) { |
|
146 | + foreach ($backends as $backend) { |
|
147 | + $this->registerBackend($backend); |
|
148 | + } |
|
149 | + } |
|
150 | + /** |
|
151 | + * Register an authentication mechanism |
|
152 | + * |
|
153 | + * @deprecated 9.1.0 use registerAuthMechanismProvider() |
|
154 | + * @param AuthMechanism $authMech |
|
155 | + */ |
|
156 | + public function registerAuthMechanism(AuthMechanism $authMech) { |
|
157 | + if (!$this->isAllowedAuthMechanism($authMech)) { |
|
158 | + $authMech->removeVisibility(BackendService::VISIBILITY_PERSONAL); |
|
159 | + } |
|
160 | + foreach ($authMech->getIdentifierAliases() as $alias) { |
|
161 | + $this->authMechanisms[$alias] = $authMech; |
|
162 | + } |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @deprecated 9.1.0 use registerAuthMechanismProvider() |
|
167 | + * @param AuthMechanism[] $mechanisms |
|
168 | + */ |
|
169 | + public function registerAuthMechanisms(array $mechanisms) { |
|
170 | + foreach ($mechanisms as $mechanism) { |
|
171 | + $this->registerAuthMechanism($mechanism); |
|
172 | + } |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Get all backends |
|
177 | + * |
|
178 | + * @return Backend[] |
|
179 | + */ |
|
180 | + public function getBackends() { |
|
181 | + $this->loadBackendProviders(); |
|
182 | + // only return real identifiers, no aliases |
|
183 | + $backends = []; |
|
184 | + foreach ($this->backends as $backend) { |
|
185 | + $backends[$backend->getIdentifier()] = $backend; |
|
186 | + } |
|
187 | + return $backends; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Get all available backends |
|
192 | + * |
|
193 | + * @return Backend[] |
|
194 | + */ |
|
195 | + public function getAvailableBackends() { |
|
196 | + return array_filter($this->getBackends(), function($backend) { |
|
197 | + return !($backend->checkDependencies()); |
|
198 | + }); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * @param string $identifier |
|
203 | + * @return Backend|null |
|
204 | + */ |
|
205 | + public function getBackend($identifier) { |
|
206 | + $this->loadBackendProviders(); |
|
207 | + if (isset($this->backends[$identifier])) { |
|
208 | + return $this->backends[$identifier]; |
|
209 | + } |
|
210 | + return null; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Get all authentication mechanisms |
|
215 | + * |
|
216 | + * @return AuthMechanism[] |
|
217 | + */ |
|
218 | + public function getAuthMechanisms() { |
|
219 | + $this->loadAuthMechanismProviders(); |
|
220 | + // only return real identifiers, no aliases |
|
221 | + $mechanisms = []; |
|
222 | + foreach ($this->authMechanisms as $mechanism) { |
|
223 | + $mechanisms[$mechanism->getIdentifier()] = $mechanism; |
|
224 | + } |
|
225 | + return $mechanisms; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Get all authentication mechanisms for schemes |
|
230 | + * |
|
231 | + * @param string[] $schemes |
|
232 | + * @return AuthMechanism[] |
|
233 | + */ |
|
234 | + public function getAuthMechanismsByScheme(array $schemes) { |
|
235 | + return array_filter($this->getAuthMechanisms(), function($authMech) use ($schemes) { |
|
236 | + return in_array($authMech->getScheme(), $schemes, true); |
|
237 | + }); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @param string $identifier |
|
242 | + * @return AuthMechanism|null |
|
243 | + */ |
|
244 | + public function getAuthMechanism($identifier) { |
|
245 | + $this->loadAuthMechanismProviders(); |
|
246 | + if (isset($this->authMechanisms[$identifier])) { |
|
247 | + return $this->authMechanisms[$identifier]; |
|
248 | + } |
|
249 | + return null; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * @return bool |
|
254 | + */ |
|
255 | + public function isUserMountingAllowed() { |
|
256 | + return $this->userMountingAllowed; |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * Check a backend if a user is allowed to mount it |
|
261 | + * |
|
262 | + * @param Backend $backend |
|
263 | + * @return bool |
|
264 | + */ |
|
265 | + protected function isAllowedUserBackend(Backend $backend) { |
|
266 | + if ($this->userMountingAllowed && |
|
267 | + array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends) |
|
268 | + ) { |
|
269 | + return true; |
|
270 | + } |
|
271 | + return false; |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Check an authentication mechanism if a user is allowed to use it |
|
276 | + * |
|
277 | + * @param AuthMechanism $authMechanism |
|
278 | + * @return bool |
|
279 | + */ |
|
280 | + protected function isAllowedAuthMechanism(AuthMechanism $authMechanism) { |
|
281 | + return true; // not implemented |
|
282 | + } |
|
283 | 283 | } |
@@ -35,151 +35,151 @@ |
||
35 | 35 | * Service class to manage global external storages |
36 | 36 | */ |
37 | 37 | class GlobalStoragesService extends StoragesService { |
38 | - /** |
|
39 | - * Triggers $signal for all applicable users of the given |
|
40 | - * storage |
|
41 | - * |
|
42 | - * @param StorageConfig $storage storage data |
|
43 | - * @param string $signal signal to trigger |
|
44 | - */ |
|
45 | - protected function triggerHooks(StorageConfig $storage, $signal) { |
|
46 | - // FIXME: Use as expression in empty once PHP 5.4 support is dropped |
|
47 | - $applicableUsers = $storage->getApplicableUsers(); |
|
48 | - $applicableGroups = $storage->getApplicableGroups(); |
|
49 | - if (empty($applicableUsers) && empty($applicableGroups)) { |
|
50 | - // raise for user "all" |
|
51 | - $this->triggerApplicableHooks( |
|
52 | - $signal, |
|
53 | - $storage->getMountPoint(), |
|
54 | - \OC_Mount_Config::MOUNT_TYPE_USER, |
|
55 | - ['all'] |
|
56 | - ); |
|
57 | - return; |
|
58 | - } |
|
59 | - |
|
60 | - $this->triggerApplicableHooks( |
|
61 | - $signal, |
|
62 | - $storage->getMountPoint(), |
|
63 | - \OC_Mount_Config::MOUNT_TYPE_USER, |
|
64 | - $applicableUsers |
|
65 | - ); |
|
66 | - $this->triggerApplicableHooks( |
|
67 | - $signal, |
|
68 | - $storage->getMountPoint(), |
|
69 | - \OC_Mount_Config::MOUNT_TYPE_GROUP, |
|
70 | - $applicableGroups |
|
71 | - ); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Triggers signal_create_mount or signal_delete_mount to |
|
76 | - * accommodate for additions/deletions in applicableUsers |
|
77 | - * and applicableGroups fields. |
|
78 | - * |
|
79 | - * @param StorageConfig $oldStorage old storage config |
|
80 | - * @param StorageConfig $newStorage new storage config |
|
81 | - */ |
|
82 | - protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { |
|
83 | - // if mount point changed, it's like a deletion + creation |
|
84 | - if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { |
|
85 | - $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); |
|
86 | - $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
87 | - return; |
|
88 | - } |
|
89 | - |
|
90 | - $userAdditions = array_diff($newStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); |
|
91 | - $userDeletions = array_diff($oldStorage->getApplicableUsers(), $newStorage->getApplicableUsers()); |
|
92 | - $groupAdditions = array_diff($newStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); |
|
93 | - $groupDeletions = array_diff($oldStorage->getApplicableGroups(), $newStorage->getApplicableGroups()); |
|
94 | - |
|
95 | - // FIXME: Use as expression in empty once PHP 5.4 support is dropped |
|
96 | - // if no applicable were set, raise a signal for "all" |
|
97 | - $oldApplicableUsers = $oldStorage->getApplicableUsers(); |
|
98 | - $oldApplicableGroups = $oldStorage->getApplicableGroups(); |
|
99 | - if (empty($oldApplicableUsers) && empty($oldApplicableGroups)) { |
|
100 | - $this->triggerApplicableHooks( |
|
101 | - Filesystem::signal_delete_mount, |
|
102 | - $oldStorage->getMountPoint(), |
|
103 | - \OC_Mount_Config::MOUNT_TYPE_USER, |
|
104 | - ['all'] |
|
105 | - ); |
|
106 | - } |
|
107 | - |
|
108 | - // trigger delete for removed users |
|
109 | - $this->triggerApplicableHooks( |
|
110 | - Filesystem::signal_delete_mount, |
|
111 | - $oldStorage->getMountPoint(), |
|
112 | - \OC_Mount_Config::MOUNT_TYPE_USER, |
|
113 | - $userDeletions |
|
114 | - ); |
|
115 | - |
|
116 | - // trigger delete for removed groups |
|
117 | - $this->triggerApplicableHooks( |
|
118 | - Filesystem::signal_delete_mount, |
|
119 | - $oldStorage->getMountPoint(), |
|
120 | - \OC_Mount_Config::MOUNT_TYPE_GROUP, |
|
121 | - $groupDeletions |
|
122 | - ); |
|
123 | - |
|
124 | - // and now add the new users |
|
125 | - $this->triggerApplicableHooks( |
|
126 | - Filesystem::signal_create_mount, |
|
127 | - $newStorage->getMountPoint(), |
|
128 | - \OC_Mount_Config::MOUNT_TYPE_USER, |
|
129 | - $userAdditions |
|
130 | - ); |
|
131 | - |
|
132 | - // and now add the new groups |
|
133 | - $this->triggerApplicableHooks( |
|
134 | - Filesystem::signal_create_mount, |
|
135 | - $newStorage->getMountPoint(), |
|
136 | - \OC_Mount_Config::MOUNT_TYPE_GROUP, |
|
137 | - $groupAdditions |
|
138 | - ); |
|
139 | - |
|
140 | - // FIXME: Use as expression in empty once PHP 5.4 support is dropped |
|
141 | - // if no applicable, raise a signal for "all" |
|
142 | - $newApplicableUsers = $newStorage->getApplicableUsers(); |
|
143 | - $newApplicableGroups = $newStorage->getApplicableGroups(); |
|
144 | - if (empty($newApplicableUsers) && empty($newApplicableGroups)) { |
|
145 | - $this->triggerApplicableHooks( |
|
146 | - Filesystem::signal_create_mount, |
|
147 | - $newStorage->getMountPoint(), |
|
148 | - \OC_Mount_Config::MOUNT_TYPE_USER, |
|
149 | - ['all'] |
|
150 | - ); |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Get the visibility type for this controller, used in validation |
|
156 | - * |
|
157 | - * @return string BackendService::VISIBILITY_* constants |
|
158 | - */ |
|
159 | - public function getVisibilityType() { |
|
160 | - return BackendService::VISIBILITY_ADMIN; |
|
161 | - } |
|
162 | - |
|
163 | - protected function isApplicable(StorageConfig $config) { |
|
164 | - return true; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Get all configured admin and personal mounts |
|
169 | - * |
|
170 | - * @return array map of storage id to storage config |
|
171 | - */ |
|
172 | - public function getStorageForAllUsers() { |
|
173 | - $mounts = $this->dbConfig->getAllMounts(); |
|
174 | - $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
175 | - $configs = array_filter($configs, function ($config) { |
|
176 | - return $config instanceof StorageConfig; |
|
177 | - }); |
|
178 | - |
|
179 | - $keys = array_map(function (StorageConfig $config) { |
|
180 | - return $config->getId(); |
|
181 | - }, $configs); |
|
182 | - |
|
183 | - return array_combine($keys, $configs); |
|
184 | - } |
|
38 | + /** |
|
39 | + * Triggers $signal for all applicable users of the given |
|
40 | + * storage |
|
41 | + * |
|
42 | + * @param StorageConfig $storage storage data |
|
43 | + * @param string $signal signal to trigger |
|
44 | + */ |
|
45 | + protected function triggerHooks(StorageConfig $storage, $signal) { |
|
46 | + // FIXME: Use as expression in empty once PHP 5.4 support is dropped |
|
47 | + $applicableUsers = $storage->getApplicableUsers(); |
|
48 | + $applicableGroups = $storage->getApplicableGroups(); |
|
49 | + if (empty($applicableUsers) && empty($applicableGroups)) { |
|
50 | + // raise for user "all" |
|
51 | + $this->triggerApplicableHooks( |
|
52 | + $signal, |
|
53 | + $storage->getMountPoint(), |
|
54 | + \OC_Mount_Config::MOUNT_TYPE_USER, |
|
55 | + ['all'] |
|
56 | + ); |
|
57 | + return; |
|
58 | + } |
|
59 | + |
|
60 | + $this->triggerApplicableHooks( |
|
61 | + $signal, |
|
62 | + $storage->getMountPoint(), |
|
63 | + \OC_Mount_Config::MOUNT_TYPE_USER, |
|
64 | + $applicableUsers |
|
65 | + ); |
|
66 | + $this->triggerApplicableHooks( |
|
67 | + $signal, |
|
68 | + $storage->getMountPoint(), |
|
69 | + \OC_Mount_Config::MOUNT_TYPE_GROUP, |
|
70 | + $applicableGroups |
|
71 | + ); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Triggers signal_create_mount or signal_delete_mount to |
|
76 | + * accommodate for additions/deletions in applicableUsers |
|
77 | + * and applicableGroups fields. |
|
78 | + * |
|
79 | + * @param StorageConfig $oldStorage old storage config |
|
80 | + * @param StorageConfig $newStorage new storage config |
|
81 | + */ |
|
82 | + protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { |
|
83 | + // if mount point changed, it's like a deletion + creation |
|
84 | + if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { |
|
85 | + $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); |
|
86 | + $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
87 | + return; |
|
88 | + } |
|
89 | + |
|
90 | + $userAdditions = array_diff($newStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); |
|
91 | + $userDeletions = array_diff($oldStorage->getApplicableUsers(), $newStorage->getApplicableUsers()); |
|
92 | + $groupAdditions = array_diff($newStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); |
|
93 | + $groupDeletions = array_diff($oldStorage->getApplicableGroups(), $newStorage->getApplicableGroups()); |
|
94 | + |
|
95 | + // FIXME: Use as expression in empty once PHP 5.4 support is dropped |
|
96 | + // if no applicable were set, raise a signal for "all" |
|
97 | + $oldApplicableUsers = $oldStorage->getApplicableUsers(); |
|
98 | + $oldApplicableGroups = $oldStorage->getApplicableGroups(); |
|
99 | + if (empty($oldApplicableUsers) && empty($oldApplicableGroups)) { |
|
100 | + $this->triggerApplicableHooks( |
|
101 | + Filesystem::signal_delete_mount, |
|
102 | + $oldStorage->getMountPoint(), |
|
103 | + \OC_Mount_Config::MOUNT_TYPE_USER, |
|
104 | + ['all'] |
|
105 | + ); |
|
106 | + } |
|
107 | + |
|
108 | + // trigger delete for removed users |
|
109 | + $this->triggerApplicableHooks( |
|
110 | + Filesystem::signal_delete_mount, |
|
111 | + $oldStorage->getMountPoint(), |
|
112 | + \OC_Mount_Config::MOUNT_TYPE_USER, |
|
113 | + $userDeletions |
|
114 | + ); |
|
115 | + |
|
116 | + // trigger delete for removed groups |
|
117 | + $this->triggerApplicableHooks( |
|
118 | + Filesystem::signal_delete_mount, |
|
119 | + $oldStorage->getMountPoint(), |
|
120 | + \OC_Mount_Config::MOUNT_TYPE_GROUP, |
|
121 | + $groupDeletions |
|
122 | + ); |
|
123 | + |
|
124 | + // and now add the new users |
|
125 | + $this->triggerApplicableHooks( |
|
126 | + Filesystem::signal_create_mount, |
|
127 | + $newStorage->getMountPoint(), |
|
128 | + \OC_Mount_Config::MOUNT_TYPE_USER, |
|
129 | + $userAdditions |
|
130 | + ); |
|
131 | + |
|
132 | + // and now add the new groups |
|
133 | + $this->triggerApplicableHooks( |
|
134 | + Filesystem::signal_create_mount, |
|
135 | + $newStorage->getMountPoint(), |
|
136 | + \OC_Mount_Config::MOUNT_TYPE_GROUP, |
|
137 | + $groupAdditions |
|
138 | + ); |
|
139 | + |
|
140 | + // FIXME: Use as expression in empty once PHP 5.4 support is dropped |
|
141 | + // if no applicable, raise a signal for "all" |
|
142 | + $newApplicableUsers = $newStorage->getApplicableUsers(); |
|
143 | + $newApplicableGroups = $newStorage->getApplicableGroups(); |
|
144 | + if (empty($newApplicableUsers) && empty($newApplicableGroups)) { |
|
145 | + $this->triggerApplicableHooks( |
|
146 | + Filesystem::signal_create_mount, |
|
147 | + $newStorage->getMountPoint(), |
|
148 | + \OC_Mount_Config::MOUNT_TYPE_USER, |
|
149 | + ['all'] |
|
150 | + ); |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Get the visibility type for this controller, used in validation |
|
156 | + * |
|
157 | + * @return string BackendService::VISIBILITY_* constants |
|
158 | + */ |
|
159 | + public function getVisibilityType() { |
|
160 | + return BackendService::VISIBILITY_ADMIN; |
|
161 | + } |
|
162 | + |
|
163 | + protected function isApplicable(StorageConfig $config) { |
|
164 | + return true; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Get all configured admin and personal mounts |
|
169 | + * |
|
170 | + * @return array map of storage id to storage config |
|
171 | + */ |
|
172 | + public function getStorageForAllUsers() { |
|
173 | + $mounts = $this->dbConfig->getAllMounts(); |
|
174 | + $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
175 | + $configs = array_filter($configs, function ($config) { |
|
176 | + return $config instanceof StorageConfig; |
|
177 | + }); |
|
178 | + |
|
179 | + $keys = array_map(function (StorageConfig $config) { |
|
180 | + return $config->getId(); |
|
181 | + }, $configs); |
|
182 | + |
|
183 | + return array_combine($keys, $configs); |
|
184 | + } |
|
185 | 185 | } |
@@ -29,28 +29,28 @@ |
||
29 | 29 | * Read user defined mounts from the legacy mount.json |
30 | 30 | */ |
31 | 31 | class UserLegacyStoragesService extends LegacyStoragesService { |
32 | - /** |
|
33 | - * @var IUserSession |
|
34 | - */ |
|
35 | - private $userSession; |
|
32 | + /** |
|
33 | + * @var IUserSession |
|
34 | + */ |
|
35 | + private $userSession; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param BackendService $backendService |
|
39 | - * @param IUserSession $userSession |
|
40 | - */ |
|
41 | - public function __construct(BackendService $backendService, IUserSession $userSession) { |
|
42 | - $this->backendService = $backendService; |
|
43 | - $this->userSession = $userSession; |
|
44 | - } |
|
37 | + /** |
|
38 | + * @param BackendService $backendService |
|
39 | + * @param IUserSession $userSession |
|
40 | + */ |
|
41 | + public function __construct(BackendService $backendService, IUserSession $userSession) { |
|
42 | + $this->backendService = $backendService; |
|
43 | + $this->userSession = $userSession; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Read legacy config data |
|
48 | - * |
|
49 | - * @return array list of storage configs |
|
50 | - */ |
|
51 | - protected function readLegacyConfig() { |
|
52 | - // read user config |
|
53 | - $user = $this->userSession->getUser()->getUID(); |
|
54 | - return \OC_Mount_Config::readData($user); |
|
55 | - } |
|
46 | + /** |
|
47 | + * Read legacy config data |
|
48 | + * |
|
49 | + * @return array list of storage configs |
|
50 | + */ |
|
51 | + protected function readLegacyConfig() { |
|
52 | + // read user config |
|
53 | + $user = $this->userSession->getUser()->getUID(); |
|
54 | + return \OC_Mount_Config::readData($user); |
|
55 | + } |
|
56 | 56 | } |
@@ -30,183 +30,183 @@ |
||
30 | 30 | * Read mount config from legacy mount.json |
31 | 31 | */ |
32 | 32 | abstract class LegacyStoragesService { |
33 | - /** @var BackendService */ |
|
34 | - protected $backendService; |
|
33 | + /** @var BackendService */ |
|
34 | + protected $backendService; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Read legacy config data |
|
38 | - * |
|
39 | - * @return array list of mount configs |
|
40 | - */ |
|
41 | - abstract protected function readLegacyConfig(); |
|
36 | + /** |
|
37 | + * Read legacy config data |
|
38 | + * |
|
39 | + * @return array list of mount configs |
|
40 | + */ |
|
41 | + abstract protected function readLegacyConfig(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * Copy legacy storage options into the given storage config object. |
|
45 | - * |
|
46 | - * @param StorageConfig $storageConfig storage config to populate |
|
47 | - * @param string $mountType mount type |
|
48 | - * @param string $applicable applicable user or group |
|
49 | - * @param array $storageOptions legacy storage options |
|
50 | - * |
|
51 | - * @return StorageConfig populated storage config |
|
52 | - */ |
|
53 | - protected function populateStorageConfigWithLegacyOptions( |
|
54 | - &$storageConfig, |
|
55 | - $mountType, |
|
56 | - $applicable, |
|
57 | - $storageOptions |
|
58 | - ) { |
|
59 | - $backend = $this->backendService->getBackend($storageOptions['backend']); |
|
60 | - if (!$backend) { |
|
61 | - throw new \UnexpectedValueException('Invalid backend ' . $storageOptions['backend']); |
|
62 | - } |
|
63 | - $storageConfig->setBackend($backend); |
|
64 | - if (isset($storageOptions['authMechanism']) && $storageOptions['authMechanism'] !== 'builtin::builtin') { |
|
65 | - $authMechanism = $this->backendService->getAuthMechanism($storageOptions['authMechanism']); |
|
66 | - } else { |
|
67 | - $authMechanism = $backend->getLegacyAuthMechanism($storageOptions); |
|
68 | - $storageOptions['authMechanism'] = 'null'; // to make error handling easier |
|
69 | - } |
|
70 | - if (!$authMechanism) { |
|
71 | - throw new \UnexpectedValueException('Invalid authentication mechanism ' . $storageOptions['authMechanism']); |
|
72 | - } |
|
73 | - $storageConfig->setAuthMechanism($authMechanism); |
|
74 | - $storageConfig->setBackendOptions($storageOptions['options']); |
|
75 | - if (isset($storageOptions['mountOptions'])) { |
|
76 | - $storageConfig->setMountOptions($storageOptions['mountOptions']); |
|
77 | - } |
|
78 | - if (!isset($storageOptions['priority'])) { |
|
79 | - $storageOptions['priority'] = $backend->getPriority(); |
|
80 | - } |
|
81 | - $storageConfig->setPriority($storageOptions['priority']); |
|
82 | - if ($mountType === \OC_Mount_Config::MOUNT_TYPE_USER) { |
|
83 | - $applicableUsers = $storageConfig->getApplicableUsers(); |
|
84 | - if ($applicable !== 'all') { |
|
85 | - $applicableUsers[] = $applicable; |
|
86 | - $storageConfig->setApplicableUsers($applicableUsers); |
|
87 | - } |
|
88 | - } else if ($mountType === \OC_Mount_Config::MOUNT_TYPE_GROUP) { |
|
89 | - $applicableGroups = $storageConfig->getApplicableGroups(); |
|
90 | - $applicableGroups[] = $applicable; |
|
91 | - $storageConfig->setApplicableGroups($applicableGroups); |
|
92 | - } |
|
93 | - return $storageConfig; |
|
94 | - } |
|
43 | + /** |
|
44 | + * Copy legacy storage options into the given storage config object. |
|
45 | + * |
|
46 | + * @param StorageConfig $storageConfig storage config to populate |
|
47 | + * @param string $mountType mount type |
|
48 | + * @param string $applicable applicable user or group |
|
49 | + * @param array $storageOptions legacy storage options |
|
50 | + * |
|
51 | + * @return StorageConfig populated storage config |
|
52 | + */ |
|
53 | + protected function populateStorageConfigWithLegacyOptions( |
|
54 | + &$storageConfig, |
|
55 | + $mountType, |
|
56 | + $applicable, |
|
57 | + $storageOptions |
|
58 | + ) { |
|
59 | + $backend = $this->backendService->getBackend($storageOptions['backend']); |
|
60 | + if (!$backend) { |
|
61 | + throw new \UnexpectedValueException('Invalid backend ' . $storageOptions['backend']); |
|
62 | + } |
|
63 | + $storageConfig->setBackend($backend); |
|
64 | + if (isset($storageOptions['authMechanism']) && $storageOptions['authMechanism'] !== 'builtin::builtin') { |
|
65 | + $authMechanism = $this->backendService->getAuthMechanism($storageOptions['authMechanism']); |
|
66 | + } else { |
|
67 | + $authMechanism = $backend->getLegacyAuthMechanism($storageOptions); |
|
68 | + $storageOptions['authMechanism'] = 'null'; // to make error handling easier |
|
69 | + } |
|
70 | + if (!$authMechanism) { |
|
71 | + throw new \UnexpectedValueException('Invalid authentication mechanism ' . $storageOptions['authMechanism']); |
|
72 | + } |
|
73 | + $storageConfig->setAuthMechanism($authMechanism); |
|
74 | + $storageConfig->setBackendOptions($storageOptions['options']); |
|
75 | + if (isset($storageOptions['mountOptions'])) { |
|
76 | + $storageConfig->setMountOptions($storageOptions['mountOptions']); |
|
77 | + } |
|
78 | + if (!isset($storageOptions['priority'])) { |
|
79 | + $storageOptions['priority'] = $backend->getPriority(); |
|
80 | + } |
|
81 | + $storageConfig->setPriority($storageOptions['priority']); |
|
82 | + if ($mountType === \OC_Mount_Config::MOUNT_TYPE_USER) { |
|
83 | + $applicableUsers = $storageConfig->getApplicableUsers(); |
|
84 | + if ($applicable !== 'all') { |
|
85 | + $applicableUsers[] = $applicable; |
|
86 | + $storageConfig->setApplicableUsers($applicableUsers); |
|
87 | + } |
|
88 | + } else if ($mountType === \OC_Mount_Config::MOUNT_TYPE_GROUP) { |
|
89 | + $applicableGroups = $storageConfig->getApplicableGroups(); |
|
90 | + $applicableGroups[] = $applicable; |
|
91 | + $storageConfig->setApplicableGroups($applicableGroups); |
|
92 | + } |
|
93 | + return $storageConfig; |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Read the external storages config |
|
98 | - * |
|
99 | - * @return StorageConfig[] map of storage id to storage config |
|
100 | - */ |
|
101 | - public function getAllStorages() { |
|
102 | - $mountPoints = $this->readLegacyConfig(); |
|
103 | - /** |
|
104 | - * Here is the how the horribly messy mount point array looks like |
|
105 | - * from the mount.json file: |
|
106 | - * |
|
107 | - * $storageOptions = $mountPoints[$mountType][$applicable][$mountPath] |
|
108 | - * |
|
109 | - * - $mountType is either "user" or "group" |
|
110 | - * - $applicable is the name of a user or group (or the current user for personal mounts) |
|
111 | - * - $mountPath is the mount point path (where the storage must be mounted) |
|
112 | - * - $storageOptions is a map of storage options: |
|
113 | - * - "priority": storage priority |
|
114 | - * - "backend": backend identifier |
|
115 | - * - "class": LEGACY backend class name |
|
116 | - * - "options": backend-specific options |
|
117 | - * - "authMechanism": authentication mechanism identifier |
|
118 | - * - "mountOptions": mount-specific options (ex: disable previews, scanner, etc) |
|
119 | - */ |
|
120 | - // group by storage id |
|
121 | - /** @var StorageConfig[] $storages */ |
|
122 | - $storages = []; |
|
123 | - // for storages without id (legacy), group by config hash for |
|
124 | - // later processing |
|
125 | - $storagesWithConfigHash = []; |
|
126 | - foreach ($mountPoints as $mountType => $applicables) { |
|
127 | - foreach ($applicables as $applicable => $mountPaths) { |
|
128 | - foreach ($mountPaths as $rootMountPath => $storageOptions) { |
|
129 | - $currentStorage = null; |
|
130 | - /** |
|
131 | - * Flag whether the config that was read already has an id. |
|
132 | - * If not, it will use a config hash instead and generate |
|
133 | - * a proper id later |
|
134 | - * |
|
135 | - * @var boolean |
|
136 | - */ |
|
137 | - $hasId = false; |
|
138 | - // the root mount point is in the format "/$user/files/the/mount/point" |
|
139 | - // we remove the "/$user/files" prefix |
|
140 | - $parts = explode('/', ltrim($rootMountPath, '/'), 3); |
|
141 | - if (count($parts) < 3) { |
|
142 | - // something went wrong, skip |
|
143 | - \OCP\Util::writeLog( |
|
144 | - 'files_external', |
|
145 | - 'Could not parse mount point "' . $rootMountPath . '"', |
|
146 | - \OCP\Util::ERROR |
|
147 | - ); |
|
148 | - continue; |
|
149 | - } |
|
150 | - $relativeMountPath = rtrim($parts[2], '/'); |
|
151 | - // note: we cannot do this after the loop because the decrypted config |
|
152 | - // options might be needed for the config hash |
|
153 | - $storageOptions['options'] = \OC_Mount_Config::decryptPasswords($storageOptions['options']); |
|
154 | - if (!isset($storageOptions['backend'])) { |
|
155 | - $storageOptions['backend'] = $storageOptions['class']; // legacy compat |
|
156 | - } |
|
157 | - if (!isset($storageOptions['authMechanism'])) { |
|
158 | - $storageOptions['authMechanism'] = null; // ensure config hash works |
|
159 | - } |
|
160 | - if (isset($storageOptions['id'])) { |
|
161 | - $configId = (int)$storageOptions['id']; |
|
162 | - if (isset($storages[$configId])) { |
|
163 | - $currentStorage = $storages[$configId]; |
|
164 | - } |
|
165 | - $hasId = true; |
|
166 | - } else { |
|
167 | - // missing id in legacy config, need to generate |
|
168 | - // but at this point we don't know the max-id, so use |
|
169 | - // first group it by config hash |
|
170 | - $storageOptions['mountpoint'] = $rootMountPath; |
|
171 | - $configId = \OC_Mount_Config::makeConfigHash($storageOptions); |
|
172 | - if (isset($storagesWithConfigHash[$configId])) { |
|
173 | - $currentStorage = $storagesWithConfigHash[$configId]; |
|
174 | - } |
|
175 | - } |
|
176 | - if (is_null($currentStorage)) { |
|
177 | - // create new |
|
178 | - $currentStorage = new StorageConfig($configId); |
|
179 | - $currentStorage->setMountPoint($relativeMountPath); |
|
180 | - } |
|
181 | - try { |
|
182 | - $this->populateStorageConfigWithLegacyOptions( |
|
183 | - $currentStorage, |
|
184 | - $mountType, |
|
185 | - $applicable, |
|
186 | - $storageOptions |
|
187 | - ); |
|
188 | - if ($hasId) { |
|
189 | - $storages[$configId] = $currentStorage; |
|
190 | - } else { |
|
191 | - $storagesWithConfigHash[$configId] = $currentStorage; |
|
192 | - } |
|
193 | - } catch (\UnexpectedValueException $e) { |
|
194 | - // don't die if a storage backend doesn't exist |
|
195 | - \OCP\Util::writeLog( |
|
196 | - 'files_external', |
|
197 | - 'Could not load storage: "' . $e->getMessage() . '"', |
|
198 | - \OCP\Util::ERROR |
|
199 | - ); |
|
200 | - } |
|
201 | - } |
|
202 | - } |
|
203 | - } |
|
96 | + /** |
|
97 | + * Read the external storages config |
|
98 | + * |
|
99 | + * @return StorageConfig[] map of storage id to storage config |
|
100 | + */ |
|
101 | + public function getAllStorages() { |
|
102 | + $mountPoints = $this->readLegacyConfig(); |
|
103 | + /** |
|
104 | + * Here is the how the horribly messy mount point array looks like |
|
105 | + * from the mount.json file: |
|
106 | + * |
|
107 | + * $storageOptions = $mountPoints[$mountType][$applicable][$mountPath] |
|
108 | + * |
|
109 | + * - $mountType is either "user" or "group" |
|
110 | + * - $applicable is the name of a user or group (or the current user for personal mounts) |
|
111 | + * - $mountPath is the mount point path (where the storage must be mounted) |
|
112 | + * - $storageOptions is a map of storage options: |
|
113 | + * - "priority": storage priority |
|
114 | + * - "backend": backend identifier |
|
115 | + * - "class": LEGACY backend class name |
|
116 | + * - "options": backend-specific options |
|
117 | + * - "authMechanism": authentication mechanism identifier |
|
118 | + * - "mountOptions": mount-specific options (ex: disable previews, scanner, etc) |
|
119 | + */ |
|
120 | + // group by storage id |
|
121 | + /** @var StorageConfig[] $storages */ |
|
122 | + $storages = []; |
|
123 | + // for storages without id (legacy), group by config hash for |
|
124 | + // later processing |
|
125 | + $storagesWithConfigHash = []; |
|
126 | + foreach ($mountPoints as $mountType => $applicables) { |
|
127 | + foreach ($applicables as $applicable => $mountPaths) { |
|
128 | + foreach ($mountPaths as $rootMountPath => $storageOptions) { |
|
129 | + $currentStorage = null; |
|
130 | + /** |
|
131 | + * Flag whether the config that was read already has an id. |
|
132 | + * If not, it will use a config hash instead and generate |
|
133 | + * a proper id later |
|
134 | + * |
|
135 | + * @var boolean |
|
136 | + */ |
|
137 | + $hasId = false; |
|
138 | + // the root mount point is in the format "/$user/files/the/mount/point" |
|
139 | + // we remove the "/$user/files" prefix |
|
140 | + $parts = explode('/', ltrim($rootMountPath, '/'), 3); |
|
141 | + if (count($parts) < 3) { |
|
142 | + // something went wrong, skip |
|
143 | + \OCP\Util::writeLog( |
|
144 | + 'files_external', |
|
145 | + 'Could not parse mount point "' . $rootMountPath . '"', |
|
146 | + \OCP\Util::ERROR |
|
147 | + ); |
|
148 | + continue; |
|
149 | + } |
|
150 | + $relativeMountPath = rtrim($parts[2], '/'); |
|
151 | + // note: we cannot do this after the loop because the decrypted config |
|
152 | + // options might be needed for the config hash |
|
153 | + $storageOptions['options'] = \OC_Mount_Config::decryptPasswords($storageOptions['options']); |
|
154 | + if (!isset($storageOptions['backend'])) { |
|
155 | + $storageOptions['backend'] = $storageOptions['class']; // legacy compat |
|
156 | + } |
|
157 | + if (!isset($storageOptions['authMechanism'])) { |
|
158 | + $storageOptions['authMechanism'] = null; // ensure config hash works |
|
159 | + } |
|
160 | + if (isset($storageOptions['id'])) { |
|
161 | + $configId = (int)$storageOptions['id']; |
|
162 | + if (isset($storages[$configId])) { |
|
163 | + $currentStorage = $storages[$configId]; |
|
164 | + } |
|
165 | + $hasId = true; |
|
166 | + } else { |
|
167 | + // missing id in legacy config, need to generate |
|
168 | + // but at this point we don't know the max-id, so use |
|
169 | + // first group it by config hash |
|
170 | + $storageOptions['mountpoint'] = $rootMountPath; |
|
171 | + $configId = \OC_Mount_Config::makeConfigHash($storageOptions); |
|
172 | + if (isset($storagesWithConfigHash[$configId])) { |
|
173 | + $currentStorage = $storagesWithConfigHash[$configId]; |
|
174 | + } |
|
175 | + } |
|
176 | + if (is_null($currentStorage)) { |
|
177 | + // create new |
|
178 | + $currentStorage = new StorageConfig($configId); |
|
179 | + $currentStorage->setMountPoint($relativeMountPath); |
|
180 | + } |
|
181 | + try { |
|
182 | + $this->populateStorageConfigWithLegacyOptions( |
|
183 | + $currentStorage, |
|
184 | + $mountType, |
|
185 | + $applicable, |
|
186 | + $storageOptions |
|
187 | + ); |
|
188 | + if ($hasId) { |
|
189 | + $storages[$configId] = $currentStorage; |
|
190 | + } else { |
|
191 | + $storagesWithConfigHash[$configId] = $currentStorage; |
|
192 | + } |
|
193 | + } catch (\UnexpectedValueException $e) { |
|
194 | + // don't die if a storage backend doesn't exist |
|
195 | + \OCP\Util::writeLog( |
|
196 | + 'files_external', |
|
197 | + 'Could not load storage: "' . $e->getMessage() . '"', |
|
198 | + \OCP\Util::ERROR |
|
199 | + ); |
|
200 | + } |
|
201 | + } |
|
202 | + } |
|
203 | + } |
|
204 | 204 | |
205 | - // convert parameter values |
|
206 | - foreach ($storages as $storage) { |
|
207 | - $storage->getBackend()->validateStorageDefinition($storage); |
|
208 | - $storage->getAuthMechanism()->validateStorageDefinition($storage); |
|
209 | - } |
|
210 | - return $storages; |
|
211 | - } |
|
205 | + // convert parameter values |
|
206 | + foreach ($storages as $storage) { |
|
207 | + $storage->getBackend()->validateStorageDefinition($storage); |
|
208 | + $storage->getAuthMechanism()->validateStorageDefinition($storage); |
|
209 | + } |
|
210 | + return $storages; |
|
211 | + } |
|
212 | 212 | } |
@@ -30,46 +30,46 @@ |
||
30 | 30 | */ |
31 | 31 | trait UserTrait { |
32 | 32 | |
33 | - /** @var IUserSession */ |
|
34 | - protected $userSession; |
|
33 | + /** @var IUserSession */ |
|
34 | + protected $userSession; |
|
35 | 35 | |
36 | - /** |
|
37 | - * User override |
|
38 | - * |
|
39 | - * @var IUser|null |
|
40 | - */ |
|
41 | - private $user = null; |
|
36 | + /** |
|
37 | + * User override |
|
38 | + * |
|
39 | + * @var IUser|null |
|
40 | + */ |
|
41 | + private $user = null; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @return IUser|null |
|
45 | - */ |
|
46 | - protected function getUser() { |
|
47 | - if ($this->user) { |
|
48 | - return $this->user; |
|
49 | - } |
|
50 | - return $this->userSession->getUser(); |
|
51 | - } |
|
43 | + /** |
|
44 | + * @return IUser|null |
|
45 | + */ |
|
46 | + protected function getUser() { |
|
47 | + if ($this->user) { |
|
48 | + return $this->user; |
|
49 | + } |
|
50 | + return $this->userSession->getUser(); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Override the user from the session |
|
55 | - * Unset with ->resetUser() when finished! |
|
56 | - * |
|
57 | - * @param IUser |
|
58 | - * @return self |
|
59 | - */ |
|
60 | - public function setUser(IUser $user) { |
|
61 | - $this->user = $user; |
|
62 | - return $this; |
|
63 | - } |
|
53 | + /** |
|
54 | + * Override the user from the session |
|
55 | + * Unset with ->resetUser() when finished! |
|
56 | + * |
|
57 | + * @param IUser |
|
58 | + * @return self |
|
59 | + */ |
|
60 | + public function setUser(IUser $user) { |
|
61 | + $this->user = $user; |
|
62 | + return $this; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Reset the user override |
|
67 | - * |
|
68 | - * @return self |
|
69 | - */ |
|
70 | - public function resetUser() { |
|
71 | - $this->user = null; |
|
72 | - return $this; |
|
73 | - } |
|
65 | + /** |
|
66 | + * Reset the user override |
|
67 | + * |
|
68 | + * @return self |
|
69 | + */ |
|
70 | + public function resetUser() { |
|
71 | + $this->user = null; |
|
72 | + return $this; |
|
73 | + } |
|
74 | 74 | } |
75 | 75 |
@@ -40,105 +40,105 @@ |
||
40 | 40 | * (aka personal storages) |
41 | 41 | */ |
42 | 42 | class UserStoragesService extends StoragesService { |
43 | - use UserTrait; |
|
43 | + use UserTrait; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Create a user storages service |
|
47 | - * |
|
48 | - * @param BackendService $backendService |
|
49 | - * @param DBConfigService $dbConfig |
|
50 | - * @param IUserSession $userSession user session |
|
51 | - * @param IUserMountCache $userMountCache |
|
52 | - */ |
|
53 | - public function __construct( |
|
54 | - BackendService $backendService, |
|
55 | - DBConfigService $dbConfig, |
|
56 | - IUserSession $userSession, |
|
57 | - IUserMountCache $userMountCache |
|
58 | - ) { |
|
59 | - $this->userSession = $userSession; |
|
60 | - parent::__construct($backendService, $dbConfig, $userMountCache); |
|
61 | - } |
|
45 | + /** |
|
46 | + * Create a user storages service |
|
47 | + * |
|
48 | + * @param BackendService $backendService |
|
49 | + * @param DBConfigService $dbConfig |
|
50 | + * @param IUserSession $userSession user session |
|
51 | + * @param IUserMountCache $userMountCache |
|
52 | + */ |
|
53 | + public function __construct( |
|
54 | + BackendService $backendService, |
|
55 | + DBConfigService $dbConfig, |
|
56 | + IUserSession $userSession, |
|
57 | + IUserMountCache $userMountCache |
|
58 | + ) { |
|
59 | + $this->userSession = $userSession; |
|
60 | + parent::__construct($backendService, $dbConfig, $userMountCache); |
|
61 | + } |
|
62 | 62 | |
63 | - protected function readDBConfig() { |
|
64 | - return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); |
|
65 | - } |
|
63 | + protected function readDBConfig() { |
|
64 | + return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Triggers $signal for all applicable users of the given |
|
69 | - * storage |
|
70 | - * |
|
71 | - * @param StorageConfig $storage storage data |
|
72 | - * @param string $signal signal to trigger |
|
73 | - */ |
|
74 | - protected function triggerHooks(StorageConfig $storage, $signal) { |
|
75 | - $user = $this->getUser()->getUID(); |
|
67 | + /** |
|
68 | + * Triggers $signal for all applicable users of the given |
|
69 | + * storage |
|
70 | + * |
|
71 | + * @param StorageConfig $storage storage data |
|
72 | + * @param string $signal signal to trigger |
|
73 | + */ |
|
74 | + protected function triggerHooks(StorageConfig $storage, $signal) { |
|
75 | + $user = $this->getUser()->getUID(); |
|
76 | 76 | |
77 | - // trigger hook for the current user |
|
78 | - $this->triggerApplicableHooks( |
|
79 | - $signal, |
|
80 | - $storage->getMountPoint(), |
|
81 | - \OC_Mount_Config::MOUNT_TYPE_USER, |
|
82 | - [$user] |
|
83 | - ); |
|
84 | - } |
|
77 | + // trigger hook for the current user |
|
78 | + $this->triggerApplicableHooks( |
|
79 | + $signal, |
|
80 | + $storage->getMountPoint(), |
|
81 | + \OC_Mount_Config::MOUNT_TYPE_USER, |
|
82 | + [$user] |
|
83 | + ); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Triggers signal_create_mount or signal_delete_mount to |
|
88 | - * accommodate for additions/deletions in applicableUsers |
|
89 | - * and applicableGroups fields. |
|
90 | - * |
|
91 | - * @param StorageConfig $oldStorage old storage data |
|
92 | - * @param StorageConfig $newStorage new storage data |
|
93 | - */ |
|
94 | - protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { |
|
95 | - // if mount point changed, it's like a deletion + creation |
|
96 | - if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { |
|
97 | - $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); |
|
98 | - $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
99 | - } |
|
100 | - } |
|
86 | + /** |
|
87 | + * Triggers signal_create_mount or signal_delete_mount to |
|
88 | + * accommodate for additions/deletions in applicableUsers |
|
89 | + * and applicableGroups fields. |
|
90 | + * |
|
91 | + * @param StorageConfig $oldStorage old storage data |
|
92 | + * @param StorageConfig $newStorage new storage data |
|
93 | + */ |
|
94 | + protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { |
|
95 | + // if mount point changed, it's like a deletion + creation |
|
96 | + if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { |
|
97 | + $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); |
|
98 | + $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
99 | + } |
|
100 | + } |
|
101 | 101 | |
102 | - protected function getType() { |
|
103 | - return DBConfigService::MOUNT_TYPE_PERSONAl; |
|
104 | - } |
|
102 | + protected function getType() { |
|
103 | + return DBConfigService::MOUNT_TYPE_PERSONAl; |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Add new storage to the configuration |
|
108 | - * |
|
109 | - * @param StorageConfig $newStorage storage attributes |
|
110 | - * |
|
111 | - * @return StorageConfig storage config, with added id |
|
112 | - */ |
|
113 | - public function addStorage(StorageConfig $newStorage) { |
|
114 | - $newStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
115 | - $config = parent::addStorage($newStorage); |
|
116 | - return $config; |
|
117 | - } |
|
106 | + /** |
|
107 | + * Add new storage to the configuration |
|
108 | + * |
|
109 | + * @param StorageConfig $newStorage storage attributes |
|
110 | + * |
|
111 | + * @return StorageConfig storage config, with added id |
|
112 | + */ |
|
113 | + public function addStorage(StorageConfig $newStorage) { |
|
114 | + $newStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
115 | + $config = parent::addStorage($newStorage); |
|
116 | + return $config; |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * Update storage to the configuration |
|
121 | - * |
|
122 | - * @param StorageConfig $updatedStorage storage attributes |
|
123 | - * |
|
124 | - * @return StorageConfig storage config |
|
125 | - * @throws NotFoundException if the given storage does not exist in the config |
|
126 | - */ |
|
127 | - public function updateStorage(StorageConfig $updatedStorage) { |
|
128 | - $updatedStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
129 | - return parent::updateStorage($updatedStorage); |
|
130 | - } |
|
119 | + /** |
|
120 | + * Update storage to the configuration |
|
121 | + * |
|
122 | + * @param StorageConfig $updatedStorage storage attributes |
|
123 | + * |
|
124 | + * @return StorageConfig storage config |
|
125 | + * @throws NotFoundException if the given storage does not exist in the config |
|
126 | + */ |
|
127 | + public function updateStorage(StorageConfig $updatedStorage) { |
|
128 | + $updatedStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
129 | + return parent::updateStorage($updatedStorage); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Get the visibility type for this controller, used in validation |
|
134 | - * |
|
135 | - * @return string BackendService::VISIBILITY_* constants |
|
136 | - */ |
|
137 | - public function getVisibilityType() { |
|
138 | - return BackendService::VISIBILITY_PERSONAL; |
|
139 | - } |
|
132 | + /** |
|
133 | + * Get the visibility type for this controller, used in validation |
|
134 | + * |
|
135 | + * @return string BackendService::VISIBILITY_* constants |
|
136 | + */ |
|
137 | + public function getVisibilityType() { |
|
138 | + return BackendService::VISIBILITY_PERSONAL; |
|
139 | + } |
|
140 | 140 | |
141 | - protected function isApplicable(StorageConfig $config) { |
|
142 | - return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === StorageConfig::MOUNT_TYPE_PERSONAl; |
|
143 | - } |
|
141 | + protected function isApplicable(StorageConfig $config) { |
|
142 | + return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === StorageConfig::MOUNT_TYPE_PERSONAl; |
|
143 | + } |
|
144 | 144 | } |
@@ -45,143 +45,143 @@ |
||
45 | 45 | */ |
46 | 46 | class ConfigAdapter implements IMountProvider { |
47 | 47 | |
48 | - /** @var UserStoragesService */ |
|
49 | - private $userStoragesService; |
|
50 | - |
|
51 | - /** @var UserGlobalStoragesService */ |
|
52 | - private $userGlobalStoragesService; |
|
53 | - /** @var StorageMigrator */ |
|
54 | - private $migrator; |
|
55 | - |
|
56 | - /** |
|
57 | - * @param UserStoragesService $userStoragesService |
|
58 | - * @param UserGlobalStoragesService $userGlobalStoragesService |
|
59 | - * @param StorageMigrator $migrator |
|
60 | - */ |
|
61 | - public function __construct( |
|
62 | - UserStoragesService $userStoragesService, |
|
63 | - UserGlobalStoragesService $userGlobalStoragesService, |
|
64 | - StorageMigrator $migrator |
|
65 | - ) { |
|
66 | - $this->userStoragesService = $userStoragesService; |
|
67 | - $this->userGlobalStoragesService = $userGlobalStoragesService; |
|
68 | - $this->migrator = $migrator; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Process storage ready for mounting |
|
73 | - * |
|
74 | - * @param StorageConfig $storage |
|
75 | - * @param IUser $user |
|
76 | - */ |
|
77 | - private function prepareStorageConfig(StorageConfig &$storage, IUser $user) { |
|
78 | - foreach ($storage->getBackendOptions() as $option => $value) { |
|
79 | - $storage->setBackendOption($option, \OC_Mount_Config::setUserVars( |
|
80 | - $user->getUID(), $value |
|
81 | - )); |
|
82 | - } |
|
83 | - |
|
84 | - $objectStore = $storage->getBackendOption('objectstore'); |
|
85 | - if ($objectStore) { |
|
86 | - $objectClass = $objectStore['class']; |
|
87 | - if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) { |
|
88 | - throw new \InvalidArgumentException('Invalid object store'); |
|
89 | - } |
|
90 | - $storage->setBackendOption('objectstore', new $objectClass($objectStore)); |
|
91 | - } |
|
92 | - |
|
93 | - $storage->getAuthMechanism()->manipulateStorageConfig($storage, $user); |
|
94 | - $storage->getBackend()->manipulateStorageConfig($storage, $user); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Construct the storage implementation |
|
99 | - * |
|
100 | - * @param StorageConfig $storageConfig |
|
101 | - * @return Storage |
|
102 | - */ |
|
103 | - private function constructStorage(StorageConfig $storageConfig) { |
|
104 | - $class = $storageConfig->getBackend()->getStorageClass(); |
|
105 | - $storage = new $class($storageConfig->getBackendOptions()); |
|
106 | - |
|
107 | - // auth mechanism should fire first |
|
108 | - $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
109 | - $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
110 | - |
|
111 | - return $storage; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Get all mountpoints applicable for the user |
|
116 | - * |
|
117 | - * @param \OCP\IUser $user |
|
118 | - * @param \OCP\Files\Storage\IStorageFactory $loader |
|
119 | - * @return \OCP\Files\Mount\IMountPoint[] |
|
120 | - */ |
|
121 | - public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
|
122 | - $this->migrator->migrateUser($user); |
|
123 | - |
|
124 | - $this->userStoragesService->setUser($user); |
|
125 | - $this->userGlobalStoragesService->setUser($user); |
|
126 | - |
|
127 | - $storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser(); |
|
128 | - |
|
129 | - $storages = array_map(function(StorageConfig $storageConfig) use ($user) { |
|
130 | - try { |
|
131 | - $this->prepareStorageConfig($storageConfig, $user); |
|
132 | - return $this->constructStorage($storageConfig); |
|
133 | - } catch (\Exception $e) { |
|
134 | - // propagate exception into filesystem |
|
135 | - return new FailedStorage(['exception' => $e]); |
|
136 | - } |
|
137 | - }, $storageConfigs); |
|
138 | - |
|
139 | - |
|
140 | - \OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function(Storage\IStorage $storage) { |
|
141 | - return $storage->getId(); |
|
142 | - }, $storages)); |
|
143 | - |
|
144 | - $availableStorages = array_map(function (Storage\IStorage $storage, StorageConfig $storageConfig) { |
|
145 | - try { |
|
146 | - $availability = $storage->getAvailability(); |
|
147 | - if (!$availability['available'] && !Availability::shouldRecheck($availability)) { |
|
148 | - $storage = new FailedStorage([ |
|
149 | - 'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available') |
|
150 | - ]); |
|
151 | - } |
|
152 | - } catch (\Exception $e) { |
|
153 | - // propagate exception into filesystem |
|
154 | - $storage = new FailedStorage(['exception' => $e]); |
|
155 | - } |
|
156 | - return $storage; |
|
157 | - }, $storages, $storageConfigs); |
|
158 | - |
|
159 | - $mounts = array_map(function(StorageConfig $storageConfig, Storage\IStorage $storage) use ($user, $loader) { |
|
160 | - if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { |
|
161 | - return new PersonalMount( |
|
162 | - $this->userStoragesService, |
|
163 | - $storageConfig->getId(), |
|
164 | - $storage, |
|
165 | - '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), |
|
166 | - null, |
|
167 | - $loader, |
|
168 | - $storageConfig->getMountOptions() |
|
169 | - ); |
|
170 | - } else { |
|
171 | - return new MountPoint( |
|
172 | - $storage, |
|
173 | - '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), |
|
174 | - null, |
|
175 | - $loader, |
|
176 | - $storageConfig->getMountOptions(), |
|
177 | - $storageConfig->getId() |
|
178 | - ); |
|
179 | - } |
|
180 | - }, $storageConfigs, $availableStorages); |
|
181 | - |
|
182 | - $this->userStoragesService->resetUser(); |
|
183 | - $this->userGlobalStoragesService->resetUser(); |
|
184 | - |
|
185 | - return $mounts; |
|
186 | - } |
|
48 | + /** @var UserStoragesService */ |
|
49 | + private $userStoragesService; |
|
50 | + |
|
51 | + /** @var UserGlobalStoragesService */ |
|
52 | + private $userGlobalStoragesService; |
|
53 | + /** @var StorageMigrator */ |
|
54 | + private $migrator; |
|
55 | + |
|
56 | + /** |
|
57 | + * @param UserStoragesService $userStoragesService |
|
58 | + * @param UserGlobalStoragesService $userGlobalStoragesService |
|
59 | + * @param StorageMigrator $migrator |
|
60 | + */ |
|
61 | + public function __construct( |
|
62 | + UserStoragesService $userStoragesService, |
|
63 | + UserGlobalStoragesService $userGlobalStoragesService, |
|
64 | + StorageMigrator $migrator |
|
65 | + ) { |
|
66 | + $this->userStoragesService = $userStoragesService; |
|
67 | + $this->userGlobalStoragesService = $userGlobalStoragesService; |
|
68 | + $this->migrator = $migrator; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Process storage ready for mounting |
|
73 | + * |
|
74 | + * @param StorageConfig $storage |
|
75 | + * @param IUser $user |
|
76 | + */ |
|
77 | + private function prepareStorageConfig(StorageConfig &$storage, IUser $user) { |
|
78 | + foreach ($storage->getBackendOptions() as $option => $value) { |
|
79 | + $storage->setBackendOption($option, \OC_Mount_Config::setUserVars( |
|
80 | + $user->getUID(), $value |
|
81 | + )); |
|
82 | + } |
|
83 | + |
|
84 | + $objectStore = $storage->getBackendOption('objectstore'); |
|
85 | + if ($objectStore) { |
|
86 | + $objectClass = $objectStore['class']; |
|
87 | + if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) { |
|
88 | + throw new \InvalidArgumentException('Invalid object store'); |
|
89 | + } |
|
90 | + $storage->setBackendOption('objectstore', new $objectClass($objectStore)); |
|
91 | + } |
|
92 | + |
|
93 | + $storage->getAuthMechanism()->manipulateStorageConfig($storage, $user); |
|
94 | + $storage->getBackend()->manipulateStorageConfig($storage, $user); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Construct the storage implementation |
|
99 | + * |
|
100 | + * @param StorageConfig $storageConfig |
|
101 | + * @return Storage |
|
102 | + */ |
|
103 | + private function constructStorage(StorageConfig $storageConfig) { |
|
104 | + $class = $storageConfig->getBackend()->getStorageClass(); |
|
105 | + $storage = new $class($storageConfig->getBackendOptions()); |
|
106 | + |
|
107 | + // auth mechanism should fire first |
|
108 | + $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
109 | + $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
110 | + |
|
111 | + return $storage; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Get all mountpoints applicable for the user |
|
116 | + * |
|
117 | + * @param \OCP\IUser $user |
|
118 | + * @param \OCP\Files\Storage\IStorageFactory $loader |
|
119 | + * @return \OCP\Files\Mount\IMountPoint[] |
|
120 | + */ |
|
121 | + public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
|
122 | + $this->migrator->migrateUser($user); |
|
123 | + |
|
124 | + $this->userStoragesService->setUser($user); |
|
125 | + $this->userGlobalStoragesService->setUser($user); |
|
126 | + |
|
127 | + $storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser(); |
|
128 | + |
|
129 | + $storages = array_map(function(StorageConfig $storageConfig) use ($user) { |
|
130 | + try { |
|
131 | + $this->prepareStorageConfig($storageConfig, $user); |
|
132 | + return $this->constructStorage($storageConfig); |
|
133 | + } catch (\Exception $e) { |
|
134 | + // propagate exception into filesystem |
|
135 | + return new FailedStorage(['exception' => $e]); |
|
136 | + } |
|
137 | + }, $storageConfigs); |
|
138 | + |
|
139 | + |
|
140 | + \OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function(Storage\IStorage $storage) { |
|
141 | + return $storage->getId(); |
|
142 | + }, $storages)); |
|
143 | + |
|
144 | + $availableStorages = array_map(function (Storage\IStorage $storage, StorageConfig $storageConfig) { |
|
145 | + try { |
|
146 | + $availability = $storage->getAvailability(); |
|
147 | + if (!$availability['available'] && !Availability::shouldRecheck($availability)) { |
|
148 | + $storage = new FailedStorage([ |
|
149 | + 'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available') |
|
150 | + ]); |
|
151 | + } |
|
152 | + } catch (\Exception $e) { |
|
153 | + // propagate exception into filesystem |
|
154 | + $storage = new FailedStorage(['exception' => $e]); |
|
155 | + } |
|
156 | + return $storage; |
|
157 | + }, $storages, $storageConfigs); |
|
158 | + |
|
159 | + $mounts = array_map(function(StorageConfig $storageConfig, Storage\IStorage $storage) use ($user, $loader) { |
|
160 | + if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { |
|
161 | + return new PersonalMount( |
|
162 | + $this->userStoragesService, |
|
163 | + $storageConfig->getId(), |
|
164 | + $storage, |
|
165 | + '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), |
|
166 | + null, |
|
167 | + $loader, |
|
168 | + $storageConfig->getMountOptions() |
|
169 | + ); |
|
170 | + } else { |
|
171 | + return new MountPoint( |
|
172 | + $storage, |
|
173 | + '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), |
|
174 | + null, |
|
175 | + $loader, |
|
176 | + $storageConfig->getMountOptions(), |
|
177 | + $storageConfig->getId() |
|
178 | + ); |
|
179 | + } |
|
180 | + }, $storageConfigs, $availableStorages); |
|
181 | + |
|
182 | + $this->userStoragesService->resetUser(); |
|
183 | + $this->userGlobalStoragesService->resetUser(); |
|
184 | + |
|
185 | + return $mounts; |
|
186 | + } |
|
187 | 187 | } |