StorageConfig.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. use OCA\Files_External\ResponseDefinitions;
  34. /**
  35. * External storage configuration
  36. *
  37. * @psalm-import-type Files_ExternalStorageConfig from ResponseDefinitions
  38. */
  39. class StorageConfig implements \JsonSerializable {
  40. public const MOUNT_TYPE_ADMIN = 1;
  41. public const MOUNT_TYPE_PERSONAL = 2;
  42. /** @deprecated use MOUNT_TYPE_PERSONAL (full uppercase) instead */
  43. public const MOUNT_TYPE_PERSONAl = 2;
  44. /**
  45. * Storage config id
  46. *
  47. * @var int
  48. */
  49. private $id;
  50. /**
  51. * Backend
  52. *
  53. * @var Backend
  54. */
  55. private $backend;
  56. /**
  57. * Authentication mechanism
  58. *
  59. * @var AuthMechanism
  60. */
  61. private $authMechanism;
  62. /**
  63. * Backend options
  64. *
  65. * @var array<string, mixed>
  66. */
  67. private $backendOptions = [];
  68. /**
  69. * Mount point path, relative to the user's "files" folder
  70. *
  71. * @var string
  72. */
  73. private $mountPoint;
  74. /**
  75. * Storage status
  76. *
  77. * @var int
  78. */
  79. private $status;
  80. /**
  81. * Status message
  82. *
  83. * @var string
  84. */
  85. private $statusMessage;
  86. /**
  87. * Priority
  88. *
  89. * @var int
  90. */
  91. private $priority;
  92. /**
  93. * List of users who have access to this storage
  94. *
  95. * @var string[]
  96. */
  97. private $applicableUsers = [];
  98. /**
  99. * List of groups that have access to this storage
  100. *
  101. * @var string[]
  102. */
  103. private $applicableGroups = [];
  104. /**
  105. * Mount-specific options
  106. *
  107. * @var array<string, mixed>
  108. */
  109. private $mountOptions = [];
  110. /**
  111. * Whether it's a personal or admin mount
  112. *
  113. * @var int
  114. */
  115. private $type;
  116. /**
  117. * Creates a storage config
  118. *
  119. * @param int|string $id config id or null for a new config
  120. */
  121. public function __construct($id = null) {
  122. $this->id = $id ?? -1;
  123. $this->mountOptions['enable_sharing'] = false;
  124. }
  125. /**
  126. * Returns the configuration id
  127. *
  128. * @return int
  129. */
  130. public function getId() {
  131. return $this->id;
  132. }
  133. /**
  134. * Sets the configuration id
  135. *
  136. * @param int $id configuration id
  137. */
  138. public function setId(int $id): void {
  139. $this->id = $id;
  140. }
  141. /**
  142. * Returns mount point path relative to the user's
  143. * "files" folder.
  144. *
  145. * @return string path
  146. */
  147. public function getMountPoint() {
  148. return $this->mountPoint;
  149. }
  150. /**
  151. * Sets mount point path relative to the user's
  152. * "files" folder.
  153. * The path will be normalized.
  154. *
  155. * @param string $mountPoint path
  156. */
  157. public function setMountPoint($mountPoint) {
  158. $this->mountPoint = \OC\Files\Filesystem::normalizePath($mountPoint);
  159. }
  160. /**
  161. * @return Backend
  162. */
  163. public function getBackend() {
  164. return $this->backend;
  165. }
  166. /**
  167. * @param Backend $backend
  168. */
  169. public function setBackend(Backend $backend) {
  170. $this->backend = $backend;
  171. }
  172. /**
  173. * @return AuthMechanism
  174. */
  175. public function getAuthMechanism() {
  176. return $this->authMechanism;
  177. }
  178. /**
  179. * @param AuthMechanism $authMechanism
  180. */
  181. public function setAuthMechanism(AuthMechanism $authMechanism) {
  182. $this->authMechanism = $authMechanism;
  183. }
  184. /**
  185. * Returns the external storage backend-specific options
  186. *
  187. * @return array backend options
  188. */
  189. public function getBackendOptions() {
  190. return $this->backendOptions;
  191. }
  192. /**
  193. * Sets the external storage backend-specific options
  194. *
  195. * @param array $backendOptions backend options
  196. */
  197. public function setBackendOptions($backendOptions) {
  198. if ($this->getBackend() instanceof Backend) {
  199. $parameters = $this->getBackend()->getParameters();
  200. foreach ($backendOptions as $key => $value) {
  201. if (isset($parameters[$key])) {
  202. switch ($parameters[$key]->getType()) {
  203. case \OCA\Files_External\Lib\DefinitionParameter::VALUE_BOOLEAN:
  204. $value = (bool)$value;
  205. break;
  206. }
  207. $backendOptions[$key] = $value;
  208. }
  209. }
  210. }
  211. $this->backendOptions = $backendOptions;
  212. }
  213. /**
  214. * @param string $key
  215. * @return mixed
  216. */
  217. public function getBackendOption($key) {
  218. if (isset($this->backendOptions[$key])) {
  219. return $this->backendOptions[$key];
  220. }
  221. return null;
  222. }
  223. /**
  224. * @param string $key
  225. * @param mixed $value
  226. */
  227. public function setBackendOption($key, $value) {
  228. $this->backendOptions[$key] = $value;
  229. }
  230. /**
  231. * Returns the mount priority
  232. *
  233. * @return int priority
  234. */
  235. public function getPriority() {
  236. return $this->priority;
  237. }
  238. /**
  239. * Sets the mount priority
  240. *
  241. * @param int $priority priority
  242. */
  243. public function setPriority($priority) {
  244. $this->priority = $priority;
  245. }
  246. /**
  247. * Returns the users for which to mount this storage
  248. *
  249. * @return string[] applicable users
  250. */
  251. public function getApplicableUsers() {
  252. return $this->applicableUsers;
  253. }
  254. /**
  255. * Sets the users for which to mount this storage
  256. *
  257. * @param string[]|null $applicableUsers applicable users
  258. */
  259. public function setApplicableUsers($applicableUsers) {
  260. if (is_null($applicableUsers)) {
  261. $applicableUsers = [];
  262. }
  263. $this->applicableUsers = $applicableUsers;
  264. }
  265. /**
  266. * Returns the groups for which to mount this storage
  267. *
  268. * @return string[] applicable groups
  269. */
  270. public function getApplicableGroups() {
  271. return $this->applicableGroups;
  272. }
  273. /**
  274. * Sets the groups for which to mount this storage
  275. *
  276. * @param string[]|null $applicableGroups applicable groups
  277. */
  278. public function setApplicableGroups($applicableGroups) {
  279. if (is_null($applicableGroups)) {
  280. $applicableGroups = [];
  281. }
  282. $this->applicableGroups = $applicableGroups;
  283. }
  284. /**
  285. * Returns the mount-specific options
  286. *
  287. * @return array mount specific options
  288. */
  289. public function getMountOptions() {
  290. return $this->mountOptions;
  291. }
  292. /**
  293. * Sets the mount-specific options
  294. *
  295. * @param array $mountOptions applicable groups
  296. */
  297. public function setMountOptions($mountOptions) {
  298. if (is_null($mountOptions)) {
  299. $mountOptions = [];
  300. }
  301. $this->mountOptions = $mountOptions;
  302. }
  303. /**
  304. * @param string $key
  305. * @return mixed
  306. */
  307. public function getMountOption($key) {
  308. if (isset($this->mountOptions[$key])) {
  309. return $this->mountOptions[$key];
  310. }
  311. return null;
  312. }
  313. /**
  314. * @param string $key
  315. * @param mixed $value
  316. */
  317. public function setMountOption($key, $value) {
  318. $this->mountOptions[$key] = $value;
  319. }
  320. /**
  321. * Gets the storage status, whether the config worked last time
  322. *
  323. * @return int $status status
  324. */
  325. public function getStatus() {
  326. return $this->status;
  327. }
  328. /**
  329. * Gets the message describing the storage status
  330. *
  331. * @return string|null
  332. */
  333. public function getStatusMessage() {
  334. return $this->statusMessage;
  335. }
  336. /**
  337. * Sets the storage status, whether the config worked last time
  338. *
  339. * @param int $status status
  340. * @param string|null $message optional message
  341. */
  342. public function setStatus($status, $message = null) {
  343. $this->status = $status;
  344. $this->statusMessage = $message;
  345. }
  346. /**
  347. * @return int self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL
  348. */
  349. public function getType() {
  350. return $this->type;
  351. }
  352. /**
  353. * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL
  354. */
  355. public function setType($type) {
  356. $this->type = $type;
  357. }
  358. /**
  359. * Serialize config to JSON
  360. * @return Files_ExternalStorageConfig
  361. */
  362. public function jsonSerialize(bool $obfuscate = false): array {
  363. $result = [];
  364. if (!is_null($this->id)) {
  365. $result['id'] = $this->id;
  366. }
  367. // obfuscate sensitive data if requested
  368. if ($obfuscate) {
  369. $this->formatStorageForUI();
  370. }
  371. $result['mountPoint'] = $this->mountPoint;
  372. $result['backend'] = $this->backend->getIdentifier();
  373. $result['authMechanism'] = $this->authMechanism->getIdentifier();
  374. $result['backendOptions'] = $this->backendOptions;
  375. if (!is_null($this->priority)) {
  376. $result['priority'] = $this->priority;
  377. }
  378. if (!empty($this->applicableUsers)) {
  379. $result['applicableUsers'] = $this->applicableUsers;
  380. }
  381. if (!empty($this->applicableGroups)) {
  382. $result['applicableGroups'] = $this->applicableGroups;
  383. }
  384. if (!empty($this->mountOptions)) {
  385. $result['mountOptions'] = $this->mountOptions;
  386. }
  387. if (!is_null($this->status)) {
  388. $result['status'] = $this->status;
  389. }
  390. if (!is_null($this->statusMessage)) {
  391. $result['statusMessage'] = $this->statusMessage;
  392. }
  393. $result['userProvided'] = $this->authMechanism instanceof IUserProvided;
  394. $result['type'] = ($this->getType() === self::MOUNT_TYPE_PERSONAL) ? 'personal': 'system';
  395. return $result;
  396. }
  397. protected function formatStorageForUI(): void {
  398. /** @var DefinitionParameter[] $parameters */
  399. $parameters = array_merge($this->getBackend()->getParameters(), $this->getAuthMechanism()->getParameters());
  400. $options = $this->getBackendOptions();
  401. foreach ($options as $key => $value) {
  402. foreach ($parameters as $parameter) {
  403. if ($parameter->getName() === $key && $parameter->getType() === DefinitionParameter::VALUE_PASSWORD) {
  404. $this->setBackendOption($key, DefinitionParameter::UNMODIFIED_PLACEHOLDER);
  405. break;
  406. }
  407. }
  408. }
  409. }
  410. }