StorageConfig.php 8.9 KB

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