1
0

BackendService.php 9.9 KB

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