storageconfig.php 6.6 KB

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