@@ -27,171 +27,171 @@ |
||
27 | 27 | * Parameter for an external storage definition |
28 | 28 | */ |
29 | 29 | class DefinitionParameter implements \JsonSerializable { |
30 | - // placeholder value for password fields, when the client updates a storage configuration |
|
31 | - // placeholder values are ignored and the field is left unmodified |
|
32 | - const UNMODIFIED_PLACEHOLDER = '__unmodified__'; |
|
33 | - |
|
34 | - /** Value constants */ |
|
35 | - const VALUE_TEXT = 0; |
|
36 | - const VALUE_BOOLEAN = 1; |
|
37 | - const VALUE_PASSWORD = 2; |
|
38 | - const VALUE_HIDDEN = 3; |
|
39 | - |
|
40 | - /** Flag constants */ |
|
41 | - const FLAG_NONE = 0; |
|
42 | - const FLAG_OPTIONAL = 1; |
|
43 | - const FLAG_USER_PROVIDED = 2; |
|
44 | - |
|
45 | - /** @var string name of parameter */ |
|
46 | - private $name; |
|
47 | - |
|
48 | - /** @var string human-readable parameter text */ |
|
49 | - private $text; |
|
50 | - |
|
51 | - /** @var int value type, see self::VALUE_* constants */ |
|
52 | - private $type = self::VALUE_TEXT; |
|
53 | - |
|
54 | - /** @var int flags, see self::FLAG_* constants */ |
|
55 | - private $flags = self::FLAG_NONE; |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $name |
|
59 | - * @param string $text |
|
60 | - */ |
|
61 | - public function __construct($name, $text) { |
|
62 | - $this->name = $name; |
|
63 | - $this->text = $text; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function getName() { |
|
70 | - return $this->name; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @return string |
|
75 | - */ |
|
76 | - public function getText() { |
|
77 | - return $this->text; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Get value type |
|
82 | - * |
|
83 | - * @return int |
|
84 | - */ |
|
85 | - public function getType() { |
|
86 | - return $this->type; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Set value type |
|
91 | - * |
|
92 | - * @param int $type |
|
93 | - * @return self |
|
94 | - */ |
|
95 | - public function setType($type) { |
|
96 | - $this->type = $type; |
|
97 | - return $this; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - public function getTypeName() { |
|
104 | - switch ($this->type) { |
|
105 | - case self::VALUE_BOOLEAN: |
|
106 | - return 'boolean'; |
|
107 | - case self::VALUE_TEXT: |
|
108 | - return 'text'; |
|
109 | - case self::VALUE_PASSWORD: |
|
110 | - return 'password'; |
|
111 | - default: |
|
112 | - return 'unknown'; |
|
113 | - } |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * @return int |
|
118 | - */ |
|
119 | - public function getFlags() { |
|
120 | - return $this->flags; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * @param int $flags |
|
125 | - * @return self |
|
126 | - */ |
|
127 | - public function setFlags($flags) { |
|
128 | - $this->flags = $flags; |
|
129 | - return $this; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @param int $flag |
|
134 | - * @return self |
|
135 | - */ |
|
136 | - public function setFlag($flag) { |
|
137 | - $this->flags |= $flag; |
|
138 | - return $this; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @param int $flag |
|
143 | - * @return bool |
|
144 | - */ |
|
145 | - public function isFlagSet($flag) { |
|
146 | - return (bool)($this->flags & $flag); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Serialize into JSON for client-side JS |
|
151 | - * |
|
152 | - * @return string |
|
153 | - */ |
|
154 | - public function jsonSerialize() { |
|
155 | - return [ |
|
156 | - 'value' => $this->getText(), |
|
157 | - 'flags' => $this->getFlags(), |
|
158 | - 'type' => $this->getType() |
|
159 | - ]; |
|
160 | - } |
|
161 | - |
|
162 | - public function isOptional() { |
|
163 | - return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Validate a parameter value against this |
|
168 | - * Convert type as necessary |
|
169 | - * |
|
170 | - * @param mixed $value Value to check |
|
171 | - * @return bool success |
|
172 | - */ |
|
173 | - public function validateValue(&$value) { |
|
174 | - switch ($this->getType()) { |
|
175 | - case self::VALUE_BOOLEAN: |
|
176 | - if (!is_bool($value)) { |
|
177 | - switch ($value) { |
|
178 | - case 'true': |
|
179 | - $value = true; |
|
180 | - break; |
|
181 | - case 'false': |
|
182 | - $value = false; |
|
183 | - break; |
|
184 | - default: |
|
185 | - return false; |
|
186 | - } |
|
187 | - } |
|
188 | - break; |
|
189 | - default: |
|
190 | - if (!$value && !$this->isOptional()) { |
|
191 | - return false; |
|
192 | - } |
|
193 | - break; |
|
194 | - } |
|
195 | - return true; |
|
196 | - } |
|
30 | + // placeholder value for password fields, when the client updates a storage configuration |
|
31 | + // placeholder values are ignored and the field is left unmodified |
|
32 | + const UNMODIFIED_PLACEHOLDER = '__unmodified__'; |
|
33 | + |
|
34 | + /** Value constants */ |
|
35 | + const VALUE_TEXT = 0; |
|
36 | + const VALUE_BOOLEAN = 1; |
|
37 | + const VALUE_PASSWORD = 2; |
|
38 | + const VALUE_HIDDEN = 3; |
|
39 | + |
|
40 | + /** Flag constants */ |
|
41 | + const FLAG_NONE = 0; |
|
42 | + const FLAG_OPTIONAL = 1; |
|
43 | + const FLAG_USER_PROVIDED = 2; |
|
44 | + |
|
45 | + /** @var string name of parameter */ |
|
46 | + private $name; |
|
47 | + |
|
48 | + /** @var string human-readable parameter text */ |
|
49 | + private $text; |
|
50 | + |
|
51 | + /** @var int value type, see self::VALUE_* constants */ |
|
52 | + private $type = self::VALUE_TEXT; |
|
53 | + |
|
54 | + /** @var int flags, see self::FLAG_* constants */ |
|
55 | + private $flags = self::FLAG_NONE; |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $name |
|
59 | + * @param string $text |
|
60 | + */ |
|
61 | + public function __construct($name, $text) { |
|
62 | + $this->name = $name; |
|
63 | + $this->text = $text; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function getName() { |
|
70 | + return $this->name; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @return string |
|
75 | + */ |
|
76 | + public function getText() { |
|
77 | + return $this->text; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Get value type |
|
82 | + * |
|
83 | + * @return int |
|
84 | + */ |
|
85 | + public function getType() { |
|
86 | + return $this->type; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Set value type |
|
91 | + * |
|
92 | + * @param int $type |
|
93 | + * @return self |
|
94 | + */ |
|
95 | + public function setType($type) { |
|
96 | + $this->type = $type; |
|
97 | + return $this; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + public function getTypeName() { |
|
104 | + switch ($this->type) { |
|
105 | + case self::VALUE_BOOLEAN: |
|
106 | + return 'boolean'; |
|
107 | + case self::VALUE_TEXT: |
|
108 | + return 'text'; |
|
109 | + case self::VALUE_PASSWORD: |
|
110 | + return 'password'; |
|
111 | + default: |
|
112 | + return 'unknown'; |
|
113 | + } |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * @return int |
|
118 | + */ |
|
119 | + public function getFlags() { |
|
120 | + return $this->flags; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * @param int $flags |
|
125 | + * @return self |
|
126 | + */ |
|
127 | + public function setFlags($flags) { |
|
128 | + $this->flags = $flags; |
|
129 | + return $this; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @param int $flag |
|
134 | + * @return self |
|
135 | + */ |
|
136 | + public function setFlag($flag) { |
|
137 | + $this->flags |= $flag; |
|
138 | + return $this; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @param int $flag |
|
143 | + * @return bool |
|
144 | + */ |
|
145 | + public function isFlagSet($flag) { |
|
146 | + return (bool)($this->flags & $flag); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Serialize into JSON for client-side JS |
|
151 | + * |
|
152 | + * @return string |
|
153 | + */ |
|
154 | + public function jsonSerialize() { |
|
155 | + return [ |
|
156 | + 'value' => $this->getText(), |
|
157 | + 'flags' => $this->getFlags(), |
|
158 | + 'type' => $this->getType() |
|
159 | + ]; |
|
160 | + } |
|
161 | + |
|
162 | + public function isOptional() { |
|
163 | + return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Validate a parameter value against this |
|
168 | + * Convert type as necessary |
|
169 | + * |
|
170 | + * @param mixed $value Value to check |
|
171 | + * @return bool success |
|
172 | + */ |
|
173 | + public function validateValue(&$value) { |
|
174 | + switch ($this->getType()) { |
|
175 | + case self::VALUE_BOOLEAN: |
|
176 | + if (!is_bool($value)) { |
|
177 | + switch ($value) { |
|
178 | + case 'true': |
|
179 | + $value = true; |
|
180 | + break; |
|
181 | + case 'false': |
|
182 | + $value = false; |
|
183 | + break; |
|
184 | + default: |
|
185 | + return false; |
|
186 | + } |
|
187 | + } |
|
188 | + break; |
|
189 | + default: |
|
190 | + if (!$value && !$this->isOptional()) { |
|
191 | + return false; |
|
192 | + } |
|
193 | + break; |
|
194 | + } |
|
195 | + return true; |
|
196 | + } |
|
197 | 197 | } |
@@ -51,72 +51,72 @@ |
||
51 | 51 | * Object can affect storage mounting |
52 | 52 | */ |
53 | 53 | class AuthMechanism implements \JsonSerializable { |
54 | - /** Standard authentication schemes */ |
|
55 | - const SCHEME_NULL = 'null'; |
|
56 | - const SCHEME_BUILTIN = 'builtin'; |
|
57 | - const SCHEME_PASSWORD = 'password'; |
|
58 | - const SCHEME_OAUTH1 = 'oauth1'; |
|
59 | - const SCHEME_OAUTH2 = 'oauth2'; |
|
60 | - const SCHEME_PUBLICKEY = 'publickey'; |
|
61 | - const SCHEME_OPENSTACK = 'openstack'; |
|
62 | - const SCHEME_SMB = 'smb'; |
|
54 | + /** Standard authentication schemes */ |
|
55 | + const SCHEME_NULL = 'null'; |
|
56 | + const SCHEME_BUILTIN = 'builtin'; |
|
57 | + const SCHEME_PASSWORD = 'password'; |
|
58 | + const SCHEME_OAUTH1 = 'oauth1'; |
|
59 | + const SCHEME_OAUTH2 = 'oauth2'; |
|
60 | + const SCHEME_PUBLICKEY = 'publickey'; |
|
61 | + const SCHEME_OPENSTACK = 'openstack'; |
|
62 | + const SCHEME_SMB = 'smb'; |
|
63 | 63 | |
64 | - use VisibilityTrait; |
|
65 | - use FrontendDefinitionTrait; |
|
66 | - use StorageModifierTrait; |
|
67 | - use IdentifierTrait; |
|
64 | + use VisibilityTrait; |
|
65 | + use FrontendDefinitionTrait; |
|
66 | + use StorageModifierTrait; |
|
67 | + use IdentifierTrait; |
|
68 | 68 | |
69 | - /** @var string */ |
|
70 | - protected $scheme; |
|
69 | + /** @var string */ |
|
70 | + protected $scheme; |
|
71 | 71 | |
72 | - /** |
|
73 | - * Get the authentication scheme implemented |
|
74 | - * See self::SCHEME_* constants |
|
75 | - * |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - public function getScheme() { |
|
79 | - return $this->scheme; |
|
80 | - } |
|
72 | + /** |
|
73 | + * Get the authentication scheme implemented |
|
74 | + * See self::SCHEME_* constants |
|
75 | + * |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + public function getScheme() { |
|
79 | + return $this->scheme; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @param string $scheme |
|
84 | - * @return $this |
|
85 | - */ |
|
86 | - public function setScheme($scheme) { |
|
87 | - $this->scheme = $scheme; |
|
88 | - return $this; |
|
89 | - } |
|
82 | + /** |
|
83 | + * @param string $scheme |
|
84 | + * @return $this |
|
85 | + */ |
|
86 | + public function setScheme($scheme) { |
|
87 | + $this->scheme = $scheme; |
|
88 | + return $this; |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * Serialize into JSON for client-side JS |
|
93 | - * |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - public function jsonSerialize() { |
|
97 | - $data = $this->jsonSerializeDefinition(); |
|
98 | - $data += $this->jsonSerializeIdentifier(); |
|
91 | + /** |
|
92 | + * Serialize into JSON for client-side JS |
|
93 | + * |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + public function jsonSerialize() { |
|
97 | + $data = $this->jsonSerializeDefinition(); |
|
98 | + $data += $this->jsonSerializeIdentifier(); |
|
99 | 99 | |
100 | - $data['scheme'] = $this->getScheme(); |
|
101 | - $data['visibility'] = $this->getVisibility(); |
|
100 | + $data['scheme'] = $this->getScheme(); |
|
101 | + $data['visibility'] = $this->getVisibility(); |
|
102 | 102 | |
103 | - return $data; |
|
104 | - } |
|
103 | + return $data; |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Check if parameters are satisfied in a StorageConfig |
|
108 | - * |
|
109 | - * @param StorageConfig $storage |
|
110 | - * @return bool |
|
111 | - */ |
|
112 | - public function validateStorage(StorageConfig $storage) { |
|
113 | - // does the backend actually support this scheme |
|
114 | - $supportedSchemes = $storage->getBackend()->getAuthSchemes(); |
|
115 | - if (!isset($supportedSchemes[$this->getScheme()])) { |
|
116 | - return false; |
|
117 | - } |
|
106 | + /** |
|
107 | + * Check if parameters are satisfied in a StorageConfig |
|
108 | + * |
|
109 | + * @param StorageConfig $storage |
|
110 | + * @return bool |
|
111 | + */ |
|
112 | + public function validateStorage(StorageConfig $storage) { |
|
113 | + // does the backend actually support this scheme |
|
114 | + $supportedSchemes = $storage->getBackend()->getAuthSchemes(); |
|
115 | + if (!isset($supportedSchemes[$this->getScheme()])) { |
|
116 | + return false; |
|
117 | + } |
|
118 | 118 | |
119 | - return $this->validateStorageDefinition($storage); |
|
120 | - } |
|
119 | + return $this->validateStorageDefinition($storage); |
|
120 | + } |
|
121 | 121 | |
122 | 122 | } |
@@ -49,320 +49,320 @@ |
||
49 | 49 | */ |
50 | 50 | abstract class StoragesController extends Controller { |
51 | 51 | |
52 | - /** |
|
53 | - * L10N service |
|
54 | - * |
|
55 | - * @var IL10N |
|
56 | - */ |
|
57 | - protected $l10n; |
|
52 | + /** |
|
53 | + * L10N service |
|
54 | + * |
|
55 | + * @var IL10N |
|
56 | + */ |
|
57 | + protected $l10n; |
|
58 | 58 | |
59 | - /** |
|
60 | - * Storages service |
|
61 | - * |
|
62 | - * @var StoragesService |
|
63 | - */ |
|
64 | - protected $service; |
|
59 | + /** |
|
60 | + * Storages service |
|
61 | + * |
|
62 | + * @var StoragesService |
|
63 | + */ |
|
64 | + protected $service; |
|
65 | 65 | |
66 | - /** |
|
67 | - * @var ILogger |
|
68 | - */ |
|
69 | - protected $logger; |
|
66 | + /** |
|
67 | + * @var ILogger |
|
68 | + */ |
|
69 | + protected $logger; |
|
70 | 70 | |
71 | - /** |
|
72 | - * Creates a new storages controller. |
|
73 | - * |
|
74 | - * @param string $AppName application name |
|
75 | - * @param IRequest $request request object |
|
76 | - * @param IL10N $l10n l10n service |
|
77 | - * @param StoragesService $storagesService storage service |
|
78 | - * @param ILogger $logger |
|
79 | - */ |
|
80 | - public function __construct( |
|
81 | - $AppName, |
|
82 | - IRequest $request, |
|
83 | - IL10N $l10n, |
|
84 | - StoragesService $storagesService, |
|
85 | - ILogger $logger |
|
86 | - ) { |
|
87 | - parent::__construct($AppName, $request); |
|
88 | - $this->l10n = $l10n; |
|
89 | - $this->service = $storagesService; |
|
90 | - $this->logger = $logger; |
|
91 | - } |
|
71 | + /** |
|
72 | + * Creates a new storages controller. |
|
73 | + * |
|
74 | + * @param string $AppName application name |
|
75 | + * @param IRequest $request request object |
|
76 | + * @param IL10N $l10n l10n service |
|
77 | + * @param StoragesService $storagesService storage service |
|
78 | + * @param ILogger $logger |
|
79 | + */ |
|
80 | + public function __construct( |
|
81 | + $AppName, |
|
82 | + IRequest $request, |
|
83 | + IL10N $l10n, |
|
84 | + StoragesService $storagesService, |
|
85 | + ILogger $logger |
|
86 | + ) { |
|
87 | + parent::__construct($AppName, $request); |
|
88 | + $this->l10n = $l10n; |
|
89 | + $this->service = $storagesService; |
|
90 | + $this->logger = $logger; |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Create a storage from its parameters |
|
95 | - * |
|
96 | - * @param string $mountPoint storage mount point |
|
97 | - * @param string $backend backend identifier |
|
98 | - * @param string $authMechanism authentication mechanism identifier |
|
99 | - * @param array $backendOptions backend-specific options |
|
100 | - * @param array|null $mountOptions mount-specific options |
|
101 | - * @param array|null $applicableUsers users for which to mount the storage |
|
102 | - * @param array|null $applicableGroups groups for which to mount the storage |
|
103 | - * @param int|null $priority priority |
|
104 | - * |
|
105 | - * @return StorageConfig|DataResponse |
|
106 | - */ |
|
107 | - protected function createStorage( |
|
108 | - $mountPoint, |
|
109 | - $backend, |
|
110 | - $authMechanism, |
|
111 | - $backendOptions, |
|
112 | - $mountOptions = null, |
|
113 | - $applicableUsers = null, |
|
114 | - $applicableGroups = null, |
|
115 | - $priority = null |
|
116 | - ) { |
|
117 | - try { |
|
118 | - return $this->service->createStorage( |
|
119 | - $mountPoint, |
|
120 | - $backend, |
|
121 | - $authMechanism, |
|
122 | - $backendOptions, |
|
123 | - $mountOptions, |
|
124 | - $applicableUsers, |
|
125 | - $applicableGroups, |
|
126 | - $priority |
|
127 | - ); |
|
128 | - } catch (\InvalidArgumentException $e) { |
|
129 | - $this->logger->logException($e); |
|
130 | - return new DataResponse( |
|
131 | - [ |
|
132 | - 'message' => (string)$this->l10n->t('Invalid backend or authentication mechanism class') |
|
133 | - ], |
|
134 | - Http::STATUS_UNPROCESSABLE_ENTITY |
|
135 | - ); |
|
136 | - } |
|
137 | - } |
|
93 | + /** |
|
94 | + * Create a storage from its parameters |
|
95 | + * |
|
96 | + * @param string $mountPoint storage mount point |
|
97 | + * @param string $backend backend identifier |
|
98 | + * @param string $authMechanism authentication mechanism identifier |
|
99 | + * @param array $backendOptions backend-specific options |
|
100 | + * @param array|null $mountOptions mount-specific options |
|
101 | + * @param array|null $applicableUsers users for which to mount the storage |
|
102 | + * @param array|null $applicableGroups groups for which to mount the storage |
|
103 | + * @param int|null $priority priority |
|
104 | + * |
|
105 | + * @return StorageConfig|DataResponse |
|
106 | + */ |
|
107 | + protected function createStorage( |
|
108 | + $mountPoint, |
|
109 | + $backend, |
|
110 | + $authMechanism, |
|
111 | + $backendOptions, |
|
112 | + $mountOptions = null, |
|
113 | + $applicableUsers = null, |
|
114 | + $applicableGroups = null, |
|
115 | + $priority = null |
|
116 | + ) { |
|
117 | + try { |
|
118 | + return $this->service->createStorage( |
|
119 | + $mountPoint, |
|
120 | + $backend, |
|
121 | + $authMechanism, |
|
122 | + $backendOptions, |
|
123 | + $mountOptions, |
|
124 | + $applicableUsers, |
|
125 | + $applicableGroups, |
|
126 | + $priority |
|
127 | + ); |
|
128 | + } catch (\InvalidArgumentException $e) { |
|
129 | + $this->logger->logException($e); |
|
130 | + return new DataResponse( |
|
131 | + [ |
|
132 | + 'message' => (string)$this->l10n->t('Invalid backend or authentication mechanism class') |
|
133 | + ], |
|
134 | + Http::STATUS_UNPROCESSABLE_ENTITY |
|
135 | + ); |
|
136 | + } |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * Validate storage config |
|
141 | - * |
|
142 | - * @param StorageConfig $storage storage config |
|
143 | - *1 |
|
144 | - * @return DataResponse|null returns response in case of validation error |
|
145 | - */ |
|
146 | - protected function validate(StorageConfig $storage) { |
|
147 | - $mountPoint = $storage->getMountPoint(); |
|
148 | - if ($mountPoint === '') { |
|
149 | - return new DataResponse( |
|
150 | - [ |
|
151 | - 'message' => (string)$this->l10n->t('Invalid mount point'), |
|
152 | - ], |
|
153 | - Http::STATUS_UNPROCESSABLE_ENTITY |
|
154 | - ); |
|
155 | - } |
|
139 | + /** |
|
140 | + * Validate storage config |
|
141 | + * |
|
142 | + * @param StorageConfig $storage storage config |
|
143 | + *1 |
|
144 | + * @return DataResponse|null returns response in case of validation error |
|
145 | + */ |
|
146 | + protected function validate(StorageConfig $storage) { |
|
147 | + $mountPoint = $storage->getMountPoint(); |
|
148 | + if ($mountPoint === '') { |
|
149 | + return new DataResponse( |
|
150 | + [ |
|
151 | + 'message' => (string)$this->l10n->t('Invalid mount point'), |
|
152 | + ], |
|
153 | + Http::STATUS_UNPROCESSABLE_ENTITY |
|
154 | + ); |
|
155 | + } |
|
156 | 156 | |
157 | - if ($storage->getBackendOption('objectstore')) { |
|
158 | - // objectstore must not be sent from client side |
|
159 | - return new DataResponse( |
|
160 | - [ |
|
161 | - 'message' => (string)$this->l10n->t('Objectstore forbidden'), |
|
162 | - ], |
|
163 | - Http::STATUS_UNPROCESSABLE_ENTITY |
|
164 | - ); |
|
165 | - } |
|
157 | + if ($storage->getBackendOption('objectstore')) { |
|
158 | + // objectstore must not be sent from client side |
|
159 | + return new DataResponse( |
|
160 | + [ |
|
161 | + 'message' => (string)$this->l10n->t('Objectstore forbidden'), |
|
162 | + ], |
|
163 | + Http::STATUS_UNPROCESSABLE_ENTITY |
|
164 | + ); |
|
165 | + } |
|
166 | 166 | |
167 | - /** @var Backend */ |
|
168 | - $backend = $storage->getBackend(); |
|
169 | - /** @var AuthMechanism */ |
|
170 | - $authMechanism = $storage->getAuthMechanism(); |
|
171 | - if ($backend->checkDependencies()) { |
|
172 | - // invalid backend |
|
173 | - return new DataResponse( |
|
174 | - [ |
|
175 | - 'message' => (string)$this->l10n->t('Invalid storage backend "%s"', [ |
|
176 | - $backend->getIdentifier(), |
|
177 | - ]), |
|
178 | - ], |
|
179 | - Http::STATUS_UNPROCESSABLE_ENTITY |
|
180 | - ); |
|
181 | - } |
|
167 | + /** @var Backend */ |
|
168 | + $backend = $storage->getBackend(); |
|
169 | + /** @var AuthMechanism */ |
|
170 | + $authMechanism = $storage->getAuthMechanism(); |
|
171 | + if ($backend->checkDependencies()) { |
|
172 | + // invalid backend |
|
173 | + return new DataResponse( |
|
174 | + [ |
|
175 | + 'message' => (string)$this->l10n->t('Invalid storage backend "%s"', [ |
|
176 | + $backend->getIdentifier(), |
|
177 | + ]), |
|
178 | + ], |
|
179 | + Http::STATUS_UNPROCESSABLE_ENTITY |
|
180 | + ); |
|
181 | + } |
|
182 | 182 | |
183 | - if (!$backend->isVisibleFor($this->service->getVisibilityType())) { |
|
184 | - // not permitted to use backend |
|
185 | - return new DataResponse( |
|
186 | - [ |
|
187 | - 'message' => (string)$this->l10n->t('Not permitted to use backend "%s"', [ |
|
188 | - $backend->getIdentifier(), |
|
189 | - ]), |
|
190 | - ], |
|
191 | - Http::STATUS_UNPROCESSABLE_ENTITY |
|
192 | - ); |
|
193 | - } |
|
194 | - if (!$authMechanism->isVisibleFor($this->service->getVisibilityType())) { |
|
195 | - // not permitted to use auth mechanism |
|
196 | - return new DataResponse( |
|
197 | - [ |
|
198 | - 'message' => (string)$this->l10n->t('Not permitted to use authentication mechanism "%s"', [ |
|
199 | - $authMechanism->getIdentifier(), |
|
200 | - ]), |
|
201 | - ], |
|
202 | - Http::STATUS_UNPROCESSABLE_ENTITY |
|
203 | - ); |
|
204 | - } |
|
183 | + if (!$backend->isVisibleFor($this->service->getVisibilityType())) { |
|
184 | + // not permitted to use backend |
|
185 | + return new DataResponse( |
|
186 | + [ |
|
187 | + 'message' => (string)$this->l10n->t('Not permitted to use backend "%s"', [ |
|
188 | + $backend->getIdentifier(), |
|
189 | + ]), |
|
190 | + ], |
|
191 | + Http::STATUS_UNPROCESSABLE_ENTITY |
|
192 | + ); |
|
193 | + } |
|
194 | + if (!$authMechanism->isVisibleFor($this->service->getVisibilityType())) { |
|
195 | + // not permitted to use auth mechanism |
|
196 | + return new DataResponse( |
|
197 | + [ |
|
198 | + 'message' => (string)$this->l10n->t('Not permitted to use authentication mechanism "%s"', [ |
|
199 | + $authMechanism->getIdentifier(), |
|
200 | + ]), |
|
201 | + ], |
|
202 | + Http::STATUS_UNPROCESSABLE_ENTITY |
|
203 | + ); |
|
204 | + } |
|
205 | 205 | |
206 | - if (!$backend->validateStorage($storage)) { |
|
207 | - // unsatisfied parameters |
|
208 | - return new DataResponse( |
|
209 | - [ |
|
210 | - 'message' => (string)$this->l10n->t('Unsatisfied backend parameters'), |
|
211 | - ], |
|
212 | - Http::STATUS_UNPROCESSABLE_ENTITY |
|
213 | - ); |
|
214 | - } |
|
215 | - if (!$authMechanism->validateStorage($storage)) { |
|
216 | - // unsatisfied parameters |
|
217 | - return new DataResponse( |
|
218 | - [ |
|
219 | - 'message' => (string)$this->l10n->t('Unsatisfied authentication mechanism parameters'), |
|
220 | - ], |
|
221 | - Http::STATUS_UNPROCESSABLE_ENTITY |
|
222 | - ); |
|
223 | - } |
|
206 | + if (!$backend->validateStorage($storage)) { |
|
207 | + // unsatisfied parameters |
|
208 | + return new DataResponse( |
|
209 | + [ |
|
210 | + 'message' => (string)$this->l10n->t('Unsatisfied backend parameters'), |
|
211 | + ], |
|
212 | + Http::STATUS_UNPROCESSABLE_ENTITY |
|
213 | + ); |
|
214 | + } |
|
215 | + if (!$authMechanism->validateStorage($storage)) { |
|
216 | + // unsatisfied parameters |
|
217 | + return new DataResponse( |
|
218 | + [ |
|
219 | + 'message' => (string)$this->l10n->t('Unsatisfied authentication mechanism parameters'), |
|
220 | + ], |
|
221 | + Http::STATUS_UNPROCESSABLE_ENTITY |
|
222 | + ); |
|
223 | + } |
|
224 | 224 | |
225 | - return null; |
|
226 | - } |
|
225 | + return null; |
|
226 | + } |
|
227 | 227 | |
228 | - protected function manipulateStorageConfig(StorageConfig $storage) { |
|
229 | - /** @var AuthMechanism */ |
|
230 | - $authMechanism = $storage->getAuthMechanism(); |
|
231 | - $authMechanism->manipulateStorageConfig($storage); |
|
232 | - /** @var Backend */ |
|
233 | - $backend = $storage->getBackend(); |
|
234 | - $backend->manipulateStorageConfig($storage); |
|
235 | - } |
|
228 | + protected function manipulateStorageConfig(StorageConfig $storage) { |
|
229 | + /** @var AuthMechanism */ |
|
230 | + $authMechanism = $storage->getAuthMechanism(); |
|
231 | + $authMechanism->manipulateStorageConfig($storage); |
|
232 | + /** @var Backend */ |
|
233 | + $backend = $storage->getBackend(); |
|
234 | + $backend->manipulateStorageConfig($storage); |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Check whether the given storage is available / valid. |
|
239 | - * |
|
240 | - * Note that this operation can be time consuming depending |
|
241 | - * on whether the remote storage is available or not. |
|
242 | - * |
|
243 | - * @param StorageConfig $storage storage configuration |
|
244 | - * @param bool $testOnly whether to storage should only test the connection or do more things |
|
245 | - */ |
|
246 | - protected function updateStorageStatus(StorageConfig &$storage, $testOnly = true) { |
|
247 | - try { |
|
248 | - $this->manipulateStorageConfig($storage); |
|
237 | + /** |
|
238 | + * Check whether the given storage is available / valid. |
|
239 | + * |
|
240 | + * Note that this operation can be time consuming depending |
|
241 | + * on whether the remote storage is available or not. |
|
242 | + * |
|
243 | + * @param StorageConfig $storage storage configuration |
|
244 | + * @param bool $testOnly whether to storage should only test the connection or do more things |
|
245 | + */ |
|
246 | + protected function updateStorageStatus(StorageConfig &$storage, $testOnly = true) { |
|
247 | + try { |
|
248 | + $this->manipulateStorageConfig($storage); |
|
249 | 249 | |
250 | - /** @var Backend */ |
|
251 | - $backend = $storage->getBackend(); |
|
252 | - // update status (can be time-consuming) |
|
253 | - $storage->setStatus( |
|
254 | - \OC_Mount_Config::getBackendStatus( |
|
255 | - $backend->getStorageClass(), |
|
256 | - $storage->getBackendOptions(), |
|
257 | - false, |
|
258 | - $testOnly |
|
259 | - ) |
|
260 | - ); |
|
261 | - } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
262 | - $status = $e->getCode() ? $e->getCode() : StorageNotAvailableException::STATUS_INDETERMINATE; |
|
263 | - $storage->setStatus( |
|
264 | - $status, |
|
265 | - $this->l10n->t('Insufficient data: %s', [$e->getMessage()]) |
|
266 | - ); |
|
267 | - } catch (StorageNotAvailableException $e) { |
|
268 | - $storage->setStatus( |
|
269 | - $e->getCode(), |
|
270 | - $this->l10n->t('%s', [$e->getMessage()]) |
|
271 | - ); |
|
272 | - } catch (\Exception $e) { |
|
273 | - // FIXME: convert storage exceptions to StorageNotAvailableException |
|
274 | - $storage->setStatus( |
|
275 | - StorageNotAvailableException::STATUS_ERROR, |
|
276 | - get_class($e) . ': ' . $e->getMessage() |
|
277 | - ); |
|
278 | - } |
|
279 | - } |
|
250 | + /** @var Backend */ |
|
251 | + $backend = $storage->getBackend(); |
|
252 | + // update status (can be time-consuming) |
|
253 | + $storage->setStatus( |
|
254 | + \OC_Mount_Config::getBackendStatus( |
|
255 | + $backend->getStorageClass(), |
|
256 | + $storage->getBackendOptions(), |
|
257 | + false, |
|
258 | + $testOnly |
|
259 | + ) |
|
260 | + ); |
|
261 | + } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
262 | + $status = $e->getCode() ? $e->getCode() : StorageNotAvailableException::STATUS_INDETERMINATE; |
|
263 | + $storage->setStatus( |
|
264 | + $status, |
|
265 | + $this->l10n->t('Insufficient data: %s', [$e->getMessage()]) |
|
266 | + ); |
|
267 | + } catch (StorageNotAvailableException $e) { |
|
268 | + $storage->setStatus( |
|
269 | + $e->getCode(), |
|
270 | + $this->l10n->t('%s', [$e->getMessage()]) |
|
271 | + ); |
|
272 | + } catch (\Exception $e) { |
|
273 | + // FIXME: convert storage exceptions to StorageNotAvailableException |
|
274 | + $storage->setStatus( |
|
275 | + StorageNotAvailableException::STATUS_ERROR, |
|
276 | + get_class($e) . ': ' . $e->getMessage() |
|
277 | + ); |
|
278 | + } |
|
279 | + } |
|
280 | 280 | |
281 | - /** |
|
282 | - * Get all storage entries |
|
283 | - * |
|
284 | - * @return DataResponse |
|
285 | - */ |
|
286 | - public function index() { |
|
287 | - $storages = $this->formatStoragesForUI($this->service->getStorages()); |
|
281 | + /** |
|
282 | + * Get all storage entries |
|
283 | + * |
|
284 | + * @return DataResponse |
|
285 | + */ |
|
286 | + public function index() { |
|
287 | + $storages = $this->formatStoragesForUI($this->service->getStorages()); |
|
288 | 288 | |
289 | - return new DataResponse( |
|
290 | - $storages, |
|
291 | - Http::STATUS_OK |
|
292 | - ); |
|
293 | - } |
|
289 | + return new DataResponse( |
|
290 | + $storages, |
|
291 | + Http::STATUS_OK |
|
292 | + ); |
|
293 | + } |
|
294 | 294 | |
295 | - protected function formatStoragesForUI(array $storages): array { |
|
296 | - return array_map(function ($storage) { |
|
297 | - return $this->formatStorageForUI($storage); |
|
298 | - }, $storages); |
|
299 | - } |
|
295 | + protected function formatStoragesForUI(array $storages): array { |
|
296 | + return array_map(function ($storage) { |
|
297 | + return $this->formatStorageForUI($storage); |
|
298 | + }, $storages); |
|
299 | + } |
|
300 | 300 | |
301 | - protected function formatStorageForUI(StorageConfig $storage): StorageConfig { |
|
302 | - /** @var DefinitionParameter[] $parameters */ |
|
303 | - $parameters = array_merge($storage->getBackend()->getParameters(), $storage->getAuthMechanism()->getParameters()); |
|
301 | + protected function formatStorageForUI(StorageConfig $storage): StorageConfig { |
|
302 | + /** @var DefinitionParameter[] $parameters */ |
|
303 | + $parameters = array_merge($storage->getBackend()->getParameters(), $storage->getAuthMechanism()->getParameters()); |
|
304 | 304 | |
305 | - $options = $storage->getBackendOptions(); |
|
306 | - foreach ($options as $key => $value) { |
|
307 | - foreach ($parameters as $parameter) { |
|
308 | - if ($parameter->getName() === $key && $parameter->getType() === DefinitionParameter::VALUE_PASSWORD) { |
|
309 | - $storage->setBackendOption($key, DefinitionParameter::UNMODIFIED_PLACEHOLDER); |
|
310 | - break; |
|
311 | - } |
|
312 | - } |
|
313 | - } |
|
305 | + $options = $storage->getBackendOptions(); |
|
306 | + foreach ($options as $key => $value) { |
|
307 | + foreach ($parameters as $parameter) { |
|
308 | + if ($parameter->getName() === $key && $parameter->getType() === DefinitionParameter::VALUE_PASSWORD) { |
|
309 | + $storage->setBackendOption($key, DefinitionParameter::UNMODIFIED_PLACEHOLDER); |
|
310 | + break; |
|
311 | + } |
|
312 | + } |
|
313 | + } |
|
314 | 314 | |
315 | - return $storage; |
|
316 | - } |
|
315 | + return $storage; |
|
316 | + } |
|
317 | 317 | |
318 | - /** |
|
319 | - * Get an external storage entry. |
|
320 | - * |
|
321 | - * @param int $id storage id |
|
322 | - * @param bool $testOnly whether to storage should only test the connection or do more things |
|
323 | - * |
|
324 | - * @return DataResponse |
|
325 | - */ |
|
326 | - public function show($id, $testOnly = true) { |
|
327 | - try { |
|
328 | - $storage = $this->service->getStorage($id); |
|
318 | + /** |
|
319 | + * Get an external storage entry. |
|
320 | + * |
|
321 | + * @param int $id storage id |
|
322 | + * @param bool $testOnly whether to storage should only test the connection or do more things |
|
323 | + * |
|
324 | + * @return DataResponse |
|
325 | + */ |
|
326 | + public function show($id, $testOnly = true) { |
|
327 | + try { |
|
328 | + $storage = $this->service->getStorage($id); |
|
329 | 329 | |
330 | - $this->updateStorageStatus($storage, $testOnly); |
|
331 | - } catch (NotFoundException $e) { |
|
332 | - return new DataResponse( |
|
333 | - [ |
|
334 | - 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id]), |
|
335 | - ], |
|
336 | - Http::STATUS_NOT_FOUND |
|
337 | - ); |
|
338 | - } |
|
330 | + $this->updateStorageStatus($storage, $testOnly); |
|
331 | + } catch (NotFoundException $e) { |
|
332 | + return new DataResponse( |
|
333 | + [ |
|
334 | + 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id]), |
|
335 | + ], |
|
336 | + Http::STATUS_NOT_FOUND |
|
337 | + ); |
|
338 | + } |
|
339 | 339 | |
340 | - return new DataResponse( |
|
341 | - $this->formatStorageForUI($storage), |
|
342 | - Http::STATUS_OK |
|
343 | - ); |
|
344 | - } |
|
340 | + return new DataResponse( |
|
341 | + $this->formatStorageForUI($storage), |
|
342 | + Http::STATUS_OK |
|
343 | + ); |
|
344 | + } |
|
345 | 345 | |
346 | - /** |
|
347 | - * Deletes the storage with the given id. |
|
348 | - * |
|
349 | - * @param int $id storage id |
|
350 | - * |
|
351 | - * @return DataResponse |
|
352 | - */ |
|
353 | - public function destroy($id) { |
|
354 | - try { |
|
355 | - $this->service->removeStorage($id); |
|
356 | - } catch (NotFoundException $e) { |
|
357 | - return new DataResponse( |
|
358 | - [ |
|
359 | - 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id]), |
|
360 | - ], |
|
361 | - Http::STATUS_NOT_FOUND |
|
362 | - ); |
|
363 | - } |
|
346 | + /** |
|
347 | + * Deletes the storage with the given id. |
|
348 | + * |
|
349 | + * @param int $id storage id |
|
350 | + * |
|
351 | + * @return DataResponse |
|
352 | + */ |
|
353 | + public function destroy($id) { |
|
354 | + try { |
|
355 | + $this->service->removeStorage($id); |
|
356 | + } catch (NotFoundException $e) { |
|
357 | + return new DataResponse( |
|
358 | + [ |
|
359 | + 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id]), |
|
360 | + ], |
|
361 | + Http::STATUS_NOT_FOUND |
|
362 | + ); |
|
363 | + } |
|
364 | 364 | |
365 | - return new DataResponse([], Http::STATUS_NO_CONTENT); |
|
366 | - } |
|
365 | + return new DataResponse([], Http::STATUS_NO_CONTENT); |
|
366 | + } |
|
367 | 367 | |
368 | 368 | } |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | $this->logger->logException($e); |
130 | 130 | return new DataResponse( |
131 | 131 | [ |
132 | - 'message' => (string)$this->l10n->t('Invalid backend or authentication mechanism class') |
|
132 | + 'message' => (string) $this->l10n->t('Invalid backend or authentication mechanism class') |
|
133 | 133 | ], |
134 | 134 | Http::STATUS_UNPROCESSABLE_ENTITY |
135 | 135 | ); |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | if ($mountPoint === '') { |
149 | 149 | return new DataResponse( |
150 | 150 | [ |
151 | - 'message' => (string)$this->l10n->t('Invalid mount point'), |
|
151 | + 'message' => (string) $this->l10n->t('Invalid mount point'), |
|
152 | 152 | ], |
153 | 153 | Http::STATUS_UNPROCESSABLE_ENTITY |
154 | 154 | ); |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | // objectstore must not be sent from client side |
159 | 159 | return new DataResponse( |
160 | 160 | [ |
161 | - 'message' => (string)$this->l10n->t('Objectstore forbidden'), |
|
161 | + 'message' => (string) $this->l10n->t('Objectstore forbidden'), |
|
162 | 162 | ], |
163 | 163 | Http::STATUS_UNPROCESSABLE_ENTITY |
164 | 164 | ); |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | // invalid backend |
173 | 173 | return new DataResponse( |
174 | 174 | [ |
175 | - 'message' => (string)$this->l10n->t('Invalid storage backend "%s"', [ |
|
175 | + 'message' => (string) $this->l10n->t('Invalid storage backend "%s"', [ |
|
176 | 176 | $backend->getIdentifier(), |
177 | 177 | ]), |
178 | 178 | ], |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | // not permitted to use backend |
185 | 185 | return new DataResponse( |
186 | 186 | [ |
187 | - 'message' => (string)$this->l10n->t('Not permitted to use backend "%s"', [ |
|
187 | + 'message' => (string) $this->l10n->t('Not permitted to use backend "%s"', [ |
|
188 | 188 | $backend->getIdentifier(), |
189 | 189 | ]), |
190 | 190 | ], |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | // not permitted to use auth mechanism |
196 | 196 | return new DataResponse( |
197 | 197 | [ |
198 | - 'message' => (string)$this->l10n->t('Not permitted to use authentication mechanism "%s"', [ |
|
198 | + 'message' => (string) $this->l10n->t('Not permitted to use authentication mechanism "%s"', [ |
|
199 | 199 | $authMechanism->getIdentifier(), |
200 | 200 | ]), |
201 | 201 | ], |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | // unsatisfied parameters |
208 | 208 | return new DataResponse( |
209 | 209 | [ |
210 | - 'message' => (string)$this->l10n->t('Unsatisfied backend parameters'), |
|
210 | + 'message' => (string) $this->l10n->t('Unsatisfied backend parameters'), |
|
211 | 211 | ], |
212 | 212 | Http::STATUS_UNPROCESSABLE_ENTITY |
213 | 213 | ); |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | // unsatisfied parameters |
217 | 217 | return new DataResponse( |
218 | 218 | [ |
219 | - 'message' => (string)$this->l10n->t('Unsatisfied authentication mechanism parameters'), |
|
219 | + 'message' => (string) $this->l10n->t('Unsatisfied authentication mechanism parameters'), |
|
220 | 220 | ], |
221 | 221 | Http::STATUS_UNPROCESSABLE_ENTITY |
222 | 222 | ); |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | // FIXME: convert storage exceptions to StorageNotAvailableException |
274 | 274 | $storage->setStatus( |
275 | 275 | StorageNotAvailableException::STATUS_ERROR, |
276 | - get_class($e) . ': ' . $e->getMessage() |
|
276 | + get_class($e).': '.$e->getMessage() |
|
277 | 277 | ); |
278 | 278 | } |
279 | 279 | } |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | } |
294 | 294 | |
295 | 295 | protected function formatStoragesForUI(array $storages): array { |
296 | - return array_map(function ($storage) { |
|
296 | + return array_map(function($storage) { |
|
297 | 297 | return $this->formatStorageForUI($storage); |
298 | 298 | }, $storages); |
299 | 299 | } |
@@ -331,7 +331,7 @@ discard block |
||
331 | 331 | } catch (NotFoundException $e) { |
332 | 332 | return new DataResponse( |
333 | 333 | [ |
334 | - 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id]), |
|
334 | + 'message' => (string) $this->l10n->t('Storage with ID "%d" not found', [$id]), |
|
335 | 335 | ], |
336 | 336 | Http::STATUS_NOT_FOUND |
337 | 337 | ); |
@@ -356,7 +356,7 @@ discard block |
||
356 | 356 | } catch (NotFoundException $e) { |
357 | 357 | return new DataResponse( |
358 | 358 | [ |
359 | - 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id]), |
|
359 | + 'message' => (string) $this->l10n->t('Storage with ID "%d" not found', [$id]), |
|
360 | 360 | ], |
361 | 361 | Http::STATUS_NOT_FOUND |
362 | 362 | ); |
@@ -40,151 +40,151 @@ |
||
40 | 40 | * Global storages controller |
41 | 41 | */ |
42 | 42 | class GlobalStoragesController extends StoragesController { |
43 | - /** |
|
44 | - * Creates a new global storages controller. |
|
45 | - * |
|
46 | - * @param string $AppName application name |
|
47 | - * @param IRequest $request request object |
|
48 | - * @param IL10N $l10n l10n service |
|
49 | - * @param GlobalStoragesService $globalStoragesService storage service |
|
50 | - * @param ILogger $logger |
|
51 | - */ |
|
52 | - public function __construct( |
|
53 | - $AppName, |
|
54 | - IRequest $request, |
|
55 | - IL10N $l10n, |
|
56 | - GlobalStoragesService $globalStoragesService, |
|
57 | - ILogger $logger |
|
58 | - ) { |
|
59 | - parent::__construct( |
|
60 | - $AppName, |
|
61 | - $request, |
|
62 | - $l10n, |
|
63 | - $globalStoragesService, |
|
64 | - $logger |
|
65 | - ); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Create an external storage entry. |
|
70 | - * |
|
71 | - * @param string $mountPoint storage mount point |
|
72 | - * @param string $backend backend identifier |
|
73 | - * @param string $authMechanism authentication mechanism identifier |
|
74 | - * @param array $backendOptions backend-specific options |
|
75 | - * @param array $mountOptions mount-specific options |
|
76 | - * @param array $applicableUsers users for which to mount the storage |
|
77 | - * @param array $applicableGroups groups for which to mount the storage |
|
78 | - * @param int $priority priority |
|
79 | - * |
|
80 | - * @return DataResponse |
|
81 | - */ |
|
82 | - public function create( |
|
83 | - $mountPoint, |
|
84 | - $backend, |
|
85 | - $authMechanism, |
|
86 | - $backendOptions, |
|
87 | - $mountOptions, |
|
88 | - $applicableUsers, |
|
89 | - $applicableGroups, |
|
90 | - $priority |
|
91 | - ) { |
|
92 | - $newStorage = $this->createStorage( |
|
93 | - $mountPoint, |
|
94 | - $backend, |
|
95 | - $authMechanism, |
|
96 | - $backendOptions, |
|
97 | - $mountOptions, |
|
98 | - $applicableUsers, |
|
99 | - $applicableGroups, |
|
100 | - $priority |
|
101 | - ); |
|
102 | - if ($newStorage instanceof DataResponse) { |
|
103 | - return $newStorage; |
|
104 | - } |
|
105 | - |
|
106 | - $response = $this->validate($newStorage); |
|
107 | - if (!empty($response)) { |
|
108 | - return $response; |
|
109 | - } |
|
110 | - |
|
111 | - $newStorage = $this->service->addStorage($newStorage); |
|
112 | - |
|
113 | - $this->updateStorageStatus($newStorage); |
|
114 | - |
|
115 | - return new DataResponse( |
|
116 | - $this->formatStorageForUI($newStorage), |
|
117 | - Http::STATUS_CREATED |
|
118 | - ); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Update an external storage entry. |
|
123 | - * |
|
124 | - * @param int $id storage id |
|
125 | - * @param string $mountPoint storage mount point |
|
126 | - * @param string $backend backend identifier |
|
127 | - * @param string $authMechanism authentication mechansim identifier |
|
128 | - * @param array $backendOptions backend-specific options |
|
129 | - * @param array $mountOptions mount-specific options |
|
130 | - * @param array $applicableUsers users for which to mount the storage |
|
131 | - * @param array $applicableGroups groups for which to mount the storage |
|
132 | - * @param int $priority priority |
|
133 | - * @param bool $testOnly whether to storage should only test the connection or do more things |
|
134 | - * |
|
135 | - * @return DataResponse |
|
136 | - */ |
|
137 | - public function update( |
|
138 | - $id, |
|
139 | - $mountPoint, |
|
140 | - $backend, |
|
141 | - $authMechanism, |
|
142 | - $backendOptions, |
|
143 | - $mountOptions, |
|
144 | - $applicableUsers, |
|
145 | - $applicableGroups, |
|
146 | - $priority, |
|
147 | - $testOnly = true |
|
148 | - ) { |
|
149 | - $storage = $this->createStorage( |
|
150 | - $mountPoint, |
|
151 | - $backend, |
|
152 | - $authMechanism, |
|
153 | - $backendOptions, |
|
154 | - $mountOptions, |
|
155 | - $applicableUsers, |
|
156 | - $applicableGroups, |
|
157 | - $priority |
|
158 | - ); |
|
159 | - if ($storage instanceof DataResponse) { |
|
160 | - return $storage; |
|
161 | - } |
|
162 | - $storage->setId($id); |
|
163 | - |
|
164 | - $response = $this->validate($storage); |
|
165 | - if (!empty($response)) { |
|
166 | - return $response; |
|
167 | - } |
|
168 | - |
|
169 | - try { |
|
170 | - $storage = $this->service->updateStorage($storage); |
|
171 | - } catch (NotFoundException $e) { |
|
172 | - return new DataResponse( |
|
173 | - [ |
|
174 | - 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', array($id)) |
|
175 | - ], |
|
176 | - Http::STATUS_NOT_FOUND |
|
177 | - ); |
|
178 | - } |
|
179 | - |
|
180 | - $this->updateStorageStatus($storage, $testOnly); |
|
181 | - |
|
182 | - return new DataResponse( |
|
183 | - $this->formatStorageForUI($storage), |
|
184 | - Http::STATUS_OK |
|
185 | - ); |
|
186 | - |
|
187 | - } |
|
43 | + /** |
|
44 | + * Creates a new global storages controller. |
|
45 | + * |
|
46 | + * @param string $AppName application name |
|
47 | + * @param IRequest $request request object |
|
48 | + * @param IL10N $l10n l10n service |
|
49 | + * @param GlobalStoragesService $globalStoragesService storage service |
|
50 | + * @param ILogger $logger |
|
51 | + */ |
|
52 | + public function __construct( |
|
53 | + $AppName, |
|
54 | + IRequest $request, |
|
55 | + IL10N $l10n, |
|
56 | + GlobalStoragesService $globalStoragesService, |
|
57 | + ILogger $logger |
|
58 | + ) { |
|
59 | + parent::__construct( |
|
60 | + $AppName, |
|
61 | + $request, |
|
62 | + $l10n, |
|
63 | + $globalStoragesService, |
|
64 | + $logger |
|
65 | + ); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Create an external storage entry. |
|
70 | + * |
|
71 | + * @param string $mountPoint storage mount point |
|
72 | + * @param string $backend backend identifier |
|
73 | + * @param string $authMechanism authentication mechanism identifier |
|
74 | + * @param array $backendOptions backend-specific options |
|
75 | + * @param array $mountOptions mount-specific options |
|
76 | + * @param array $applicableUsers users for which to mount the storage |
|
77 | + * @param array $applicableGroups groups for which to mount the storage |
|
78 | + * @param int $priority priority |
|
79 | + * |
|
80 | + * @return DataResponse |
|
81 | + */ |
|
82 | + public function create( |
|
83 | + $mountPoint, |
|
84 | + $backend, |
|
85 | + $authMechanism, |
|
86 | + $backendOptions, |
|
87 | + $mountOptions, |
|
88 | + $applicableUsers, |
|
89 | + $applicableGroups, |
|
90 | + $priority |
|
91 | + ) { |
|
92 | + $newStorage = $this->createStorage( |
|
93 | + $mountPoint, |
|
94 | + $backend, |
|
95 | + $authMechanism, |
|
96 | + $backendOptions, |
|
97 | + $mountOptions, |
|
98 | + $applicableUsers, |
|
99 | + $applicableGroups, |
|
100 | + $priority |
|
101 | + ); |
|
102 | + if ($newStorage instanceof DataResponse) { |
|
103 | + return $newStorage; |
|
104 | + } |
|
105 | + |
|
106 | + $response = $this->validate($newStorage); |
|
107 | + if (!empty($response)) { |
|
108 | + return $response; |
|
109 | + } |
|
110 | + |
|
111 | + $newStorage = $this->service->addStorage($newStorage); |
|
112 | + |
|
113 | + $this->updateStorageStatus($newStorage); |
|
114 | + |
|
115 | + return new DataResponse( |
|
116 | + $this->formatStorageForUI($newStorage), |
|
117 | + Http::STATUS_CREATED |
|
118 | + ); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Update an external storage entry. |
|
123 | + * |
|
124 | + * @param int $id storage id |
|
125 | + * @param string $mountPoint storage mount point |
|
126 | + * @param string $backend backend identifier |
|
127 | + * @param string $authMechanism authentication mechansim identifier |
|
128 | + * @param array $backendOptions backend-specific options |
|
129 | + * @param array $mountOptions mount-specific options |
|
130 | + * @param array $applicableUsers users for which to mount the storage |
|
131 | + * @param array $applicableGroups groups for which to mount the storage |
|
132 | + * @param int $priority priority |
|
133 | + * @param bool $testOnly whether to storage should only test the connection or do more things |
|
134 | + * |
|
135 | + * @return DataResponse |
|
136 | + */ |
|
137 | + public function update( |
|
138 | + $id, |
|
139 | + $mountPoint, |
|
140 | + $backend, |
|
141 | + $authMechanism, |
|
142 | + $backendOptions, |
|
143 | + $mountOptions, |
|
144 | + $applicableUsers, |
|
145 | + $applicableGroups, |
|
146 | + $priority, |
|
147 | + $testOnly = true |
|
148 | + ) { |
|
149 | + $storage = $this->createStorage( |
|
150 | + $mountPoint, |
|
151 | + $backend, |
|
152 | + $authMechanism, |
|
153 | + $backendOptions, |
|
154 | + $mountOptions, |
|
155 | + $applicableUsers, |
|
156 | + $applicableGroups, |
|
157 | + $priority |
|
158 | + ); |
|
159 | + if ($storage instanceof DataResponse) { |
|
160 | + return $storage; |
|
161 | + } |
|
162 | + $storage->setId($id); |
|
163 | + |
|
164 | + $response = $this->validate($storage); |
|
165 | + if (!empty($response)) { |
|
166 | + return $response; |
|
167 | + } |
|
168 | + |
|
169 | + try { |
|
170 | + $storage = $this->service->updateStorage($storage); |
|
171 | + } catch (NotFoundException $e) { |
|
172 | + return new DataResponse( |
|
173 | + [ |
|
174 | + 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', array($id)) |
|
175 | + ], |
|
176 | + Http::STATUS_NOT_FOUND |
|
177 | + ); |
|
178 | + } |
|
179 | + |
|
180 | + $this->updateStorageStatus($storage, $testOnly); |
|
181 | + |
|
182 | + return new DataResponse( |
|
183 | + $this->formatStorageForUI($storage), |
|
184 | + Http::STATUS_OK |
|
185 | + ); |
|
186 | + |
|
187 | + } |
|
188 | 188 | |
189 | 189 | |
190 | 190 | } |
@@ -45,165 +45,165 @@ |
||
45 | 45 | * User global storages controller |
46 | 46 | */ |
47 | 47 | class UserGlobalStoragesController extends StoragesController { |
48 | - /** |
|
49 | - * @var IUserSession |
|
50 | - */ |
|
51 | - private $userSession; |
|
52 | - |
|
53 | - /** |
|
54 | - * Creates a new user global storages controller. |
|
55 | - * |
|
56 | - * @param string $AppName application name |
|
57 | - * @param IRequest $request request object |
|
58 | - * @param IL10N $l10n l10n service |
|
59 | - * @param UserGlobalStoragesService $userGlobalStoragesService storage service |
|
60 | - * @param IUserSession $userSession |
|
61 | - */ |
|
62 | - public function __construct( |
|
63 | - $AppName, |
|
64 | - IRequest $request, |
|
65 | - IL10N $l10n, |
|
66 | - UserGlobalStoragesService $userGlobalStoragesService, |
|
67 | - IUserSession $userSession, |
|
68 | - ILogger $logger |
|
69 | - ) { |
|
70 | - parent::__construct( |
|
71 | - $AppName, |
|
72 | - $request, |
|
73 | - $l10n, |
|
74 | - $userGlobalStoragesService, |
|
75 | - $logger |
|
76 | - ); |
|
77 | - $this->userSession = $userSession; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Get all storage entries |
|
82 | - * |
|
83 | - * @return DataResponse |
|
84 | - * |
|
85 | - * @NoAdminRequired |
|
86 | - */ |
|
87 | - public function index() { |
|
88 | - $storages = $this->formatStoragesForUI($this->service->getUniqueStorages()); |
|
89 | - |
|
90 | - // remove configuration data, this must be kept private |
|
91 | - foreach ($storages as $storage) { |
|
92 | - $this->sanitizeStorage($storage); |
|
93 | - } |
|
94 | - |
|
95 | - return new DataResponse( |
|
96 | - $storages, |
|
97 | - Http::STATUS_OK |
|
98 | - ); |
|
99 | - } |
|
100 | - |
|
101 | - protected function manipulateStorageConfig(StorageConfig $storage) { |
|
102 | - /** @var AuthMechanism */ |
|
103 | - $authMechanism = $storage->getAuthMechanism(); |
|
104 | - $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
105 | - /** @var Backend */ |
|
106 | - $backend = $storage->getBackend(); |
|
107 | - $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Get an external storage entry. |
|
112 | - * |
|
113 | - * @param int $id storage id |
|
114 | - * @param bool $testOnly whether to storage should only test the connection or do more things |
|
115 | - * @return DataResponse |
|
116 | - * |
|
117 | - * @NoAdminRequired |
|
118 | - */ |
|
119 | - public function show($id, $testOnly = true) { |
|
120 | - try { |
|
121 | - $storage = $this->service->getStorage($id); |
|
122 | - |
|
123 | - $this->updateStorageStatus($storage, $testOnly); |
|
124 | - } catch (NotFoundException $e) { |
|
125 | - return new DataResponse( |
|
126 | - [ |
|
127 | - 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', array($id)) |
|
128 | - ], |
|
129 | - Http::STATUS_NOT_FOUND |
|
130 | - ); |
|
131 | - } |
|
132 | - |
|
133 | - $this->sanitizeStorage($storage); |
|
134 | - |
|
135 | - return new DataResponse( |
|
136 | - $this->formatStorageForUI($storage), |
|
137 | - Http::STATUS_OK |
|
138 | - ); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Update an external storage entry. |
|
143 | - * Only allows setting user provided backend fields |
|
144 | - * |
|
145 | - * @param int $id storage id |
|
146 | - * @param array $backendOptions backend-specific options |
|
147 | - * @param bool $testOnly whether to storage should only test the connection or do more things |
|
148 | - * |
|
149 | - * @return DataResponse |
|
150 | - * |
|
151 | - * @NoAdminRequired |
|
152 | - */ |
|
153 | - public function update( |
|
154 | - $id, |
|
155 | - $backendOptions, |
|
156 | - $testOnly = true |
|
157 | - ) { |
|
158 | - try { |
|
159 | - $storage = $this->service->getStorage($id); |
|
160 | - $authMechanism = $storage->getAuthMechanism(); |
|
161 | - if ($authMechanism instanceof IUserProvided || $authMechanism instanceof UserGlobalAuth) { |
|
162 | - $authMechanism->saveBackendOptions($this->userSession->getUser(), $id, $backendOptions); |
|
163 | - $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
164 | - } else { |
|
165 | - return new DataResponse( |
|
166 | - [ |
|
167 | - 'message' => (string)$this->l10n->t('Storage with ID "%d" is not user editable', array($id)) |
|
168 | - ], |
|
169 | - Http::STATUS_FORBIDDEN |
|
170 | - ); |
|
171 | - } |
|
172 | - } catch (NotFoundException $e) { |
|
173 | - return new DataResponse( |
|
174 | - [ |
|
175 | - 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', array($id)) |
|
176 | - ], |
|
177 | - Http::STATUS_NOT_FOUND |
|
178 | - ); |
|
179 | - } |
|
180 | - |
|
181 | - $this->updateStorageStatus($storage, $testOnly); |
|
182 | - $this->sanitizeStorage($storage); |
|
183 | - |
|
184 | - return new DataResponse( |
|
185 | - $this->formatStorageForUI($storage), |
|
186 | - Http::STATUS_OK |
|
187 | - ); |
|
188 | - |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Remove sensitive data from a StorageConfig before returning it to the user |
|
193 | - * |
|
194 | - * @param StorageConfig $storage |
|
195 | - */ |
|
196 | - protected function sanitizeStorage(StorageConfig $storage) { |
|
197 | - $storage->setBackendOptions([]); |
|
198 | - $storage->setMountOptions([]); |
|
199 | - |
|
200 | - if ($storage->getAuthMechanism() instanceof IUserProvided) { |
|
201 | - try { |
|
202 | - $storage->getAuthMechanism()->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
203 | - } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
204 | - // not configured yet |
|
205 | - } |
|
206 | - } |
|
207 | - } |
|
48 | + /** |
|
49 | + * @var IUserSession |
|
50 | + */ |
|
51 | + private $userSession; |
|
52 | + |
|
53 | + /** |
|
54 | + * Creates a new user global storages controller. |
|
55 | + * |
|
56 | + * @param string $AppName application name |
|
57 | + * @param IRequest $request request object |
|
58 | + * @param IL10N $l10n l10n service |
|
59 | + * @param UserGlobalStoragesService $userGlobalStoragesService storage service |
|
60 | + * @param IUserSession $userSession |
|
61 | + */ |
|
62 | + public function __construct( |
|
63 | + $AppName, |
|
64 | + IRequest $request, |
|
65 | + IL10N $l10n, |
|
66 | + UserGlobalStoragesService $userGlobalStoragesService, |
|
67 | + IUserSession $userSession, |
|
68 | + ILogger $logger |
|
69 | + ) { |
|
70 | + parent::__construct( |
|
71 | + $AppName, |
|
72 | + $request, |
|
73 | + $l10n, |
|
74 | + $userGlobalStoragesService, |
|
75 | + $logger |
|
76 | + ); |
|
77 | + $this->userSession = $userSession; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Get all storage entries |
|
82 | + * |
|
83 | + * @return DataResponse |
|
84 | + * |
|
85 | + * @NoAdminRequired |
|
86 | + */ |
|
87 | + public function index() { |
|
88 | + $storages = $this->formatStoragesForUI($this->service->getUniqueStorages()); |
|
89 | + |
|
90 | + // remove configuration data, this must be kept private |
|
91 | + foreach ($storages as $storage) { |
|
92 | + $this->sanitizeStorage($storage); |
|
93 | + } |
|
94 | + |
|
95 | + return new DataResponse( |
|
96 | + $storages, |
|
97 | + Http::STATUS_OK |
|
98 | + ); |
|
99 | + } |
|
100 | + |
|
101 | + protected function manipulateStorageConfig(StorageConfig $storage) { |
|
102 | + /** @var AuthMechanism */ |
|
103 | + $authMechanism = $storage->getAuthMechanism(); |
|
104 | + $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
105 | + /** @var Backend */ |
|
106 | + $backend = $storage->getBackend(); |
|
107 | + $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Get an external storage entry. |
|
112 | + * |
|
113 | + * @param int $id storage id |
|
114 | + * @param bool $testOnly whether to storage should only test the connection or do more things |
|
115 | + * @return DataResponse |
|
116 | + * |
|
117 | + * @NoAdminRequired |
|
118 | + */ |
|
119 | + public function show($id, $testOnly = true) { |
|
120 | + try { |
|
121 | + $storage = $this->service->getStorage($id); |
|
122 | + |
|
123 | + $this->updateStorageStatus($storage, $testOnly); |
|
124 | + } catch (NotFoundException $e) { |
|
125 | + return new DataResponse( |
|
126 | + [ |
|
127 | + 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', array($id)) |
|
128 | + ], |
|
129 | + Http::STATUS_NOT_FOUND |
|
130 | + ); |
|
131 | + } |
|
132 | + |
|
133 | + $this->sanitizeStorage($storage); |
|
134 | + |
|
135 | + return new DataResponse( |
|
136 | + $this->formatStorageForUI($storage), |
|
137 | + Http::STATUS_OK |
|
138 | + ); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Update an external storage entry. |
|
143 | + * Only allows setting user provided backend fields |
|
144 | + * |
|
145 | + * @param int $id storage id |
|
146 | + * @param array $backendOptions backend-specific options |
|
147 | + * @param bool $testOnly whether to storage should only test the connection or do more things |
|
148 | + * |
|
149 | + * @return DataResponse |
|
150 | + * |
|
151 | + * @NoAdminRequired |
|
152 | + */ |
|
153 | + public function update( |
|
154 | + $id, |
|
155 | + $backendOptions, |
|
156 | + $testOnly = true |
|
157 | + ) { |
|
158 | + try { |
|
159 | + $storage = $this->service->getStorage($id); |
|
160 | + $authMechanism = $storage->getAuthMechanism(); |
|
161 | + if ($authMechanism instanceof IUserProvided || $authMechanism instanceof UserGlobalAuth) { |
|
162 | + $authMechanism->saveBackendOptions($this->userSession->getUser(), $id, $backendOptions); |
|
163 | + $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
164 | + } else { |
|
165 | + return new DataResponse( |
|
166 | + [ |
|
167 | + 'message' => (string)$this->l10n->t('Storage with ID "%d" is not user editable', array($id)) |
|
168 | + ], |
|
169 | + Http::STATUS_FORBIDDEN |
|
170 | + ); |
|
171 | + } |
|
172 | + } catch (NotFoundException $e) { |
|
173 | + return new DataResponse( |
|
174 | + [ |
|
175 | + 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', array($id)) |
|
176 | + ], |
|
177 | + Http::STATUS_NOT_FOUND |
|
178 | + ); |
|
179 | + } |
|
180 | + |
|
181 | + $this->updateStorageStatus($storage, $testOnly); |
|
182 | + $this->sanitizeStorage($storage); |
|
183 | + |
|
184 | + return new DataResponse( |
|
185 | + $this->formatStorageForUI($storage), |
|
186 | + Http::STATUS_OK |
|
187 | + ); |
|
188 | + |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Remove sensitive data from a StorageConfig before returning it to the user |
|
193 | + * |
|
194 | + * @param StorageConfig $storage |
|
195 | + */ |
|
196 | + protected function sanitizeStorage(StorageConfig $storage) { |
|
197 | + $storage->setBackendOptions([]); |
|
198 | + $storage->setMountOptions([]); |
|
199 | + |
|
200 | + if ($storage->getAuthMechanism() instanceof IUserProvided) { |
|
201 | + try { |
|
202 | + $storage->getAuthMechanism()->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
203 | + } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
204 | + // not configured yet |
|
205 | + } |
|
206 | + } |
|
207 | + } |
|
208 | 208 | |
209 | 209 | } |
@@ -44,185 +44,185 @@ |
||
44 | 44 | * User storages controller |
45 | 45 | */ |
46 | 46 | class UserStoragesController extends StoragesController { |
47 | - /** |
|
48 | - * @var IUserSession |
|
49 | - */ |
|
50 | - private $userSession; |
|
51 | - |
|
52 | - /** |
|
53 | - * Creates a new user storages controller. |
|
54 | - * |
|
55 | - * @param string $AppName application name |
|
56 | - * @param IRequest $request request object |
|
57 | - * @param IL10N $l10n l10n service |
|
58 | - * @param UserStoragesService $userStoragesService storage service |
|
59 | - * @param IUserSession $userSession |
|
60 | - * @param ILogger $logger |
|
61 | - */ |
|
62 | - public function __construct( |
|
63 | - $AppName, |
|
64 | - IRequest $request, |
|
65 | - IL10N $l10n, |
|
66 | - UserStoragesService $userStoragesService, |
|
67 | - IUserSession $userSession, |
|
68 | - ILogger $logger |
|
69 | - ) { |
|
70 | - parent::__construct( |
|
71 | - $AppName, |
|
72 | - $request, |
|
73 | - $l10n, |
|
74 | - $userStoragesService, |
|
75 | - $logger |
|
76 | - ); |
|
77 | - $this->userSession = $userSession; |
|
78 | - } |
|
79 | - |
|
80 | - protected function manipulateStorageConfig(StorageConfig $storage) { |
|
81 | - /** @var AuthMechanism */ |
|
82 | - $authMechanism = $storage->getAuthMechanism(); |
|
83 | - $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
84 | - /** @var Backend */ |
|
85 | - $backend = $storage->getBackend(); |
|
86 | - $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Get all storage entries |
|
91 | - * |
|
92 | - * @NoAdminRequired |
|
93 | - * |
|
94 | - * @return DataResponse |
|
95 | - */ |
|
96 | - public function index() { |
|
97 | - return parent::index(); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Return storage |
|
102 | - * |
|
103 | - * @NoAdminRequired |
|
104 | - * |
|
105 | - * {@inheritdoc} |
|
106 | - */ |
|
107 | - public function show($id, $testOnly = true) { |
|
108 | - return parent::show($id, $testOnly); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Create an external storage entry. |
|
113 | - * |
|
114 | - * @param string $mountPoint storage mount point |
|
115 | - * @param string $backend backend identifier |
|
116 | - * @param string $authMechanism authentication mechanism identifier |
|
117 | - * @param array $backendOptions backend-specific options |
|
118 | - * @param array $mountOptions backend-specific mount options |
|
119 | - * |
|
120 | - * @return DataResponse |
|
121 | - * |
|
122 | - * @NoAdminRequired |
|
123 | - */ |
|
124 | - public function create( |
|
125 | - $mountPoint, |
|
126 | - $backend, |
|
127 | - $authMechanism, |
|
128 | - $backendOptions, |
|
129 | - $mountOptions |
|
130 | - ) { |
|
131 | - $newStorage = $this->createStorage( |
|
132 | - $mountPoint, |
|
133 | - $backend, |
|
134 | - $authMechanism, |
|
135 | - $backendOptions, |
|
136 | - $mountOptions |
|
137 | - ); |
|
138 | - if ($newStorage instanceOf DataResponse) { |
|
139 | - return $newStorage; |
|
140 | - } |
|
141 | - |
|
142 | - $response = $this->validate($newStorage); |
|
143 | - if (!empty($response)) { |
|
144 | - return $response; |
|
145 | - } |
|
146 | - |
|
147 | - $newStorage = $this->service->addStorage($newStorage); |
|
148 | - $this->updateStorageStatus($newStorage); |
|
149 | - |
|
150 | - return new DataResponse( |
|
151 | - $this->formatStorageForUI($newStorage), |
|
152 | - Http::STATUS_CREATED |
|
153 | - ); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Update an external storage entry. |
|
158 | - * |
|
159 | - * @param int $id storage id |
|
160 | - * @param string $mountPoint storage mount point |
|
161 | - * @param string $backend backend identifier |
|
162 | - * @param string $authMechanism authentication mechanism identifier |
|
163 | - * @param array $backendOptions backend-specific options |
|
164 | - * @param array $mountOptions backend-specific mount options |
|
165 | - * @param bool $testOnly whether to storage should only test the connection or do more things |
|
166 | - * |
|
167 | - * @return DataResponse |
|
168 | - * |
|
169 | - * @NoAdminRequired |
|
170 | - */ |
|
171 | - public function update( |
|
172 | - $id, |
|
173 | - $mountPoint, |
|
174 | - $backend, |
|
175 | - $authMechanism, |
|
176 | - $backendOptions, |
|
177 | - $mountOptions, |
|
178 | - $testOnly = true |
|
179 | - ) { |
|
180 | - $storage = $this->createStorage( |
|
181 | - $mountPoint, |
|
182 | - $backend, |
|
183 | - $authMechanism, |
|
184 | - $backendOptions, |
|
185 | - $mountOptions |
|
186 | - ); |
|
187 | - if ($storage instanceOf DataResponse) { |
|
188 | - return $storage; |
|
189 | - } |
|
190 | - $storage->setId($id); |
|
191 | - |
|
192 | - $response = $this->validate($storage); |
|
193 | - if (!empty($response)) { |
|
194 | - return $response; |
|
195 | - } |
|
196 | - |
|
197 | - try { |
|
198 | - $storage = $this->service->updateStorage($storage); |
|
199 | - } catch (NotFoundException $e) { |
|
200 | - return new DataResponse( |
|
201 | - [ |
|
202 | - 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', array($id)) |
|
203 | - ], |
|
204 | - Http::STATUS_NOT_FOUND |
|
205 | - ); |
|
206 | - } |
|
207 | - |
|
208 | - $this->updateStorageStatus($storage, $testOnly); |
|
209 | - |
|
210 | - return new DataResponse( |
|
211 | - $this->formatStorageForUI($storage), |
|
212 | - Http::STATUS_OK |
|
213 | - ); |
|
214 | - |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Delete storage |
|
219 | - * |
|
220 | - * @NoAdminRequired |
|
221 | - * |
|
222 | - * {@inheritdoc} |
|
223 | - */ |
|
224 | - public function destroy($id) { |
|
225 | - return parent::destroy($id); |
|
226 | - } |
|
47 | + /** |
|
48 | + * @var IUserSession |
|
49 | + */ |
|
50 | + private $userSession; |
|
51 | + |
|
52 | + /** |
|
53 | + * Creates a new user storages controller. |
|
54 | + * |
|
55 | + * @param string $AppName application name |
|
56 | + * @param IRequest $request request object |
|
57 | + * @param IL10N $l10n l10n service |
|
58 | + * @param UserStoragesService $userStoragesService storage service |
|
59 | + * @param IUserSession $userSession |
|
60 | + * @param ILogger $logger |
|
61 | + */ |
|
62 | + public function __construct( |
|
63 | + $AppName, |
|
64 | + IRequest $request, |
|
65 | + IL10N $l10n, |
|
66 | + UserStoragesService $userStoragesService, |
|
67 | + IUserSession $userSession, |
|
68 | + ILogger $logger |
|
69 | + ) { |
|
70 | + parent::__construct( |
|
71 | + $AppName, |
|
72 | + $request, |
|
73 | + $l10n, |
|
74 | + $userStoragesService, |
|
75 | + $logger |
|
76 | + ); |
|
77 | + $this->userSession = $userSession; |
|
78 | + } |
|
79 | + |
|
80 | + protected function manipulateStorageConfig(StorageConfig $storage) { |
|
81 | + /** @var AuthMechanism */ |
|
82 | + $authMechanism = $storage->getAuthMechanism(); |
|
83 | + $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
84 | + /** @var Backend */ |
|
85 | + $backend = $storage->getBackend(); |
|
86 | + $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Get all storage entries |
|
91 | + * |
|
92 | + * @NoAdminRequired |
|
93 | + * |
|
94 | + * @return DataResponse |
|
95 | + */ |
|
96 | + public function index() { |
|
97 | + return parent::index(); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Return storage |
|
102 | + * |
|
103 | + * @NoAdminRequired |
|
104 | + * |
|
105 | + * {@inheritdoc} |
|
106 | + */ |
|
107 | + public function show($id, $testOnly = true) { |
|
108 | + return parent::show($id, $testOnly); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Create an external storage entry. |
|
113 | + * |
|
114 | + * @param string $mountPoint storage mount point |
|
115 | + * @param string $backend backend identifier |
|
116 | + * @param string $authMechanism authentication mechanism identifier |
|
117 | + * @param array $backendOptions backend-specific options |
|
118 | + * @param array $mountOptions backend-specific mount options |
|
119 | + * |
|
120 | + * @return DataResponse |
|
121 | + * |
|
122 | + * @NoAdminRequired |
|
123 | + */ |
|
124 | + public function create( |
|
125 | + $mountPoint, |
|
126 | + $backend, |
|
127 | + $authMechanism, |
|
128 | + $backendOptions, |
|
129 | + $mountOptions |
|
130 | + ) { |
|
131 | + $newStorage = $this->createStorage( |
|
132 | + $mountPoint, |
|
133 | + $backend, |
|
134 | + $authMechanism, |
|
135 | + $backendOptions, |
|
136 | + $mountOptions |
|
137 | + ); |
|
138 | + if ($newStorage instanceOf DataResponse) { |
|
139 | + return $newStorage; |
|
140 | + } |
|
141 | + |
|
142 | + $response = $this->validate($newStorage); |
|
143 | + if (!empty($response)) { |
|
144 | + return $response; |
|
145 | + } |
|
146 | + |
|
147 | + $newStorage = $this->service->addStorage($newStorage); |
|
148 | + $this->updateStorageStatus($newStorage); |
|
149 | + |
|
150 | + return new DataResponse( |
|
151 | + $this->formatStorageForUI($newStorage), |
|
152 | + Http::STATUS_CREATED |
|
153 | + ); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Update an external storage entry. |
|
158 | + * |
|
159 | + * @param int $id storage id |
|
160 | + * @param string $mountPoint storage mount point |
|
161 | + * @param string $backend backend identifier |
|
162 | + * @param string $authMechanism authentication mechanism identifier |
|
163 | + * @param array $backendOptions backend-specific options |
|
164 | + * @param array $mountOptions backend-specific mount options |
|
165 | + * @param bool $testOnly whether to storage should only test the connection or do more things |
|
166 | + * |
|
167 | + * @return DataResponse |
|
168 | + * |
|
169 | + * @NoAdminRequired |
|
170 | + */ |
|
171 | + public function update( |
|
172 | + $id, |
|
173 | + $mountPoint, |
|
174 | + $backend, |
|
175 | + $authMechanism, |
|
176 | + $backendOptions, |
|
177 | + $mountOptions, |
|
178 | + $testOnly = true |
|
179 | + ) { |
|
180 | + $storage = $this->createStorage( |
|
181 | + $mountPoint, |
|
182 | + $backend, |
|
183 | + $authMechanism, |
|
184 | + $backendOptions, |
|
185 | + $mountOptions |
|
186 | + ); |
|
187 | + if ($storage instanceOf DataResponse) { |
|
188 | + return $storage; |
|
189 | + } |
|
190 | + $storage->setId($id); |
|
191 | + |
|
192 | + $response = $this->validate($storage); |
|
193 | + if (!empty($response)) { |
|
194 | + return $response; |
|
195 | + } |
|
196 | + |
|
197 | + try { |
|
198 | + $storage = $this->service->updateStorage($storage); |
|
199 | + } catch (NotFoundException $e) { |
|
200 | + return new DataResponse( |
|
201 | + [ |
|
202 | + 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', array($id)) |
|
203 | + ], |
|
204 | + Http::STATUS_NOT_FOUND |
|
205 | + ); |
|
206 | + } |
|
207 | + |
|
208 | + $this->updateStorageStatus($storage, $testOnly); |
|
209 | + |
|
210 | + return new DataResponse( |
|
211 | + $this->formatStorageForUI($storage), |
|
212 | + Http::STATUS_OK |
|
213 | + ); |
|
214 | + |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Delete storage |
|
219 | + * |
|
220 | + * @NoAdminRequired |
|
221 | + * |
|
222 | + * {@inheritdoc} |
|
223 | + */ |
|
224 | + public function destroy($id) { |
|
225 | + return parent::destroy($id); |
|
226 | + } |
|
227 | 227 | |
228 | 228 | } |
@@ -48,495 +48,495 @@ |
||
48 | 48 | */ |
49 | 49 | abstract class StoragesService { |
50 | 50 | |
51 | - /** @var BackendService */ |
|
52 | - protected $backendService; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var DBConfigService |
|
56 | - */ |
|
57 | - protected $dbConfig; |
|
58 | - |
|
59 | - /** |
|
60 | - * @var IUserMountCache |
|
61 | - */ |
|
62 | - protected $userMountCache; |
|
63 | - |
|
64 | - /** |
|
65 | - * @param BackendService $backendService |
|
66 | - * @param DBConfigService $dbConfigService |
|
67 | - * @param IUserMountCache $userMountCache |
|
68 | - */ |
|
69 | - public function __construct(BackendService $backendService, DBConfigService $dbConfigService, IUserMountCache $userMountCache) { |
|
70 | - $this->backendService = $backendService; |
|
71 | - $this->dbConfig = $dbConfigService; |
|
72 | - $this->userMountCache = $userMountCache; |
|
73 | - } |
|
74 | - |
|
75 | - protected function readDBConfig() { |
|
76 | - return $this->dbConfig->getAdminMounts(); |
|
77 | - } |
|
78 | - |
|
79 | - protected function getStorageConfigFromDBMount(array $mount) { |
|
80 | - $applicableUsers = array_filter($mount['applicable'], function ($applicable) { |
|
81 | - return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; |
|
82 | - }); |
|
83 | - $applicableUsers = array_map(function ($applicable) { |
|
84 | - return $applicable['value']; |
|
85 | - }, $applicableUsers); |
|
86 | - |
|
87 | - $applicableGroups = array_filter($mount['applicable'], function ($applicable) { |
|
88 | - return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; |
|
89 | - }); |
|
90 | - $applicableGroups = array_map(function ($applicable) { |
|
91 | - return $applicable['value']; |
|
92 | - }, $applicableGroups); |
|
93 | - |
|
94 | - try { |
|
95 | - $config = $this->createStorage( |
|
96 | - $mount['mount_point'], |
|
97 | - $mount['storage_backend'], |
|
98 | - $mount['auth_backend'], |
|
99 | - $mount['config'], |
|
100 | - $mount['options'], |
|
101 | - array_values($applicableUsers), |
|
102 | - array_values($applicableGroups), |
|
103 | - $mount['priority'] |
|
104 | - ); |
|
105 | - $config->setType($mount['type']); |
|
106 | - $config->setId((int)$mount['mount_id']); |
|
107 | - return $config; |
|
108 | - } catch (\UnexpectedValueException $e) { |
|
109 | - // don't die if a storage backend doesn't exist |
|
110 | - \OC::$server->getLogger()->logException($e, [ |
|
111 | - 'message' => 'Could not load storage.', |
|
112 | - 'level' => ILogger::ERROR, |
|
113 | - 'app' => 'files_external', |
|
114 | - ]); |
|
115 | - return null; |
|
116 | - } catch (\InvalidArgumentException $e) { |
|
117 | - \OC::$server->getLogger()->logException($e, [ |
|
118 | - 'message' => 'Could not load storage.', |
|
119 | - 'level' => ILogger::ERROR, |
|
120 | - 'app' => 'files_external', |
|
121 | - ]); |
|
122 | - return null; |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Read the external storages config |
|
128 | - * |
|
129 | - * @return array map of storage id to storage config |
|
130 | - */ |
|
131 | - protected function readConfig() { |
|
132 | - $mounts = $this->readDBConfig(); |
|
133 | - $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
134 | - $configs = array_filter($configs, function ($config) { |
|
135 | - return $config instanceof StorageConfig; |
|
136 | - }); |
|
137 | - |
|
138 | - $keys = array_map(function (StorageConfig $config) { |
|
139 | - return $config->getId(); |
|
140 | - }, $configs); |
|
141 | - |
|
142 | - return array_combine($keys, $configs); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Get a storage with status |
|
147 | - * |
|
148 | - * @param int $id storage id |
|
149 | - * |
|
150 | - * @return StorageConfig |
|
151 | - * @throws NotFoundException if the storage with the given id was not found |
|
152 | - */ |
|
153 | - public function getStorage($id) { |
|
154 | - $mount = $this->dbConfig->getMountById($id); |
|
155 | - |
|
156 | - if (!is_array($mount)) { |
|
157 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
158 | - } |
|
159 | - |
|
160 | - $config = $this->getStorageConfigFromDBMount($mount); |
|
161 | - if ($this->isApplicable($config)) { |
|
162 | - return $config; |
|
163 | - } else { |
|
164 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Check whether this storage service should provide access to a storage |
|
170 | - * |
|
171 | - * @param StorageConfig $config |
|
172 | - * @return bool |
|
173 | - */ |
|
174 | - abstract protected function isApplicable(StorageConfig $config); |
|
175 | - |
|
176 | - /** |
|
177 | - * Gets all storages, valid or not |
|
178 | - * |
|
179 | - * @return StorageConfig[] array of storage configs |
|
180 | - */ |
|
181 | - public function getAllStorages() { |
|
182 | - return $this->readConfig(); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Gets all valid storages |
|
187 | - * |
|
188 | - * @return StorageConfig[] |
|
189 | - */ |
|
190 | - public function getStorages() { |
|
191 | - return array_filter($this->getAllStorages(), [$this, 'validateStorage']); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Validate storage |
|
196 | - * FIXME: De-duplicate with StoragesController::validate() |
|
197 | - * |
|
198 | - * @param StorageConfig $storage |
|
199 | - * @return bool |
|
200 | - */ |
|
201 | - protected function validateStorage(StorageConfig $storage) { |
|
202 | - /** @var Backend */ |
|
203 | - $backend = $storage->getBackend(); |
|
204 | - /** @var AuthMechanism */ |
|
205 | - $authMechanism = $storage->getAuthMechanism(); |
|
206 | - |
|
207 | - if (!$backend->isVisibleFor($this->getVisibilityType())) { |
|
208 | - // not permitted to use backend |
|
209 | - return false; |
|
210 | - } |
|
211 | - if (!$authMechanism->isVisibleFor($this->getVisibilityType())) { |
|
212 | - // not permitted to use auth mechanism |
|
213 | - return false; |
|
214 | - } |
|
215 | - |
|
216 | - return true; |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Get the visibility type for this controller, used in validation |
|
221 | - * |
|
222 | - * @return string BackendService::VISIBILITY_* constants |
|
223 | - */ |
|
224 | - abstract public function getVisibilityType(); |
|
225 | - |
|
226 | - /** |
|
227 | - * @return integer |
|
228 | - */ |
|
229 | - protected function getType() { |
|
230 | - return DBConfigService::MOUNT_TYPE_ADMIN; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Add new storage to the configuration |
|
235 | - * |
|
236 | - * @param StorageConfig $newStorage storage attributes |
|
237 | - * |
|
238 | - * @return StorageConfig storage config, with added id |
|
239 | - */ |
|
240 | - public function addStorage(StorageConfig $newStorage) { |
|
241 | - $allStorages = $this->readConfig(); |
|
242 | - |
|
243 | - $configId = $this->dbConfig->addMount( |
|
244 | - $newStorage->getMountPoint(), |
|
245 | - $newStorage->getBackend()->getIdentifier(), |
|
246 | - $newStorage->getAuthMechanism()->getIdentifier(), |
|
247 | - $newStorage->getPriority(), |
|
248 | - $this->getType() |
|
249 | - ); |
|
250 | - |
|
251 | - $newStorage->setId($configId); |
|
252 | - |
|
253 | - foreach ($newStorage->getApplicableUsers() as $user) { |
|
254 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
255 | - } |
|
256 | - foreach ($newStorage->getApplicableGroups() as $group) { |
|
257 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
258 | - } |
|
259 | - foreach ($newStorage->getBackendOptions() as $key => $value) { |
|
260 | - $this->dbConfig->setConfig($configId, $key, $value); |
|
261 | - } |
|
262 | - foreach ($newStorage->getMountOptions() as $key => $value) { |
|
263 | - $this->dbConfig->setOption($configId, $key, $value); |
|
264 | - } |
|
265 | - |
|
266 | - if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) { |
|
267 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
268 | - } |
|
269 | - |
|
270 | - // add new storage |
|
271 | - $allStorages[$configId] = $newStorage; |
|
272 | - |
|
273 | - $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
274 | - |
|
275 | - $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS); |
|
276 | - return $newStorage; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Create a storage from its parameters |
|
281 | - * |
|
282 | - * @param string $mountPoint storage mount point |
|
283 | - * @param string $backendIdentifier backend identifier |
|
284 | - * @param string $authMechanismIdentifier authentication mechanism identifier |
|
285 | - * @param array $backendOptions backend-specific options |
|
286 | - * @param array|null $mountOptions mount-specific options |
|
287 | - * @param array|null $applicableUsers users for which to mount the storage |
|
288 | - * @param array|null $applicableGroups groups for which to mount the storage |
|
289 | - * @param int|null $priority priority |
|
290 | - * |
|
291 | - * @return StorageConfig |
|
292 | - */ |
|
293 | - public function createStorage( |
|
294 | - $mountPoint, |
|
295 | - $backendIdentifier, |
|
296 | - $authMechanismIdentifier, |
|
297 | - $backendOptions, |
|
298 | - $mountOptions = null, |
|
299 | - $applicableUsers = null, |
|
300 | - $applicableGroups = null, |
|
301 | - $priority = null |
|
302 | - ) { |
|
303 | - $backend = $this->backendService->getBackend($backendIdentifier); |
|
304 | - if (!$backend) { |
|
305 | - $backend = new InvalidBackend($backendIdentifier); |
|
306 | - } |
|
307 | - $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); |
|
308 | - if (!$authMechanism) { |
|
309 | - $authMechanism = new InvalidAuth($authMechanismIdentifier); |
|
310 | - } |
|
311 | - $newStorage = new StorageConfig(); |
|
312 | - $newStorage->setMountPoint($mountPoint); |
|
313 | - $newStorage->setBackend($backend); |
|
314 | - $newStorage->setAuthMechanism($authMechanism); |
|
315 | - $newStorage->setBackendOptions($backendOptions); |
|
316 | - if (isset($mountOptions)) { |
|
317 | - $newStorage->setMountOptions($mountOptions); |
|
318 | - } |
|
319 | - if (isset($applicableUsers)) { |
|
320 | - $newStorage->setApplicableUsers($applicableUsers); |
|
321 | - } |
|
322 | - if (isset($applicableGroups)) { |
|
323 | - $newStorage->setApplicableGroups($applicableGroups); |
|
324 | - } |
|
325 | - if (isset($priority)) { |
|
326 | - $newStorage->setPriority($priority); |
|
327 | - } |
|
328 | - |
|
329 | - return $newStorage; |
|
330 | - } |
|
331 | - |
|
332 | - /** |
|
333 | - * Triggers the given hook signal for all the applicables given |
|
334 | - * |
|
335 | - * @param string $signal signal |
|
336 | - * @param string $mountPoint hook mount pount param |
|
337 | - * @param string $mountType hook mount type param |
|
338 | - * @param array $applicableArray array of applicable users/groups for which to trigger the hook |
|
339 | - */ |
|
340 | - protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) { |
|
341 | - foreach ($applicableArray as $applicable) { |
|
342 | - \OCP\Util::emitHook( |
|
343 | - Filesystem::CLASSNAME, |
|
344 | - $signal, |
|
345 | - [ |
|
346 | - Filesystem::signal_param_path => $mountPoint, |
|
347 | - Filesystem::signal_param_mount_type => $mountType, |
|
348 | - Filesystem::signal_param_users => $applicable, |
|
349 | - ] |
|
350 | - ); |
|
351 | - } |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Triggers $signal for all applicable users of the given |
|
356 | - * storage |
|
357 | - * |
|
358 | - * @param StorageConfig $storage storage data |
|
359 | - * @param string $signal signal to trigger |
|
360 | - */ |
|
361 | - abstract protected function triggerHooks(StorageConfig $storage, $signal); |
|
362 | - |
|
363 | - /** |
|
364 | - * Triggers signal_create_mount or signal_delete_mount to |
|
365 | - * accommodate for additions/deletions in applicableUsers |
|
366 | - * and applicableGroups fields. |
|
367 | - * |
|
368 | - * @param StorageConfig $oldStorage old storage data |
|
369 | - * @param StorageConfig $newStorage new storage data |
|
370 | - */ |
|
371 | - abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage); |
|
372 | - |
|
373 | - /** |
|
374 | - * Update storage to the configuration |
|
375 | - * |
|
376 | - * @param StorageConfig $updatedStorage storage attributes |
|
377 | - * |
|
378 | - * @return StorageConfig storage config |
|
379 | - * @throws NotFoundException if the given storage does not exist in the config |
|
380 | - */ |
|
381 | - public function updateStorage(StorageConfig $updatedStorage) { |
|
382 | - $id = $updatedStorage->getId(); |
|
383 | - |
|
384 | - $existingMount = $this->dbConfig->getMountById($id); |
|
385 | - |
|
386 | - if (!is_array($existingMount)) { |
|
387 | - throw new NotFoundException('Storage with ID "' . $id . '" not found while updating storage'); |
|
388 | - } |
|
389 | - |
|
390 | - $oldStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
391 | - |
|
392 | - if ($oldStorage->getBackend() instanceof InvalidBackend) { |
|
393 | - throw new NotFoundException('Storage with id "' . $id . '" cannot be edited due to missing backend'); |
|
394 | - } |
|
395 | - |
|
396 | - $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); |
|
397 | - $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups()); |
|
398 | - $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); |
|
399 | - $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); |
|
400 | - |
|
401 | - $oldUserCount = count($oldStorage->getApplicableUsers()); |
|
402 | - $oldGroupCount = count($oldStorage->getApplicableGroups()); |
|
403 | - $newUserCount = count($updatedStorage->getApplicableUsers()); |
|
404 | - $newGroupCount = count($updatedStorage->getApplicableGroups()); |
|
405 | - $wasGlobal = ($oldUserCount + $oldGroupCount) === 0; |
|
406 | - $isGlobal = ($newUserCount + $newGroupCount) === 0; |
|
407 | - |
|
408 | - foreach ($removedUsers as $user) { |
|
409 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
410 | - } |
|
411 | - foreach ($removedGroups as $group) { |
|
412 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
413 | - } |
|
414 | - foreach ($addedUsers as $user) { |
|
415 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
416 | - } |
|
417 | - foreach ($addedGroups as $group) { |
|
418 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
419 | - } |
|
420 | - |
|
421 | - if ($wasGlobal && !$isGlobal) { |
|
422 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
423 | - } else if (!$wasGlobal && $isGlobal) { |
|
424 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
425 | - } |
|
426 | - |
|
427 | - $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions()); |
|
428 | - $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions()); |
|
429 | - |
|
430 | - foreach ($changedConfig as $key => $value) { |
|
431 | - if ($value !== DefinitionParameter::UNMODIFIED_PLACEHOLDER) { |
|
432 | - $this->dbConfig->setConfig($id, $key, $value); |
|
433 | - } |
|
434 | - } |
|
435 | - foreach ($changedOptions as $key => $value) { |
|
436 | - $this->dbConfig->setOption($id, $key, $value); |
|
437 | - } |
|
438 | - |
|
439 | - if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) { |
|
440 | - $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint()); |
|
441 | - } |
|
442 | - |
|
443 | - if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) { |
|
444 | - $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier()); |
|
445 | - } |
|
446 | - |
|
447 | - $this->triggerChangeHooks($oldStorage, $updatedStorage); |
|
448 | - |
|
449 | - if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly |
|
450 | - $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage)); |
|
451 | - } else { |
|
452 | - $storageId = $this->getStorageId($updatedStorage); |
|
453 | - foreach ($removedUsers as $userId) { |
|
454 | - $this->userMountCache->removeUserStorageMount($storageId, $userId); |
|
455 | - } |
|
456 | - } |
|
457 | - |
|
458 | - return $this->getStorage($id); |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Delete the storage with the given id. |
|
463 | - * |
|
464 | - * @param int $id storage id |
|
465 | - * |
|
466 | - * @throws NotFoundException if no storage was found with the given id |
|
467 | - */ |
|
468 | - public function removeStorage($id) { |
|
469 | - $existingMount = $this->dbConfig->getMountById($id); |
|
470 | - |
|
471 | - if (!is_array($existingMount)) { |
|
472 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
473 | - } |
|
474 | - |
|
475 | - $this->dbConfig->removeMount($id); |
|
476 | - |
|
477 | - $deletedStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
478 | - $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); |
|
479 | - |
|
480 | - // delete oc_storages entries and oc_filecache |
|
481 | - try { |
|
482 | - $rustyStorageId = $this->getRustyStorageIdFromConfig($deletedStorage); |
|
483 | - \OC\Files\Cache\Storage::remove($rustyStorageId); |
|
484 | - } catch (\Exception $e) { |
|
485 | - // can happen either for invalid configs where the storage could not |
|
486 | - // be instantiated or whenever $user vars where used, in which case |
|
487 | - // the storage id could not be computed |
|
488 | - \OC::$server->getLogger()->logException($e, [ |
|
489 | - 'level' => ILogger::ERROR, |
|
490 | - 'app' => 'files_external', |
|
491 | - ]); |
|
492 | - } |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * Returns the rusty storage id from oc_storages from the given storage config. |
|
497 | - * |
|
498 | - * @param StorageConfig $storageConfig |
|
499 | - * @return string rusty storage id |
|
500 | - */ |
|
501 | - private function getRustyStorageIdFromConfig(StorageConfig $storageConfig) { |
|
502 | - // if any of the storage options contains $user, it is not possible |
|
503 | - // to compute the possible storage id as we don't know which users |
|
504 | - // mounted it already (and we certainly don't want to iterate over ALL users) |
|
505 | - foreach ($storageConfig->getBackendOptions() as $value) { |
|
506 | - if (strpos($value, '$user') !== false) { |
|
507 | - throw new \Exception('Cannot compute storage id for deletion due to $user vars in the configuration'); |
|
508 | - } |
|
509 | - } |
|
510 | - |
|
511 | - // note: similar to ConfigAdapter->prepateStorageConfig() |
|
512 | - $storageConfig->getAuthMechanism()->manipulateStorageConfig($storageConfig); |
|
513 | - $storageConfig->getBackend()->manipulateStorageConfig($storageConfig); |
|
514 | - |
|
515 | - $class = $storageConfig->getBackend()->getStorageClass(); |
|
516 | - $storageImpl = new $class($storageConfig->getBackendOptions()); |
|
517 | - |
|
518 | - return $storageImpl->getId(); |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Construct the storage implementation |
|
523 | - * |
|
524 | - * @param StorageConfig $storageConfig |
|
525 | - * @return int |
|
526 | - */ |
|
527 | - private function getStorageId(StorageConfig $storageConfig) { |
|
528 | - try { |
|
529 | - $class = $storageConfig->getBackend()->getStorageClass(); |
|
530 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
531 | - $storage = new $class($storageConfig->getBackendOptions()); |
|
532 | - |
|
533 | - // auth mechanism should fire first |
|
534 | - $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
535 | - $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
536 | - |
|
537 | - return $storage->getStorageCache()->getNumericId(); |
|
538 | - } catch (\Exception $e) { |
|
539 | - return -1; |
|
540 | - } |
|
541 | - } |
|
51 | + /** @var BackendService */ |
|
52 | + protected $backendService; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var DBConfigService |
|
56 | + */ |
|
57 | + protected $dbConfig; |
|
58 | + |
|
59 | + /** |
|
60 | + * @var IUserMountCache |
|
61 | + */ |
|
62 | + protected $userMountCache; |
|
63 | + |
|
64 | + /** |
|
65 | + * @param BackendService $backendService |
|
66 | + * @param DBConfigService $dbConfigService |
|
67 | + * @param IUserMountCache $userMountCache |
|
68 | + */ |
|
69 | + public function __construct(BackendService $backendService, DBConfigService $dbConfigService, IUserMountCache $userMountCache) { |
|
70 | + $this->backendService = $backendService; |
|
71 | + $this->dbConfig = $dbConfigService; |
|
72 | + $this->userMountCache = $userMountCache; |
|
73 | + } |
|
74 | + |
|
75 | + protected function readDBConfig() { |
|
76 | + return $this->dbConfig->getAdminMounts(); |
|
77 | + } |
|
78 | + |
|
79 | + protected function getStorageConfigFromDBMount(array $mount) { |
|
80 | + $applicableUsers = array_filter($mount['applicable'], function ($applicable) { |
|
81 | + return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; |
|
82 | + }); |
|
83 | + $applicableUsers = array_map(function ($applicable) { |
|
84 | + return $applicable['value']; |
|
85 | + }, $applicableUsers); |
|
86 | + |
|
87 | + $applicableGroups = array_filter($mount['applicable'], function ($applicable) { |
|
88 | + return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; |
|
89 | + }); |
|
90 | + $applicableGroups = array_map(function ($applicable) { |
|
91 | + return $applicable['value']; |
|
92 | + }, $applicableGroups); |
|
93 | + |
|
94 | + try { |
|
95 | + $config = $this->createStorage( |
|
96 | + $mount['mount_point'], |
|
97 | + $mount['storage_backend'], |
|
98 | + $mount['auth_backend'], |
|
99 | + $mount['config'], |
|
100 | + $mount['options'], |
|
101 | + array_values($applicableUsers), |
|
102 | + array_values($applicableGroups), |
|
103 | + $mount['priority'] |
|
104 | + ); |
|
105 | + $config->setType($mount['type']); |
|
106 | + $config->setId((int)$mount['mount_id']); |
|
107 | + return $config; |
|
108 | + } catch (\UnexpectedValueException $e) { |
|
109 | + // don't die if a storage backend doesn't exist |
|
110 | + \OC::$server->getLogger()->logException($e, [ |
|
111 | + 'message' => 'Could not load storage.', |
|
112 | + 'level' => ILogger::ERROR, |
|
113 | + 'app' => 'files_external', |
|
114 | + ]); |
|
115 | + return null; |
|
116 | + } catch (\InvalidArgumentException $e) { |
|
117 | + \OC::$server->getLogger()->logException($e, [ |
|
118 | + 'message' => 'Could not load storage.', |
|
119 | + 'level' => ILogger::ERROR, |
|
120 | + 'app' => 'files_external', |
|
121 | + ]); |
|
122 | + return null; |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Read the external storages config |
|
128 | + * |
|
129 | + * @return array map of storage id to storage config |
|
130 | + */ |
|
131 | + protected function readConfig() { |
|
132 | + $mounts = $this->readDBConfig(); |
|
133 | + $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
134 | + $configs = array_filter($configs, function ($config) { |
|
135 | + return $config instanceof StorageConfig; |
|
136 | + }); |
|
137 | + |
|
138 | + $keys = array_map(function (StorageConfig $config) { |
|
139 | + return $config->getId(); |
|
140 | + }, $configs); |
|
141 | + |
|
142 | + return array_combine($keys, $configs); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Get a storage with status |
|
147 | + * |
|
148 | + * @param int $id storage id |
|
149 | + * |
|
150 | + * @return StorageConfig |
|
151 | + * @throws NotFoundException if the storage with the given id was not found |
|
152 | + */ |
|
153 | + public function getStorage($id) { |
|
154 | + $mount = $this->dbConfig->getMountById($id); |
|
155 | + |
|
156 | + if (!is_array($mount)) { |
|
157 | + throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
158 | + } |
|
159 | + |
|
160 | + $config = $this->getStorageConfigFromDBMount($mount); |
|
161 | + if ($this->isApplicable($config)) { |
|
162 | + return $config; |
|
163 | + } else { |
|
164 | + throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Check whether this storage service should provide access to a storage |
|
170 | + * |
|
171 | + * @param StorageConfig $config |
|
172 | + * @return bool |
|
173 | + */ |
|
174 | + abstract protected function isApplicable(StorageConfig $config); |
|
175 | + |
|
176 | + /** |
|
177 | + * Gets all storages, valid or not |
|
178 | + * |
|
179 | + * @return StorageConfig[] array of storage configs |
|
180 | + */ |
|
181 | + public function getAllStorages() { |
|
182 | + return $this->readConfig(); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Gets all valid storages |
|
187 | + * |
|
188 | + * @return StorageConfig[] |
|
189 | + */ |
|
190 | + public function getStorages() { |
|
191 | + return array_filter($this->getAllStorages(), [$this, 'validateStorage']); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Validate storage |
|
196 | + * FIXME: De-duplicate with StoragesController::validate() |
|
197 | + * |
|
198 | + * @param StorageConfig $storage |
|
199 | + * @return bool |
|
200 | + */ |
|
201 | + protected function validateStorage(StorageConfig $storage) { |
|
202 | + /** @var Backend */ |
|
203 | + $backend = $storage->getBackend(); |
|
204 | + /** @var AuthMechanism */ |
|
205 | + $authMechanism = $storage->getAuthMechanism(); |
|
206 | + |
|
207 | + if (!$backend->isVisibleFor($this->getVisibilityType())) { |
|
208 | + // not permitted to use backend |
|
209 | + return false; |
|
210 | + } |
|
211 | + if (!$authMechanism->isVisibleFor($this->getVisibilityType())) { |
|
212 | + // not permitted to use auth mechanism |
|
213 | + return false; |
|
214 | + } |
|
215 | + |
|
216 | + return true; |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Get the visibility type for this controller, used in validation |
|
221 | + * |
|
222 | + * @return string BackendService::VISIBILITY_* constants |
|
223 | + */ |
|
224 | + abstract public function getVisibilityType(); |
|
225 | + |
|
226 | + /** |
|
227 | + * @return integer |
|
228 | + */ |
|
229 | + protected function getType() { |
|
230 | + return DBConfigService::MOUNT_TYPE_ADMIN; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Add new storage to the configuration |
|
235 | + * |
|
236 | + * @param StorageConfig $newStorage storage attributes |
|
237 | + * |
|
238 | + * @return StorageConfig storage config, with added id |
|
239 | + */ |
|
240 | + public function addStorage(StorageConfig $newStorage) { |
|
241 | + $allStorages = $this->readConfig(); |
|
242 | + |
|
243 | + $configId = $this->dbConfig->addMount( |
|
244 | + $newStorage->getMountPoint(), |
|
245 | + $newStorage->getBackend()->getIdentifier(), |
|
246 | + $newStorage->getAuthMechanism()->getIdentifier(), |
|
247 | + $newStorage->getPriority(), |
|
248 | + $this->getType() |
|
249 | + ); |
|
250 | + |
|
251 | + $newStorage->setId($configId); |
|
252 | + |
|
253 | + foreach ($newStorage->getApplicableUsers() as $user) { |
|
254 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
255 | + } |
|
256 | + foreach ($newStorage->getApplicableGroups() as $group) { |
|
257 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
258 | + } |
|
259 | + foreach ($newStorage->getBackendOptions() as $key => $value) { |
|
260 | + $this->dbConfig->setConfig($configId, $key, $value); |
|
261 | + } |
|
262 | + foreach ($newStorage->getMountOptions() as $key => $value) { |
|
263 | + $this->dbConfig->setOption($configId, $key, $value); |
|
264 | + } |
|
265 | + |
|
266 | + if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) { |
|
267 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
268 | + } |
|
269 | + |
|
270 | + // add new storage |
|
271 | + $allStorages[$configId] = $newStorage; |
|
272 | + |
|
273 | + $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
274 | + |
|
275 | + $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS); |
|
276 | + return $newStorage; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Create a storage from its parameters |
|
281 | + * |
|
282 | + * @param string $mountPoint storage mount point |
|
283 | + * @param string $backendIdentifier backend identifier |
|
284 | + * @param string $authMechanismIdentifier authentication mechanism identifier |
|
285 | + * @param array $backendOptions backend-specific options |
|
286 | + * @param array|null $mountOptions mount-specific options |
|
287 | + * @param array|null $applicableUsers users for which to mount the storage |
|
288 | + * @param array|null $applicableGroups groups for which to mount the storage |
|
289 | + * @param int|null $priority priority |
|
290 | + * |
|
291 | + * @return StorageConfig |
|
292 | + */ |
|
293 | + public function createStorage( |
|
294 | + $mountPoint, |
|
295 | + $backendIdentifier, |
|
296 | + $authMechanismIdentifier, |
|
297 | + $backendOptions, |
|
298 | + $mountOptions = null, |
|
299 | + $applicableUsers = null, |
|
300 | + $applicableGroups = null, |
|
301 | + $priority = null |
|
302 | + ) { |
|
303 | + $backend = $this->backendService->getBackend($backendIdentifier); |
|
304 | + if (!$backend) { |
|
305 | + $backend = new InvalidBackend($backendIdentifier); |
|
306 | + } |
|
307 | + $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); |
|
308 | + if (!$authMechanism) { |
|
309 | + $authMechanism = new InvalidAuth($authMechanismIdentifier); |
|
310 | + } |
|
311 | + $newStorage = new StorageConfig(); |
|
312 | + $newStorage->setMountPoint($mountPoint); |
|
313 | + $newStorage->setBackend($backend); |
|
314 | + $newStorage->setAuthMechanism($authMechanism); |
|
315 | + $newStorage->setBackendOptions($backendOptions); |
|
316 | + if (isset($mountOptions)) { |
|
317 | + $newStorage->setMountOptions($mountOptions); |
|
318 | + } |
|
319 | + if (isset($applicableUsers)) { |
|
320 | + $newStorage->setApplicableUsers($applicableUsers); |
|
321 | + } |
|
322 | + if (isset($applicableGroups)) { |
|
323 | + $newStorage->setApplicableGroups($applicableGroups); |
|
324 | + } |
|
325 | + if (isset($priority)) { |
|
326 | + $newStorage->setPriority($priority); |
|
327 | + } |
|
328 | + |
|
329 | + return $newStorage; |
|
330 | + } |
|
331 | + |
|
332 | + /** |
|
333 | + * Triggers the given hook signal for all the applicables given |
|
334 | + * |
|
335 | + * @param string $signal signal |
|
336 | + * @param string $mountPoint hook mount pount param |
|
337 | + * @param string $mountType hook mount type param |
|
338 | + * @param array $applicableArray array of applicable users/groups for which to trigger the hook |
|
339 | + */ |
|
340 | + protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) { |
|
341 | + foreach ($applicableArray as $applicable) { |
|
342 | + \OCP\Util::emitHook( |
|
343 | + Filesystem::CLASSNAME, |
|
344 | + $signal, |
|
345 | + [ |
|
346 | + Filesystem::signal_param_path => $mountPoint, |
|
347 | + Filesystem::signal_param_mount_type => $mountType, |
|
348 | + Filesystem::signal_param_users => $applicable, |
|
349 | + ] |
|
350 | + ); |
|
351 | + } |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Triggers $signal for all applicable users of the given |
|
356 | + * storage |
|
357 | + * |
|
358 | + * @param StorageConfig $storage storage data |
|
359 | + * @param string $signal signal to trigger |
|
360 | + */ |
|
361 | + abstract protected function triggerHooks(StorageConfig $storage, $signal); |
|
362 | + |
|
363 | + /** |
|
364 | + * Triggers signal_create_mount or signal_delete_mount to |
|
365 | + * accommodate for additions/deletions in applicableUsers |
|
366 | + * and applicableGroups fields. |
|
367 | + * |
|
368 | + * @param StorageConfig $oldStorage old storage data |
|
369 | + * @param StorageConfig $newStorage new storage data |
|
370 | + */ |
|
371 | + abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage); |
|
372 | + |
|
373 | + /** |
|
374 | + * Update storage to the configuration |
|
375 | + * |
|
376 | + * @param StorageConfig $updatedStorage storage attributes |
|
377 | + * |
|
378 | + * @return StorageConfig storage config |
|
379 | + * @throws NotFoundException if the given storage does not exist in the config |
|
380 | + */ |
|
381 | + public function updateStorage(StorageConfig $updatedStorage) { |
|
382 | + $id = $updatedStorage->getId(); |
|
383 | + |
|
384 | + $existingMount = $this->dbConfig->getMountById($id); |
|
385 | + |
|
386 | + if (!is_array($existingMount)) { |
|
387 | + throw new NotFoundException('Storage with ID "' . $id . '" not found while updating storage'); |
|
388 | + } |
|
389 | + |
|
390 | + $oldStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
391 | + |
|
392 | + if ($oldStorage->getBackend() instanceof InvalidBackend) { |
|
393 | + throw new NotFoundException('Storage with id "' . $id . '" cannot be edited due to missing backend'); |
|
394 | + } |
|
395 | + |
|
396 | + $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); |
|
397 | + $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups()); |
|
398 | + $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); |
|
399 | + $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); |
|
400 | + |
|
401 | + $oldUserCount = count($oldStorage->getApplicableUsers()); |
|
402 | + $oldGroupCount = count($oldStorage->getApplicableGroups()); |
|
403 | + $newUserCount = count($updatedStorage->getApplicableUsers()); |
|
404 | + $newGroupCount = count($updatedStorage->getApplicableGroups()); |
|
405 | + $wasGlobal = ($oldUserCount + $oldGroupCount) === 0; |
|
406 | + $isGlobal = ($newUserCount + $newGroupCount) === 0; |
|
407 | + |
|
408 | + foreach ($removedUsers as $user) { |
|
409 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
410 | + } |
|
411 | + foreach ($removedGroups as $group) { |
|
412 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
413 | + } |
|
414 | + foreach ($addedUsers as $user) { |
|
415 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
416 | + } |
|
417 | + foreach ($addedGroups as $group) { |
|
418 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
419 | + } |
|
420 | + |
|
421 | + if ($wasGlobal && !$isGlobal) { |
|
422 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
423 | + } else if (!$wasGlobal && $isGlobal) { |
|
424 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
425 | + } |
|
426 | + |
|
427 | + $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions()); |
|
428 | + $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions()); |
|
429 | + |
|
430 | + foreach ($changedConfig as $key => $value) { |
|
431 | + if ($value !== DefinitionParameter::UNMODIFIED_PLACEHOLDER) { |
|
432 | + $this->dbConfig->setConfig($id, $key, $value); |
|
433 | + } |
|
434 | + } |
|
435 | + foreach ($changedOptions as $key => $value) { |
|
436 | + $this->dbConfig->setOption($id, $key, $value); |
|
437 | + } |
|
438 | + |
|
439 | + if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) { |
|
440 | + $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint()); |
|
441 | + } |
|
442 | + |
|
443 | + if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) { |
|
444 | + $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier()); |
|
445 | + } |
|
446 | + |
|
447 | + $this->triggerChangeHooks($oldStorage, $updatedStorage); |
|
448 | + |
|
449 | + if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly |
|
450 | + $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage)); |
|
451 | + } else { |
|
452 | + $storageId = $this->getStorageId($updatedStorage); |
|
453 | + foreach ($removedUsers as $userId) { |
|
454 | + $this->userMountCache->removeUserStorageMount($storageId, $userId); |
|
455 | + } |
|
456 | + } |
|
457 | + |
|
458 | + return $this->getStorage($id); |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Delete the storage with the given id. |
|
463 | + * |
|
464 | + * @param int $id storage id |
|
465 | + * |
|
466 | + * @throws NotFoundException if no storage was found with the given id |
|
467 | + */ |
|
468 | + public function removeStorage($id) { |
|
469 | + $existingMount = $this->dbConfig->getMountById($id); |
|
470 | + |
|
471 | + if (!is_array($existingMount)) { |
|
472 | + throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
473 | + } |
|
474 | + |
|
475 | + $this->dbConfig->removeMount($id); |
|
476 | + |
|
477 | + $deletedStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
478 | + $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); |
|
479 | + |
|
480 | + // delete oc_storages entries and oc_filecache |
|
481 | + try { |
|
482 | + $rustyStorageId = $this->getRustyStorageIdFromConfig($deletedStorage); |
|
483 | + \OC\Files\Cache\Storage::remove($rustyStorageId); |
|
484 | + } catch (\Exception $e) { |
|
485 | + // can happen either for invalid configs where the storage could not |
|
486 | + // be instantiated or whenever $user vars where used, in which case |
|
487 | + // the storage id could not be computed |
|
488 | + \OC::$server->getLogger()->logException($e, [ |
|
489 | + 'level' => ILogger::ERROR, |
|
490 | + 'app' => 'files_external', |
|
491 | + ]); |
|
492 | + } |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * Returns the rusty storage id from oc_storages from the given storage config. |
|
497 | + * |
|
498 | + * @param StorageConfig $storageConfig |
|
499 | + * @return string rusty storage id |
|
500 | + */ |
|
501 | + private function getRustyStorageIdFromConfig(StorageConfig $storageConfig) { |
|
502 | + // if any of the storage options contains $user, it is not possible |
|
503 | + // to compute the possible storage id as we don't know which users |
|
504 | + // mounted it already (and we certainly don't want to iterate over ALL users) |
|
505 | + foreach ($storageConfig->getBackendOptions() as $value) { |
|
506 | + if (strpos($value, '$user') !== false) { |
|
507 | + throw new \Exception('Cannot compute storage id for deletion due to $user vars in the configuration'); |
|
508 | + } |
|
509 | + } |
|
510 | + |
|
511 | + // note: similar to ConfigAdapter->prepateStorageConfig() |
|
512 | + $storageConfig->getAuthMechanism()->manipulateStorageConfig($storageConfig); |
|
513 | + $storageConfig->getBackend()->manipulateStorageConfig($storageConfig); |
|
514 | + |
|
515 | + $class = $storageConfig->getBackend()->getStorageClass(); |
|
516 | + $storageImpl = new $class($storageConfig->getBackendOptions()); |
|
517 | + |
|
518 | + return $storageImpl->getId(); |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Construct the storage implementation |
|
523 | + * |
|
524 | + * @param StorageConfig $storageConfig |
|
525 | + * @return int |
|
526 | + */ |
|
527 | + private function getStorageId(StorageConfig $storageConfig) { |
|
528 | + try { |
|
529 | + $class = $storageConfig->getBackend()->getStorageClass(); |
|
530 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
531 | + $storage = new $class($storageConfig->getBackendOptions()); |
|
532 | + |
|
533 | + // auth mechanism should fire first |
|
534 | + $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
535 | + $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
536 | + |
|
537 | + return $storage->getStorageCache()->getNumericId(); |
|
538 | + } catch (\Exception $e) { |
|
539 | + return -1; |
|
540 | + } |
|
541 | + } |
|
542 | 542 | } |