StorageConfig.php 8.7 KB

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