@@ -122,7 +122,7 @@ |
||
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | $eventName = $addToFavorite ? 'addFavorite' : 'removeFavorite'; |
| 125 | - $this->dispatcher->dispatch(self::class . '::' . $eventName, new GenericEvent(null, [ |
|
| 125 | + $this->dispatcher->dispatch(self::class.'::'.$eventName, new GenericEvent(null, [ |
|
| 126 | 126 | 'userId' => $user->getUID(), |
| 127 | 127 | 'fileId' => $fileId, |
| 128 | 128 | 'path' => $path, |
@@ -38,110 +38,110 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class TagService { |
| 40 | 40 | |
| 41 | - /** @var IUserSession */ |
|
| 42 | - private $userSession; |
|
| 43 | - /** @var IManager */ |
|
| 44 | - private $activityManager; |
|
| 45 | - /** @var ITags */ |
|
| 46 | - private $tagger; |
|
| 47 | - /** @var Folder */ |
|
| 48 | - private $homeFolder; |
|
| 49 | - /** @var EventDispatcherInterface */ |
|
| 50 | - private $dispatcher; |
|
| 41 | + /** @var IUserSession */ |
|
| 42 | + private $userSession; |
|
| 43 | + /** @var IManager */ |
|
| 44 | + private $activityManager; |
|
| 45 | + /** @var ITags */ |
|
| 46 | + private $tagger; |
|
| 47 | + /** @var Folder */ |
|
| 48 | + private $homeFolder; |
|
| 49 | + /** @var EventDispatcherInterface */ |
|
| 50 | + private $dispatcher; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @param IUserSession $userSession |
|
| 54 | - * @param IManager $activityManager |
|
| 55 | - * @param ITags $tagger |
|
| 56 | - * @param Folder $homeFolder |
|
| 57 | - * @param EventDispatcherInterface $dispatcher |
|
| 58 | - */ |
|
| 59 | - public function __construct( |
|
| 60 | - IUserSession $userSession, |
|
| 61 | - IManager $activityManager, |
|
| 62 | - ITags $tagger, |
|
| 63 | - Folder $homeFolder, |
|
| 64 | - EventDispatcherInterface $dispatcher |
|
| 65 | - ) { |
|
| 66 | - $this->userSession = $userSession; |
|
| 67 | - $this->activityManager = $activityManager; |
|
| 68 | - $this->tagger = $tagger; |
|
| 69 | - $this->homeFolder = $homeFolder; |
|
| 70 | - $this->dispatcher = $dispatcher; |
|
| 71 | - } |
|
| 52 | + /** |
|
| 53 | + * @param IUserSession $userSession |
|
| 54 | + * @param IManager $activityManager |
|
| 55 | + * @param ITags $tagger |
|
| 56 | + * @param Folder $homeFolder |
|
| 57 | + * @param EventDispatcherInterface $dispatcher |
|
| 58 | + */ |
|
| 59 | + public function __construct( |
|
| 60 | + IUserSession $userSession, |
|
| 61 | + IManager $activityManager, |
|
| 62 | + ITags $tagger, |
|
| 63 | + Folder $homeFolder, |
|
| 64 | + EventDispatcherInterface $dispatcher |
|
| 65 | + ) { |
|
| 66 | + $this->userSession = $userSession; |
|
| 67 | + $this->activityManager = $activityManager; |
|
| 68 | + $this->tagger = $tagger; |
|
| 69 | + $this->homeFolder = $homeFolder; |
|
| 70 | + $this->dispatcher = $dispatcher; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Updates the tags of the specified file path. |
|
| 75 | - * The passed tags are absolute, which means they will |
|
| 76 | - * replace the actual tag selection. |
|
| 77 | - * |
|
| 78 | - * @param string $path path |
|
| 79 | - * @param array $tags array of tags |
|
| 80 | - * @return array list of tags |
|
| 81 | - * @throws \OCP\Files\NotFoundException if the file does not exist |
|
| 82 | - */ |
|
| 83 | - public function updateFileTags($path, $tags) { |
|
| 84 | - $fileId = $this->homeFolder->get($path)->getId(); |
|
| 73 | + /** |
|
| 74 | + * Updates the tags of the specified file path. |
|
| 75 | + * The passed tags are absolute, which means they will |
|
| 76 | + * replace the actual tag selection. |
|
| 77 | + * |
|
| 78 | + * @param string $path path |
|
| 79 | + * @param array $tags array of tags |
|
| 80 | + * @return array list of tags |
|
| 81 | + * @throws \OCP\Files\NotFoundException if the file does not exist |
|
| 82 | + */ |
|
| 83 | + public function updateFileTags($path, $tags) { |
|
| 84 | + $fileId = $this->homeFolder->get($path)->getId(); |
|
| 85 | 85 | |
| 86 | - $currentTags = $this->tagger->getTagsForObjects([$fileId]); |
|
| 86 | + $currentTags = $this->tagger->getTagsForObjects([$fileId]); |
|
| 87 | 87 | |
| 88 | - if (!empty($currentTags)) { |
|
| 89 | - $currentTags = current($currentTags); |
|
| 90 | - } |
|
| 88 | + if (!empty($currentTags)) { |
|
| 89 | + $currentTags = current($currentTags); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - $newTags = array_diff($tags, $currentTags); |
|
| 93 | - foreach ($newTags as $tag) { |
|
| 94 | - if ($tag === ITags::TAG_FAVORITE) { |
|
| 95 | - $this->addActivity(true, $fileId, $path); |
|
| 96 | - } |
|
| 97 | - $this->tagger->tagAs($fileId, $tag); |
|
| 98 | - } |
|
| 99 | - $deletedTags = array_diff($currentTags, $tags); |
|
| 100 | - foreach ($deletedTags as $tag) { |
|
| 101 | - if ($tag === ITags::TAG_FAVORITE) { |
|
| 102 | - $this->addActivity(false, $fileId, $path); |
|
| 103 | - } |
|
| 104 | - $this->tagger->unTag($fileId, $tag); |
|
| 105 | - } |
|
| 92 | + $newTags = array_diff($tags, $currentTags); |
|
| 93 | + foreach ($newTags as $tag) { |
|
| 94 | + if ($tag === ITags::TAG_FAVORITE) { |
|
| 95 | + $this->addActivity(true, $fileId, $path); |
|
| 96 | + } |
|
| 97 | + $this->tagger->tagAs($fileId, $tag); |
|
| 98 | + } |
|
| 99 | + $deletedTags = array_diff($currentTags, $tags); |
|
| 100 | + foreach ($deletedTags as $tag) { |
|
| 101 | + if ($tag === ITags::TAG_FAVORITE) { |
|
| 102 | + $this->addActivity(false, $fileId, $path); |
|
| 103 | + } |
|
| 104 | + $this->tagger->unTag($fileId, $tag); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - // TODO: re-read from tagger to make sure the |
|
| 108 | - // list is up to date, in case of concurrent changes ? |
|
| 109 | - return $tags; |
|
| 110 | - } |
|
| 107 | + // TODO: re-read from tagger to make sure the |
|
| 108 | + // list is up to date, in case of concurrent changes ? |
|
| 109 | + return $tags; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * @param bool $addToFavorite |
|
| 114 | - * @param int $fileId |
|
| 115 | - * @param string $path |
|
| 116 | - */ |
|
| 117 | - protected function addActivity($addToFavorite, $fileId, $path) { |
|
| 118 | - $user = $this->userSession->getUser(); |
|
| 119 | - if (!$user instanceof IUser) { |
|
| 120 | - return; |
|
| 121 | - } |
|
| 112 | + /** |
|
| 113 | + * @param bool $addToFavorite |
|
| 114 | + * @param int $fileId |
|
| 115 | + * @param string $path |
|
| 116 | + */ |
|
| 117 | + protected function addActivity($addToFavorite, $fileId, $path) { |
|
| 118 | + $user = $this->userSession->getUser(); |
|
| 119 | + if (!$user instanceof IUser) { |
|
| 120 | + return; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - $eventName = $addToFavorite ? 'addFavorite' : 'removeFavorite'; |
|
| 124 | - $this->dispatcher->dispatch(self::class . '::' . $eventName, new GenericEvent(null, [ |
|
| 125 | - 'userId' => $user->getUID(), |
|
| 126 | - 'fileId' => $fileId, |
|
| 127 | - 'path' => $path, |
|
| 128 | - ])); |
|
| 123 | + $eventName = $addToFavorite ? 'addFavorite' : 'removeFavorite'; |
|
| 124 | + $this->dispatcher->dispatch(self::class . '::' . $eventName, new GenericEvent(null, [ |
|
| 125 | + 'userId' => $user->getUID(), |
|
| 126 | + 'fileId' => $fileId, |
|
| 127 | + 'path' => $path, |
|
| 128 | + ])); |
|
| 129 | 129 | |
| 130 | - $event = $this->activityManager->generateEvent(); |
|
| 131 | - try { |
|
| 132 | - $event->setApp('files') |
|
| 133 | - ->setObject('files', $fileId, $path) |
|
| 134 | - ->setType('favorite') |
|
| 135 | - ->setAuthor($user->getUID()) |
|
| 136 | - ->setAffectedUser($user->getUID()) |
|
| 137 | - ->setTimestamp(time()) |
|
| 138 | - ->setSubject( |
|
| 139 | - $addToFavorite ? FavoriteProvider::SUBJECT_ADDED : FavoriteProvider::SUBJECT_REMOVED, |
|
| 140 | - ['id' => $fileId, 'path' => $path] |
|
| 141 | - ); |
|
| 142 | - $this->activityManager->publish($event); |
|
| 143 | - } catch (\InvalidArgumentException $e) { |
|
| 144 | - } catch (\BadMethodCallException $e) { |
|
| 145 | - } |
|
| 146 | - } |
|
| 130 | + $event = $this->activityManager->generateEvent(); |
|
| 131 | + try { |
|
| 132 | + $event->setApp('files') |
|
| 133 | + ->setObject('files', $fileId, $path) |
|
| 134 | + ->setType('favorite') |
|
| 135 | + ->setAuthor($user->getUID()) |
|
| 136 | + ->setAffectedUser($user->getUID()) |
|
| 137 | + ->setTimestamp(time()) |
|
| 138 | + ->setSubject( |
|
| 139 | + $addToFavorite ? FavoriteProvider::SUBJECT_ADDED : FavoriteProvider::SUBJECT_REMOVED, |
|
| 140 | + ['id' => $fileId, 'path' => $path] |
|
| 141 | + ); |
|
| 142 | + $this->activityManager->publish($event); |
|
| 143 | + } catch (\InvalidArgumentException $e) { |
|
| 144 | + } catch (\BadMethodCallException $e) { |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | 147 | } |
@@ -30,35 +30,35 @@ |
||
| 30 | 30 | * @since 12.0.0 |
| 31 | 31 | */ |
| 32 | 32 | interface ICloudId { |
| 33 | - /** |
|
| 34 | - * The remote cloud id |
|
| 35 | - * |
|
| 36 | - * @return string |
|
| 37 | - * @since 12.0.0 |
|
| 38 | - */ |
|
| 39 | - public function getId(): string; |
|
| 33 | + /** |
|
| 34 | + * The remote cloud id |
|
| 35 | + * |
|
| 36 | + * @return string |
|
| 37 | + * @since 12.0.0 |
|
| 38 | + */ |
|
| 39 | + public function getId(): string; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Get a clean representation of the cloud id for display |
|
| 43 | - * |
|
| 44 | - * @return string |
|
| 45 | - * @since 12.0.0 |
|
| 46 | - */ |
|
| 47 | - public function getDisplayId(): string; |
|
| 41 | + /** |
|
| 42 | + * Get a clean representation of the cloud id for display |
|
| 43 | + * |
|
| 44 | + * @return string |
|
| 45 | + * @since 12.0.0 |
|
| 46 | + */ |
|
| 47 | + public function getDisplayId(): string; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * The username on the remote server |
|
| 51 | - * |
|
| 52 | - * @return string |
|
| 53 | - * @since 12.0.0 |
|
| 54 | - */ |
|
| 55 | - public function getUser(): string; |
|
| 49 | + /** |
|
| 50 | + * The username on the remote server |
|
| 51 | + * |
|
| 52 | + * @return string |
|
| 53 | + * @since 12.0.0 |
|
| 54 | + */ |
|
| 55 | + public function getUser(): string; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * The base address of the remote server |
|
| 59 | - * |
|
| 60 | - * @return string |
|
| 61 | - * @since 12.0.0 |
|
| 62 | - */ |
|
| 63 | - public function getRemote(): string; |
|
| 57 | + /** |
|
| 58 | + * The base address of the remote server |
|
| 59 | + * |
|
| 60 | + * @return string |
|
| 61 | + * @since 12.0.0 |
|
| 62 | + */ |
|
| 63 | + public function getRemote(): string; |
|
| 64 | 64 | } |
@@ -31,9 +31,9 @@ |
||
| 31 | 31 | * @since 8.1.0 |
| 32 | 32 | */ |
| 33 | 33 | interface IClientService { |
| 34 | - /** |
|
| 35 | - * @return IClient |
|
| 36 | - * @since 8.1.0 |
|
| 37 | - */ |
|
| 38 | - public function newClient(): IClient; |
|
| 34 | + /** |
|
| 35 | + * @return IClient |
|
| 36 | + * @since 8.1.0 |
|
| 37 | + */ |
|
| 38 | + public function newClient(): IClient; |
|
| 39 | 39 | } |
@@ -32,28 +32,28 @@ |
||
| 32 | 32 | * @since 8.1.0 |
| 33 | 33 | */ |
| 34 | 34 | interface IResponse { |
| 35 | - /** |
|
| 36 | - * @return string|resource |
|
| 37 | - * @since 8.1.0 |
|
| 38 | - */ |
|
| 39 | - public function getBody(); |
|
| 35 | + /** |
|
| 36 | + * @return string|resource |
|
| 37 | + * @since 8.1.0 |
|
| 38 | + */ |
|
| 39 | + public function getBody(); |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @return int |
|
| 43 | - * @since 8.1.0 |
|
| 44 | - */ |
|
| 45 | - public function getStatusCode(): int; |
|
| 41 | + /** |
|
| 42 | + * @return int |
|
| 43 | + * @since 8.1.0 |
|
| 44 | + */ |
|
| 45 | + public function getStatusCode(): int; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @param string $key |
|
| 49 | - * @return string |
|
| 50 | - * @since 8.1.0 |
|
| 51 | - */ |
|
| 52 | - public function getHeader(string $key): string; |
|
| 47 | + /** |
|
| 48 | + * @param string $key |
|
| 49 | + * @return string |
|
| 50 | + * @since 8.1.0 |
|
| 51 | + */ |
|
| 52 | + public function getHeader(string $key): string; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @return array |
|
| 56 | - * @since 8.1.0 |
|
| 57 | - */ |
|
| 58 | - public function getHeaders(): array; |
|
| 54 | + /** |
|
| 55 | + * @return array |
|
| 56 | + * @since 8.1.0 |
|
| 57 | + */ |
|
| 58 | + public function getHeaders(): array; |
|
| 59 | 59 | } |
@@ -45,23 +45,23 @@ |
||
| 45 | 45 | * @since 8.0.0 |
| 46 | 46 | */ |
| 47 | 47 | interface IHasher { |
| 48 | - /** |
|
| 49 | - * Hashes a message using PHP's `password_hash` functionality. |
|
| 50 | - * Please note that the size of the returned string is not guaranteed |
|
| 51 | - * and can be up to 255 characters. |
|
| 52 | - * |
|
| 53 | - * @param string $message Message to generate hash from |
|
| 54 | - * @return string Hash of the message with appended version parameter |
|
| 55 | - * @since 8.0.0 |
|
| 56 | - */ |
|
| 57 | - public function hash(string $message): string; |
|
| 48 | + /** |
|
| 49 | + * Hashes a message using PHP's `password_hash` functionality. |
|
| 50 | + * Please note that the size of the returned string is not guaranteed |
|
| 51 | + * and can be up to 255 characters. |
|
| 52 | + * |
|
| 53 | + * @param string $message Message to generate hash from |
|
| 54 | + * @return string Hash of the message with appended version parameter |
|
| 55 | + * @since 8.0.0 |
|
| 56 | + */ |
|
| 57 | + public function hash(string $message): string; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * @param string $message Message to verify |
|
| 61 | - * @param string $hash Assumed hash of the message |
|
| 62 | - * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one. |
|
| 63 | - * @return bool Whether $hash is a valid hash of $message |
|
| 64 | - * @since 8.0.0 |
|
| 65 | - */ |
|
| 66 | - public function verify(string $message, string $hash, &$newHash = null): bool ; |
|
| 59 | + /** |
|
| 60 | + * @param string $message Message to verify |
|
| 61 | + * @param string $hash Assumed hash of the message |
|
| 62 | + * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one. |
|
| 63 | + * @return bool Whether $hash is a valid hash of $message |
|
| 64 | + * @since 8.0.0 |
|
| 65 | + */ |
|
| 66 | + public function verify(string $message, string $hash, &$newHash = null): bool ; |
|
| 67 | 67 | } |
@@ -63,5 +63,5 @@ |
||
| 63 | 63 | * @return bool Whether $hash is a valid hash of $message |
| 64 | 64 | * @since 8.0.0 |
| 65 | 65 | */ |
| 66 | - public function verify(string $message, string $hash, &$newHash = null): bool ; |
|
| 66 | + public function verify(string $message, string $hash, &$newHash = null): bool; |
|
| 67 | 67 | } |
@@ -29,50 +29,50 @@ |
||
| 29 | 29 | |
| 30 | 30 | class Version13000Date20170814074715 extends SimpleMigrationStep { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @param IOutput $output |
|
| 34 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 35 | - * @param array $options |
|
| 36 | - * @since 13.0.0 |
|
| 37 | - */ |
|
| 38 | - public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 39 | - } |
|
| 32 | + /** |
|
| 33 | + * @param IOutput $output |
|
| 34 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 35 | + * @param array $options |
|
| 36 | + * @since 13.0.0 |
|
| 37 | + */ |
|
| 38 | + public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @param IOutput $output |
|
| 43 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 44 | - * @param array $options |
|
| 45 | - * @return null|ISchemaWrapper |
|
| 46 | - * @since 13.0.0 |
|
| 47 | - */ |
|
| 48 | - public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 49 | - /** @var ISchemaWrapper $schema */ |
|
| 50 | - $schema = $schemaClosure(); |
|
| 41 | + /** |
|
| 42 | + * @param IOutput $output |
|
| 43 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 44 | + * @param array $options |
|
| 45 | + * @return null|ISchemaWrapper |
|
| 46 | + * @since 13.0.0 |
|
| 47 | + */ |
|
| 48 | + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 49 | + /** @var ISchemaWrapper $schema */ |
|
| 50 | + $schema = $schemaClosure(); |
|
| 51 | 51 | |
| 52 | 52 | |
| 53 | - if (!$schema->hasTable('accounts')) { |
|
| 54 | - $table = $schema->createTable('accounts'); |
|
| 55 | - $table->addColumn('uid', 'string', [ |
|
| 56 | - 'notnull' => true, |
|
| 57 | - 'length' => 64, |
|
| 58 | - 'default' => '', |
|
| 59 | - ]); |
|
| 60 | - $table->addColumn('data', 'text', [ |
|
| 61 | - 'notnull' => true, |
|
| 62 | - 'default' => '', |
|
| 63 | - ]); |
|
| 64 | - $table->setPrimaryKey(['uid']); |
|
| 65 | - } |
|
| 53 | + if (!$schema->hasTable('accounts')) { |
|
| 54 | + $table = $schema->createTable('accounts'); |
|
| 55 | + $table->addColumn('uid', 'string', [ |
|
| 56 | + 'notnull' => true, |
|
| 57 | + 'length' => 64, |
|
| 58 | + 'default' => '', |
|
| 59 | + ]); |
|
| 60 | + $table->addColumn('data', 'text', [ |
|
| 61 | + 'notnull' => true, |
|
| 62 | + 'default' => '', |
|
| 63 | + ]); |
|
| 64 | + $table->setPrimaryKey(['uid']); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - return $schema; |
|
| 68 | - } |
|
| 67 | + return $schema; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * @param IOutput $output |
|
| 72 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 73 | - * @param array $options |
|
| 74 | - * @since 13.0.0 |
|
| 75 | - */ |
|
| 76 | - public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 77 | - } |
|
| 70 | + /** |
|
| 71 | + * @param IOutput $output |
|
| 72 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 73 | + * @param array $options |
|
| 74 | + * @since 13.0.0 |
|
| 75 | + */ |
|
| 76 | + public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 77 | + } |
|
| 78 | 78 | } |
@@ -31,75 +31,75 @@ |
||
| 31 | 31 | |
| 32 | 32 | class Version1002Date20170607113030 extends SimpleMigrationStep { |
| 33 | 33 | |
| 34 | - /** @var IDBConnection */ |
|
| 35 | - protected $connection; |
|
| 34 | + /** @var IDBConnection */ |
|
| 35 | + protected $connection; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @param IDBConnection $connection |
|
| 39 | - */ |
|
| 40 | - public function __construct(IDBConnection $connection) { |
|
| 41 | - $this->connection = $connection; |
|
| 42 | - } |
|
| 37 | + /** |
|
| 38 | + * @param IDBConnection $connection |
|
| 39 | + */ |
|
| 40 | + public function __construct(IDBConnection $connection) { |
|
| 41 | + $this->connection = $connection; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @param IOutput $output |
|
| 46 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 47 | - * @param array $options |
|
| 48 | - * @since 13.0.0 |
|
| 49 | - */ |
|
| 50 | - public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 51 | - /** @var ISchemaWrapper $schema */ |
|
| 52 | - $schema = $schemaClosure(); |
|
| 44 | + /** |
|
| 45 | + * @param IOutput $output |
|
| 46 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 47 | + * @param array $options |
|
| 48 | + * @since 13.0.0 |
|
| 49 | + */ |
|
| 50 | + public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 51 | + /** @var ISchemaWrapper $schema */ |
|
| 52 | + $schema = $schemaClosure(); |
|
| 53 | 53 | |
| 54 | - if (!$schema->hasTable('twofactor_backup_codes')) { |
|
| 55 | - // Legacy table does not exist |
|
| 56 | - return; |
|
| 57 | - } |
|
| 54 | + if (!$schema->hasTable('twofactor_backup_codes')) { |
|
| 55 | + // Legacy table does not exist |
|
| 56 | + return; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - $insert = $this->connection->getQueryBuilder(); |
|
| 60 | - $insert->insert('twofactor_backupcodes') |
|
| 61 | - ->values([ |
|
| 62 | - // Inserting with id might fail: 'id' => $insert->createParameter('id'), |
|
| 63 | - 'user_id' => $insert->createParameter('user_id'), |
|
| 64 | - 'code' => $insert->createParameter('code'), |
|
| 65 | - 'used' => $insert->createParameter('used'), |
|
| 66 | - ]); |
|
| 59 | + $insert = $this->connection->getQueryBuilder(); |
|
| 60 | + $insert->insert('twofactor_backupcodes') |
|
| 61 | + ->values([ |
|
| 62 | + // Inserting with id might fail: 'id' => $insert->createParameter('id'), |
|
| 63 | + 'user_id' => $insert->createParameter('user_id'), |
|
| 64 | + 'code' => $insert->createParameter('code'), |
|
| 65 | + 'used' => $insert->createParameter('used'), |
|
| 66 | + ]); |
|
| 67 | 67 | |
| 68 | - $query = $this->connection->getQueryBuilder(); |
|
| 69 | - $query->select('*') |
|
| 70 | - ->from('twofactor_backup_codes') |
|
| 71 | - ->orderBy('id', 'ASC'); |
|
| 72 | - $result = $query->execute(); |
|
| 68 | + $query = $this->connection->getQueryBuilder(); |
|
| 69 | + $query->select('*') |
|
| 70 | + ->from('twofactor_backup_codes') |
|
| 71 | + ->orderBy('id', 'ASC'); |
|
| 72 | + $result = $query->execute(); |
|
| 73 | 73 | |
| 74 | - $output->startProgress(); |
|
| 75 | - while ($row = $result->fetch()) { |
|
| 76 | - $output->advance(); |
|
| 74 | + $output->startProgress(); |
|
| 75 | + while ($row = $result->fetch()) { |
|
| 76 | + $output->advance(); |
|
| 77 | 77 | |
| 78 | - $insert |
|
| 79 | - // Inserting with id might fail: ->setParameter('id', $row['id'], IQueryBuilder::PARAM_INT) |
|
| 80 | - ->setParameter('user_id', $row['user_id'], IQueryBuilder::PARAM_STR) |
|
| 81 | - ->setParameter('code', $row['code'], IQueryBuilder::PARAM_STR) |
|
| 82 | - ->setParameter('used', $row['used'], IQueryBuilder::PARAM_INT) |
|
| 83 | - ->execute(); |
|
| 84 | - } |
|
| 85 | - $output->finishProgress(); |
|
| 86 | - } |
|
| 78 | + $insert |
|
| 79 | + // Inserting with id might fail: ->setParameter('id', $row['id'], IQueryBuilder::PARAM_INT) |
|
| 80 | + ->setParameter('user_id', $row['user_id'], IQueryBuilder::PARAM_STR) |
|
| 81 | + ->setParameter('code', $row['code'], IQueryBuilder::PARAM_STR) |
|
| 82 | + ->setParameter('used', $row['used'], IQueryBuilder::PARAM_INT) |
|
| 83 | + ->execute(); |
|
| 84 | + } |
|
| 85 | + $output->finishProgress(); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * @param IOutput $output |
|
| 90 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 91 | - * @param array $options |
|
| 92 | - * @return null|ISchemaWrapper |
|
| 93 | - * @since 13.0.0 |
|
| 94 | - */ |
|
| 95 | - public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 96 | - /** @var ISchemaWrapper $schema */ |
|
| 97 | - $schema = $schemaClosure(); |
|
| 88 | + /** |
|
| 89 | + * @param IOutput $output |
|
| 90 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 91 | + * @param array $options |
|
| 92 | + * @return null|ISchemaWrapper |
|
| 93 | + * @since 13.0.0 |
|
| 94 | + */ |
|
| 95 | + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 96 | + /** @var ISchemaWrapper $schema */ |
|
| 97 | + $schema = $schemaClosure(); |
|
| 98 | 98 | |
| 99 | - if ($schema->hasTable('twofactor_backup_codes')) { |
|
| 100 | - $schema->dropTable('twofactor_backup_codes'); |
|
| 101 | - return $schema; |
|
| 102 | - } |
|
| 103 | - return null; |
|
| 104 | - } |
|
| 99 | + if ($schema->hasTable('twofactor_backup_codes')) { |
|
| 100 | + $schema->dropTable('twofactor_backup_codes'); |
|
| 101 | + return $schema; |
|
| 102 | + } |
|
| 103 | + return null; |
|
| 104 | + } |
|
| 105 | 105 | } |
@@ -72,17 +72,17 @@ discard block |
||
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | protected function getStorageConfigFromDBMount(array $mount) { |
| 75 | - $applicableUsers = array_filter($mount['applicable'], function ($applicable) { |
|
| 75 | + $applicableUsers = array_filter($mount['applicable'], function($applicable) { |
|
| 76 | 76 | return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; |
| 77 | 77 | }); |
| 78 | - $applicableUsers = array_map(function ($applicable) { |
|
| 78 | + $applicableUsers = array_map(function($applicable) { |
|
| 79 | 79 | return $applicable['value']; |
| 80 | 80 | }, $applicableUsers); |
| 81 | 81 | |
| 82 | - $applicableGroups = array_filter($mount['applicable'], function ($applicable) { |
|
| 82 | + $applicableGroups = array_filter($mount['applicable'], function($applicable) { |
|
| 83 | 83 | return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; |
| 84 | 84 | }); |
| 85 | - $applicableGroups = array_map(function ($applicable) { |
|
| 85 | + $applicableGroups = array_map(function($applicable) { |
|
| 86 | 86 | return $applicable['value']; |
| 87 | 87 | }, $applicableGroups); |
| 88 | 88 | |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | $mount['priority'] |
| 99 | 99 | ); |
| 100 | 100 | $config->setType($mount['type']); |
| 101 | - $config->setId((int)$mount['mount_id']); |
|
| 101 | + $config->setId((int) $mount['mount_id']); |
|
| 102 | 102 | return $config; |
| 103 | 103 | } catch (\UnexpectedValueException $e) { |
| 104 | 104 | // don't die if a storage backend doesn't exist |
@@ -126,11 +126,11 @@ discard block |
||
| 126 | 126 | protected function readConfig() { |
| 127 | 127 | $mounts = $this->readDBConfig(); |
| 128 | 128 | $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
| 129 | - $configs = array_filter($configs, function ($config) { |
|
| 129 | + $configs = array_filter($configs, function($config) { |
|
| 130 | 130 | return $config instanceof StorageConfig; |
| 131 | 131 | }); |
| 132 | 132 | |
| 133 | - $keys = array_map(function (StorageConfig $config) { |
|
| 133 | + $keys = array_map(function(StorageConfig $config) { |
|
| 134 | 134 | return $config->getId(); |
| 135 | 135 | }, $configs); |
| 136 | 136 | |
@@ -149,14 +149,14 @@ discard block |
||
| 149 | 149 | $mount = $this->dbConfig->getMountById($id); |
| 150 | 150 | |
| 151 | 151 | if (!is_array($mount)) { |
| 152 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 152 | + throw new NotFoundException('Storage with ID "'.$id.'" not found'); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | $config = $this->getStorageConfigFromDBMount($mount); |
| 156 | 156 | if ($this->isApplicable($config)) { |
| 157 | 157 | return $config; |
| 158 | 158 | } else { |
| 159 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 159 | + throw new NotFoundException('Storage with ID "'.$id.'" not found'); |
|
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | |
@@ -379,13 +379,13 @@ discard block |
||
| 379 | 379 | $existingMount = $this->dbConfig->getMountById($id); |
| 380 | 380 | |
| 381 | 381 | if (!is_array($existingMount)) { |
| 382 | - throw new NotFoundException('Storage with ID "' . $id . '" not found while updating storage'); |
|
| 382 | + throw new NotFoundException('Storage with ID "'.$id.'" not found while updating storage'); |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | $oldStorage = $this->getStorageConfigFromDBMount($existingMount); |
| 386 | 386 | |
| 387 | 387 | if ($oldStorage->getBackend() instanceof InvalidBackend) { |
| 388 | - throw new NotFoundException('Storage with id "' . $id . '" cannot be edited due to missing backend'); |
|
| 388 | + throw new NotFoundException('Storage with id "'.$id.'" cannot be edited due to missing backend'); |
|
| 389 | 389 | } |
| 390 | 390 | |
| 391 | 391 | $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); |
@@ -462,7 +462,7 @@ discard block |
||
| 462 | 462 | $existingMount = $this->dbConfig->getMountById($id); |
| 463 | 463 | |
| 464 | 464 | if (!is_array($existingMount)) { |
| 465 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 465 | + throw new NotFoundException('Storage with ID "'.$id.'" not found'); |
|
| 466 | 466 | } |
| 467 | 467 | |
| 468 | 468 | $this->dbConfig->removeMount($id); |
@@ -51,468 +51,468 @@ |
||
| 51 | 51 | */ |
| 52 | 52 | abstract class StoragesService { |
| 53 | 53 | |
| 54 | - /** @var BackendService */ |
|
| 55 | - protected $backendService; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var DBConfigService |
|
| 59 | - */ |
|
| 60 | - protected $dbConfig; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @var IUserMountCache |
|
| 64 | - */ |
|
| 65 | - protected $userMountCache; |
|
| 66 | - |
|
| 67 | - protected IEventDispatcher $eventDispatcher; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @param BackendService $backendService |
|
| 71 | - * @param DBConfigService $dbConfigService |
|
| 72 | - * @param IUserMountCache $userMountCache |
|
| 73 | - * @param IEventDispatcher $eventDispatcher |
|
| 74 | - */ |
|
| 75 | - public function __construct( |
|
| 76 | - BackendService $backendService, |
|
| 77 | - DBConfigService $dbConfigService, |
|
| 78 | - IUserMountCache $userMountCache, |
|
| 79 | - IEventDispatcher $eventDispatcher |
|
| 80 | - ) { |
|
| 81 | - $this->backendService = $backendService; |
|
| 82 | - $this->dbConfig = $dbConfigService; |
|
| 83 | - $this->userMountCache = $userMountCache; |
|
| 84 | - $this->eventDispatcher = $eventDispatcher; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - protected function readDBConfig() { |
|
| 88 | - return $this->dbConfig->getAdminMounts(); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - protected function getStorageConfigFromDBMount(array $mount) { |
|
| 92 | - $applicableUsers = array_filter($mount['applicable'], function ($applicable) { |
|
| 93 | - return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; |
|
| 94 | - }); |
|
| 95 | - $applicableUsers = array_map(function ($applicable) { |
|
| 96 | - return $applicable['value']; |
|
| 97 | - }, $applicableUsers); |
|
| 98 | - |
|
| 99 | - $applicableGroups = array_filter($mount['applicable'], function ($applicable) { |
|
| 100 | - return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; |
|
| 101 | - }); |
|
| 102 | - $applicableGroups = array_map(function ($applicable) { |
|
| 103 | - return $applicable['value']; |
|
| 104 | - }, $applicableGroups); |
|
| 105 | - |
|
| 106 | - try { |
|
| 107 | - $config = $this->createStorage( |
|
| 108 | - $mount['mount_point'], |
|
| 109 | - $mount['storage_backend'], |
|
| 110 | - $mount['auth_backend'], |
|
| 111 | - $mount['config'], |
|
| 112 | - $mount['options'], |
|
| 113 | - array_values($applicableUsers), |
|
| 114 | - array_values($applicableGroups), |
|
| 115 | - $mount['priority'] |
|
| 116 | - ); |
|
| 117 | - $config->setType($mount['type']); |
|
| 118 | - $config->setId((int)$mount['mount_id']); |
|
| 119 | - return $config; |
|
| 120 | - } catch (\UnexpectedValueException $e) { |
|
| 121 | - // don't die if a storage backend doesn't exist |
|
| 122 | - \OC::$server->getLogger()->logException($e, [ |
|
| 123 | - 'message' => 'Could not load storage.', |
|
| 124 | - 'level' => ILogger::ERROR, |
|
| 125 | - 'app' => 'files_external', |
|
| 126 | - ]); |
|
| 127 | - return null; |
|
| 128 | - } catch (\InvalidArgumentException $e) { |
|
| 129 | - \OC::$server->getLogger()->logException($e, [ |
|
| 130 | - 'message' => 'Could not load storage.', |
|
| 131 | - 'level' => ILogger::ERROR, |
|
| 132 | - 'app' => 'files_external', |
|
| 133 | - ]); |
|
| 134 | - return null; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Read the external storage config |
|
| 140 | - * |
|
| 141 | - * @return array map of storage id to storage config |
|
| 142 | - */ |
|
| 143 | - protected function readConfig() { |
|
| 144 | - $mounts = $this->readDBConfig(); |
|
| 145 | - $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
| 146 | - $configs = array_filter($configs, function ($config) { |
|
| 147 | - return $config instanceof StorageConfig; |
|
| 148 | - }); |
|
| 149 | - |
|
| 150 | - $keys = array_map(function (StorageConfig $config) { |
|
| 151 | - return $config->getId(); |
|
| 152 | - }, $configs); |
|
| 153 | - |
|
| 154 | - return array_combine($keys, $configs); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Get a storage with status |
|
| 159 | - * |
|
| 160 | - * @param int $id storage id |
|
| 161 | - * |
|
| 162 | - * @return StorageConfig |
|
| 163 | - * @throws NotFoundException if the storage with the given id was not found |
|
| 164 | - */ |
|
| 165 | - public function getStorage($id) { |
|
| 166 | - $mount = $this->dbConfig->getMountById($id); |
|
| 167 | - |
|
| 168 | - if (!is_array($mount)) { |
|
| 169 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - $config = $this->getStorageConfigFromDBMount($mount); |
|
| 173 | - if ($this->isApplicable($config)) { |
|
| 174 | - return $config; |
|
| 175 | - } else { |
|
| 176 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Check whether this storage service should provide access to a storage |
|
| 182 | - * |
|
| 183 | - * @param StorageConfig $config |
|
| 184 | - * @return bool |
|
| 185 | - */ |
|
| 186 | - abstract protected function isApplicable(StorageConfig $config); |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Gets all storages, valid or not |
|
| 190 | - * |
|
| 191 | - * @return StorageConfig[] array of storage configs |
|
| 192 | - */ |
|
| 193 | - public function getAllStorages() { |
|
| 194 | - return $this->readConfig(); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Gets all valid storages |
|
| 199 | - * |
|
| 200 | - * @return StorageConfig[] |
|
| 201 | - */ |
|
| 202 | - public function getStorages() { |
|
| 203 | - return array_filter($this->getAllStorages(), [$this, 'validateStorage']); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Validate storage |
|
| 208 | - * FIXME: De-duplicate with StoragesController::validate() |
|
| 209 | - * |
|
| 210 | - * @param StorageConfig $storage |
|
| 211 | - * @return bool |
|
| 212 | - */ |
|
| 213 | - protected function validateStorage(StorageConfig $storage) { |
|
| 214 | - /** @var Backend */ |
|
| 215 | - $backend = $storage->getBackend(); |
|
| 216 | - /** @var AuthMechanism */ |
|
| 217 | - $authMechanism = $storage->getAuthMechanism(); |
|
| 218 | - |
|
| 219 | - if (!$backend->isVisibleFor($this->getVisibilityType())) { |
|
| 220 | - // not permitted to use backend |
|
| 221 | - return false; |
|
| 222 | - } |
|
| 223 | - if (!$authMechanism->isVisibleFor($this->getVisibilityType())) { |
|
| 224 | - // not permitted to use auth mechanism |
|
| 225 | - return false; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - return true; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Get the visibility type for this controller, used in validation |
|
| 233 | - * |
|
| 234 | - * @return string BackendService::VISIBILITY_* constants |
|
| 235 | - */ |
|
| 236 | - abstract public function getVisibilityType(); |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * @return integer |
|
| 240 | - */ |
|
| 241 | - protected function getType() { |
|
| 242 | - return DBConfigService::MOUNT_TYPE_ADMIN; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Add new storage to the configuration |
|
| 247 | - * |
|
| 248 | - * @param StorageConfig $newStorage storage attributes |
|
| 249 | - * |
|
| 250 | - * @return StorageConfig storage config, with added id |
|
| 251 | - */ |
|
| 252 | - public function addStorage(StorageConfig $newStorage) { |
|
| 253 | - $allStorages = $this->readConfig(); |
|
| 254 | - |
|
| 255 | - $configId = $this->dbConfig->addMount( |
|
| 256 | - $newStorage->getMountPoint(), |
|
| 257 | - $newStorage->getBackend()->getIdentifier(), |
|
| 258 | - $newStorage->getAuthMechanism()->getIdentifier(), |
|
| 259 | - $newStorage->getPriority(), |
|
| 260 | - $this->getType() |
|
| 261 | - ); |
|
| 262 | - |
|
| 263 | - $newStorage->setId($configId); |
|
| 264 | - |
|
| 265 | - foreach ($newStorage->getApplicableUsers() as $user) { |
|
| 266 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 267 | - } |
|
| 268 | - foreach ($newStorage->getApplicableGroups() as $group) { |
|
| 269 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 270 | - } |
|
| 271 | - foreach ($newStorage->getBackendOptions() as $key => $value) { |
|
| 272 | - $this->dbConfig->setConfig($configId, $key, $value); |
|
| 273 | - } |
|
| 274 | - foreach ($newStorage->getMountOptions() as $key => $value) { |
|
| 275 | - $this->dbConfig->setOption($configId, $key, $value); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) { |
|
| 279 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - // add new storage |
|
| 283 | - $allStorages[$configId] = $newStorage; |
|
| 284 | - |
|
| 285 | - $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
| 286 | - |
|
| 287 | - $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS); |
|
| 288 | - return $newStorage; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Create a storage from its parameters |
|
| 293 | - * |
|
| 294 | - * @param string $mountPoint storage mount point |
|
| 295 | - * @param string $backendIdentifier backend identifier |
|
| 296 | - * @param string $authMechanismIdentifier authentication mechanism identifier |
|
| 297 | - * @param array $backendOptions backend-specific options |
|
| 298 | - * @param array|null $mountOptions mount-specific options |
|
| 299 | - * @param array|null $applicableUsers users for which to mount the storage |
|
| 300 | - * @param array|null $applicableGroups groups for which to mount the storage |
|
| 301 | - * @param int|null $priority priority |
|
| 302 | - * |
|
| 303 | - * @return StorageConfig |
|
| 304 | - */ |
|
| 305 | - public function createStorage( |
|
| 306 | - $mountPoint, |
|
| 307 | - $backendIdentifier, |
|
| 308 | - $authMechanismIdentifier, |
|
| 309 | - $backendOptions, |
|
| 310 | - $mountOptions = null, |
|
| 311 | - $applicableUsers = null, |
|
| 312 | - $applicableGroups = null, |
|
| 313 | - $priority = null |
|
| 314 | - ) { |
|
| 315 | - $backend = $this->backendService->getBackend($backendIdentifier); |
|
| 316 | - if (!$backend) { |
|
| 317 | - $backend = new InvalidBackend($backendIdentifier); |
|
| 318 | - } |
|
| 319 | - $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); |
|
| 320 | - if (!$authMechanism) { |
|
| 321 | - $authMechanism = new InvalidAuth($authMechanismIdentifier); |
|
| 322 | - } |
|
| 323 | - $newStorage = new StorageConfig(); |
|
| 324 | - $newStorage->setMountPoint($mountPoint); |
|
| 325 | - $newStorage->setBackend($backend); |
|
| 326 | - $newStorage->setAuthMechanism($authMechanism); |
|
| 327 | - $newStorage->setBackendOptions($backendOptions); |
|
| 328 | - if (isset($mountOptions)) { |
|
| 329 | - $newStorage->setMountOptions($mountOptions); |
|
| 330 | - } |
|
| 331 | - if (isset($applicableUsers)) { |
|
| 332 | - $newStorage->setApplicableUsers($applicableUsers); |
|
| 333 | - } |
|
| 334 | - if (isset($applicableGroups)) { |
|
| 335 | - $newStorage->setApplicableGroups($applicableGroups); |
|
| 336 | - } |
|
| 337 | - if (isset($priority)) { |
|
| 338 | - $newStorage->setPriority($priority); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - return $newStorage; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Triggers the given hook signal for all the applicables given |
|
| 346 | - * |
|
| 347 | - * @param string $signal signal |
|
| 348 | - * @param string $mountPoint hook mount point param |
|
| 349 | - * @param string $mountType hook mount type param |
|
| 350 | - * @param array $applicableArray array of applicable users/groups for which to trigger the hook |
|
| 351 | - */ |
|
| 352 | - protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray): void { |
|
| 353 | - $this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent(null)); |
|
| 354 | - foreach ($applicableArray as $applicable) { |
|
| 355 | - \OCP\Util::emitHook( |
|
| 356 | - Filesystem::CLASSNAME, |
|
| 357 | - $signal, |
|
| 358 | - [ |
|
| 359 | - Filesystem::signal_param_path => $mountPoint, |
|
| 360 | - Filesystem::signal_param_mount_type => $mountType, |
|
| 361 | - Filesystem::signal_param_users => $applicable, |
|
| 362 | - ] |
|
| 363 | - ); |
|
| 364 | - } |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Triggers $signal for all applicable users of the given |
|
| 369 | - * storage |
|
| 370 | - * |
|
| 371 | - * @param StorageConfig $storage storage data |
|
| 372 | - * @param string $signal signal to trigger |
|
| 373 | - */ |
|
| 374 | - abstract protected function triggerHooks(StorageConfig $storage, $signal); |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Triggers signal_create_mount or signal_delete_mount to |
|
| 378 | - * accommodate for additions/deletions in applicableUsers |
|
| 379 | - * and applicableGroups fields. |
|
| 380 | - * |
|
| 381 | - * @param StorageConfig $oldStorage old storage data |
|
| 382 | - * @param StorageConfig $newStorage new storage data |
|
| 383 | - */ |
|
| 384 | - abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage); |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Update storage to the configuration |
|
| 388 | - * |
|
| 389 | - * @param StorageConfig $updatedStorage storage attributes |
|
| 390 | - * |
|
| 391 | - * @return StorageConfig storage config |
|
| 392 | - * @throws NotFoundException if the given storage does not exist in the config |
|
| 393 | - */ |
|
| 394 | - public function updateStorage(StorageConfig $updatedStorage) { |
|
| 395 | - $id = $updatedStorage->getId(); |
|
| 396 | - |
|
| 397 | - $existingMount = $this->dbConfig->getMountById($id); |
|
| 398 | - |
|
| 399 | - if (!is_array($existingMount)) { |
|
| 400 | - throw new NotFoundException('Storage with ID "' . $id . '" not found while updating storage'); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - $oldStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 404 | - |
|
| 405 | - if ($oldStorage->getBackend() instanceof InvalidBackend) { |
|
| 406 | - throw new NotFoundException('Storage with id "' . $id . '" cannot be edited due to missing backend'); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); |
|
| 410 | - $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups()); |
|
| 411 | - $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); |
|
| 412 | - $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); |
|
| 413 | - |
|
| 414 | - $oldUserCount = count($oldStorage->getApplicableUsers()); |
|
| 415 | - $oldGroupCount = count($oldStorage->getApplicableGroups()); |
|
| 416 | - $newUserCount = count($updatedStorage->getApplicableUsers()); |
|
| 417 | - $newGroupCount = count($updatedStorage->getApplicableGroups()); |
|
| 418 | - $wasGlobal = ($oldUserCount + $oldGroupCount) === 0; |
|
| 419 | - $isGlobal = ($newUserCount + $newGroupCount) === 0; |
|
| 420 | - |
|
| 421 | - foreach ($removedUsers as $user) { |
|
| 422 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 423 | - } |
|
| 424 | - foreach ($removedGroups as $group) { |
|
| 425 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 426 | - } |
|
| 427 | - foreach ($addedUsers as $user) { |
|
| 428 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 429 | - } |
|
| 430 | - foreach ($addedGroups as $group) { |
|
| 431 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - if ($wasGlobal && !$isGlobal) { |
|
| 435 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 436 | - } elseif (!$wasGlobal && $isGlobal) { |
|
| 437 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions()); |
|
| 441 | - $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions()); |
|
| 442 | - |
|
| 443 | - foreach ($changedConfig as $key => $value) { |
|
| 444 | - if ($value !== DefinitionParameter::UNMODIFIED_PLACEHOLDER) { |
|
| 445 | - $this->dbConfig->setConfig($id, $key, $value); |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - foreach ($changedOptions as $key => $value) { |
|
| 449 | - $this->dbConfig->setOption($id, $key, $value); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) { |
|
| 453 | - $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint()); |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) { |
|
| 457 | - $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier()); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - $this->triggerChangeHooks($oldStorage, $updatedStorage); |
|
| 461 | - |
|
| 462 | - if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly |
|
| 463 | - $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage)); |
|
| 464 | - } else { |
|
| 465 | - $storageId = $this->getStorageId($updatedStorage); |
|
| 466 | - foreach ($removedUsers as $userId) { |
|
| 467 | - $this->userMountCache->removeUserStorageMount($storageId, $userId); |
|
| 468 | - } |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - return $this->getStorage($id); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Delete the storage with the given id. |
|
| 476 | - * |
|
| 477 | - * @param int $id storage id |
|
| 478 | - * |
|
| 479 | - * @throws NotFoundException if no storage was found with the given id |
|
| 480 | - */ |
|
| 481 | - public function removeStorage($id) { |
|
| 482 | - $existingMount = $this->dbConfig->getMountById($id); |
|
| 483 | - |
|
| 484 | - if (!is_array($existingMount)) { |
|
| 485 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - $this->dbConfig->removeMount($id); |
|
| 489 | - |
|
| 490 | - $deletedStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 491 | - $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); |
|
| 492 | - |
|
| 493 | - // delete oc_storages entries and oc_filecache |
|
| 494 | - \OC\Files\Cache\Storage::cleanByMountId($id); |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * Construct the storage implementation |
|
| 499 | - * |
|
| 500 | - * @param StorageConfig $storageConfig |
|
| 501 | - * @return int |
|
| 502 | - */ |
|
| 503 | - private function getStorageId(StorageConfig $storageConfig) { |
|
| 504 | - try { |
|
| 505 | - $class = $storageConfig->getBackend()->getStorageClass(); |
|
| 506 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 507 | - $storage = new $class($storageConfig->getBackendOptions()); |
|
| 508 | - |
|
| 509 | - // auth mechanism should fire first |
|
| 510 | - $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
| 511 | - $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
| 512 | - |
|
| 513 | - return $storage->getStorageCache()->getNumericId(); |
|
| 514 | - } catch (\Exception $e) { |
|
| 515 | - return -1; |
|
| 516 | - } |
|
| 517 | - } |
|
| 54 | + /** @var BackendService */ |
|
| 55 | + protected $backendService; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var DBConfigService |
|
| 59 | + */ |
|
| 60 | + protected $dbConfig; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @var IUserMountCache |
|
| 64 | + */ |
|
| 65 | + protected $userMountCache; |
|
| 66 | + |
|
| 67 | + protected IEventDispatcher $eventDispatcher; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @param BackendService $backendService |
|
| 71 | + * @param DBConfigService $dbConfigService |
|
| 72 | + * @param IUserMountCache $userMountCache |
|
| 73 | + * @param IEventDispatcher $eventDispatcher |
|
| 74 | + */ |
|
| 75 | + public function __construct( |
|
| 76 | + BackendService $backendService, |
|
| 77 | + DBConfigService $dbConfigService, |
|
| 78 | + IUserMountCache $userMountCache, |
|
| 79 | + IEventDispatcher $eventDispatcher |
|
| 80 | + ) { |
|
| 81 | + $this->backendService = $backendService; |
|
| 82 | + $this->dbConfig = $dbConfigService; |
|
| 83 | + $this->userMountCache = $userMountCache; |
|
| 84 | + $this->eventDispatcher = $eventDispatcher; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + protected function readDBConfig() { |
|
| 88 | + return $this->dbConfig->getAdminMounts(); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + protected function getStorageConfigFromDBMount(array $mount) { |
|
| 92 | + $applicableUsers = array_filter($mount['applicable'], function ($applicable) { |
|
| 93 | + return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; |
|
| 94 | + }); |
|
| 95 | + $applicableUsers = array_map(function ($applicable) { |
|
| 96 | + return $applicable['value']; |
|
| 97 | + }, $applicableUsers); |
|
| 98 | + |
|
| 99 | + $applicableGroups = array_filter($mount['applicable'], function ($applicable) { |
|
| 100 | + return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; |
|
| 101 | + }); |
|
| 102 | + $applicableGroups = array_map(function ($applicable) { |
|
| 103 | + return $applicable['value']; |
|
| 104 | + }, $applicableGroups); |
|
| 105 | + |
|
| 106 | + try { |
|
| 107 | + $config = $this->createStorage( |
|
| 108 | + $mount['mount_point'], |
|
| 109 | + $mount['storage_backend'], |
|
| 110 | + $mount['auth_backend'], |
|
| 111 | + $mount['config'], |
|
| 112 | + $mount['options'], |
|
| 113 | + array_values($applicableUsers), |
|
| 114 | + array_values($applicableGroups), |
|
| 115 | + $mount['priority'] |
|
| 116 | + ); |
|
| 117 | + $config->setType($mount['type']); |
|
| 118 | + $config->setId((int)$mount['mount_id']); |
|
| 119 | + return $config; |
|
| 120 | + } catch (\UnexpectedValueException $e) { |
|
| 121 | + // don't die if a storage backend doesn't exist |
|
| 122 | + \OC::$server->getLogger()->logException($e, [ |
|
| 123 | + 'message' => 'Could not load storage.', |
|
| 124 | + 'level' => ILogger::ERROR, |
|
| 125 | + 'app' => 'files_external', |
|
| 126 | + ]); |
|
| 127 | + return null; |
|
| 128 | + } catch (\InvalidArgumentException $e) { |
|
| 129 | + \OC::$server->getLogger()->logException($e, [ |
|
| 130 | + 'message' => 'Could not load storage.', |
|
| 131 | + 'level' => ILogger::ERROR, |
|
| 132 | + 'app' => 'files_external', |
|
| 133 | + ]); |
|
| 134 | + return null; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Read the external storage config |
|
| 140 | + * |
|
| 141 | + * @return array map of storage id to storage config |
|
| 142 | + */ |
|
| 143 | + protected function readConfig() { |
|
| 144 | + $mounts = $this->readDBConfig(); |
|
| 145 | + $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
| 146 | + $configs = array_filter($configs, function ($config) { |
|
| 147 | + return $config instanceof StorageConfig; |
|
| 148 | + }); |
|
| 149 | + |
|
| 150 | + $keys = array_map(function (StorageConfig $config) { |
|
| 151 | + return $config->getId(); |
|
| 152 | + }, $configs); |
|
| 153 | + |
|
| 154 | + return array_combine($keys, $configs); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Get a storage with status |
|
| 159 | + * |
|
| 160 | + * @param int $id storage id |
|
| 161 | + * |
|
| 162 | + * @return StorageConfig |
|
| 163 | + * @throws NotFoundException if the storage with the given id was not found |
|
| 164 | + */ |
|
| 165 | + public function getStorage($id) { |
|
| 166 | + $mount = $this->dbConfig->getMountById($id); |
|
| 167 | + |
|
| 168 | + if (!is_array($mount)) { |
|
| 169 | + throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + $config = $this->getStorageConfigFromDBMount($mount); |
|
| 173 | + if ($this->isApplicable($config)) { |
|
| 174 | + return $config; |
|
| 175 | + } else { |
|
| 176 | + throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Check whether this storage service should provide access to a storage |
|
| 182 | + * |
|
| 183 | + * @param StorageConfig $config |
|
| 184 | + * @return bool |
|
| 185 | + */ |
|
| 186 | + abstract protected function isApplicable(StorageConfig $config); |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Gets all storages, valid or not |
|
| 190 | + * |
|
| 191 | + * @return StorageConfig[] array of storage configs |
|
| 192 | + */ |
|
| 193 | + public function getAllStorages() { |
|
| 194 | + return $this->readConfig(); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Gets all valid storages |
|
| 199 | + * |
|
| 200 | + * @return StorageConfig[] |
|
| 201 | + */ |
|
| 202 | + public function getStorages() { |
|
| 203 | + return array_filter($this->getAllStorages(), [$this, 'validateStorage']); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Validate storage |
|
| 208 | + * FIXME: De-duplicate with StoragesController::validate() |
|
| 209 | + * |
|
| 210 | + * @param StorageConfig $storage |
|
| 211 | + * @return bool |
|
| 212 | + */ |
|
| 213 | + protected function validateStorage(StorageConfig $storage) { |
|
| 214 | + /** @var Backend */ |
|
| 215 | + $backend = $storage->getBackend(); |
|
| 216 | + /** @var AuthMechanism */ |
|
| 217 | + $authMechanism = $storage->getAuthMechanism(); |
|
| 218 | + |
|
| 219 | + if (!$backend->isVisibleFor($this->getVisibilityType())) { |
|
| 220 | + // not permitted to use backend |
|
| 221 | + return false; |
|
| 222 | + } |
|
| 223 | + if (!$authMechanism->isVisibleFor($this->getVisibilityType())) { |
|
| 224 | + // not permitted to use auth mechanism |
|
| 225 | + return false; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + return true; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Get the visibility type for this controller, used in validation |
|
| 233 | + * |
|
| 234 | + * @return string BackendService::VISIBILITY_* constants |
|
| 235 | + */ |
|
| 236 | + abstract public function getVisibilityType(); |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * @return integer |
|
| 240 | + */ |
|
| 241 | + protected function getType() { |
|
| 242 | + return DBConfigService::MOUNT_TYPE_ADMIN; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Add new storage to the configuration |
|
| 247 | + * |
|
| 248 | + * @param StorageConfig $newStorage storage attributes |
|
| 249 | + * |
|
| 250 | + * @return StorageConfig storage config, with added id |
|
| 251 | + */ |
|
| 252 | + public function addStorage(StorageConfig $newStorage) { |
|
| 253 | + $allStorages = $this->readConfig(); |
|
| 254 | + |
|
| 255 | + $configId = $this->dbConfig->addMount( |
|
| 256 | + $newStorage->getMountPoint(), |
|
| 257 | + $newStorage->getBackend()->getIdentifier(), |
|
| 258 | + $newStorage->getAuthMechanism()->getIdentifier(), |
|
| 259 | + $newStorage->getPriority(), |
|
| 260 | + $this->getType() |
|
| 261 | + ); |
|
| 262 | + |
|
| 263 | + $newStorage->setId($configId); |
|
| 264 | + |
|
| 265 | + foreach ($newStorage->getApplicableUsers() as $user) { |
|
| 266 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 267 | + } |
|
| 268 | + foreach ($newStorage->getApplicableGroups() as $group) { |
|
| 269 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 270 | + } |
|
| 271 | + foreach ($newStorage->getBackendOptions() as $key => $value) { |
|
| 272 | + $this->dbConfig->setConfig($configId, $key, $value); |
|
| 273 | + } |
|
| 274 | + foreach ($newStorage->getMountOptions() as $key => $value) { |
|
| 275 | + $this->dbConfig->setOption($configId, $key, $value); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) { |
|
| 279 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + // add new storage |
|
| 283 | + $allStorages[$configId] = $newStorage; |
|
| 284 | + |
|
| 285 | + $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
| 286 | + |
|
| 287 | + $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS); |
|
| 288 | + return $newStorage; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Create a storage from its parameters |
|
| 293 | + * |
|
| 294 | + * @param string $mountPoint storage mount point |
|
| 295 | + * @param string $backendIdentifier backend identifier |
|
| 296 | + * @param string $authMechanismIdentifier authentication mechanism identifier |
|
| 297 | + * @param array $backendOptions backend-specific options |
|
| 298 | + * @param array|null $mountOptions mount-specific options |
|
| 299 | + * @param array|null $applicableUsers users for which to mount the storage |
|
| 300 | + * @param array|null $applicableGroups groups for which to mount the storage |
|
| 301 | + * @param int|null $priority priority |
|
| 302 | + * |
|
| 303 | + * @return StorageConfig |
|
| 304 | + */ |
|
| 305 | + public function createStorage( |
|
| 306 | + $mountPoint, |
|
| 307 | + $backendIdentifier, |
|
| 308 | + $authMechanismIdentifier, |
|
| 309 | + $backendOptions, |
|
| 310 | + $mountOptions = null, |
|
| 311 | + $applicableUsers = null, |
|
| 312 | + $applicableGroups = null, |
|
| 313 | + $priority = null |
|
| 314 | + ) { |
|
| 315 | + $backend = $this->backendService->getBackend($backendIdentifier); |
|
| 316 | + if (!$backend) { |
|
| 317 | + $backend = new InvalidBackend($backendIdentifier); |
|
| 318 | + } |
|
| 319 | + $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); |
|
| 320 | + if (!$authMechanism) { |
|
| 321 | + $authMechanism = new InvalidAuth($authMechanismIdentifier); |
|
| 322 | + } |
|
| 323 | + $newStorage = new StorageConfig(); |
|
| 324 | + $newStorage->setMountPoint($mountPoint); |
|
| 325 | + $newStorage->setBackend($backend); |
|
| 326 | + $newStorage->setAuthMechanism($authMechanism); |
|
| 327 | + $newStorage->setBackendOptions($backendOptions); |
|
| 328 | + if (isset($mountOptions)) { |
|
| 329 | + $newStorage->setMountOptions($mountOptions); |
|
| 330 | + } |
|
| 331 | + if (isset($applicableUsers)) { |
|
| 332 | + $newStorage->setApplicableUsers($applicableUsers); |
|
| 333 | + } |
|
| 334 | + if (isset($applicableGroups)) { |
|
| 335 | + $newStorage->setApplicableGroups($applicableGroups); |
|
| 336 | + } |
|
| 337 | + if (isset($priority)) { |
|
| 338 | + $newStorage->setPriority($priority); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + return $newStorage; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Triggers the given hook signal for all the applicables given |
|
| 346 | + * |
|
| 347 | + * @param string $signal signal |
|
| 348 | + * @param string $mountPoint hook mount point param |
|
| 349 | + * @param string $mountType hook mount type param |
|
| 350 | + * @param array $applicableArray array of applicable users/groups for which to trigger the hook |
|
| 351 | + */ |
|
| 352 | + protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray): void { |
|
| 353 | + $this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent(null)); |
|
| 354 | + foreach ($applicableArray as $applicable) { |
|
| 355 | + \OCP\Util::emitHook( |
|
| 356 | + Filesystem::CLASSNAME, |
|
| 357 | + $signal, |
|
| 358 | + [ |
|
| 359 | + Filesystem::signal_param_path => $mountPoint, |
|
| 360 | + Filesystem::signal_param_mount_type => $mountType, |
|
| 361 | + Filesystem::signal_param_users => $applicable, |
|
| 362 | + ] |
|
| 363 | + ); |
|
| 364 | + } |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Triggers $signal for all applicable users of the given |
|
| 369 | + * storage |
|
| 370 | + * |
|
| 371 | + * @param StorageConfig $storage storage data |
|
| 372 | + * @param string $signal signal to trigger |
|
| 373 | + */ |
|
| 374 | + abstract protected function triggerHooks(StorageConfig $storage, $signal); |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Triggers signal_create_mount or signal_delete_mount to |
|
| 378 | + * accommodate for additions/deletions in applicableUsers |
|
| 379 | + * and applicableGroups fields. |
|
| 380 | + * |
|
| 381 | + * @param StorageConfig $oldStorage old storage data |
|
| 382 | + * @param StorageConfig $newStorage new storage data |
|
| 383 | + */ |
|
| 384 | + abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage); |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Update storage to the configuration |
|
| 388 | + * |
|
| 389 | + * @param StorageConfig $updatedStorage storage attributes |
|
| 390 | + * |
|
| 391 | + * @return StorageConfig storage config |
|
| 392 | + * @throws NotFoundException if the given storage does not exist in the config |
|
| 393 | + */ |
|
| 394 | + public function updateStorage(StorageConfig $updatedStorage) { |
|
| 395 | + $id = $updatedStorage->getId(); |
|
| 396 | + |
|
| 397 | + $existingMount = $this->dbConfig->getMountById($id); |
|
| 398 | + |
|
| 399 | + if (!is_array($existingMount)) { |
|
| 400 | + throw new NotFoundException('Storage with ID "' . $id . '" not found while updating storage'); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + $oldStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 404 | + |
|
| 405 | + if ($oldStorage->getBackend() instanceof InvalidBackend) { |
|
| 406 | + throw new NotFoundException('Storage with id "' . $id . '" cannot be edited due to missing backend'); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); |
|
| 410 | + $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups()); |
|
| 411 | + $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); |
|
| 412 | + $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); |
|
| 413 | + |
|
| 414 | + $oldUserCount = count($oldStorage->getApplicableUsers()); |
|
| 415 | + $oldGroupCount = count($oldStorage->getApplicableGroups()); |
|
| 416 | + $newUserCount = count($updatedStorage->getApplicableUsers()); |
|
| 417 | + $newGroupCount = count($updatedStorage->getApplicableGroups()); |
|
| 418 | + $wasGlobal = ($oldUserCount + $oldGroupCount) === 0; |
|
| 419 | + $isGlobal = ($newUserCount + $newGroupCount) === 0; |
|
| 420 | + |
|
| 421 | + foreach ($removedUsers as $user) { |
|
| 422 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 423 | + } |
|
| 424 | + foreach ($removedGroups as $group) { |
|
| 425 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 426 | + } |
|
| 427 | + foreach ($addedUsers as $user) { |
|
| 428 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 429 | + } |
|
| 430 | + foreach ($addedGroups as $group) { |
|
| 431 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + if ($wasGlobal && !$isGlobal) { |
|
| 435 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 436 | + } elseif (!$wasGlobal && $isGlobal) { |
|
| 437 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions()); |
|
| 441 | + $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions()); |
|
| 442 | + |
|
| 443 | + foreach ($changedConfig as $key => $value) { |
|
| 444 | + if ($value !== DefinitionParameter::UNMODIFIED_PLACEHOLDER) { |
|
| 445 | + $this->dbConfig->setConfig($id, $key, $value); |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + foreach ($changedOptions as $key => $value) { |
|
| 449 | + $this->dbConfig->setOption($id, $key, $value); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) { |
|
| 453 | + $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint()); |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) { |
|
| 457 | + $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier()); |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + $this->triggerChangeHooks($oldStorage, $updatedStorage); |
|
| 461 | + |
|
| 462 | + if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly |
|
| 463 | + $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage)); |
|
| 464 | + } else { |
|
| 465 | + $storageId = $this->getStorageId($updatedStorage); |
|
| 466 | + foreach ($removedUsers as $userId) { |
|
| 467 | + $this->userMountCache->removeUserStorageMount($storageId, $userId); |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + return $this->getStorage($id); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Delete the storage with the given id. |
|
| 476 | + * |
|
| 477 | + * @param int $id storage id |
|
| 478 | + * |
|
| 479 | + * @throws NotFoundException if no storage was found with the given id |
|
| 480 | + */ |
|
| 481 | + public function removeStorage($id) { |
|
| 482 | + $existingMount = $this->dbConfig->getMountById($id); |
|
| 483 | + |
|
| 484 | + if (!is_array($existingMount)) { |
|
| 485 | + throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + $this->dbConfig->removeMount($id); |
|
| 489 | + |
|
| 490 | + $deletedStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 491 | + $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); |
|
| 492 | + |
|
| 493 | + // delete oc_storages entries and oc_filecache |
|
| 494 | + \OC\Files\Cache\Storage::cleanByMountId($id); |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * Construct the storage implementation |
|
| 499 | + * |
|
| 500 | + * @param StorageConfig $storageConfig |
|
| 501 | + * @return int |
|
| 502 | + */ |
|
| 503 | + private function getStorageId(StorageConfig $storageConfig) { |
|
| 504 | + try { |
|
| 505 | + $class = $storageConfig->getBackend()->getStorageClass(); |
|
| 506 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 507 | + $storage = new $class($storageConfig->getBackendOptions()); |
|
| 508 | + |
|
| 509 | + // auth mechanism should fire first |
|
| 510 | + $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
| 511 | + $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
| 512 | + |
|
| 513 | + return $storage->getStorageCache()->getNumericId(); |
|
| 514 | + } catch (\Exception $e) { |
|
| 515 | + return -1; |
|
| 516 | + } |
|
| 517 | + } |
|
| 518 | 518 | } |
@@ -31,41 +31,41 @@ |
||
| 31 | 31 | * @since 7.0.0 |
| 32 | 32 | */ |
| 33 | 33 | interface IAppConfig { |
| 34 | - /** |
|
| 35 | - * check if a key is set in the appconfig |
|
| 36 | - * @param string $app |
|
| 37 | - * @param string $key |
|
| 38 | - * @return bool |
|
| 39 | - * @since 7.0.0 |
|
| 40 | - */ |
|
| 41 | - public function hasKey($app, $key); |
|
| 34 | + /** |
|
| 35 | + * check if a key is set in the appconfig |
|
| 36 | + * @param string $app |
|
| 37 | + * @param string $key |
|
| 38 | + * @return bool |
|
| 39 | + * @since 7.0.0 |
|
| 40 | + */ |
|
| 41 | + public function hasKey($app, $key); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * get multiply values, either the app or key can be used as wildcard by setting it to false |
|
| 45 | - * |
|
| 46 | - * @param string|false $key |
|
| 47 | - * @param string|false $app |
|
| 48 | - * @return array|false |
|
| 49 | - * @since 7.0.0 |
|
| 50 | - */ |
|
| 51 | - public function getValues($app, $key); |
|
| 43 | + /** |
|
| 44 | + * get multiply values, either the app or key can be used as wildcard by setting it to false |
|
| 45 | + * |
|
| 46 | + * @param string|false $key |
|
| 47 | + * @param string|false $app |
|
| 48 | + * @return array|false |
|
| 49 | + * @since 7.0.0 |
|
| 50 | + */ |
|
| 51 | + public function getValues($app, $key); |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * get all values of the app or and filters out sensitive data |
|
| 55 | - * |
|
| 56 | - * @param string $app |
|
| 57 | - * @return array |
|
| 58 | - * @since 12.0.0 |
|
| 59 | - */ |
|
| 60 | - public function getFilteredValues($app); |
|
| 53 | + /** |
|
| 54 | + * get all values of the app or and filters out sensitive data |
|
| 55 | + * |
|
| 56 | + * @param string $app |
|
| 57 | + * @return array |
|
| 58 | + * @since 12.0.0 |
|
| 59 | + */ |
|
| 60 | + public function getFilteredValues($app); |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Get all apps using the config |
|
| 64 | - * @return array an array of app ids |
|
| 65 | - * |
|
| 66 | - * This function returns a list of all apps that have at least one |
|
| 67 | - * entry in the appconfig table. |
|
| 68 | - * @since 7.0.0 |
|
| 69 | - */ |
|
| 70 | - public function getApps(); |
|
| 62 | + /** |
|
| 63 | + * Get all apps using the config |
|
| 64 | + * @return array an array of app ids |
|
| 65 | + * |
|
| 66 | + * This function returns a list of all apps that have at least one |
|
| 67 | + * entry in the appconfig table. |
|
| 68 | + * @since 7.0.0 |
|
| 69 | + */ |
|
| 70 | + public function getApps(); |
|
| 71 | 71 | } |