storageconfig.php 8.2 KB

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