StorageConfig.php 8.9 KB

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