summaryrefslogtreecommitdiffstats
path: root/apps/files_external/service
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files_external/service')
-rw-r--r--apps/files_external/service/backendservice.php91
-rw-r--r--apps/files_external/service/storagesservice.php23
2 files changed, 114 insertions, 0 deletions
diff --git a/apps/files_external/service/backendservice.php b/apps/files_external/service/backendservice.php
index f5859bc7272..c1abbcf2b7c 100644
--- a/apps/files_external/service/backendservice.php
+++ b/apps/files_external/service/backendservice.php
@@ -24,6 +24,7 @@ namespace OCA\Files_External\Service;
use \OCP\IConfig;
use \OCA\Files_External\Lib\Backend\Backend;
+use \OCA\Files_External\Lib\Auth\AuthMechanism;
/**
* Service class to manage backend definitions
@@ -53,6 +54,9 @@ class BackendService {
/** @var Backend[] */
private $backends = [];
+ /** @var AuthMechanism[] */
+ private $authMechanisms = [];
+
/**
* @param IConfig $config
*/
@@ -90,6 +94,26 @@ class BackendService {
$this->registerBackend($backend);
}
}
+ /**
+ * Register an authentication mechanism
+ *
+ * @param AuthMechanism $authMech
+ */
+ public function registerAuthMechanism(AuthMechanism $authMech) {
+ if (!$this->isAllowedAuthMechanism($authMech)) {
+ $authMech->removeVisibility(BackendService::VISIBILITY_PERSONAL);
+ }
+ $this->authMechanisms[$authMech->getClass()] = $authMech;
+ }
+
+ /**
+ * @param AuthMechanism[] $mechanisms
+ */
+ public function registerAuthMechanisms(array $mechanisms) {
+ foreach ($mechanisms as $mechanism) {
+ $this->registerAuthMechanism($mechanism);
+ }
+ }
/**
* Get all backends
@@ -147,6 +171,63 @@ class BackendService {
}
/**
+ * Get all authentication mechanisms
+ *
+ * @return AuthMechanism[]
+ */
+ public function getAuthMechanisms() {
+ return $this->authMechanisms;
+ }
+
+ /**
+ * Get all authentication mechanisms for schemes
+ *
+ * @param string[] $schemes
+ * @return AuthMechanism[]
+ */
+ public function getAuthMechanismsByScheme(array $schemes) {
+ return array_filter($this->getAuthMechanisms(), function($authMech) use ($schemes) {
+ return in_array($authMech->getScheme(), $schemes, true);
+ });
+ }
+
+ /**
+ * Get authentication mechanisms visible for $visibleFor
+ *
+ * @param int $visibleFor
+ * @return AuthMechanism[]
+ */
+ public function getAuthMechanismsVisibleFor($visibleFor) {
+ return array_filter($this->getAuthMechanisms(), function($authMechanism) use ($visibleFor) {
+ return $authMechanism->isVisibleFor($visibleFor);
+ });
+ }
+
+ /**
+ * Get authentication mechanisms allowed to be visible for $visibleFor
+ *
+ * @param int $visibleFor
+ * @return AuthMechanism[]
+ */
+ public function getAuthMechanismsAllowedVisibleFor($visibleFor) {
+ return array_filter($this->getAuthMechanisms(), function($authMechanism) use ($visibleFor) {
+ return $authMechanism->isAllowedVisibleFor($visibleFor);
+ });
+ }
+
+
+ /**
+ * @param string $class
+ * @return AuthMechanism|null
+ */
+ public function getAuthMechanism($class) {
+ if (isset($this->authMechanisms[$class])) {
+ return $this->authMechanisms[$class];
+ }
+ return null;
+ }
+
+ /**
* @return bool
*/
public function isUserMountingAllowed() {
@@ -167,4 +248,14 @@ class BackendService {
}
return false;
}
+
+ /**
+ * Check an authentication mechanism if a user is allowed to use it
+ *
+ * @param AuthMechanism $authMechanism
+ * @return bool
+ */
+ protected function isAllowedAuthMechanism(AuthMechanism $authMechanism) {
+ return true; // not implemented
+ }
}
diff --git a/apps/files_external/service/storagesservice.php b/apps/files_external/service/storagesservice.php
index d3bde0ae96c..b8a1824ba23 100644
--- a/apps/files_external/service/storagesservice.php
+++ b/apps/files_external/service/storagesservice.php
@@ -82,8 +82,22 @@ abstract class StoragesService {
$storageOptions
) {
$backend = $this->backendService->getBackend($storageOptions['class']);
+ if (!$backend) {
+ throw new \UnexpectedValueException('Invalid backend class');
+ }
$storageConfig->setBackend($backend);
+ if (isset($storageOptions['authMechanism'])) {
+ $authMechanism = $this->backendService->getAuthMechanism($storageOptions['authMechanism']);
+ } else {
+ $authMechanism = $backend->getLegacyAuthMechanism($storageOptions);
+ $storageOptions['authMechanism'] = 'null'; // to make error handling easier
+ }
+ if (!$authMechanism) {
+ throw new \UnexpectedValueException('Invalid authentication mechanism class');
+ }
+ $storageConfig->setAuthMechanism($authMechanism);
+
$storageConfig->setBackendOptions($storageOptions['options']);
if (isset($storageOptions['mountOptions'])) {
$storageConfig->setMountOptions($storageOptions['mountOptions']);
@@ -128,6 +142,7 @@ abstract class StoragesService {
* - "priority": storage priority
* - "backend": backend class name
* - "options": backend-specific options
+ * - "authMechanism": authentication mechanism class name
* - "mountOptions": mount-specific options (ex: disable previews, scanner, etc)
*/
@@ -257,6 +272,7 @@ abstract class StoragesService {
$options = [
'id' => $storageConfig->getId(),
'class' => $storageConfig->getBackend()->getClass(),
+ 'authMechanism' => $storageConfig->getAuthMechanism()->getClass(),
'options' => $storageConfig->getBackendOptions(),
];
@@ -335,6 +351,7 @@ abstract class StoragesService {
*
* @param string $mountPoint storage mount point
* @param string $backendClass backend class name
+ * @param string $authMechanismClass authentication mechanism class
* @param array $backendOptions backend-specific options
* @param array|null $mountOptions mount-specific options
* @param array|null $applicableUsers users for which to mount the storage
@@ -346,6 +363,7 @@ abstract class StoragesService {
public function createStorage(
$mountPoint,
$backendClass,
+ $authMechanismClass,
$backendOptions,
$mountOptions = null,
$applicableUsers = null,
@@ -356,9 +374,14 @@ abstract class StoragesService {
if (!$backend) {
throw new \InvalidArgumentException('Unable to get backend for backend class '.$backendClass);
}
+ $authMechanism = $this->backendService->getAuthMechanism($authMechanismClass);
+ if (!$authMechanism) {
+ throw new \InvalidArgumentException('Unable to get authentication mechanism for class '.$authMechanismClass);
+ }
$newStorage = new StorageConfig();
$newStorage->setMountPoint($mountPoint);
$newStorage->setBackend($backend);
+ $newStorage->setAuthMechanism($authMechanism);
$newStorage->setBackendOptions($backendOptions);
if (isset($mountOptions)) {
$newStorage->setMountOptions($mountOptions);