You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BackendService.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_External\Service;
  27. use OCA\Files_External\Config\IConfigHandler;
  28. use OCA\Files_External\Lib\Auth\AuthMechanism;
  29. use OCA\Files_External\Lib\Backend\Backend;
  30. use OCA\Files_External\Lib\Config\IAuthMechanismProvider;
  31. use OCA\Files_External\Lib\Config\IBackendProvider;
  32. use OCP\EventDispatcher\GenericEvent;
  33. use OCP\IConfig;
  34. /**
  35. * Service class to manage backend definitions
  36. */
  37. class BackendService {
  38. /** Visibility constants for VisibilityTrait */
  39. const VISIBILITY_NONE = 0;
  40. const VISIBILITY_PERSONAL = 1;
  41. const VISIBILITY_ADMIN = 2;
  42. //const VISIBILITY_ALIENS = 4;
  43. const VISIBILITY_DEFAULT = 3; // PERSONAL | ADMIN
  44. /** Priority constants for PriorityTrait */
  45. const PRIORITY_DEFAULT = 100;
  46. /** @var IConfig */
  47. protected $config;
  48. /** @var bool */
  49. private $userMountingAllowed = true;
  50. /** @var string[] */
  51. private $userMountingBackends = [];
  52. /** @var Backend[] */
  53. private $backends = [];
  54. /** @var IBackendProvider[] */
  55. private $backendProviders = [];
  56. /** @var AuthMechanism[] */
  57. private $authMechanisms = [];
  58. /** @var IAuthMechanismProvider[] */
  59. private $authMechanismProviders = [];
  60. /** @var callable[] */
  61. private $configHandlerLoaders = [];
  62. private $configHandlers = [];
  63. /**
  64. * @param IConfig $config
  65. */
  66. public function __construct(
  67. IConfig $config
  68. ) {
  69. $this->config = $config;
  70. // Load config values
  71. if ($this->config->getAppValue('files_external', 'allow_user_mounting', 'yes') !== 'yes') {
  72. $this->userMountingAllowed = false;
  73. }
  74. $this->userMountingBackends = explode(',',
  75. $this->config->getAppValue('files_external', 'user_mounting_backends', '')
  76. );
  77. // if no backend is in the list an empty string is in the array and user mounting is disabled
  78. if ($this->userMountingBackends === ['']) {
  79. $this->userMountingAllowed = false;
  80. }
  81. }
  82. /**
  83. * Register a backend provider
  84. *
  85. * @since 9.1.0
  86. * @param IBackendProvider $provider
  87. */
  88. public function registerBackendProvider(IBackendProvider $provider) {
  89. $this->backendProviders[] = $provider;
  90. }
  91. private function callForRegistrations() {
  92. static $eventSent = false;
  93. if(!$eventSent) {
  94. \OC::$server->getEventDispatcher()->dispatch(
  95. 'OCA\\Files_External::loadAdditionalBackends',
  96. new GenericEvent()
  97. );
  98. $eventSent = true;
  99. }
  100. }
  101. private function loadBackendProviders() {
  102. $this->callForRegistrations();
  103. foreach ($this->backendProviders as $provider) {
  104. $this->registerBackends($provider->getBackends());
  105. }
  106. $this->backendProviders = [];
  107. }
  108. /**
  109. * Register an auth mechanism provider
  110. *
  111. * @since 9.1.0
  112. * @param IAuthMechanismProvider $provider
  113. */
  114. public function registerAuthMechanismProvider(IAuthMechanismProvider $provider) {
  115. $this->authMechanismProviders[] = $provider;
  116. }
  117. private function loadAuthMechanismProviders() {
  118. $this->callForRegistrations();
  119. foreach ($this->authMechanismProviders as $provider) {
  120. $this->registerAuthMechanisms($provider->getAuthMechanisms());
  121. }
  122. $this->authMechanismProviders = [];
  123. }
  124. /**
  125. * Register a backend
  126. *
  127. * @deprecated 9.1.0 use registerBackendProvider()
  128. * @param Backend $backend
  129. */
  130. public function registerBackend(Backend $backend) {
  131. if (!$this->isAllowedUserBackend($backend)) {
  132. $backend->removeVisibility(BackendService::VISIBILITY_PERSONAL);
  133. }
  134. foreach ($backend->getIdentifierAliases() as $alias) {
  135. $this->backends[$alias] = $backend;
  136. }
  137. }
  138. /**
  139. * @deprecated 9.1.0 use registerBackendProvider()
  140. * @param Backend[] $backends
  141. */
  142. public function registerBackends(array $backends) {
  143. foreach ($backends as $backend) {
  144. $this->registerBackend($backend);
  145. }
  146. }
  147. /**
  148. * Register an authentication mechanism
  149. *
  150. * @deprecated 9.1.0 use registerAuthMechanismProvider()
  151. * @param AuthMechanism $authMech
  152. */
  153. public function registerAuthMechanism(AuthMechanism $authMech) {
  154. if (!$this->isAllowedAuthMechanism($authMech)) {
  155. $authMech->removeVisibility(BackendService::VISIBILITY_PERSONAL);
  156. }
  157. foreach ($authMech->getIdentifierAliases() as $alias) {
  158. $this->authMechanisms[$alias] = $authMech;
  159. }
  160. }
  161. /**
  162. * @deprecated 9.1.0 use registerAuthMechanismProvider()
  163. * @param AuthMechanism[] $mechanisms
  164. */
  165. public function registerAuthMechanisms(array $mechanisms) {
  166. foreach ($mechanisms as $mechanism) {
  167. $this->registerAuthMechanism($mechanism);
  168. }
  169. }
  170. /**
  171. * Get all backends
  172. *
  173. * @return Backend[]
  174. */
  175. public function getBackends() {
  176. $this->loadBackendProviders();
  177. // only return real identifiers, no aliases
  178. $backends = [];
  179. foreach ($this->backends as $backend) {
  180. $backends[$backend->getIdentifier()] = $backend;
  181. }
  182. return $backends;
  183. }
  184. /**
  185. * Get all available backends
  186. *
  187. * @return Backend[]
  188. */
  189. public function getAvailableBackends() {
  190. return array_filter($this->getBackends(), function($backend) {
  191. return !$backend->checkDependencies();
  192. });
  193. }
  194. /**
  195. * @param string $identifier
  196. * @return Backend|null
  197. */
  198. public function getBackend($identifier) {
  199. $this->loadBackendProviders();
  200. if (isset($this->backends[$identifier])) {
  201. return $this->backends[$identifier];
  202. }
  203. return null;
  204. }
  205. /**
  206. * Get all authentication mechanisms
  207. *
  208. * @return AuthMechanism[]
  209. */
  210. public function getAuthMechanisms() {
  211. $this->loadAuthMechanismProviders();
  212. // only return real identifiers, no aliases
  213. $mechanisms = [];
  214. foreach ($this->authMechanisms as $mechanism) {
  215. $mechanisms[$mechanism->getIdentifier()] = $mechanism;
  216. }
  217. return $mechanisms;
  218. }
  219. /**
  220. * Get all authentication mechanisms for schemes
  221. *
  222. * @param string[] $schemes
  223. * @return AuthMechanism[]
  224. */
  225. public function getAuthMechanismsByScheme(array $schemes) {
  226. return array_filter($this->getAuthMechanisms(), function($authMech) use ($schemes) {
  227. return in_array($authMech->getScheme(), $schemes, true);
  228. });
  229. }
  230. /**
  231. * @param string $identifier
  232. * @return AuthMechanism|null
  233. */
  234. public function getAuthMechanism($identifier) {
  235. $this->loadAuthMechanismProviders();
  236. if (isset($this->authMechanisms[$identifier])) {
  237. return $this->authMechanisms[$identifier];
  238. }
  239. return null;
  240. }
  241. /**
  242. * @return bool
  243. */
  244. public function isUserMountingAllowed() {
  245. return $this->userMountingAllowed;
  246. }
  247. /**
  248. * Check a backend if a user is allowed to mount it
  249. *
  250. * @param Backend $backend
  251. * @return bool
  252. */
  253. protected function isAllowedUserBackend(Backend $backend) {
  254. if ($this->userMountingAllowed &&
  255. array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends)
  256. ) {
  257. return true;
  258. }
  259. return false;
  260. }
  261. /**
  262. * Check an authentication mechanism if a user is allowed to use it
  263. *
  264. * @param AuthMechanism $authMechanism
  265. * @return bool
  266. */
  267. protected function isAllowedAuthMechanism(AuthMechanism $authMechanism) {
  268. return true; // not implemented
  269. }
  270. /**
  271. * registers a configuration handler
  272. *
  273. * The function of the provided $placeholder is mostly to act a sorting
  274. * criteria, so longer placeholders are replaced first. This avoids
  275. * "$user" overwriting parts of "$userMail" and "$userLang", for example.
  276. * The provided value should not contain the $ prefix, only a-z0-9 are
  277. * allowed. Upper case letters are lower cased, the replacement is case-
  278. * insensitive.
  279. *
  280. * The configHandlerLoader should just instantiate the handler on demand.
  281. * For now all handlers are instantiated when a mount is loaded, independent
  282. * of whether the placeholder is present or not. This may change in future.
  283. *
  284. * @since 16.0.0
  285. */
  286. public function registerConfigHandler(string $placeholder, callable $configHandlerLoader) {
  287. $placeholder = trim(strtolower($placeholder));
  288. if(!(bool)\preg_match('/^[a-z0-9]*$/', $placeholder)) {
  289. throw new \RuntimeException(sprintf(
  290. 'Invalid placeholder %s, only [a-z0-9] are allowed', $placeholder
  291. ));
  292. }
  293. if($placeholder === '') {
  294. throw new \RuntimeException('Invalid empty placeholder');
  295. }
  296. if(isset($this->configHandlerLoaders[$placeholder]) || isset($this->configHandlers[$placeholder])) {
  297. throw new \RuntimeException(sprintf('A handler is already registered for %s', $placeholder));
  298. }
  299. $this->configHandlerLoaders[$placeholder] = $configHandlerLoader;
  300. }
  301. protected function loadConfigHandlers():void {
  302. $this->callForRegistrations();
  303. $newLoaded = false;
  304. foreach ($this->configHandlerLoaders as $placeholder => $loader) {
  305. $handler = $loader();
  306. if(!$handler instanceof IConfigHandler) {
  307. throw new \RuntimeException(sprintf(
  308. 'Handler for %s is not an instance of IConfigHandler', $placeholder
  309. ));
  310. }
  311. $this->configHandlers[$placeholder] = $handler;
  312. $newLoaded = true;
  313. }
  314. $this->configHandlerLoaders = [];
  315. if($newLoaded) {
  316. // ensure those with longest placeholders come first,
  317. // to avoid substring matches
  318. uksort($this->configHandlers, function ($phA, $phB) {
  319. return strlen($phB) <=> strlen($phA);
  320. });
  321. }
  322. }
  323. /**
  324. * @since 16.0.0
  325. */
  326. public function getConfigHandlers() {
  327. $this->loadConfigHandlers();
  328. return $this->configHandlers;
  329. }
  330. }