backendservice.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Robin McCorkell <robin@mccorkell.me.uk>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Files_External\Service;
  23. use \OCP\IConfig;
  24. use \OCA\Files_External\Lib\Backend\Backend;
  25. use \OCA\Files_External\Lib\Auth\AuthMechanism;
  26. /**
  27. * Service class to manage backend definitions
  28. */
  29. class BackendService {
  30. /** Visibility constants for VisibilityTrait */
  31. const VISIBILITY_NONE = 0;
  32. const VISIBILITY_PERSONAL = 1;
  33. const VISIBILITY_ADMIN = 2;
  34. //const VISIBILITY_ALIENS = 4;
  35. const VISIBILITY_DEFAULT = 3; // PERSONAL | ADMIN
  36. /** Priority constants for PriorityTrait */
  37. const PRIORITY_DEFAULT = 100;
  38. /** @var IConfig */
  39. protected $config;
  40. /** @var bool */
  41. private $userMountingAllowed = true;
  42. /** @var string[] */
  43. private $userMountingBackends = [];
  44. /** @var Backend[] */
  45. private $backends = [];
  46. /** @var AuthMechanism[] */
  47. private $authMechanisms = [];
  48. /**
  49. * @param IConfig $config
  50. */
  51. public function __construct(
  52. IConfig $config
  53. ) {
  54. $this->config = $config;
  55. // Load config values
  56. if ($this->config->getAppValue('files_external', 'allow_user_mounting', 'yes') !== 'yes') {
  57. $this->userMountingAllowed = false;
  58. }
  59. $this->userMountingBackends = explode(',',
  60. $this->config->getAppValue('files_external', 'user_mounting_backends', '')
  61. );
  62. // if no backend is in the list an empty string is in the array and user mounting is disabled
  63. if ($this->userMountingBackends === ['']) {
  64. $this->userMountingAllowed = false;
  65. }
  66. }
  67. /**
  68. * Register a backend
  69. *
  70. * @param Backend $backend
  71. */
  72. public function registerBackend(Backend $backend) {
  73. if (!$this->isAllowedUserBackend($backend)) {
  74. $backend->removeVisibility(BackendService::VISIBILITY_PERSONAL);
  75. }
  76. foreach ($backend->getIdentifierAliases() as $alias) {
  77. $this->backends[$alias] = $backend;
  78. }
  79. }
  80. /**
  81. * @param Backend[] $backends
  82. */
  83. public function registerBackends(array $backends) {
  84. foreach ($backends as $backend) {
  85. $this->registerBackend($backend);
  86. }
  87. }
  88. /**
  89. * Register an authentication mechanism
  90. *
  91. * @param AuthMechanism $authMech
  92. */
  93. public function registerAuthMechanism(AuthMechanism $authMech) {
  94. if (!$this->isAllowedAuthMechanism($authMech)) {
  95. $authMech->removeVisibility(BackendService::VISIBILITY_PERSONAL);
  96. }
  97. foreach ($authMech->getIdentifierAliases() as $alias) {
  98. $this->authMechanisms[$alias] = $authMech;
  99. }
  100. }
  101. /**
  102. * @param AuthMechanism[] $mechanisms
  103. */
  104. public function registerAuthMechanisms(array $mechanisms) {
  105. foreach ($mechanisms as $mechanism) {
  106. $this->registerAuthMechanism($mechanism);
  107. }
  108. }
  109. /**
  110. * Get all backends
  111. *
  112. * @return Backend[]
  113. */
  114. public function getBackends() {
  115. // only return real identifiers, no aliases
  116. $backends = [];
  117. foreach ($this->backends as $backend) {
  118. $backends[$backend->getIdentifier()] = $backend;
  119. }
  120. return $backends;
  121. }
  122. /**
  123. * Get all available backends
  124. *
  125. * @return Backend[]
  126. */
  127. public function getAvailableBackends() {
  128. return array_filter($this->getBackends(), function($backend) {
  129. return !($backend->checkDependencies());
  130. });
  131. }
  132. /**
  133. * @param string $identifier
  134. * @return Backend|null
  135. */
  136. public function getBackend($identifier) {
  137. if (isset($this->backends[$identifier])) {
  138. return $this->backends[$identifier];
  139. }
  140. return null;
  141. }
  142. /**
  143. * Get all authentication mechanisms
  144. *
  145. * @return AuthMechanism[]
  146. */
  147. public function getAuthMechanisms() {
  148. // only return real identifiers, no aliases
  149. $mechanisms = [];
  150. foreach ($this->authMechanisms as $mechanism) {
  151. $mechanisms[$mechanism->getIdentifier()] = $mechanism;
  152. }
  153. return $mechanisms;
  154. }
  155. /**
  156. * Get all authentication mechanisms for schemes
  157. *
  158. * @param string[] $schemes
  159. * @return AuthMechanism[]
  160. */
  161. public function getAuthMechanismsByScheme(array $schemes) {
  162. return array_filter($this->getAuthMechanisms(), function($authMech) use ($schemes) {
  163. return in_array($authMech->getScheme(), $schemes, true);
  164. });
  165. }
  166. /**
  167. * @param string $identifier
  168. * @return AuthMechanism|null
  169. */
  170. public function getAuthMechanism($identifier) {
  171. if (isset($this->authMechanisms[$identifier])) {
  172. return $this->authMechanisms[$identifier];
  173. }
  174. return null;
  175. }
  176. /**
  177. * @return bool
  178. */
  179. public function isUserMountingAllowed() {
  180. return $this->userMountingAllowed;
  181. }
  182. /**
  183. * Check a backend if a user is allowed to mount it
  184. *
  185. * @param Backend $backend
  186. * @return bool
  187. */
  188. protected function isAllowedUserBackend(Backend $backend) {
  189. if ($this->userMountingAllowed &&
  190. array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends)
  191. ) {
  192. return true;
  193. }
  194. return false;
  195. }
  196. /**
  197. * Check an authentication mechanism if a user is allowed to use it
  198. *
  199. * @param AuthMechanism $authMechanism
  200. * @return bool
  201. */
  202. protected function isAllowedAuthMechanism(AuthMechanism $authMechanism) {
  203. return true; // not implemented
  204. }
  205. }