You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

432 lines
8.7 KiB

9 years ago
9 years ago
10 years ago
Authentication mechanisms for external storage backends A backend can now specify generic authentication schemes that it supports, instead of specifying the parameters for its authentication method directly. This allows multiple authentication mechanisms to be implemented for a single scheme, providing altered functionality. This commit introduces the backend framework for this feature, and so at this point the UI will be broken as the frontend does not specify the required information. Terminology: - authentication scheme Parameter interface for the authentication method. A backend supporting the 'password' scheme accepts two parameters, 'user' and 'password'. - authentication mechanism Specific mechanism implementing a scheme. Basic mechanisms may forward configuration options directly to the backend, more advanced ones may lookup parameters or retrieve them from the session New dropdown selector for external storage configurations to select the authentication mechanism to be used. Authentication mechanisms can have visibilities, just like backends. The API was extended too to make it easier to add/remove visibilities. In addition, the concept of 'allowed visibility' has been introduced, so a backend/auth mechanism can force a maximum visibility level (e.g. Local storage type) that cannot be overridden by configuration in the web UI. An authentication mechanism is a fully instantiated implementation. This allows an implementation to have dependencies injected into it, e.g. an \OCP\IDB for database operations. When a StorageConfig is being prepared for mounting, the authentication mechanism implementation has manipulateStorage() called, which inserts the relevant authentication method options into the storage ready for mounting.
10 years ago
Authentication mechanisms for external storage backends A backend can now specify generic authentication schemes that it supports, instead of specifying the parameters for its authentication method directly. This allows multiple authentication mechanisms to be implemented for a single scheme, providing altered functionality. This commit introduces the backend framework for this feature, and so at this point the UI will be broken as the frontend does not specify the required information. Terminology: - authentication scheme Parameter interface for the authentication method. A backend supporting the 'password' scheme accepts two parameters, 'user' and 'password'. - authentication mechanism Specific mechanism implementing a scheme. Basic mechanisms may forward configuration options directly to the backend, more advanced ones may lookup parameters or retrieve them from the session New dropdown selector for external storage configurations to select the authentication mechanism to be used. Authentication mechanisms can have visibilities, just like backends. The API was extended too to make it easier to add/remove visibilities. In addition, the concept of 'allowed visibility' has been introduced, so a backend/auth mechanism can force a maximum visibility level (e.g. Local storage type) that cannot be overridden by configuration in the web UI. An authentication mechanism is a fully instantiated implementation. This allows an implementation to have dependencies injected into it, e.g. an \OCP\IDB for database operations. When a StorageConfig is being prepared for mounting, the authentication mechanism implementation has manipulateStorage() called, which inserts the relevant authentication method options into the storage ready for mounting.
10 years ago
Authentication mechanisms for external storage backends A backend can now specify generic authentication schemes that it supports, instead of specifying the parameters for its authentication method directly. This allows multiple authentication mechanisms to be implemented for a single scheme, providing altered functionality. This commit introduces the backend framework for this feature, and so at this point the UI will be broken as the frontend does not specify the required information. Terminology: - authentication scheme Parameter interface for the authentication method. A backend supporting the 'password' scheme accepts two parameters, 'user' and 'password'. - authentication mechanism Specific mechanism implementing a scheme. Basic mechanisms may forward configuration options directly to the backend, more advanced ones may lookup parameters or retrieve them from the session New dropdown selector for external storage configurations to select the authentication mechanism to be used. Authentication mechanisms can have visibilities, just like backends. The API was extended too to make it easier to add/remove visibilities. In addition, the concept of 'allowed visibility' has been introduced, so a backend/auth mechanism can force a maximum visibility level (e.g. Local storage type) that cannot be overridden by configuration in the web UI. An authentication mechanism is a fully instantiated implementation. This allows an implementation to have dependencies injected into it, e.g. an \OCP\IDB for database operations. When a StorageConfig is being prepared for mounting, the authentication mechanism implementation has manipulateStorage() called, which inserts the relevant authentication method options into the storage ready for mounting.
10 years ago
Authentication mechanisms for external storage backends A backend can now specify generic authentication schemes that it supports, instead of specifying the parameters for its authentication method directly. This allows multiple authentication mechanisms to be implemented for a single scheme, providing altered functionality. This commit introduces the backend framework for this feature, and so at this point the UI will be broken as the frontend does not specify the required information. Terminology: - authentication scheme Parameter interface for the authentication method. A backend supporting the 'password' scheme accepts two parameters, 'user' and 'password'. - authentication mechanism Specific mechanism implementing a scheme. Basic mechanisms may forward configuration options directly to the backend, more advanced ones may lookup parameters or retrieve them from the session New dropdown selector for external storage configurations to select the authentication mechanism to be used. Authentication mechanisms can have visibilities, just like backends. The API was extended too to make it easier to add/remove visibilities. In addition, the concept of 'allowed visibility' has been introduced, so a backend/auth mechanism can force a maximum visibility level (e.g. Local storage type) that cannot be overridden by configuration in the web UI. An authentication mechanism is a fully instantiated implementation. This allows an implementation to have dependencies injected into it, e.g. an \OCP\IDB for database operations. When a StorageConfig is being prepared for mounting, the authentication mechanism implementation has manipulateStorage() called, which inserts the relevant authentication method options into the storage ready for mounting.
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Jesús Macias <jmacias@solidgear.es>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_External\Lib;
  28. use OCA\Files_External\Lib\Auth\IUserProvided;
  29. use \OCA\Files_External\Lib\Backend\Backend;
  30. use \OCA\Files_External\Lib\Auth\AuthMechanism;
  31. /**
  32. * External storage configuration
  33. */
  34. class StorageConfig implements \JsonSerializable {
  35. const MOUNT_TYPE_ADMIN = 1;
  36. const MOUNT_TYPE_PERSONAl = 2;
  37. /**
  38. * Storage config id
  39. *
  40. * @var int
  41. */
  42. private $id;
  43. /**
  44. * Backend
  45. *
  46. * @var Backend
  47. */
  48. private $backend;
  49. /**
  50. * Authentication mechanism
  51. *
  52. * @var AuthMechanism
  53. */
  54. private $authMechanism;
  55. /**
  56. * Backend options
  57. *
  58. * @var array
  59. */
  60. private $backendOptions = [];
  61. /**
  62. * Mount point path, relative to the user's "files" folder
  63. *
  64. * @var string
  65. */
  66. private $mountPoint;
  67. /**
  68. * Storage status
  69. *
  70. * @var int
  71. */
  72. private $status;
  73. /**
  74. * Status message
  75. *
  76. * @var string
  77. */
  78. private $statusMessage;
  79. /**
  80. * Priority
  81. *
  82. * @var int
  83. */
  84. private $priority;
  85. /**
  86. * List of users who have access to this storage
  87. *
  88. * @var array
  89. */
  90. private $applicableUsers = [];
  91. /**
  92. * List of groups that have access to this storage
  93. *
  94. * @var array
  95. */
  96. private $applicableGroups = [];
  97. /**
  98. * Mount-specific options
  99. *
  100. * @var array
  101. */
  102. private $mountOptions = [];
  103. /**
  104. * Whether it's a personal or admin mount
  105. *
  106. * @var int
  107. */
  108. private $type;
  109. /**
  110. * Creates a storage config
  111. *
  112. * @param int|null $id config id or null for a new config
  113. */
  114. public function __construct($id = null) {
  115. $this->id = $id;
  116. $this->mountOptions['enable_sharing'] = false;
  117. }
  118. /**
  119. * Returns the configuration id
  120. *
  121. * @return int
  122. */
  123. public function getId() {
  124. return $this->id;
  125. }
  126. /**
  127. * Sets the configuration id
  128. *
  129. * @param int $id configuration id
  130. */
  131. public function setId($id) {
  132. $this->id = $id;
  133. }
  134. /**
  135. * Returns mount point path relative to the user's
  136. * "files" folder.
  137. *
  138. * @return string path
  139. */
  140. public function getMountPoint() {
  141. return $this->mountPoint;
  142. }
  143. /**
  144. * Sets mount point path relative to the user's
  145. * "files" folder.
  146. * The path will be normalized.
  147. *
  148. * @param string $mountPoint path
  149. */
  150. public function setMountPoint($mountPoint) {
  151. $this->mountPoint = \OC\Files\Filesystem::normalizePath($mountPoint);
  152. }
  153. /**
  154. * @return Backend
  155. */
  156. public function getBackend() {
  157. return $this->backend;
  158. }
  159. /**
  160. * @param Backend $backend
  161. */
  162. public function setBackend(Backend $backend) {
  163. $this->backend= $backend;
  164. }
  165. /**
  166. * @return AuthMechanism
  167. */
  168. public function getAuthMechanism() {
  169. return $this->authMechanism;
  170. }
  171. /**
  172. * @param AuthMechanism $authMechanism
  173. */
  174. public function setAuthMechanism(AuthMechanism $authMechanism) {
  175. $this->authMechanism = $authMechanism;
  176. }
  177. /**
  178. * Returns the external storage backend-specific options
  179. *
  180. * @return array backend options
  181. */
  182. public function getBackendOptions() {
  183. return $this->backendOptions;
  184. }
  185. /**
  186. * Sets the external storage backend-specific options
  187. *
  188. * @param array $backendOptions backend options
  189. */
  190. public function setBackendOptions($backendOptions) {
  191. if($this->getBackend() instanceof Backend) {
  192. $parameters = $this->getBackend()->getParameters();
  193. foreach($backendOptions as $key => $value) {
  194. if(isset($parameters[$key])) {
  195. switch ($parameters[$key]->getType()) {
  196. case \OCA\Files_External\Lib\DefinitionParameter::VALUE_BOOLEAN:
  197. $value = (bool)$value;
  198. break;
  199. }
  200. $backendOptions[$key] = $value;
  201. }
  202. }
  203. }
  204. $this->backendOptions = $backendOptions;
  205. }
  206. /**
  207. * @param string $key
  208. * @return mixed
  209. */
  210. public function getBackendOption($key) {
  211. if (isset($this->backendOptions[$key])) {
  212. return $this->backendOptions[$key];
  213. }
  214. return null;
  215. }
  216. /**
  217. * @param string $key
  218. * @param mixed $value
  219. */
  220. public function setBackendOption($key, $value) {
  221. $this->backendOptions[$key] = $value;
  222. }
  223. /**
  224. * Returns the mount priority
  225. *
  226. * @return int priority
  227. */
  228. public function getPriority() {
  229. return $this->priority;
  230. }
  231. /**
  232. * Sets the mount priotity
  233. *
  234. * @param int $priority priority
  235. */
  236. public function setPriority($priority) {
  237. $this->priority = $priority;
  238. }
  239. /**
  240. * Returns the users for which to mount this storage
  241. *
  242. * @return array applicable users
  243. */
  244. public function getApplicableUsers() {
  245. return $this->applicableUsers;
  246. }
  247. /**
  248. * Sets the users for which to mount this storage
  249. *
  250. * @param array|null $applicableUsers applicable users
  251. */
  252. public function setApplicableUsers($applicableUsers) {
  253. if (is_null($applicableUsers)) {
  254. $applicableUsers = [];
  255. }
  256. $this->applicableUsers = $applicableUsers;
  257. }
  258. /**
  259. * Returns the groups for which to mount this storage
  260. *
  261. * @return array applicable groups
  262. */
  263. public function getApplicableGroups() {
  264. return $this->applicableGroups;
  265. }
  266. /**
  267. * Sets the groups for which to mount this storage
  268. *
  269. * @param array|null $applicableGroups applicable groups
  270. */
  271. public function setApplicableGroups($applicableGroups) {
  272. if (is_null($applicableGroups)) {
  273. $applicableGroups = [];
  274. }
  275. $this->applicableGroups = $applicableGroups;
  276. }
  277. /**
  278. * Returns the mount-specific options
  279. *
  280. * @return array mount specific options
  281. */
  282. public function getMountOptions() {
  283. return $this->mountOptions;
  284. }
  285. /**
  286. * Sets the mount-specific options
  287. *
  288. * @param array $mountOptions applicable groups
  289. */
  290. public function setMountOptions($mountOptions) {
  291. if (is_null($mountOptions)) {
  292. $mountOptions = [];
  293. }
  294. $this->mountOptions = $mountOptions;
  295. }
  296. /**
  297. * @param string $key
  298. * @return mixed
  299. */
  300. public function getMountOption($key) {
  301. if (isset($this->mountOptions[$key])) {
  302. return $this->mountOptions[$key];
  303. }
  304. return null;
  305. }
  306. /**
  307. * @param string $key
  308. * @param mixed $value
  309. */
  310. public function setMountOption($key, $value) {
  311. $this->mountOptions[$key] = $value;
  312. }
  313. /**
  314. * Gets the storage status, whether the config worked last time
  315. *
  316. * @return int $status status
  317. */
  318. public function getStatus() {
  319. return $this->status;
  320. }
  321. /**
  322. * Gets the message describing the storage status
  323. *
  324. * @return string|null
  325. */
  326. public function getStatusMessage() {
  327. return $this->statusMessage;
  328. }
  329. /**
  330. * Sets the storage status, whether the config worked last time
  331. *
  332. * @param int $status status
  333. * @param string|null $message optional message
  334. */
  335. public function setStatus($status, $message = null) {
  336. $this->status = $status;
  337. $this->statusMessage = $message;
  338. }
  339. /**
  340. * @return int self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl
  341. */
  342. public function getType() {
  343. return $this->type;
  344. }
  345. /**
  346. * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl
  347. */
  348. public function setType($type) {
  349. $this->type = $type;
  350. }
  351. /**
  352. * Serialize config to JSON
  353. *
  354. * @return array
  355. */
  356. public function jsonSerialize() {
  357. $result = [];
  358. if (!is_null($this->id)) {
  359. $result['id'] = $this->id;
  360. }
  361. $result['mountPoint'] = $this->mountPoint;
  362. $result['backend'] = $this->backend->getIdentifier();
  363. $result['authMechanism'] = $this->authMechanism->getIdentifier();
  364. $result['backendOptions'] = $this->backendOptions;
  365. if (!is_null($this->priority)) {
  366. $result['priority'] = $this->priority;
  367. }
  368. if (!empty($this->applicableUsers)) {
  369. $result['applicableUsers'] = $this->applicableUsers;
  370. }
  371. if (!empty($this->applicableGroups)) {
  372. $result['applicableGroups'] = $this->applicableGroups;
  373. }
  374. if (!empty($this->mountOptions)) {
  375. $result['mountOptions'] = $this->mountOptions;
  376. }
  377. if (!is_null($this->status)) {
  378. $result['status'] = $this->status;
  379. }
  380. if (!is_null($this->statusMessage)) {
  381. $result['statusMessage'] = $this->statusMessage;
  382. }
  383. $result['userProvided'] = $this->authMechanism instanceof IUserProvided;
  384. $result['type'] = ($this->getType() === self::MOUNT_TYPE_PERSONAl) ? 'personal': 'system';
  385. return $result;
  386. }
  387. }