diff --git a/apps/admin_audit/composer/composer/autoload_classmap.php b/apps/admin_audit/composer/composer/autoload_classmap.php index fc4be52ebbb23..1f831dd430cbe 100644 --- a/apps/admin_audit/composer/composer/autoload_classmap.php +++ b/apps/admin_audit/composer/composer/autoload_classmap.php @@ -8,12 +8,7 @@ return array( 'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php', 'OCA\\AdminAudit\\Actions\\Action' => $baseDir . '/../lib/Actions/Action.php', - 'OCA\\AdminAudit\\Actions\\AppManagement' => $baseDir . '/../lib/Actions/AppManagement.php', - 'OCA\\AdminAudit\\Actions\\Auth' => $baseDir . '/../lib/Actions/Auth.php', - 'OCA\\AdminAudit\\Actions\\Console' => $baseDir . '/../lib/Actions/Console.php', 'OCA\\AdminAudit\\Actions\\Files' => $baseDir . '/../lib/Actions/Files.php', - 'OCA\\AdminAudit\\Actions\\GroupManagement' => $baseDir . '/../lib/Actions/GroupManagement.php', - 'OCA\\AdminAudit\\Actions\\Security' => $baseDir . '/../lib/Actions/Security.php', 'OCA\\AdminAudit\\Actions\\Sharing' => $baseDir . '/../lib/Actions/Sharing.php', 'OCA\\AdminAudit\\Actions\\Trashbin' => $baseDir . '/../lib/Actions/Trashbin.php', 'OCA\\AdminAudit\\Actions\\UserManagement' => $baseDir . '/../lib/Actions/UserManagement.php', @@ -22,5 +17,13 @@ 'OCA\\AdminAudit\\AuditLogger' => $baseDir . '/../lib/AuditLogger.php', 'OCA\\AdminAudit\\BackgroundJobs\\Rotate' => $baseDir . '/../lib/BackgroundJobs/Rotate.php', 'OCA\\AdminAudit\\IAuditLogger' => $baseDir . '/../lib/IAuditLogger.php', + 'OCA\\AdminAudit\\Listener\\AppManagementEventListener' => $baseDir . '/../lib/Listener/AppManagementEventListener.php', + 'OCA\\AdminAudit\\Listener\\AuthEventListener' => $baseDir . '/../lib/Listener/AuthEventListener.php', + 'OCA\\AdminAudit\\Listener\\ConsoleEventListener' => $baseDir . '/../lib/Listener/ConsoleEventListener.php', 'OCA\\AdminAudit\\Listener\\CriticalActionPerformedEventListener' => $baseDir . '/../lib/Listener/CriticalActionPerformedEventListener.php', + 'OCA\\AdminAudit\\Listener\\FileEventListener' => $baseDir . '/../lib/Listener/FileEventListener.php', + 'OCA\\AdminAudit\\Listener\\GroupManagementEventListener' => $baseDir . '/../lib/Listener/GroupManagementEventListener.php', + 'OCA\\AdminAudit\\Listener\\SecurityEventListener' => $baseDir . '/../lib/Listener/SecurityEventListener.php', + 'OCA\\AdminAudit\\Listener\\SharingEventListener' => $baseDir . '/../lib/Listener/SharingEventListener.php', + 'OCA\\AdminAudit\\Listener\\UserManagementEventListener' => $baseDir . '/../lib/Listener/UserManagementEventListener.php', ); diff --git a/apps/admin_audit/composer/composer/autoload_static.php b/apps/admin_audit/composer/composer/autoload_static.php index 38518c8a9ba52..24b3ae3de1e9f 100644 --- a/apps/admin_audit/composer/composer/autoload_static.php +++ b/apps/admin_audit/composer/composer/autoload_static.php @@ -23,12 +23,7 @@ class ComposerStaticInitAdminAudit public static $classMap = array ( 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', 'OCA\\AdminAudit\\Actions\\Action' => __DIR__ . '/..' . '/../lib/Actions/Action.php', - 'OCA\\AdminAudit\\Actions\\AppManagement' => __DIR__ . '/..' . '/../lib/Actions/AppManagement.php', - 'OCA\\AdminAudit\\Actions\\Auth' => __DIR__ . '/..' . '/../lib/Actions/Auth.php', - 'OCA\\AdminAudit\\Actions\\Console' => __DIR__ . '/..' . '/../lib/Actions/Console.php', 'OCA\\AdminAudit\\Actions\\Files' => __DIR__ . '/..' . '/../lib/Actions/Files.php', - 'OCA\\AdminAudit\\Actions\\GroupManagement' => __DIR__ . '/..' . '/../lib/Actions/GroupManagement.php', - 'OCA\\AdminAudit\\Actions\\Security' => __DIR__ . '/..' . '/../lib/Actions/Security.php', 'OCA\\AdminAudit\\Actions\\Sharing' => __DIR__ . '/..' . '/../lib/Actions/Sharing.php', 'OCA\\AdminAudit\\Actions\\Trashbin' => __DIR__ . '/..' . '/../lib/Actions/Trashbin.php', 'OCA\\AdminAudit\\Actions\\UserManagement' => __DIR__ . '/..' . '/../lib/Actions/UserManagement.php', @@ -37,7 +32,15 @@ class ComposerStaticInitAdminAudit 'OCA\\AdminAudit\\AuditLogger' => __DIR__ . '/..' . '/../lib/AuditLogger.php', 'OCA\\AdminAudit\\BackgroundJobs\\Rotate' => __DIR__ . '/..' . '/../lib/BackgroundJobs/Rotate.php', 'OCA\\AdminAudit\\IAuditLogger' => __DIR__ . '/..' . '/../lib/IAuditLogger.php', + 'OCA\\AdminAudit\\Listener\\AppManagementEventListener' => __DIR__ . '/..' . '/../lib/Listener/AppManagementEventListener.php', + 'OCA\\AdminAudit\\Listener\\AuthEventListener' => __DIR__ . '/..' . '/../lib/Listener/AuthEventListener.php', + 'OCA\\AdminAudit\\Listener\\ConsoleEventListener' => __DIR__ . '/..' . '/../lib/Listener/ConsoleEventListener.php', 'OCA\\AdminAudit\\Listener\\CriticalActionPerformedEventListener' => __DIR__ . '/..' . '/../lib/Listener/CriticalActionPerformedEventListener.php', + 'OCA\\AdminAudit\\Listener\\FileEventListener' => __DIR__ . '/..' . '/../lib/Listener/FileEventListener.php', + 'OCA\\AdminAudit\\Listener\\GroupManagementEventListener' => __DIR__ . '/..' . '/../lib/Listener/GroupManagementEventListener.php', + 'OCA\\AdminAudit\\Listener\\SecurityEventListener' => __DIR__ . '/..' . '/../lib/Listener/SecurityEventListener.php', + 'OCA\\AdminAudit\\Listener\\SharingEventListener' => __DIR__ . '/..' . '/../lib/Listener/SharingEventListener.php', + 'OCA\\AdminAudit\\Listener\\UserManagementEventListener' => __DIR__ . '/..' . '/../lib/Listener/UserManagementEventListener.php', ); public static function getInitializer(ClassLoader $loader) diff --git a/apps/admin_audit/lib/Actions/AppManagement.php b/apps/admin_audit/lib/Actions/AppManagement.php deleted file mode 100644 index d6bc7d2c61f26..0000000000000 --- a/apps/admin_audit/lib/Actions/AppManagement.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * @author Joas Schilling - * @author Roeland Jago Douma - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ -namespace OCA\AdminAudit\Actions; - -class AppManagement extends Action { - - /** - * @param string $appName - */ - public function enableApp(string $appName): void { - $this->log('App "%s" enabled', - ['app' => $appName], - ['app'] - ); - } - - /** - * @param string $appName - * @param string[] $groups - */ - public function enableAppForGroups(string $appName, array $groups): void { - $this->log('App "%1$s" enabled for groups: %2$s', - ['app' => $appName, 'groups' => implode(', ', $groups)], - ['app', 'groups'] - ); - } - - /** - * @param string $appName - */ - public function disableApp(string $appName): void { - $this->log('App "%s" disabled', - ['app' => $appName], - ['app'] - ); - } -} diff --git a/apps/admin_audit/lib/Actions/Auth.php b/apps/admin_audit/lib/Actions/Auth.php deleted file mode 100644 index 27722d4a41c1b..0000000000000 --- a/apps/admin_audit/lib/Actions/Auth.php +++ /dev/null @@ -1,65 +0,0 @@ - - * - * @author Joas Schilling - * @author Lukas Reschke - * @author Roeland Jago Douma - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ -namespace OCA\AdminAudit\Actions; - -/** - * Class Auth logs all auth related actions - * - * @package OCA\AdminAudit\Actions - */ -class Auth extends Action { - public function loginAttempt(array $params): void { - $this->log( - 'Login attempt: "%s"', - $params, - [ - 'uid', - ], - true - ); - } - - public function loginSuccessful(array $params): void { - $this->log( - 'Login successful: "%s"', - $params, - [ - 'uid', - ], - true - ); - } - - public function logout(array $params): void { - $this->log( - 'Logout occurred', - [], - [] - ); - } -} diff --git a/apps/admin_audit/lib/Actions/GroupManagement.php b/apps/admin_audit/lib/Actions/GroupManagement.php deleted file mode 100644 index e79b86bb88bcc..0000000000000 --- a/apps/admin_audit/lib/Actions/GroupManagement.php +++ /dev/null @@ -1,109 +0,0 @@ - - * - * @author Bjoern Schiessle - * @author Joas Schilling - * @author Lukas Reschke - * @author Roeland Jago Douma - * @author Roger Szabo - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ -namespace OCA\AdminAudit\Actions; - -use OCP\IGroup; -use OCP\IUser; - -/** - * Class GroupManagement logs all group manager related events - * - * @package OCA\AdminAudit\Actions - */ -class GroupManagement extends Action { - - /** - * log add user to group event - * - * @param IGroup $group - * @param IUser $user - */ - public function addUser(IGroup $group, IUser $user): void { - $this->log('User "%s" added to group "%s"', - [ - 'group' => $group->getGID(), - 'user' => $user->getUID() - ], - [ - 'user', 'group' - ] - ); - } - - /** - * log remove user from group event - * - * @param IGroup $group - * @param IUser $user - */ - public function removeUser(IGroup $group, IUser $user): void { - $this->log('User "%s" removed from group "%s"', - [ - 'group' => $group->getGID(), - 'user' => $user->getUID() - ], - [ - 'user', 'group' - ] - ); - } - - /** - * log create group to group event - * - * @param IGroup $group - */ - public function createGroup(IGroup $group): void { - $this->log('Group created: "%s"', - [ - 'group' => $group->getGID() - ], - [ - 'group' - ] - ); - } - - /** - * log delete group to group event - * - * @param IGroup $group - */ - public function deleteGroup(IGroup $group): void { - $this->log('Group deleted: "%s"', - [ - 'group' => $group->getGID() - ], - [ - 'group' - ] - ); - } -} diff --git a/apps/admin_audit/lib/Actions/Security.php b/apps/admin_audit/lib/Actions/Security.php deleted file mode 100644 index e4831ac6fc1ac..0000000000000 --- a/apps/admin_audit/lib/Actions/Security.php +++ /dev/null @@ -1,78 +0,0 @@ - - * - * @author Christoph Wurst - * @author Roeland Jago Douma - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ -namespace OCA\AdminAudit\Actions; - -use OCP\IUser; - -/** - * Class Sharing logs the sharing actions - * - * @package OCA\AdminAudit\Actions - */ -class Security extends Action { - /** - * Log twofactor auth enabled - * - * @param IUser $user - * @param array $params - */ - public function twofactorFailed(IUser $user, array $params): void { - $params['uid'] = $user->getUID(); - $params['displayName'] = $user->getDisplayName(); - - $this->log( - 'Failed two factor attempt by user %s (%s) with provider %s', - $params, - [ - 'displayName', - 'uid', - 'provider', - ] - ); - } - - /** - * Logs unsharing of data - * - * @param IUser $user - * @param array $params - */ - public function twofactorSuccess(IUser $user, array $params): void { - $params['uid'] = $user->getUID(); - $params['displayName'] = $user->getDisplayName(); - - $this->log( - 'Successful two factor attempt by user %s (%s) with provider %s', - $params, - [ - 'displayName', - 'uid', - 'provider', - ] - ); - } -} diff --git a/apps/admin_audit/lib/Actions/UserManagement.php b/apps/admin_audit/lib/Actions/UserManagement.php index 02d5b60d2fa29..a3a3afb23e39e 100644 --- a/apps/admin_audit/lib/Actions/UserManagement.php +++ b/apps/admin_audit/lib/Actions/UserManagement.php @@ -31,28 +31,12 @@ */ namespace OCA\AdminAudit\Actions; -use OCP\IUser; - /** * Class UserManagement logs all user management related actions. * * @package OCA\AdminAudit\Actions */ class UserManagement extends Action { - /** - * Log creation of users - * - * @param array $params - */ - public function create(array $params): void { - $this->log( - 'User created: "%s"', - $params, - [ - 'uid', - ] - ); - } /** * Log assignments of users (typically user backends) @@ -67,21 +51,6 @@ public function assign(string $uid): void { ); } - /** - * Log deletion of users - * - * @param array $params - */ - public function delete(array $params): void { - $this->log( - 'User deleted: "%s"', - $params, - [ - 'uid', - ] - ); - } - /** * Log unassignments of users (typically user backends, no data removed) * @@ -94,53 +63,4 @@ public function unassign(string $uid): void { [ 'uid' ] ); } - - /** - * Log enabling of users - * - * @param array $params - */ - public function change(array $params): void { - switch ($params['feature']) { - case 'enabled': - $this->log( - $params['value'] === true - ? 'User enabled: "%s"' - : 'User disabled: "%s"', - ['user' => $params['user']->getUID()], - [ - 'user', - ] - ); - break; - case 'eMailAddress': - $this->log( - 'Email address changed for user %s', - ['user' => $params['user']->getUID()], - [ - 'user', - ] - ); - break; - } - } - - /** - * Logs changing of the user scope - * - * @param IUser $user - */ - public function setPassword(IUser $user): void { - if ($user->getBackendClassName() === 'Database') { - $this->log( - 'Password of user "%s" has been changed', - [ - 'user' => $user->getUID(), - ], - [ - 'user', - ] - ); - } - } } diff --git a/apps/admin_audit/lib/AppInfo/Application.php b/apps/admin_audit/lib/AppInfo/Application.php index 1160d151710c0..6f18990dc3737 100644 --- a/apps/admin_audit/lib/AppInfo/Application.php +++ b/apps/admin_audit/lib/AppInfo/Application.php @@ -13,6 +13,7 @@ * @author Joas Schilling * @author Morris Jobke * @author Roeland Jago Douma + * @author Thomas Citharel * @author Tiago Flores * * @license GNU AGPL version 3 or any later version @@ -33,50 +34,58 @@ */ namespace OCA\AdminAudit\AppInfo; -use Closure; use OC\Files\Filesystem; -use OC\Files\Node\File; -use OC\Group\Manager as GroupManager; -use OC\User\Session as UserSession; -use OCA\AdminAudit\Actions\AppManagement; -use OCA\AdminAudit\Actions\Auth; -use OCA\AdminAudit\Actions\Console; use OCA\AdminAudit\Actions\Files; -use OCA\AdminAudit\Actions\GroupManagement; -use OCA\AdminAudit\Actions\Security; use OCA\AdminAudit\Actions\Sharing; use OCA\AdminAudit\Actions\Trashbin; -use OCA\AdminAudit\Actions\UserManagement; use OCA\AdminAudit\Actions\Versions; use OCA\AdminAudit\AuditLogger; use OCA\AdminAudit\IAuditLogger; +use OCA\AdminAudit\Listener\AppManagementEventListener; +use OCA\AdminAudit\Listener\AuthEventListener; +use OCA\AdminAudit\Listener\ConsoleEventListener; use OCA\AdminAudit\Listener\CriticalActionPerformedEventListener; -use OCP\App\ManagerEvent; +use OCA\AdminAudit\Listener\FileEventListener; +use OCA\AdminAudit\Listener\GroupManagementEventListener; +use OCA\AdminAudit\Listener\SecurityEventListener; +use OCA\AdminAudit\Listener\SharingEventListener; +use OCA\AdminAudit\Listener\UserManagementEventListener; +use OCP\App\Events\AppDisableEvent; +use OCP\App\Events\AppEnableEvent; +use OCP\App\Events\AppUpdateEvent; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; -use OCP\Authentication\TwoFactorAuth\IProvider; -use OCP\Console\ConsoleEvent; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserDisabled; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserEnabled; +use OCP\Console\ConsoleEventV2; +use OCP\Group\Events\GroupCreatedEvent; +use OCP\Group\Events\GroupDeletedEvent; +use OCP\Group\Events\UserAddedEvent; +use OCP\Group\Events\UserRemovedEvent; use OCP\IConfig; -use OCP\IGroupManager; -use OCP\IPreview; -use OCP\IServerContainer; -use OCP\IUserSession; use OCP\Log\Audit\CriticalActionPerformedEvent; use OCP\Log\ILogFactory; +use OCP\Preview\BeforePreviewFetchedEvent; use OCP\Share; +use OCP\Share\Events\ShareCreatedEvent; +use OCP\Share\Events\ShareDeletedEvent; +use OCP\User\Events\BeforeUserLoggedInEvent; +use OCP\User\Events\BeforeUserLoggedOutEvent; +use OCP\User\Events\PasswordUpdatedEvent; +use OCP\User\Events\UserChangedEvent; +use OCP\User\Events\UserCreatedEvent; +use OCP\User\Events\UserDeletedEvent; +use OCP\User\Events\UserIdAssignedEvent; +use OCP\User\Events\UserIdUnassignedEvent; +use OCP\User\Events\UserLoggedInEvent; +use OCP\User\Events\UserLoggedInWithCookieEvent; use OCP\Util; use Psr\Container\ContainerInterface; -use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; class Application extends App implements IBootstrap { - /** @var LoggerInterface */ - protected $logger; - public function __construct() { parent::__construct('admin_audit'); } @@ -87,6 +96,45 @@ public function register(IRegistrationContext $context): void { }); $context->registerEventListener(CriticalActionPerformedEvent::class, CriticalActionPerformedEventListener::class); + + // User management events + $context->registerEventListener(UserCreatedEvent::class, UserManagementEventListener::class); + $context->registerEventListener(UserDeletedEvent::class, UserManagementEventListener::class); + $context->registerEventListener(UserChangedEvent::class, UserManagementEventListener::class); + $context->registerEventListener(PasswordUpdatedEvent::class, UserManagementEventListener::class); + $context->registerEventListener(UserIdAssignedEvent::class, UserManagementEventListener::class); + $context->registerEventListener(UserIdUnassignedEvent::class, UserManagementEventListener::class); + + // Group management events + $context->registerEventListener(UserAddedEvent::class, GroupManagementEventListener::class); + $context->registerEventListener(UserRemovedEvent::class, GroupManagementEventListener::class); + $context->registerEventListener(GroupCreatedEvent::class, GroupManagementEventListener::class); + $context->registerEventListener(GroupDeletedEvent::class, GroupManagementEventListener::class); + + // Sharing events + $context->registerEventListener(ShareCreatedEvent::class, SharingEventListener::class); + $context->registerEventListener(ShareDeletedEvent::class, SharingEventListener::class); + + // Auth events + $context->registerEventListener(BeforeUserLoggedInEvent::class, AuthEventListener::class); + $context->registerEventListener(UserLoggedInWithCookieEvent::class, AuthEventListener::class); + $context->registerEventListener(UserLoggedInEvent::class, AuthEventListener::class); + $context->registerEventListener(BeforeUserLoggedOutEvent::class, AuthEventListener::class); + + // File events + $context->registerEventListener(BeforePreviewFetchedEvent::class, FileEventListener::class); + + // Security events + $context->registerEventListener(TwoFactorProviderForUserEnabled::class, SecurityEventListener::class); + $context->registerEventListener(TwoFactorProviderForUserDisabled::class, SecurityEventListener::class); + + // App management events + $context->registerEventListener(AppEnableEvent::class, AppManagementEventListener::class); + $context->registerEventListener(AppDisableEvent::class, AppManagementEventListener::class); + $context->registerEventListener(AppUpdateEvent::class, AppManagementEventListener::class); + + // Console events + $context->registerEventListener(ConsoleEventV2::class, ConsoleEventListener::class); } public function boot(IBootContext $context): void { @@ -97,118 +145,32 @@ public function boot(IBootContext $context): void { * TODO: once the hooks are migrated to lazy events, this should be done * in \OCA\AdminAudit\AppInfo\Application::register */ - $this->registerHooks($logger, $context->getServerContainer()); + $this->registerLegacyHooks($logger); } /** * Register hooks in order to log them */ - private function registerHooks(IAuditLogger $logger, - IServerContainer $serverContainer): void { - $this->userManagementHooks($logger, $serverContainer->get(IUserSession::class)); - $this->groupHooks($logger, $serverContainer->get(IGroupManager::class)); - $this->authHooks($logger); + private function registerLegacyHooks(IAuditLogger $logger): void { - /** @var EventDispatcherInterface $eventDispatcher */ - $eventDispatcher = $serverContainer->get(EventDispatcherInterface::class); - $this->consoleHooks($logger, $eventDispatcher); - $this->appHooks($logger, $eventDispatcher); + $this->sharingLegacyHooks($logger); - $this->sharingHooks($logger); - - $this->fileHooks($logger, $eventDispatcher); + $this->fileHooks($logger); $this->trashbinHooks($logger); $this->versionsHooks($logger); - - $this->securityHooks($logger, $eventDispatcher); - } - - private function userManagementHooks(IAuditLogger $logger, - IUserSession $userSession): void { - $userActions = new UserManagement($logger); - - Util::connectHook('OC_User', 'post_createUser', $userActions, 'create'); - Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete'); - Util::connectHook('OC_User', 'changeUser', $userActions, 'change'); - - assert($userSession instanceof UserSession); - $userSession->listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']); - $userSession->listen('\OC\User', 'assignedUserId', [$userActions, 'assign']); - $userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']); - } - - private function groupHooks(IAuditLogger $logger, - IGroupManager $groupManager): void { - $groupActions = new GroupManagement($logger); - - assert($groupManager instanceof GroupManager); - $groupManager->listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']); - $groupManager->listen('\OC\Group', 'postAddUser', [$groupActions, 'addUser']); - $groupManager->listen('\OC\Group', 'postDelete', [$groupActions, 'deleteGroup']); - $groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']); } - private function sharingHooks(IAuditLogger $logger): void { + private function sharingLegacyHooks(IAuditLogger $logger): void { $shareActions = new Sharing($logger); - Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared'); - Util::connectHook(Share::class, 'post_unshare', $shareActions, 'unshare'); - Util::connectHook(Share::class, 'post_unshareFromSelf', $shareActions, 'unshare'); Util::connectHook(Share::class, 'post_update_permissions', $shareActions, 'updatePermissions'); Util::connectHook(Share::class, 'post_update_password', $shareActions, 'updatePassword'); Util::connectHook(Share::class, 'post_set_expiration_date', $shareActions, 'updateExpirationDate'); Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed'); } - private function authHooks(IAuditLogger $logger): void { - $authActions = new Auth($logger); - - Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt'); - Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful'); - Util::connectHook('OC_User', 'logout', $authActions, 'logout'); - } - - private function appHooks(IAuditLogger $logger, - EventDispatcherInterface $eventDispatcher): void { - $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function (ManagerEvent $event) use ($logger) { - $appActions = new AppManagement($logger); - $appActions->enableApp($event->getAppID()); - }); - $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function (ManagerEvent $event) use ($logger) { - $appActions = new AppManagement($logger); - $appActions->enableAppForGroups($event->getAppID(), $event->getGroups()); - }); - $eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function (ManagerEvent $event) use ($logger) { - $appActions = new AppManagement($logger); - $appActions->disableApp($event->getAppID()); - }); - } - - private function consoleHooks(IAuditLogger $logger, - EventDispatcherInterface $eventDispatcher): void { - $eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function (ConsoleEvent $event) use ($logger) { - $appActions = new Console($logger); - $appActions->runCommand($event->getArguments()); - }); - } - - private function fileHooks(IAuditLogger $logger, - EventDispatcherInterface $eventDispatcher): void { + private function fileHooks(IAuditLogger $logger): void { $fileActions = new Files($logger); - $eventDispatcher->addListener( - IPreview::EVENT, - function (GenericEvent $event) use ($fileActions) { - /** @var File $file */ - $file = $event->getSubject(); - $fileActions->preview([ - 'path' => mb_substr($file->getInternalPath(), 5), - 'width' => $event->getArguments()['width'], - 'height' => $event->getArguments()['height'], - 'crop' => $event->getArguments()['crop'], - 'mode' => $event->getArguments()['mode'] - ]); - } - ); Util::connectHook( Filesystem::CLASSNAME, @@ -265,16 +227,4 @@ private function trashbinHooks(IAuditLogger $logger): void { Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete'); Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore'); } - - private function securityHooks(IAuditLogger $logger, - EventDispatcherInterface $eventDispatcher): void { - $eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function (GenericEvent $event) use ($logger) { - $security = new Security($logger); - $security->twofactorSuccess($event->getSubject(), $event->getArguments()); - }); - $eventDispatcher->addListener(IProvider::EVENT_FAILED, function (GenericEvent $event) use ($logger) { - $security = new Security($logger); - $security->twofactorFailed($event->getSubject(), $event->getArguments()); - }); - } } diff --git a/apps/admin_audit/lib/AuditLogger.php b/apps/admin_audit/lib/AuditLogger.php index 0a7a330a7434e..c985d770f625e 100644 --- a/apps/admin_audit/lib/AuditLogger.php +++ b/apps/admin_audit/lib/AuditLogger.php @@ -1,4 +1,7 @@ * @@ -31,9 +34,7 @@ * Logger that logs in the audit log file instead of the normal log file */ class AuditLogger implements IAuditLogger { - - /** @var LoggerInterface */ - private $parentLogger; + private LoggerInterface $parentLogger; public function __construct(ILogFactory $logFactory, IConfig $config) { $auditType = $config->getSystemValueString('log_type_audit', 'file'); @@ -50,39 +51,39 @@ public function __construct(ILogFactory $logFactory, IConfig $config) { $this->parentLogger = $logFactory->getCustomPsrLogger($logFile, $auditType, $auditTag); } - public function emergency($message, array $context = array()) { + public function emergency($message, array $context = array()): void { $this->parentLogger->emergency($message, $context); } - public function alert($message, array $context = array()) { + public function alert($message, array $context = array()): void { $this->parentLogger->alert($message, $context); } - public function critical($message, array $context = array()) { + public function critical($message, array $context = array()): void { $this->parentLogger->critical($message, $context); } - public function error($message, array $context = array()) { + public function error($message, array $context = array()): void { $this->parentLogger->error($message, $context); } - public function warning($message, array $context = array()) { + public function warning($message, array $context = array()): void { $this->parentLogger->warning($message, $context); } - public function notice($message, array $context = array()) { + public function notice($message, array $context = array()): void { $this->parentLogger->notice($message, $context); } - public function info($message, array $context = array()) { + public function info($message, array $context = array()): void { $this->parentLogger->info($message, $context); } - public function debug($message, array $context = array()) { + public function debug($message, array $context = array()): void { $this->parentLogger->debug($message, $context); } - public function log($level, $message, array $context = array()) { + public function log($level, $message, array $context = array()): void { $this->parentLogger->log($level, $message, $context); } } diff --git a/apps/admin_audit/lib/Listener/AppManagementEventListener.php b/apps/admin_audit/lib/Listener/AppManagementEventListener.php new file mode 100644 index 0000000000000..43f3359b8eaa0 --- /dev/null +++ b/apps/admin_audit/lib/Listener/AppManagementEventListener.php @@ -0,0 +1,76 @@ + + * + * @author Thomas Citharel + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\AdminAudit\Listener; + +use OCA\AdminAudit\Actions\Action; +use OCP\App\Events\AppDisableEvent; +use OCP\App\Events\AppEnableEvent; +use OCP\App\Events\AppUpdateEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; + +/** + * @template-implements UserManagementEventListener + */ +class AppManagementEventListener extends Action implements IEventListener { + public function handle(Event $event): void { + if ($event instanceof AppEnableEvent) { + $this->appEnable($event); + } elseif ($event instanceof AppDisableEvent) { + $this->appDisable($event); + } elseif ($event instanceof AppUpdateEvent) { + $this->appUpdate($event); + } + } + + private function appEnable(AppEnableEvent $event): void { + if (empty($event->getGroupIds())) { + $this->log('App "%s" enabled', + ['app' => $event->getAppId()], + ['app'] + ); + } else { + $this->log('App "%1$s" enabled for groups: %2$s', + ['app' => $event->getAppId(), 'groups' => implode(', ', $event->getGroupIds())], + ['app', 'groups'] + ); + } + } + + private function appDisable(AppDisableEvent $event): void { + $this->log('App "%s" disabled', + ['app' => $event->getAppId()], + ['app'] + ); + } + + private function appUpdate(AppUpdateEvent $event): void { + $this->log('App "%s" updated', + ['app' => $event->getAppId()], + ['app'] + ); + } +} diff --git a/apps/admin_audit/lib/Listener/AuthEventListener.php b/apps/admin_audit/lib/Listener/AuthEventListener.php new file mode 100644 index 0000000000000..a9a60313f6c67 --- /dev/null +++ b/apps/admin_audit/lib/Listener/AuthEventListener.php @@ -0,0 +1,83 @@ + + * + * @author Thomas Citharel + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\AdminAudit\Listener; + +use OCA\AdminAudit\Actions\Action; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\User\Events\BeforeUserLoggedInEvent; +use OCP\User\Events\BeforeUserLoggedOutEvent; +use OCP\User\Events\UserLoggedInEvent; +use OCP\User\Events\UserLoggedInWithCookieEvent; + +/** + * @template-implements UserManagementEventListener + */ +class AuthEventListener extends Action implements IEventListener { + public function handle(Event $event): void { + if ($event instanceof BeforeUserLoggedInEvent) { + $this->beforeUserLoggedIn($event); + } elseif ($event instanceof UserLoggedInWithCookieEvent || $event instanceof UserLoggedInEvent) { + $this->userLoggedIn($event); + } elseif ($event instanceof BeforeUserLoggedOutEvent) { + $this->beforeUserLogout($event); + } + } + + private function beforeUserLoggedIn(BeforeUserLoggedInEvent $event): void { + $this->log( + 'Login attempt: "%s"', + [ + 'uid' => $event->getUsername() + ], + [ + 'uid', + ], + true + ); + } + + private function userLoggedIn(UserLoggedInWithCookieEvent|UserLoggedInEvent $event): void { + $this->log( + 'Login successful: "%s"', + [ + 'uid' => $event->getUser()->getUID() + ], + [ + 'uid', + ], + true + ); + } + + private function beforeUserLogout(BeforeUserLoggedOutEvent $event): void { + $this->log( + 'Logout occurred', + [], + [] + ); + } +} diff --git a/apps/admin_audit/lib/Actions/Console.php b/apps/admin_audit/lib/Listener/ConsoleEventListener.php similarity index 61% rename from apps/admin_audit/lib/Actions/Console.php rename to apps/admin_audit/lib/Listener/ConsoleEventListener.php index a69d1f5ff82f3..2470fcb316645 100644 --- a/apps/admin_audit/lib/Actions/Console.php +++ b/apps/admin_audit/lib/Listener/ConsoleEventListener.php @@ -3,11 +3,9 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2017 Joas Schilling + * @copyright Copyright (c) 2023 Thomas Citharel * - * @author Daniel Kesselberg - * @author Joas Schilling - * @author Roeland Jago Douma + * @author Thomas Citharel * * @license GNU AGPL version 3 or any later version * @@ -25,13 +23,25 @@ * along with this program. If not, see . * */ -namespace OCA\AdminAudit\Actions; +namespace OCA\AdminAudit\Listener; -class Console extends Action { - /** - * @param $arguments - */ - public function runCommand(array $arguments): void { +use OCA\AdminAudit\Actions\Action; +use OCP\Console\ConsoleEventV2; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; + +/** + * @template-implements UserManagementEventListener + */ +class ConsoleEventListener extends Action implements IEventListener { + public function handle(Event $event): void { + if ($event instanceof ConsoleEventV2) { + $this->runCommand($event); + } + } + + private function runCommand(ConsoleEventV2 $event): void { + $arguments = $event->getArguments(); if (!isset($arguments[1]) || $arguments[1] === '_completion') { // Don't log autocompletion return; diff --git a/apps/admin_audit/lib/Listener/CriticalActionPerformedEventListener.php b/apps/admin_audit/lib/Listener/CriticalActionPerformedEventListener.php index 3f91c0c2731ad..8cfbc240755a0 100644 --- a/apps/admin_audit/lib/Listener/CriticalActionPerformedEventListener.php +++ b/apps/admin_audit/lib/Listener/CriticalActionPerformedEventListener.php @@ -30,6 +30,9 @@ use OCP\EventDispatcher\IEventListener; use OCP\Log\Audit\CriticalActionPerformedEvent; +/** + * @template-implements UserManagementEventListener + */ class CriticalActionPerformedEventListener extends Action implements IEventListener { public function handle(Event $event): void { if (!($event instanceof CriticalActionPerformedEvent)) { diff --git a/apps/admin_audit/lib/Listener/FileEventListener.php b/apps/admin_audit/lib/Listener/FileEventListener.php new file mode 100644 index 0000000000000..9e9305f78965e --- /dev/null +++ b/apps/admin_audit/lib/Listener/FileEventListener.php @@ -0,0 +1,65 @@ + + * + * @author Thomas Citharel + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\AdminAudit\Listener; + +use OCA\AdminAudit\Actions\Action; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Preview\BeforePreviewFetchedEvent; + + +/** + * @template-implements UserManagementEventListener + */ +class FileEventListener extends Action implements IEventListener { + public function handle(Event $event): void { + if ($event instanceof BeforePreviewFetchedEvent) { + $this->beforePreviewFetched($event); + } + } + + private function beforePreviewFetched(BeforePreviewFetchedEvent $event): void { + $file = $event->getNode(); + + $this->log( + 'Preview accessed: "%s" (width: "%s", height: "%s" crop: "%s", mode: "%s")', + [ + 'path' => mb_substr($file->getInternalPath(), 5), + 'width' => $event->getWidth(), + 'height' => $event->getHeight(), + 'crop' => $event->isCrop(), + 'mode' => $event->getMode(), + ], + [ + 'path', + 'width', + 'height', + 'crop', + 'mode' + ] + ); + } +} diff --git a/apps/admin_audit/lib/Listener/GroupManagementEventListener.php b/apps/admin_audit/lib/Listener/GroupManagementEventListener.php new file mode 100644 index 0000000000000..92a0ddb0837fe --- /dev/null +++ b/apps/admin_audit/lib/Listener/GroupManagementEventListener.php @@ -0,0 +1,97 @@ + + * + * @author Thomas Citharel + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\AdminAudit\Listener; + +use OCA\AdminAudit\Actions\Action; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Group\Events\GroupCreatedEvent; +use OCP\Group\Events\GroupDeletedEvent; +use OCP\Group\Events\UserAddedEvent; +use OCP\Group\Events\UserRemovedEvent; + +/** + * @template-implements UserManagementEventListener + */ +class GroupManagementEventListener extends Action implements IEventListener { + public function handle(Event $event): void { + if ($event instanceof UserAddedEvent) { + $this->userAdded($event); + } elseif ($event instanceof UserRemovedEvent) { + $this->userRemoved($event); + } elseif ($event instanceof GroupCreatedEvent) { + $this->groupCreated($event); + } elseif ($event instanceof GroupDeletedEvent) { + $this->groupDeleted($event); + } + } + + private function userAdded(UserAddedEvent $event): void { + $this->log('User "%s" added to group "%s"', + [ + 'group' => $event->getGroup()->getGID(), + 'user' => $event->getUser()->getUID() + ], + [ + 'user', 'group' + ] + ); + } + + private function userRemoved(UserRemovedEvent $event): void { + $this->log('User "%s" removed from group "%s"', + [ + 'group' => $event->getGroup()->getGID(), + 'user' => $event->getUser()->getUID() + ], + [ + 'user', 'group' + ] + ); + } + + private function groupCreated(GroupCreatedEvent $event): void { + $this->log('Group created: "%s"', + [ + 'group' => $event->getGroup()->getGID() + ], + [ + 'group' + ] + ); + } + + private function groupDeleted(GroupDeletedEvent $event): void { + $this->log('Group deleted: "%s"', + [ + 'group' => $event->getGroup()->getGID() + ], + [ + 'group' + ] + ); + } +} diff --git a/apps/admin_audit/lib/Listener/SecurityEventListener.php b/apps/admin_audit/lib/Listener/SecurityEventListener.php new file mode 100644 index 0000000000000..d7676cf8c4dea --- /dev/null +++ b/apps/admin_audit/lib/Listener/SecurityEventListener.php @@ -0,0 +1,77 @@ + + * + * @author Thomas Citharel + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\AdminAudit\Listener; + +use OCA\AdminAudit\Actions\Action; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserDisabled; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserEnabled; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; + +/** + * @template-implements UserManagementEventListener + */ +class SecurityEventListener extends Action implements IEventListener { + public function handle(Event $event): void { + if ($event instanceof TwoFactorProviderForUserEnabled) { + $this->twoFactorProviderForUserEnabled($event); + } elseif ($event instanceof TwoFactorProviderForUserDisabled) { + $this->twoFactorProviderForUserDisabled($event); + } + } + + private function twoFactorProviderForUserEnabled(TwoFactorProviderForUserEnabled $event): void { + $this->log( + 'Successful two factor attempt by user %s (%s) with provider %s', + [ + 'uid' => $event->getUser()->getUID(), + 'displayName' => $event->getUser()->getDisplayName(), + 'provider' => $event->getProvider()->getDisplayName() + ], + [ + 'displayName', + 'uid', + 'provider', + ] + ); + } + + private function twoFactorProviderForUserDisabled(TwoFactorProviderForUserDisabled $event): void { + $this->log( + 'Failed two factor attempt by user %s (%s) with provider %s', + [ + 'uid' => $event->getUser()->getUID(), + 'displayName' => $event->getUser()->getDisplayName(), + 'provider' => $event->getProvider()->getDisplayName() + ], + [ + 'displayName', + 'uid', + 'provider', + ] + ); + } +} diff --git a/apps/admin_audit/lib/Listener/SharingEventListener.php b/apps/admin_audit/lib/Listener/SharingEventListener.php new file mode 100644 index 0000000000000..5fcaba64c2f52 --- /dev/null +++ b/apps/admin_audit/lib/Listener/SharingEventListener.php @@ -0,0 +1,307 @@ + + * + * @author Thomas Citharel + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\AdminAudit\Listener; + +use OCA\AdminAudit\Actions\Action; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Share\Events\ShareCreatedEvent; +use OCP\Share\Events\ShareDeletedEvent; +use OCP\Share\IShare; + +/** + * @template-implements UserManagementEventListener + */ +class SharingEventListener extends Action implements IEventListener { + public function handle(Event $event): void { + if ($event instanceof ShareCreatedEvent) { + $this->shareCreated($event); + } elseif ($event instanceof ShareDeletedEvent) { + $this->shareDeleted($event); + } + } + + private function shareCreated(ShareCreatedEvent $event): void { + $share = $event->getShare(); + + $params = [ + 'itemType' => $share->getNodeType(), + 'path' => $share->getNode()->getPath(), + 'itemSource' => $share->getNodeId(), + 'shareWith' => $share->getSharedWith(), + 'permissions' => $share->getPermissions(), + 'id' => $share->getId() + ]; + + match ($share->getShareType()) { + IShare::TYPE_LINK => $this->log( + 'The %s "%s" with ID "%s" has been shared via link with permissions "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'path', + 'itemSource', + 'permissions', + 'id', + ] + ), + IShare::TYPE_USER => $this->log( + 'The %s "%s" with ID "%s" has been shared to the user "%s" with permissions "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'path', + 'itemSource', + 'shareWith', + 'permissions', + 'id', + ] + ), + IShare::TYPE_GROUP => $this->log( + 'The %s "%s" with ID "%s" has been shared to the group "%s" with permissions "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'path', + 'itemSource', + 'shareWith', + 'permissions', + 'id', + ] + ), + IShare::TYPE_ROOM => $this->log( + 'The %s "%s" with ID "%s" has been shared to the room "%s" with permissions "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'path', + 'itemSource', + 'shareWith', + 'permissions', + 'id', + ] + ), + IShare::TYPE_EMAIL => $this->log( + 'The %s "%s" with ID "%s" has been shared to the email recipient "%s" with permissions "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'path', + 'itemSource', + 'shareWith', + 'permissions', + 'id', + ] + ), + IShare::TYPE_CIRCLE => $this->log( + 'The %s "%s" with ID "%s" has been shared to the circle "%s" with permissions "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'path', + 'itemSource', + 'shareWith', + 'permissions', + 'id', + ] + ), + IShare::TYPE_REMOTE => $this->log( + 'The %s "%s" with ID "%s" has been shared to the remote user "%s" with permissions "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'path', + 'itemSource', + 'shareWith', + 'permissions', + 'id', + ] + ), + IShare::TYPE_REMOTE_GROUP => $this->log( + 'The %s "%s" with ID "%s" has been shared to the remote group "%s" with permissions "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'path', + 'itemSource', + 'shareWith', + 'permissions', + 'id', + ] + ), + IShare::TYPE_DECK => $this->log( + 'The %s "%s" with ID "%s" has been shared to the deck card "%s" with permissions "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'path', + 'itemSource', + 'shareWith', + 'permissions', + 'id', + ] + ), + IShare::TYPE_SCIENCEMESH => $this->log( + 'The %s "%s" with ID "%s" has been shared to the sciencemesh user "%s" with permissions "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'path', + 'itemSource', + 'shareWith', + 'permissions', + 'id', + ] + ), + default => null + }; + } + + private function shareDeleted(ShareDeletedEvent $event): void { + $share = $event->getShare(); + + $params = [ + 'itemType' => $share->getNodeType(), + 'fileTarget' => $share->getTarget(), + 'itemSource' => $share->getNodeId(), + 'shareWith' => $share->getSharedWith(), + 'id' => $share->getId() + ]; + + match ($share->getShareType()) { + IShare::TYPE_LINK => $this->log( + 'The %s "%s" with ID "%s" has been unshared (Share ID: %s)', + $params, + [ + 'itemType', + 'fileTarget', + 'itemSource', + 'id', + ] + ), + IShare::TYPE_USER => $this->log( + 'The %s "%s" with ID "%s" has been unshared from the user "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'fileTarget', + 'itemSource', + 'shareWith', + 'id', + ] + ), + IShare::TYPE_GROUP => $this->log( + 'The %s "%s" with ID "%s" has been unshared from the group "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'fileTarget', + 'itemSource', + 'shareWith', + 'id', + ] + ), + IShare::TYPE_ROOM => $this->log( + 'The %s "%s" with ID "%s" has been unshared from the room "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'fileTarget', + 'itemSource', + 'shareWith', + 'id', + ] + ), + IShare::TYPE_EMAIL => $this->log( + 'The %s "%s" with ID "%s" has been unshared from the email recipient "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'fileTarget', + 'itemSource', + 'shareWith', + 'id', + ] + ), + IShare::TYPE_CIRCLE => $this->log( + 'The %s "%s" with ID "%s" has been unshared from the circle "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'fileTarget', + 'itemSource', + 'shareWith', + 'id', + ] + ), + IShare::TYPE_REMOTE => $this->log( + 'The %s "%s" with ID "%s" has been unshared from the remote user "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'fileTarget', + 'itemSource', + 'shareWith', + 'id', + ] + ), + IShare::TYPE_REMOTE_GROUP => $this->log( + 'The %s "%s" with ID "%s" has been unshared from the remote group "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'fileTarget', + 'itemSource', + 'shareWith', + 'id', + ] + ), + IShare::TYPE_DECK => $this->log( + 'The %s "%s" with ID "%s" has been unshared from the deck card "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'fileTarget', + 'itemSource', + 'shareWith', + 'id', + ] + ), + IShare::TYPE_SCIENCEMESH => $this->log( + 'The %s "%s" with ID "%s" has been unshared from the sciencemesh user "%s" (Share ID: %s)', + $params, + [ + 'itemType', + 'fileTarget', + 'itemSource', + 'shareWith', + 'id', + ] + ), + default => null + }; + } +} diff --git a/apps/admin_audit/lib/Listener/UserManagementEventListener.php b/apps/admin_audit/lib/Listener/UserManagementEventListener.php new file mode 100644 index 0000000000000..40baf99cc5569 --- /dev/null +++ b/apps/admin_audit/lib/Listener/UserManagementEventListener.php @@ -0,0 +1,142 @@ + + * + * @author Thomas Citharel + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\AdminAudit\Listener; + +use OCA\AdminAudit\Actions\Action; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\User\Events\PasswordUpdatedEvent; +use OCP\User\Events\UserChangedEvent; +use OCP\User\Events\UserCreatedEvent; +use OCP\User\Events\UserDeletedEvent; +use OCP\User\Events\UserIdAssignedEvent; +use OCP\User\Events\UserIdUnassignedEvent; + +/** + * @template-implements UserManagementEventListener + */ +class UserManagementEventListener extends Action implements IEventListener { + public function handle(Event $event): void { + if ($event instanceof UserCreatedEvent) { + $this->userCreated($event); + } elseif ($event instanceof UserDeletedEvent) { + $this->userDeleted($event); + } elseif ($event instanceof UserChangedEvent) { + $this->userChanged($event); + } elseif ($event instanceof PasswordUpdatedEvent) { + $this->passwordUpdated($event); + } elseif ($event instanceof UserIdAssignedEvent) { + $this->userIdAssigned($event); + } elseif ($event instanceof UserIdUnassignedEvent) { + $this->userIdUnassigned($event); + } + } + + private function userCreated(UserCreatedEvent $event): void { + $this->log( + 'User created: "%s"', + [ + 'uid' => $event->getUid() + ], + [ + 'uid', + ] + ); + } + + private function userDeleted(UserDeletedEvent $event): void { + $this->log( + 'User deleted: "%s"', + [ + 'uid' => $event->getUser()->getUID() + ], + [ + 'uid', + ] + ); + } + + private function userChanged(UserChangedEvent $event): void { + switch ($event->getFeature()) { + case 'enabled': + $this->log( + $event->getValue() === true + ? 'User enabled: "%s"' + : 'User disabled: "%s"', + ['user' => $event->getUser()->getUID()], + [ + 'user', + ] + ); + break; + case 'eMailAddress': + $this->log( + 'Email address changed for user %s', + ['user' => $event->getUser()->getUID()], + [ + 'user', + ] + ); + break; + } + } + + private function passwordUpdated(PasswordUpdatedEvent $event): void { + if ($event->getUser()->getBackendClassName() === 'Database') { + $this->log( + 'Password of user "%s" has been changed', + [ + 'user' => $event->getUser()->getUID(), + ], + [ + 'user', + ] + ); + } + } + + /** + * Log assignments of users (typically user backends) + */ + private function userIdAssigned(UserIdAssignedEvent $event): void { + $this->log( + 'UserID assigned: "%s"', + [ 'uid' => $event->getUserId() ], + [ 'uid' ] + ); + } + + /** + * Log unassignments of users (typically user backends, no data removed) + */ + private function userIdUnassigned(UserIdUnassignedEvent $event): void { + $this->log( + 'UserID unassigned: "%s"', + [ 'uid' => $event->getUserId() ], + [ 'uid' ] + ); + } +} diff --git a/apps/admin_audit/tests/Actions/SecurityTest.php b/apps/admin_audit/tests/Listener/SecurityEventListenerTest.php similarity index 66% rename from apps/admin_audit/tests/Actions/SecurityTest.php rename to apps/admin_audit/tests/Listener/SecurityEventListenerTest.php index d45cbb75a6413..f9f236c4998f9 100644 --- a/apps/admin_audit/tests/Actions/SecurityTest.php +++ b/apps/admin_audit/tests/Listener/SecurityEventListenerTest.php @@ -7,6 +7,7 @@ * * @author Morris Jobke * @author Roeland Jago Douma + * @author Thomas Citharel * * @license GNU AGPL version 3 or any later version * @@ -26,30 +27,38 @@ */ namespace OCA\AdminAudit\Tests\Actions; -use OCA\AdminAudit\Actions\Security; +use OCA\AdminAudit\Listener\SecurityEventListener; +use OCP\Authentication\TwoFactorAuth\IProvider; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserDisabled; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserEnabled; use OCP\IUser; use OCA\AdminAudit\AuditLogger; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; -class SecurityTest extends TestCase { - /** @var AuditLogger|\PHPUnit\Framework\MockObject\MockObject */ +class SecurityEventListenerTest extends TestCase { + /** @var AuditLogger|MockObject */ private $logger; - /** @var Security */ - private $security; + private SecurityEventListener $security; - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IUser|MockObject */ private $user; + /** @var IProvider|(IProvider&MockObject)|MockObject */ + private $provider; + protected function setUp(): void { parent::setUp(); $this->logger = $this->createMock(AuditLogger::class); - $this->security = new Security($this->logger); + $this->security = new SecurityEventListener($this->logger); $this->user = $this->createMock(IUser::class); $this->user->method('getUID')->willReturn('myuid'); $this->user->method('getDisplayName')->willReturn('mydisplayname'); + $this->provider = $this->createMock(IProvider::class); + $this->provider->method('getDisplayName')->willReturn('myprovider'); } public function testTwofactorFailed() { @@ -60,7 +69,7 @@ public function testTwofactorFailed() { ['app' => 'admin_audit'] ); - $this->security->twofactorFailed($this->user, ['provider' => 'myprovider']); + $this->security->handle(new TwoFactorProviderForUserEnabled($this->user, $this->provider)); } public function testTwofactorSuccess() { @@ -71,6 +80,6 @@ public function testTwofactorSuccess() { ['app' => 'admin_audit'] ); - $this->security->twofactorSuccess($this->user, ['provider' => 'myprovider']); + $this->security->handle(new TwoFactorProviderForUserDisabled($this->user, $this->provider)); } } diff --git a/apps/user_ldap/ajax/clearMappings.php b/apps/user_ldap/ajax/clearMappings.php index f8469cc85b11c..fec36890b9932 100644 --- a/apps/user_ldap/ajax/clearMappings.php +++ b/apps/user_ldap/ajax/clearMappings.php @@ -25,6 +25,10 @@ */ use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Mapping\GroupMapping; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Server; +use OCP\User\Events\BeforeUserIdUnassignedEvent; +use OCP\User\Events\UserIdUnassignedEvent; // Check user and app status \OC_JSON::checkAdminUser(); @@ -35,12 +39,16 @@ $mapping = null; try { if ($subject === 'user') { - $mapping = \OCP\Server::get(UserMapping::class); + $mapping = Server::get(UserMapping::class); + /** @var IEventDispatcher $dispatcher */ + $dispatcher = Server::get(IEventDispatcher::class); $result = $mapping->clearCb( - function ($uid) { + function (string $uid) use ($dispatcher): void { + $dispatcher->dispatchTyped(new BeforeUserIdUnassignedEvent($uid)); \OC::$server->getUserManager()->emit('\OC\User', 'preUnassignedUserId', [$uid]); }, - function ($uid) { + function (string $uid) use ($dispatcher): void { + $dispatcher->dispatchTyped(new UserIdUnassignedEvent($uid)); \OC::$server->getUserManager()->emit('\OC\User', 'postUnassignedUserId', [$uid]); } ); diff --git a/apps/user_ldap/lib/Access.php b/apps/user_ldap/lib/Access.php index 9a97a28c376cc..ee6c492f7af05 100644 --- a/apps/user_ldap/lib/Access.php +++ b/apps/user_ldap/lib/Access.php @@ -54,6 +54,8 @@ use OCA\User_LDAP\Mapping\AbstractMapping; use OCA\User_LDAP\User\Manager; use OCA\User_LDAP\User\OfflineUser; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\User\Events\UserIdAssignedEvent; use OCP\HintException; use OCP\IConfig; use OCP\IUserManager; @@ -69,32 +71,18 @@ class Access extends LDAPUtility { public const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid']; - /** @var \OCA\User_LDAP\Connection */ - public $connection; - /** @var Manager */ - public $userManager; - /** - * never ever check this var directly, always use getPagedSearchResultState - * @var ?bool - */ - protected $pagedSearchedSuccessful; - - /** @var ?AbstractMapping */ - protected $userMapper; - - /** @var ?AbstractMapping */ - protected $groupMapper; - - /** - * @var \OCA\User_LDAP\Helper - */ - private $helper; - /** @var IConfig */ - private $config; - /** @var IUserManager */ - private $ncUserManager; - /** @var LoggerInterface */ - private $logger; + public Connection $connection; + public Manager $userManager; + /** never ever check this var directly, always use getPagedSearchResultState */ + protected ?bool $pagedSearchedSuccessful; + protected ?AbstractMapping $userMapper; + protected ?AbstractMapping $groupMapper; + + private Helper $helper; + private IConfig $config; + private IUserManager $ncUserManager; + private LoggerInterface $logger; + private IEventDispatcher $dispatcher; private string $lastCookie = ''; public function __construct( @@ -104,7 +92,8 @@ public function __construct( Helper $helper, IConfig $config, IUserManager $ncUserManager, - LoggerInterface $logger + LoggerInterface $logger, + IEventDispatcher $dispatcher ) { parent::__construct($ldap); $this->connection = $connection; @@ -114,6 +103,7 @@ public function __construct( $this->config = $config; $this->ncUserManager = $ncUserManager; $this->logger = $logger; + $this->dispatcher = $dispatcher; } /** @@ -616,6 +606,9 @@ public function mapAndAnnounceIfApplicable( bool $isUser ): bool { if ($mapper->map($fdn, $name, $uuid)) { + if ($isUser) { + $this->dispatcher->dispatchTyped(new UserIdAssignedEvent($name)); + } if ($this->ncUserManager instanceof PublicEmitter && $isUser) { $this->cacheUserExists($name); $this->ncUserManager->emit('\OC\User', 'assignedUserId', [$name]); diff --git a/apps/user_ldap/lib/AccessFactory.php b/apps/user_ldap/lib/AccessFactory.php index f0820f1444fa2..8b0964a5cc023 100644 --- a/apps/user_ldap/lib/AccessFactory.php +++ b/apps/user_ldap/lib/AccessFactory.php @@ -24,6 +24,7 @@ namespace OCA\User_LDAP; use OCA\User_LDAP\User\Manager; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IUserManager; use Psr\Log\LoggerInterface; @@ -35,6 +36,7 @@ class AccessFactory { private IConfig $config; private IUserManager $ncUserManager; private LoggerInterface $logger; + private IEventDispatcher $dispatcher; public function __construct( ILDAPWrapper $ldap, @@ -42,13 +44,15 @@ public function __construct( Helper $helper, IConfig $config, IUserManager $ncUserManager, - LoggerInterface $logger) { + LoggerInterface $logger, + IEventDispatcher $dispatcher) { $this->ldap = $ldap; $this->userManager = $userManager; $this->helper = $helper; $this->config = $config; $this->ncUserManager = $ncUserManager; $this->logger = $logger; + $this->dispatcher = $dispatcher; } public function get(Connection $connection): Access { @@ -59,7 +63,8 @@ public function get(Connection $connection): Access { $this->helper, $this->config, $this->ncUserManager, - $this->logger + $this->logger, + $this->dispatcher ); } } diff --git a/apps/user_ldap/lib/Jobs/Sync.php b/apps/user_ldap/lib/Jobs/Sync.php index 1ba24af5399b6..bc493bae9133b 100644 --- a/apps/user_ldap/lib/Jobs/Sync.php +++ b/apps/user_ldap/lib/Jobs/Sync.php @@ -5,6 +5,7 @@ * @author Arthur Schiwon * @author Christoph Wurst * @author Joas Schilling + * @author Carl Schwan * * @license GNU AGPL version 3 or any later version * @@ -34,6 +35,7 @@ use OCA\User_LDAP\User\Manager; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IAvatarManager; use OCP\IConfig; use OCP\IDBConnection; @@ -44,47 +46,72 @@ class Sync extends TimedJob { public const MAX_INTERVAL = 12 * 60 * 60; // 12h public const MIN_INTERVAL = 30 * 60; // 30min - /** @var Helper */ - protected $ldapHelper; - /** @var LDAP */ - protected $ldap; - /** @var Manager */ - protected $userManager; - /** @var UserMapping */ - protected $mapper; - /** @var IConfig */ - protected $config; - /** @var IAvatarManager */ - protected $avatarManager; - /** @var IDBConnection */ - protected $dbc; - /** @var IUserManager */ - protected $ncUserManager; - /** @var LoggerInterface */ - protected $logger; - /** @var IManager */ - protected $notificationManager; - /** @var ConnectionFactory */ - protected $connectionFactory; - /** @var AccessFactory */ - protected $accessFactory; - - public function __construct(Manager $userManager, ITimeFactory $time) { - parent::__construct($time); + protected Helper $ldapHelper; + protected LDAP $ldap; + protected Manager $userManager; + protected UserMapping $mapper; + protected IConfig $config; + protected IAvatarManager $avatarManager; + protected IDBConnection $dbc; + protected IUserManager $ncUserManager; + protected LoggerInterface $logger; + protected IManager $notificationManager; + protected ConnectionFactory $connectionFactory; + protected AccessFactory $accessFactory; + protected IEventDispatcher $dispatcher; + + public function __construct( + Manager $userManager, + IEventDispatcher $dispatcher, + IConfig $config, + ITimeFactory $timeFactory, + IDBConnection $dbConnection, + IAvatarManager $avatarManager, + IUserManager $ncUserManager, + LoggerInterface $logger, + IManager $notificationManager, + UserMapping $userMapping, + Helper $ldapHelper, + ConnectionFactory $connectionFactory + ) { + parent::__construct($timeFactory); $this->userManager = $userManager; + $this->config = $config; + $this->dispatcher = $dispatcher; + $this->setInterval( - (int)\OC::$server->getConfig()->getAppValue( + (int) $this->config->getAppValue( 'user_ldap', 'background_sync_interval', (string)self::MIN_INTERVAL ) ); + $this->ldapHelper = $ldapHelper; + $this->ldap = new LDAP($this->config->getSystemValueString('ldap_log_file')); + $this->avatarManager = $avatarManager; + $this->dbc = $dbConnection; + $this->ncUserManager = $ncUserManager; + $this->logger = $logger; + $this->userManager = $userManager; + $this->notificationManager = $notificationManager; + $this->mapper = $userMapping; + $this->connectionFactory = $connectionFactory; + + $this->accessFactory = new AccessFactory( + $this->ldap, + $this->userManager, + $this->ldapHelper, + $this->config, + $this->ncUserManager, + $this->logger, + $this->dispatcher + ); } /** - * updates the interval + * Updates the interval * - * the idea is to adjust the interval depending on the amount of known users + * The idea is to adjust the interval depending on the amount of known users * and the attempt to update each user one day. At most it would run every * 30 minutes, and at least every 12 hours. */ @@ -104,7 +131,7 @@ public function updateInterval() { * returns the smallest configured paging size * @return int */ - protected function getMinPagingSize() { + protected function getMinPagingSize(): int { $configKeys = $this->config->getAppKeys('user_ldap'); $configKeys = array_filter($configKeys, function ($key) { return strpos($key, 'ldap_paging_size') !== false; @@ -121,8 +148,6 @@ protected function getMinPagingSize() { * @param array $argument */ public function run($argument) { - $this->setArgument($argument); - $isBackgroundJobModeAjax = $this->config ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; if ($isBackgroundJobModeAjax) { @@ -157,10 +182,10 @@ public function run($argument) { } /** - * @param array $cycleData + * @param array{offset: int, prefix: string} $cycleData * @return bool whether more results are expected from the same configuration */ - public function runCycle($cycleData) { + public function runCycle(array $cycleData): bool { $connection = $this->connectionFactory->get($cycleData['prefix']); $access = $this->accessFactory->get($connection); $access->setUserMapper($this->mapper); @@ -185,24 +210,24 @@ public function runCycle($cycleData) { } /** - * returns the info about the current cycle that should be run, if any, + * Returns the info about the current cycle that should be run, if any, * otherwise null * * @return array|null */ - public function getCycle() { + public function getCycle(): ?array { $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); if (count($prefixes) === 0) { return null; } $cycleData = [ - 'prefix' => $this->config->getAppValue('user_ldap', 'background_sync_prefix', null), + 'prefix' => $this->config->getAppValue('user_ldap', 'background_sync_prefix', 'none'), 'offset' => (int)$this->config->getAppValue('user_ldap', 'background_sync_offset', '0'), ]; if ( - $cycleData['prefix'] !== null + $cycleData['prefix'] !== 'none' && in_array($cycleData['prefix'], $prefixes) ) { return $cycleData; @@ -214,11 +239,11 @@ public function getCycle() { /** * Save the provided cycle information in the DB * - * @param array $cycleData + * @param array{prefix: ?string, offset: int} $cycleData */ - public function setCycle(array $cycleData) { + public function setCycle(array $cycleData): void { $this->config->setAppValue('user_ldap', 'background_sync_prefix', $cycleData['prefix']); - $this->config->setAppValue('user_ldap', 'background_sync_offset', $cycleData['offset']); + $this->config->setAppValue('user_ldap', 'background_sync_offset', (string) $cycleData['offset']); } /** @@ -226,9 +251,9 @@ public function setCycle(array $cycleData) { * null. It also always goes for the next LDAP configuration! * * @param array|null $cycleData the old cycle - * @return array|null + * @return ?array{prefix: string, offset: int} */ - public function determineNextCycle(array $cycleData = null) { + public function determineNextCycle(array $cycleData = null): ?array { $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); if (count($prefixes) === 0) { return null; @@ -248,13 +273,12 @@ public function determineNextCycle(array $cycleData = null) { } /** - * Checks whether the provided cycle should be run. Currently only the + * Checks whether the provided cycle should be run. Currently, only the * last configuration change goes into account (at least one hour). * - * @param $cycleData - * @return bool + * @param $cycleData{prefix: string} */ - public function qualifiesToRun($cycleData) { + public function qualifiesToRun(array $cycleData): bool { $lastChange = (int)$this->config->getAppValue('user_ldap', $cycleData['prefix'] . '_lastChange', '0'); if ((time() - $lastChange) > 60 * 30) { return true; @@ -263,23 +287,20 @@ public function qualifiesToRun($cycleData) { } /** - * increases the offset of the current cycle for the next run + * Increases the offset of the current cycle for the next run * - * @param $cycleData + * @param array{prefix: string, offset: int} $cycleData */ - protected function increaseOffset($cycleData) { + protected function increaseOffset(array $cycleData): void { $ldapConfig = new Configuration($cycleData['prefix']); - $cycleData['offset'] += (int)$ldapConfig->ldapPagingSize; + $cycleData['offset'] += $ldapConfig->ldapPagingSize; $this->setCycle($cycleData); } /** - * determines the next configuration prefix based on the last one (if any) - * - * @param string|null $lastPrefix - * @return string|null + * Determines the next configuration prefix based on the last one (if any) */ - protected function getNextPrefix($lastPrefix) { + protected function getNextPrefix(?string $lastPrefix): ?string { $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); $noOfPrefixes = count($prefixes); if ($noOfPrefixes === 0) { @@ -299,84 +320,10 @@ protected function getNextPrefix($lastPrefix) { } /** - * "fixes" DI + * Only used in tests */ - public function setArgument($argument) { - if (isset($argument['config'])) { - $this->config = $argument['config']; - } else { - $this->config = \OC::$server->getConfig(); - } - - if (isset($argument['helper'])) { - $this->ldapHelper = $argument['helper']; - } else { - $this->ldapHelper = new Helper($this->config, \OC::$server->getDatabaseConnection()); - } - - if (isset($argument['ldapWrapper'])) { - $this->ldap = $argument['ldapWrapper']; - } else { - $this->ldap = new LDAP($this->config->getSystemValueString('ldap_log_file')); - } - - if (isset($argument['avatarManager'])) { - $this->avatarManager = $argument['avatarManager']; - } else { - $this->avatarManager = \OC::$server->getAvatarManager(); - } - - if (isset($argument['dbc'])) { - $this->dbc = $argument['dbc']; - } else { - $this->dbc = \OC::$server->getDatabaseConnection(); - } - - if (isset($argument['ncUserManager'])) { - $this->ncUserManager = $argument['ncUserManager']; - } else { - $this->ncUserManager = \OC::$server->getUserManager(); - } - - if (isset($argument['logger'])) { - $this->logger = $argument['logger']; - } else { - $this->logger = \OC::$server->get(LoggerInterface::class); - } - - if (isset($argument['notificationManager'])) { - $this->notificationManager = $argument['notificationManager']; - } else { - $this->notificationManager = \OC::$server->getNotificationManager(); - } - - if (isset($argument['userManager'])) { - $this->userManager = $argument['userManager']; - } - - if (isset($argument['mapper'])) { - $this->mapper = $argument['mapper']; - } else { - $this->mapper = \OCP\Server::get(UserMapping::class); - } - - if (isset($argument['connectionFactory'])) { - $this->connectionFactory = $argument['connectionFactory']; - } else { - $this->connectionFactory = new ConnectionFactory($this->ldap); - } - - if (isset($argument['accessFactory'])) { - $this->accessFactory = $argument['accessFactory']; - } else { - $this->accessFactory = new AccessFactory( - $this->ldap, - $this->userManager, - $this->ldapHelper, - $this->config, - $this->ncUserManager, - $this->logger - ); - } + public function overwritePropertiesForTest(LDAP $ldapWrapper, AccessFactory $accessFactory): void { + $this->ldap = $ldapWrapper; + $this->accessFactory = $accessFactory; } } diff --git a/apps/user_ldap/tests/AccessTest.php b/apps/user_ldap/tests/AccessTest.php index ce05839c842b2..5b7aaeab3be3a 100644 --- a/apps/user_ldap/tests/AccessTest.php +++ b/apps/user_ldap/tests/AccessTest.php @@ -44,12 +44,14 @@ use OCA\User_LDAP\User\Manager; use OCA\User_LDAP\User\OfflineUser; use OCA\User_LDAP\User\User; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IAvatarManager; use OCP\IConfig; use OCP\Image; use OCP\IUserManager; use OCP\Notification\IManager as INotificationManager; use OCP\Share\IManager; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -61,28 +63,29 @@ * @package OCA\User_LDAP\Tests */ class AccessTest extends TestCase { - /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */ + /** @var UserMapping|MockObject */ protected $userMapper; - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IManager|MockObject */ protected $shareManager; - /** @var GroupMapping|\PHPUnit\Framework\MockObject\MockObject */ + /** @var GroupMapping|MockObject */ protected $groupMapper; - /** @var Connection|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Connection|MockObject */ private $connection; - /** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */ + /** @var LDAP|MockObject */ private $ldap; - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Manager|MockObject */ private $userManager; - /** @var Helper|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Helper|MockObject */ private $helper; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IConfig|MockObject */ private $config; - /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IUserManager|MockObject */ private $ncUserManager; /** @var LoggerInterface|MockObject */ private $logger; - /** @var Access */ - private $access; + /** @var IEventDispatcher|MockObject */ + private $dispatcher; + private Access $access; protected function setUp(): void { $this->connection = $this->createMock(Connection::class); @@ -95,6 +98,7 @@ protected function setUp(): void { $this->ncUserManager = $this->createMock(IUserManager::class); $this->shareManager = $this->createMock(IManager::class); $this->logger = $this->createMock(LoggerInterface::class); + $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->access = new Access( $this->connection, @@ -103,8 +107,10 @@ protected function setUp(): void { $this->helper, $this->config, $this->ncUserManager, - $this->logger + $this->logger, + $this->dispatcher ); + $this->dispatcher->expects($this->any())->method('dispatchTyped'); $this->access->setUserMapper($this->userMapper); $this->access->setGroupMapper($this->groupMapper); } @@ -241,9 +247,9 @@ public function dnInputDataProvider() { */ public function testStringResemblesDN($case) { [$lw, $con, $um, $helper] = $this->getConnectorAndLdapMock(); - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $config */ + /** @var IConfig|MockObject $config */ $config = $this->createMock(IConfig::class); - $access = new Access($con, $lw, $um, $helper, $config, $this->ncUserManager, $this->logger); + $access = new Access($con, $lw, $um, $helper, $config, $this->ncUserManager, $this->logger, $this->dispatcher); $lw->expects($this->exactly(1)) ->method('explodeDN') @@ -263,10 +269,10 @@ public function testStringResemblesDN($case) { */ public function testStringResemblesDNLDAPmod($case) { [, $con, $um, $helper] = $this->getConnectorAndLdapMock(); - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $config */ + /** @var IConfig|MockObject $config */ $config = $this->createMock(IConfig::class); $lw = new LDAP(); - $access = new Access($con, $lw, $um, $helper, $config, $this->ncUserManager, $this->logger); + $access = new Access($con, $lw, $um, $helper, $config, $this->ncUserManager, $this->logger, $this->dispatcher); if (!function_exists('ldap_explode_dn')) { $this->markTestSkipped('LDAP Module not available'); @@ -291,7 +297,7 @@ public function testBatchApplyUserAttributes() { ->method('getAttributes') ->willReturn(['displayname' => ['bar', 'count' => 1]]); - /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject $mapperMock */ + /** @var UserMapping|MockObject $mapperMock */ $mapperMock = $this->createMock(UserMapping::class); $mapperMock->expects($this->any()) ->method('getNameByDN') @@ -336,7 +342,7 @@ public function testBatchApplyUserAttributes() { } public function testBatchApplyUserAttributesSkipped() { - /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject $mapperMock */ + /** @var UserMapping|MockObject $mapperMock */ $mapperMock = $this->createMock(UserMapping::class); $mapperMock->expects($this->any()) ->method('getNameByDN') @@ -377,7 +383,7 @@ public function testBatchApplyUserAttributesSkipped() { } public function testBatchApplyUserAttributesDontSkip() { - /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject $mapperMock */ + /** @var UserMapping|MockObject $mapperMock */ $mapperMock = $this->createMock(UserMapping::class); $mapperMock->expects($this->any()) ->method('getNameByDN') @@ -433,7 +439,7 @@ public function dNAttributeProvider() { */ public function testSanitizeDN($attribute) { [$lw, $con, $um, $helper] = $this->getConnectorAndLdapMock(); - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $config */ + /** @var IConfig|MockObject $config */ $config = $this->createMock(IConfig::class); $dnFromServer = 'cn=Mixed Cases,ou=Are Sufficient To,ou=Test,dc=example,dc=org'; @@ -447,7 +453,7 @@ public function testSanitizeDN($attribute) { $attribute => ['count' => 1, $dnFromServer] ]); - $access = new Access($con, $lw, $um, $helper, $config, $this->ncUserManager, $this->logger); + $access = new Access($con, $lw, $um, $helper, $config, $this->ncUserManager, $this->logger, $this->dispatcher); $values = $access->readAttribute('uid=whoever,dc=example,dc=org', $attribute); $this->assertSame($values[0], strtolower($dnFromServer)); } @@ -763,7 +769,7 @@ public function testUserStateUpdate() { ->with('detta') ->willReturnOnConsecutiveCalls($offlineUserMock, $regularUserMock); - /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject $mapperMock */ + /** @var UserMapping|MockObject $mapperMock */ $mapperMock = $this->createMock(UserMapping::class); $mapperMock->expects($this->any()) ->method('getNameByDN') diff --git a/apps/user_ldap/tests/Jobs/SyncTest.php b/apps/user_ldap/tests/Jobs/SyncTest.php index 8d23efb4da83a..afc4cf30acd96 100644 --- a/apps/user_ldap/tests/Jobs/SyncTest.php +++ b/apps/user_ldap/tests/Jobs/SyncTest.php @@ -35,40 +35,43 @@ use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\User\Manager; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IAvatarManager; use OCP\IConfig; use OCP\IDBConnection; use OCP\IUserManager; use OCP\Notification\IManager; +use OCP\Server; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Test\TestCase; class SyncTest extends TestCase { /** @var array */ protected $arguments; - /** @var Helper|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Helper|MockObject */ protected $helper; - /** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */ + /** @var LDAP|MockObject */ protected $ldapWrapper; - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Manager|MockObject */ protected $userManager; - /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */ + /** @var UserMapping|MockObject */ protected $mapper; - /** @var Sync */ - protected $sync; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + protected Sync $sync; + /** @var IConfig|MockObject */ protected $config; - /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IAvatarManager|MockObject */ protected $avatarManager; - /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IDBConnection|MockObject */ protected $dbc; - /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IUserManager|MockObject */ protected $ncUserManager; - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IManager|MockObject */ protected $notificationManager; - /** @var ConnectionFactory|\PHPUnit\Framework\MockObject\MockObject */ + /** @var ConnectionFactory|MockObject */ protected $connectionFactory; - /** @var AccessFactory|\PHPUnit\Framework\MockObject\MockObject */ + /** @var AccessFactory|MockObject */ protected $accessFactory; protected function setUp(): void { @@ -86,23 +89,26 @@ protected function setUp(): void { $this->connectionFactory = $this->createMock(ConnectionFactory::class); $this->accessFactory = $this->createMock(AccessFactory::class); - $this->arguments = [ - 'helper' => $this->helper, - 'ldapWrapper' => $this->ldapWrapper, - 'mapper' => $this->mapper, - 'config' => $this->config, - 'avatarManager' => $this->avatarManager, - 'dbc' => $this->dbc, - 'ncUserManager' => $this->ncUserManager, - 'notificationManager' => $this->notificationManager, - 'connectionFactory' => $this->connectionFactory, - 'accessFactory' => $this->accessFactory, - ]; - - $this->sync = new Sync($this->userManager, $this->createMock(ITimeFactory::class)); + $this->sync = new Sync( + $this->userManager, + Server::get(IEventDispatcher::class), + $this->config, + Server::get(ITimeFactory::class), + $this->dbc, + $this->avatarManager, + $this->ncUserManager, + Server::get(LoggerInterface::class), + $this->notificationManager, + $this->mapper, + $this->helper, + $this->connectionFactory + ); + + // FIXME + $this->sync->overwritePropertiesForTest($this->ldapWrapper, $this->accessFactory); } - public function intervalDataProvider() { + public function intervalDataProvider(): array { return [ [ 0, 1000, 750 @@ -125,7 +131,7 @@ public function intervalDataProvider() { /** * @dataProvider intervalDataProvider */ - public function testUpdateInterval($userCount, $pagingSize1, $pagingSize2) { + public function testUpdateInterval(int $userCount, int $pagingSize1, int $pagingSize2): void { $this->config->expects($this->once()) ->method('setAppValue') ->with('user_ldap', 'background_sync_interval', $this->anything()) @@ -182,7 +188,7 @@ public function testMoreResults($pagingSize, $results, $expected) { return null; }); - /** @var Access|\PHPUnit\Framework\MockObject\MockObject $access */ + /** @var Access|MockObject $access */ $access = $this->createMock(Access::class); $this->accessFactory->expects($this->any()) ->method('get') @@ -252,7 +258,7 @@ public function testDetermineNextCycle($cycleData, $prefixes, $expectedCycle) { } } - public function testQualifiesToRun() { + public function testQualifiesToRun(): void { $cycleData = ['prefix' => 's01']; $this->config->expects($this->exactly(2)) @@ -264,7 +270,7 @@ public function testQualifiesToRun() { $this->assertFalse($this->sync->qualifiesToRun($cycleData)); } - public function runDataProvider() { + public function runDataProvider(): array { return [ #0 - one LDAP server, reset [[ @@ -356,7 +362,7 @@ public function testRun($runData) { return null; }); - /** @var Access|\PHPUnit\Framework\MockObject\MockObject $access */ + /** @var Access|MockObject $access */ $access = $this->createMock(Access::class); $this->accessFactory->expects($this->any()) ->method('get') diff --git a/console.php b/console.php index f9a4689bd5895..41b99fdd3cd7e 100644 --- a/console.php +++ b/console.php @@ -34,6 +34,7 @@ require_once __DIR__ . '/lib/versioncheck.php'; use OC\Console\Application; +use OCP\Server; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\ConsoleOutput; @@ -92,9 +93,11 @@ function exceptionHandler($exception) { $application = new Application( \OC::$server->getConfig(), \OC::$server->getEventDispatcher(), + Server::get(\OCP\EventDispatcher\IEventDispatcher::class), \OC::$server->getRequest(), \OC::$server->get(\Psr\Log\LoggerInterface::class), - \OC::$server->query(\OC\MemoryInfo::class) + \OC::$server->query(\OC\MemoryInfo::class), + Server::get(\OCP\Defaults::class) ); $application->loadCommands(new ArgvInput(), new ConsoleOutput()); $application->run(); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index eb2f3e463546d..aa8e7bdb59f2b 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -193,6 +193,7 @@ 'OCP\\Config\\BeforePreferenceDeletedEvent' => $baseDir . '/lib/public/Config/BeforePreferenceDeletedEvent.php', 'OCP\\Config\\BeforePreferenceSetEvent' => $baseDir . '/lib/public/Config/BeforePreferenceSetEvent.php', 'OCP\\Console\\ConsoleEvent' => $baseDir . '/lib/public/Console/ConsoleEvent.php', + 'OCP\\Console\\ConsoleEventV2' => $baseDir . '/lib/public/Console/ConsoleEventV2.php', 'OCP\\Constants' => $baseDir . '/lib/public/Constants.php', 'OCP\\Contacts\\ContactsMenu\\IAction' => $baseDir . '/lib/public/Contacts/ContactsMenu/IAction.php', 'OCP\\Contacts\\ContactsMenu\\IActionFactory' => $baseDir . '/lib/public/Contacts/ContactsMenu/IActionFactory.php', @@ -645,6 +646,7 @@ 'OCP\\User\\Events\\BeforePasswordUpdatedEvent' => $baseDir . '/lib/public/User/Events/BeforePasswordUpdatedEvent.php', 'OCP\\User\\Events\\BeforeUserCreatedEvent' => $baseDir . '/lib/public/User/Events/BeforeUserCreatedEvent.php', 'OCP\\User\\Events\\BeforeUserDeletedEvent' => $baseDir . '/lib/public/User/Events/BeforeUserDeletedEvent.php', + 'OCP\\User\\Events\\BeforeUserIdUnassignedEvent' => $baseDir . '/lib/public/User/Events/BeforeUserIdUnassignedEvent.php', 'OCP\\User\\Events\\BeforeUserLoggedInEvent' => $baseDir . '/lib/public/User/Events/BeforeUserLoggedInEvent.php', 'OCP\\User\\Events\\BeforeUserLoggedInWithCookieEvent' => $baseDir . '/lib/public/User/Events/BeforeUserLoggedInWithCookieEvent.php', 'OCP\\User\\Events\\BeforeUserLoggedOutEvent' => $baseDir . '/lib/public/User/Events/BeforeUserLoggedOutEvent.php', @@ -653,6 +655,8 @@ 'OCP\\User\\Events\\UserChangedEvent' => $baseDir . '/lib/public/User/Events/UserChangedEvent.php', 'OCP\\User\\Events\\UserCreatedEvent' => $baseDir . '/lib/public/User/Events/UserCreatedEvent.php', 'OCP\\User\\Events\\UserDeletedEvent' => $baseDir . '/lib/public/User/Events/UserDeletedEvent.php', + 'OCP\\User\\Events\\UserIdAssignedEvent' => $baseDir . '/lib/public/User/Events/UserIdAssignedEvent.php', + 'OCP\\User\\Events\\UserIdUnassignedEvent' => $baseDir . '/lib/public/User/Events/UserIdUnassignedEvent.php', 'OCP\\User\\Events\\UserLiveStatusEvent' => $baseDir . '/lib/public/User/Events/UserLiveStatusEvent.php', 'OCP\\User\\Events\\UserLoggedInEvent' => $baseDir . '/lib/public/User/Events/UserLoggedInEvent.php', 'OCP\\User\\Events\\UserLoggedInWithCookieEvent' => $baseDir . '/lib/public/User/Events/UserLoggedInWithCookieEvent.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 9343231a55b56..62bc4e4fd546e 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -226,6 +226,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Config\\BeforePreferenceDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceDeletedEvent.php', 'OCP\\Config\\BeforePreferenceSetEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceSetEvent.php', 'OCP\\Console\\ConsoleEvent' => __DIR__ . '/../../..' . '/lib/public/Console/ConsoleEvent.php', + 'OCP\\Console\\ConsoleEventV2' => __DIR__ . '/../../..' . '/lib/public/Console/ConsoleEventV2.php', 'OCP\\Constants' => __DIR__ . '/../../..' . '/lib/public/Constants.php', 'OCP\\Contacts\\ContactsMenu\\IAction' => __DIR__ . '/../../..' . '/lib/public/Contacts/ContactsMenu/IAction.php', 'OCP\\Contacts\\ContactsMenu\\IActionFactory' => __DIR__ . '/../../..' . '/lib/public/Contacts/ContactsMenu/IActionFactory.php', @@ -678,6 +679,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\User\\Events\\BeforePasswordUpdatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforePasswordUpdatedEvent.php', 'OCP\\User\\Events\\BeforeUserCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserCreatedEvent.php', 'OCP\\User\\Events\\BeforeUserDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserDeletedEvent.php', + 'OCP\\User\\Events\\BeforeUserIdUnassignedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserIdUnassignedEvent.php', 'OCP\\User\\Events\\BeforeUserLoggedInEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserLoggedInEvent.php', 'OCP\\User\\Events\\BeforeUserLoggedInWithCookieEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserLoggedInWithCookieEvent.php', 'OCP\\User\\Events\\BeforeUserLoggedOutEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/BeforeUserLoggedOutEvent.php', @@ -686,6 +688,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\User\\Events\\UserChangedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserChangedEvent.php', 'OCP\\User\\Events\\UserCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserCreatedEvent.php', 'OCP\\User\\Events\\UserDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserDeletedEvent.php', + 'OCP\\User\\Events\\UserIdAssignedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserIdAssignedEvent.php', + 'OCP\\User\\Events\\UserIdUnassignedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserIdUnassignedEvent.php', 'OCP\\User\\Events\\UserLiveStatusEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserLiveStatusEvent.php', 'OCP\\User\\Events\\UserLoggedInEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserLoggedInEvent.php', 'OCP\\User\\Events\\UserLoggedInWithCookieEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserLoggedInWithCookieEvent.php', diff --git a/lib/private/Console/Application.php b/lib/private/Console/Application.php index 94956364390db..befa4dd13fcf0 100644 --- a/lib/private/Console/Application.php +++ b/lib/private/Console/Application.php @@ -30,43 +30,48 @@ */ namespace OC\Console; +use ArgumentCountError; +use Exception; use OC\MemoryInfo; use OC\NeedsUpdateException; +use OC\SystemConfig; use OC_App; -use OCP\AppFramework\QueryException; use OCP\App\IAppManager; use OCP\Console\ConsoleEvent; +use OCP\Console\ConsoleEventV2; +use OCP\Defaults; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IRequest; +use OCP\Server; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Application as SymfonyApplication; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; class Application { - /** @var IConfig */ - private $config; + private IConfig $config; private SymfonyApplication $application; - /** @var EventDispatcherInterface */ - private $dispatcher; - /** @var IRequest */ - private $request; - /** @var LoggerInterface */ - private $logger; - /** @var MemoryInfo */ - private $memoryInfo; + private EventDispatcherInterface $legacyDispatcher; + private IEventDispatcher $dispatcher; + private IRequest $request; + private LoggerInterface $logger; + private MemoryInfo $memoryInfo; public function __construct(IConfig $config, - EventDispatcherInterface $dispatcher, + EventDispatcherInterface $legacyDispatcher, + IEventDispatcher $dispatcher, IRequest $request, LoggerInterface $logger, - MemoryInfo $memoryInfo) { - $defaults = \OC::$server->getThemingDefaults(); + MemoryInfo $memoryInfo, Defaults $defaults) { $this->config = $config; $this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString()); + $this->legacyDispatcher = $legacyDispatcher; $this->dispatcher = $dispatcher; $this->request = $request; $this->logger = $logger; @@ -76,12 +81,14 @@ public function __construct(IConfig $config, /** * @param InputInterface $input * @param ConsoleOutputInterface $output - * @throws \Exception + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws Exception */ public function loadCommands( InputInterface $input, ConsoleOutputInterface $output - ) { + ): void { // $application is required to be defined in the register_command scripts $application = $this->application; $inputDefinition = $application->getDefinition(); @@ -119,7 +126,7 @@ public function loadCommands( $this->writeMaintenanceModeInfo($input, $output); } else { OC_App::loadApps(); - $appManager = \OCP\Server::get(IAppManager::class); + $appManager = Server::get(IAppManager::class); foreach ($appManager->getInstalledApps() as $app) { $appPath = \OC_App::getAppPath($app); if ($appPath === false) { @@ -136,7 +143,7 @@ public function loadCommands( if (file_exists($file)) { try { require $file; - } catch (\Exception $e) { + } catch (Exception $e) { $this->logger->error($e->getMessage(), [ 'exception' => $e, ]); @@ -148,7 +155,7 @@ public function loadCommands( $errorOutput = $output->getErrorOutput(); $errorOutput->writeln("Nextcloud is not installed - only a limited number of commands are available"); } - } catch (NeedsUpdateException $e) { + } catch (NeedsUpdateException) { if ($input->getArgument('command') !== '_completion') { $errorOutput = $output->getErrorOutput(); $errorOutput->writeln("Nextcloud or one of the apps require upgrade - only a limited number of commands are available"); @@ -157,14 +164,14 @@ public function loadCommands( } if ($input->getFirstArgument() !== 'check') { - $errors = \OC_Util::checkServer(\OC::$server->getSystemConfig()); + $errors = \OC_Util::checkServer(Server::get(SystemConfig::class)); if (!empty($errors)) { foreach ($errors as $error) { $output->writeln((string)$error['error']); $output->writeln((string)$error['hint']); $output->writeln(''); } - throw new \Exception("Environment not properly prepared."); + throw new Exception("Environment not properly prepared."); } } } @@ -193,37 +200,38 @@ private function writeMaintenanceModeInfo(InputInterface $input, ConsoleOutputIn * * @param bool $boolean Whether to automatically exit after a command execution or not */ - public function setAutoExit($boolean) { + public function setAutoExit(bool $boolean): void { $this->application->setAutoExit($boolean); } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int - * @throws \Exception + * @throws Exception */ - public function run(InputInterface $input = null, OutputInterface $output = null) { - $this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent( + public function run(InputInterface $input = null, OutputInterface $output = null): int { + $this->legacyDispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent( ConsoleEvent::EVENT_RUN, $this->request->server['argv'] )); + $this->dispatcher->dispatchTyped(new ConsoleEventV2($this->request->server['argv'])); return $this->application->run($input, $output); } - private function loadCommandsFromInfoXml($commands) { + /** + * @throws Exception + */ + private function loadCommandsFromInfoXml($commands): void { foreach ($commands as $command) { try { - $c = \OC::$server->query($command); - } catch (QueryException $e) { + $c = Server::get($command); + } catch (ContainerExceptionInterface $e) { if (class_exists($command)) { try { $c = new $command(); - } catch (\ArgumentCountError $e2) { - throw new \Exception("Failed to construct console command '$command': " . $e->getMessage(), 0, $e); + } catch (ArgumentCountError) { + throw new Exception("Failed to construct console command '$command': " . $e->getMessage(), 0, $e); } } else { - throw new \Exception("Console command '$command' is unknown and could not be loaded"); + throw new Exception("Console command '$command' is unknown and could not be loaded"); } } diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php index ed9474fafb217..9e00e1a063584 100644 --- a/lib/private/Preview/Generator.php +++ b/lib/private/Preview/Generator.php @@ -109,7 +109,12 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false, new GenericEvent($file, $specification) ); $this->eventDispatcher->dispatchTyped(new BeforePreviewFetchedEvent( - $file + $file, + $width, + $height, + $crop, + $mode, + $mimeType )); // since we only ask for one preview, and the generate method return the last one it created, it returns the one we want diff --git a/lib/public/Console/ConsoleEvent.php b/lib/public/Console/ConsoleEvent.php index 99f42d2895fbb..1f1f7c2ee0677 100644 --- a/lib/public/Console/ConsoleEvent.php +++ b/lib/public/Console/ConsoleEvent.php @@ -28,6 +28,8 @@ /** * Class ConsoleEvent * + * @deprecated 27.0.0 Use ConsoleEventV2 typed event instead + * * @since 9.0.0 */ class ConsoleEvent extends Event { diff --git a/lib/public/Console/ConsoleEventV2.php b/lib/public/Console/ConsoleEventV2.php new file mode 100644 index 0000000000000..5627a696cc292 --- /dev/null +++ b/lib/public/Console/ConsoleEventV2.php @@ -0,0 +1,56 @@ + + * + * @author Thomas Citharel + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OCP\Console; + +use OCP\EventDispatcher\Event; + +/** + * Class ConsoleEvent + * + * @since 27.0.0 + */ +class ConsoleEventV2 extends Event { + /** @var string[] */ + protected array $arguments; + + /** + * DispatcherEvent constructor. + * + * @param string[] $arguments + * @since 27.0.0 + */ + public function __construct(array $arguments) { + $this->arguments = $arguments; + parent::__construct(); + } + + /** + * @return string[] + * @since 27.0.0 + */ + public function getArguments(): array { + return $this->arguments; + } +} diff --git a/lib/public/Preview/BeforePreviewFetchedEvent.php b/lib/public/Preview/BeforePreviewFetchedEvent.php index 37da63b95a1a1..cc8a5d2458b83 100644 --- a/lib/public/Preview/BeforePreviewFetchedEvent.php +++ b/lib/public/Preview/BeforePreviewFetchedEvent.php @@ -33,13 +33,23 @@ */ class BeforePreviewFetchedEvent extends \OCP\EventDispatcher\Event { private Node $node; + private int $width; + private int $height; + private bool $crop; + private string $mode; + private ?string $mimeType; /** * @since 25.0.1 */ - public function __construct(Node $node) { + public function __construct(Node $node, int $width, int $height, bool $crop, string $mode, ?string $mimeType) { parent::__construct(); $this->node = $node; + $this->width = $width; + $this->height = $height; + $this->crop = $crop; + $this->mode = $mode; + $this->mimeType = $mimeType; } /** @@ -48,4 +58,39 @@ public function __construct(Node $node) { public function getNode(): Node { return $this->node; } + + /** + * @since 27.0.0 + */ + public function getWidth(): int { + return $this->width; + } + + /** + * @since 27.0.0 + */ + public function getHeight(): int { + return $this->height; + } + + /** + * @since 27.0.0 + */ + public function isCrop(): bool { + return $this->crop; + } + + /** + * @since 27.0.0 + */ + public function getMode(): string { + return $this->mode; + } + + /** + * @since 27.0.0 + */ + public function getMimeType(): ?string { + return $this->mimeType; + } } diff --git a/lib/public/User/Events/BeforeUserIdUnassignedEvent.php b/lib/public/User/Events/BeforeUserIdUnassignedEvent.php new file mode 100644 index 0000000000000..c8de53ab84886 --- /dev/null +++ b/lib/public/User/Events/BeforeUserIdUnassignedEvent.php @@ -0,0 +1,49 @@ + + * + * @author Thomas Citharel + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OCP\User\Events; + +use OCP\EventDispatcher\Event; + +/** + * @since 27.0.0 + */ +class BeforeUserIdUnassignedEvent extends Event { + private string $userId; + + /** + * @since 27.0.0 + */ + public function __construct(string $userId) { + parent::__construct(); + $this->userId = $userId; + } + + /** + * @since 27.0.0 + */ + public function getUserId(): string { + return $this->userId; + } +} diff --git a/lib/public/User/Events/UserIdAssignedEvent.php b/lib/public/User/Events/UserIdAssignedEvent.php new file mode 100644 index 0000000000000..fc5f2ebf989a1 --- /dev/null +++ b/lib/public/User/Events/UserIdAssignedEvent.php @@ -0,0 +1,49 @@ + + * + * @author Thomas Citharel + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OCP\User\Events; + +use OCP\EventDispatcher\Event; + +/** + * @since 27.0.0 + */ +class UserIdAssignedEvent extends Event { + private string $userId; + + /** + * @since 27.0.0 + */ + public function __construct(string $userId) { + parent::__construct(); + $this->userId = $userId; + } + + /** + * @since 27.0.0 + */ + public function getUserId(): string { + return $this->userId; + } +} diff --git a/lib/public/User/Events/UserIdUnassignedEvent.php b/lib/public/User/Events/UserIdUnassignedEvent.php new file mode 100644 index 0000000000000..359f170bef8cb --- /dev/null +++ b/lib/public/User/Events/UserIdUnassignedEvent.php @@ -0,0 +1,49 @@ + + * + * @author Thomas Citharel + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OCP\User\Events; + +use OCP\EventDispatcher\Event; + +/** + * @since 27.0.0 + */ +class UserIdUnassignedEvent extends Event { + private string $userId; + + /** + * @since 27.0.0 + */ + public function __construct(string $userId) { + parent::__construct(); + $this->userId = $userId; + } + + /** + * @since 27.0.0 + */ + public function getUserId(): string { + return $this->userId; + } +} diff --git a/tests/lib/Preview/GeneratorTest.php b/tests/lib/Preview/GeneratorTest.php index 37fc3935139e0..c0868bc18decc 100644 --- a/tests/lib/Preview/GeneratorTest.php +++ b/tests/lib/Preview/GeneratorTest.php @@ -125,7 +125,7 @@ public function testGetCachedPreview() { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null)); $result = $this->generator->getPreview($file, 100, 100); $this->assertSame($previewFile, $result); @@ -264,7 +264,7 @@ public function testGetNewPreview() { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null)); $result = $this->generator->getPreview($file, 100, 100); $this->assertSame($previewFile, $result); @@ -316,7 +316,7 @@ public function testInvalidMimeType() { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType')); $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'); } @@ -366,7 +366,7 @@ public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype() { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType')); $result = $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'); $this->assertSame($preview, $result); @@ -405,7 +405,7 @@ public function testNoProvider() { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null)); $this->expectException(NotFoundException::class); $this->generator->getPreview($file, 100, 100); @@ -543,7 +543,7 @@ public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expec $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, $reqX, $reqY, $crop, $mode, null)); $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode); if ($expectedX === $maxX && $expectedY === $maxY) {