BackendService.php 9.8 KB

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