StorageConfig.php 9.7 KB

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