StorageConfig.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Jesús Macias <jmacias@solidgear.es>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_External\Lib;
  30. use OCA\Files_External\Lib\Auth\AuthMechanism;
  31. use OCA\Files_External\Lib\Auth\IUserProvided;
  32. use OCA\Files_External\Lib\Backend\Backend;
  33. /**
  34. * External storage configuration
  35. */
  36. class StorageConfig implements \JsonSerializable {
  37. public const MOUNT_TYPE_ADMIN = 1;
  38. public const MOUNT_TYPE_PERSONAl = 2;
  39. /**
  40. * Storage config id
  41. *
  42. * @var int
  43. */
  44. private $id;
  45. /**
  46. * Backend
  47. *
  48. * @var Backend
  49. */
  50. private $backend;
  51. /**
  52. * Authentication mechanism
  53. *
  54. * @var AuthMechanism
  55. */
  56. private $authMechanism;
  57. /**
  58. * Backend options
  59. *
  60. * @var array
  61. */
  62. private $backendOptions = [];
  63. /**
  64. * Mount point path, relative to the user's "files" folder
  65. *
  66. * @var string
  67. */
  68. private $mountPoint;
  69. /**
  70. * Storage status
  71. *
  72. * @var int
  73. */
  74. private $status;
  75. /**
  76. * Status message
  77. *
  78. * @var string
  79. */
  80. private $statusMessage;
  81. /**
  82. * Priority
  83. *
  84. * @var int
  85. */
  86. private $priority;
  87. /**
  88. * List of users who have access to this storage
  89. *
  90. * @var string[]
  91. */
  92. private $applicableUsers = [];
  93. /**
  94. * List of groups that have access to this storage
  95. *
  96. * @var string[]
  97. */
  98. private $applicableGroups = [];
  99. /**
  100. * Mount-specific options
  101. *
  102. * @var array
  103. */
  104. private $mountOptions = [];
  105. /**
  106. * Whether it's a personal or admin mount
  107. *
  108. * @var int
  109. */
  110. private $type;
  111. /**
  112. * Creates a storage config
  113. *
  114. * @param int|string $id config id or null for a new config
  115. */
  116. public function __construct($id = null) {
  117. $this->id = $id ?? -1;
  118. $this->mountOptions['enable_sharing'] = false;
  119. }
  120. /**
  121. * Returns the configuration id
  122. *
  123. * @retun int
  124. */
  125. public function getId() {
  126. return $this->id;
  127. }
  128. /**
  129. * Sets the configuration id
  130. *
  131. * @param int $id configuration id
  132. */
  133. public function setId(int $id): void {
  134. $this->id = $id;
  135. }
  136. /**
  137. * Returns mount point path relative to the user's
  138. * "files" folder.
  139. *
  140. * @return string path
  141. */
  142. public function getMountPoint() {
  143. return $this->mountPoint;
  144. }
  145. /**
  146. * Sets mount point path relative to the user's
  147. * "files" folder.
  148. * The path will be normalized.
  149. *
  150. * @param string $mountPoint path
  151. */
  152. public function setMountPoint($mountPoint) {
  153. $this->mountPoint = \OC\Files\Filesystem::normalizePath($mountPoint);
  154. }
  155. /**
  156. * @return Backend
  157. */
  158. public function getBackend() {
  159. return $this->backend;
  160. }
  161. /**
  162. * @param Backend $backend
  163. */
  164. public function setBackend(Backend $backend) {
  165. $this->backend = $backend;
  166. }
  167. /**
  168. * @return AuthMechanism
  169. */
  170. public function getAuthMechanism() {
  171. return $this->authMechanism;
  172. }
  173. /**
  174. * @param AuthMechanism $authMechanism
  175. */
  176. public function setAuthMechanism(AuthMechanism $authMechanism) {
  177. $this->authMechanism = $authMechanism;
  178. }
  179. /**
  180. * Returns the external storage backend-specific options
  181. *
  182. * @return array backend options
  183. */
  184. public function getBackendOptions() {
  185. return $this->backendOptions;
  186. }
  187. /**
  188. * Sets the external storage backend-specific options
  189. *
  190. * @param array $backendOptions backend options
  191. */
  192. public function setBackendOptions($backendOptions) {
  193. if ($this->getBackend() instanceof Backend) {
  194. $parameters = $this->getBackend()->getParameters();
  195. foreach ($backendOptions as $key => $value) {
  196. if (isset($parameters[$key])) {
  197. switch ($parameters[$key]->getType()) {
  198. case \OCA\Files_External\Lib\DefinitionParameter::VALUE_BOOLEAN:
  199. $value = (bool)$value;
  200. break;
  201. }
  202. $backendOptions[$key] = $value;
  203. }
  204. }
  205. }
  206. $this->backendOptions = $backendOptions;
  207. }
  208. /**
  209. * @param string $key
  210. * @return mixed
  211. */
  212. public function getBackendOption($key) {
  213. if (isset($this->backendOptions[$key])) {
  214. return $this->backendOptions[$key];
  215. }
  216. return null;
  217. }
  218. /**
  219. * @param string $key
  220. * @param mixed $value
  221. */
  222. public function setBackendOption($key, $value) {
  223. $this->backendOptions[$key] = $value;
  224. }
  225. /**
  226. * Returns the mount priority
  227. *
  228. * @return int priority
  229. */
  230. public function getPriority() {
  231. return $this->priority;
  232. }
  233. /**
  234. * Sets the mount priority
  235. *
  236. * @param int $priority priority
  237. */
  238. public function setPriority($priority) {
  239. $this->priority = $priority;
  240. }
  241. /**
  242. * Returns the users for which to mount this storage
  243. *
  244. * @return string[] applicable users
  245. */
  246. public function getApplicableUsers() {
  247. return $this->applicableUsers;
  248. }
  249. /**
  250. * Sets the users for which to mount this storage
  251. *
  252. * @param string[]|null $applicableUsers applicable users
  253. */
  254. public function setApplicableUsers($applicableUsers) {
  255. if (is_null($applicableUsers)) {
  256. $applicableUsers = [];
  257. }
  258. $this->applicableUsers = $applicableUsers;
  259. }
  260. /**
  261. * Returns the groups for which to mount this storage
  262. *
  263. * @return string[] applicable groups
  264. */
  265. public function getApplicableGroups() {
  266. return $this->applicableGroups;
  267. }
  268. /**
  269. * Sets the groups for which to mount this storage
  270. *
  271. * @param string[]|null $applicableGroups applicable groups
  272. */
  273. public function setApplicableGroups($applicableGroups) {
  274. if (is_null($applicableGroups)) {
  275. $applicableGroups = [];
  276. }
  277. $this->applicableGroups = $applicableGroups;
  278. }
  279. /**
  280. * Returns the mount-specific options
  281. *
  282. * @return array mount specific options
  283. */
  284. public function getMountOptions() {
  285. return $this->mountOptions;
  286. }
  287. /**
  288. * Sets the mount-specific options
  289. *
  290. * @param array $mountOptions applicable groups
  291. */
  292. public function setMountOptions($mountOptions) {
  293. if (is_null($mountOptions)) {
  294. $mountOptions = [];
  295. }
  296. $this->mountOptions = $mountOptions;
  297. }
  298. /**
  299. * @param string $key
  300. * @return mixed
  301. */
  302. public function getMountOption($key) {
  303. if (isset($this->mountOptions[$key])) {
  304. return $this->mountOptions[$key];
  305. }
  306. return null;
  307. }
  308. /**
  309. * @param string $key
  310. * @param mixed $value
  311. */
  312. public function setMountOption($key, $value) {
  313. $this->mountOptions[$key] = $value;
  314. }
  315. /**
  316. * Gets the storage status, whether the config worked last time
  317. *
  318. * @return int $status status
  319. */
  320. public function getStatus() {
  321. return $this->status;
  322. }
  323. /**
  324. * Gets the message describing the storage status
  325. *
  326. * @return string|null
  327. */
  328. public function getStatusMessage() {
  329. return $this->statusMessage;
  330. }
  331. /**
  332. * Sets the storage status, whether the config worked last time
  333. *
  334. * @param int $status status
  335. * @param string|null $message optional message
  336. */
  337. public function setStatus($status, $message = null) {
  338. $this->status = $status;
  339. $this->statusMessage = $message;
  340. }
  341. /**
  342. * @return int self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl
  343. */
  344. public function getType() {
  345. return $this->type;
  346. }
  347. /**
  348. * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl
  349. */
  350. public function setType($type) {
  351. $this->type = $type;
  352. }
  353. /**
  354. * Serialize config to JSON
  355. */
  356. public function jsonSerialize(): array {
  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. }