Application.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\AdminAudit\AppInfo;
  8. use OC\Group\Manager as GroupManager;
  9. use OC\User\Session as UserSession;
  10. use OCA\AdminAudit\Actions\AppManagement;
  11. use OCA\AdminAudit\Actions\Auth;
  12. use OCA\AdminAudit\Actions\Console;
  13. use OCA\AdminAudit\Actions\Files;
  14. use OCA\AdminAudit\Actions\GroupManagement;
  15. use OCA\AdminAudit\Actions\Security;
  16. use OCA\AdminAudit\Actions\Sharing;
  17. use OCA\AdminAudit\Actions\Trashbin;
  18. use OCA\AdminAudit\Actions\UserManagement;
  19. use OCA\AdminAudit\Actions\Versions;
  20. use OCA\AdminAudit\AuditLogger;
  21. use OCA\AdminAudit\IAuditLogger;
  22. use OCA\AdminAudit\Listener\CriticalActionPerformedEventListener;
  23. use OCP\App\ManagerEvent;
  24. use OCP\AppFramework\App;
  25. use OCP\AppFramework\Bootstrap\IBootContext;
  26. use OCP\AppFramework\Bootstrap\IBootstrap;
  27. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  28. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengeFailed;
  29. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengePassed;
  30. use OCP\Console\ConsoleEvent;
  31. use OCP\EventDispatcher\IEventDispatcher;
  32. use OCP\Files\Events\Node\BeforeNodeReadEvent;
  33. use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
  34. use OCP\Files\Events\Node\NodeCopiedEvent;
  35. use OCP\Files\Events\Node\NodeCreatedEvent;
  36. use OCP\Files\Events\Node\NodeDeletedEvent;
  37. use OCP\Files\Events\Node\NodeRenamedEvent;
  38. use OCP\Files\Events\Node\NodeWrittenEvent;
  39. use OCP\IConfig;
  40. use OCP\IGroupManager;
  41. use OCP\IUserSession;
  42. use OCP\Log\Audit\CriticalActionPerformedEvent;
  43. use OCP\Log\ILogFactory;
  44. use OCP\Preview\BeforePreviewFetchedEvent;
  45. use OCP\Share;
  46. use OCP\Util;
  47. use Psr\Container\ContainerInterface;
  48. use Psr\Log\LoggerInterface;
  49. class Application extends App implements IBootstrap {
  50. /** @var LoggerInterface */
  51. protected $logger;
  52. public function __construct() {
  53. parent::__construct('admin_audit');
  54. }
  55. public function register(IRegistrationContext $context): void {
  56. $context->registerService(IAuditLogger::class, function (ContainerInterface $c) {
  57. return new AuditLogger($c->get(ILogFactory::class), $c->get(IConfig::class));
  58. });
  59. $context->registerEventListener(CriticalActionPerformedEvent::class, CriticalActionPerformedEventListener::class);
  60. }
  61. public function boot(IBootContext $context): void {
  62. /** @var IAuditLogger $logger */
  63. $logger = $context->getAppContainer()->get(IAuditLogger::class);
  64. /*
  65. * TODO: once the hooks are migrated to lazy events, this should be done
  66. * in \OCA\AdminAudit\AppInfo\Application::register
  67. */
  68. $this->registerHooks($logger, $context->getServerContainer());
  69. }
  70. /**
  71. * Register hooks in order to log them
  72. */
  73. private function registerHooks(IAuditLogger $logger,
  74. ContainerInterface $serverContainer): void {
  75. $this->userManagementHooks($logger, $serverContainer->get(IUserSession::class));
  76. $this->groupHooks($logger, $serverContainer->get(IGroupManager::class));
  77. $this->authHooks($logger);
  78. /** @var IEventDispatcher $eventDispatcher */
  79. $eventDispatcher = $serverContainer->get(IEventDispatcher::class);
  80. $this->consoleHooks($logger, $eventDispatcher);
  81. $this->appHooks($logger, $eventDispatcher);
  82. $this->sharingHooks($logger);
  83. $this->fileHooks($logger, $eventDispatcher);
  84. $this->trashbinHooks($logger);
  85. $this->versionsHooks($logger);
  86. $this->securityHooks($logger, $eventDispatcher);
  87. }
  88. private function userManagementHooks(IAuditLogger $logger,
  89. IUserSession $userSession): void {
  90. $userActions = new UserManagement($logger);
  91. Util::connectHook('OC_User', 'post_createUser', $userActions, 'create');
  92. Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete');
  93. Util::connectHook('OC_User', 'changeUser', $userActions, 'change');
  94. assert($userSession instanceof UserSession);
  95. $userSession->listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']);
  96. $userSession->listen('\OC\User', 'assignedUserId', [$userActions, 'assign']);
  97. $userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']);
  98. }
  99. private function groupHooks(IAuditLogger $logger,
  100. IGroupManager $groupManager): void {
  101. $groupActions = new GroupManagement($logger);
  102. assert($groupManager instanceof GroupManager);
  103. $groupManager->listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']);
  104. $groupManager->listen('\OC\Group', 'postAddUser', [$groupActions, 'addUser']);
  105. $groupManager->listen('\OC\Group', 'postDelete', [$groupActions, 'deleteGroup']);
  106. $groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']);
  107. }
  108. private function sharingHooks(IAuditLogger $logger): void {
  109. $shareActions = new Sharing($logger);
  110. Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared');
  111. Util::connectHook(Share::class, 'post_unshare', $shareActions, 'unshare');
  112. Util::connectHook(Share::class, 'post_unshareFromSelf', $shareActions, 'unshare');
  113. Util::connectHook(Share::class, 'post_update_permissions', $shareActions, 'updatePermissions');
  114. Util::connectHook(Share::class, 'post_update_password', $shareActions, 'updatePassword');
  115. Util::connectHook(Share::class, 'post_set_expiration_date', $shareActions, 'updateExpirationDate');
  116. Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed');
  117. }
  118. private function authHooks(IAuditLogger $logger): void {
  119. $authActions = new Auth($logger);
  120. Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt');
  121. Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful');
  122. Util::connectHook('OC_User', 'logout', $authActions, 'logout');
  123. }
  124. private function appHooks(IAuditLogger $logger,
  125. IEventDispatcher $eventDispatcher): void {
  126. $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function (ManagerEvent $event) use ($logger) {
  127. $appActions = new AppManagement($logger);
  128. $appActions->enableApp($event->getAppID());
  129. });
  130. $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function (ManagerEvent $event) use ($logger) {
  131. $appActions = new AppManagement($logger);
  132. $appActions->enableAppForGroups($event->getAppID(), $event->getGroups());
  133. });
  134. $eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function (ManagerEvent $event) use ($logger) {
  135. $appActions = new AppManagement($logger);
  136. $appActions->disableApp($event->getAppID());
  137. });
  138. }
  139. private function consoleHooks(IAuditLogger $logger,
  140. IEventDispatcher $eventDispatcher): void {
  141. $eventDispatcher->addListener(ConsoleEvent::class, function (ConsoleEvent $event) use ($logger) {
  142. $appActions = new Console($logger);
  143. $appActions->runCommand($event->getArguments());
  144. });
  145. }
  146. private function fileHooks(IAuditLogger $logger,
  147. IEventDispatcher $eventDispatcher): void {
  148. $fileActions = new Files($logger);
  149. $eventDispatcher->addListener(
  150. BeforePreviewFetchedEvent::class,
  151. function (BeforePreviewFetchedEvent $event) use ($fileActions) {
  152. $fileActions->preview($event);
  153. }
  154. );
  155. $eventDispatcher->addListener(
  156. NodeRenamedEvent::class,
  157. function (NodeRenamedEvent $event) use ($fileActions) {
  158. $fileActions->rename($event);
  159. }
  160. );
  161. $eventDispatcher->addListener(
  162. NodeCreatedEvent::class,
  163. function (NodeCreatedEvent $event) use ($fileActions) {
  164. $fileActions->create($event);
  165. }
  166. );
  167. $eventDispatcher->addListener(
  168. NodeCopiedEvent::class,
  169. function (NodeCopiedEvent $event) use ($fileActions) {
  170. $fileActions->copy($event);
  171. }
  172. );
  173. $eventDispatcher->addListener(
  174. BeforeNodeWrittenEvent::class,
  175. function (BeforeNodeWrittenEvent $event) use ($fileActions) {
  176. $fileActions->write($event);
  177. }
  178. );
  179. $eventDispatcher->addListener(
  180. NodeWrittenEvent::class,
  181. function (NodeWrittenEvent $event) use ($fileActions) {
  182. $fileActions->update($event);
  183. }
  184. );
  185. $eventDispatcher->addListener(
  186. BeforeNodeReadEvent::class,
  187. function (BeforeNodeReadEvent $event) use ($fileActions) {
  188. $fileActions->read($event);
  189. }
  190. );
  191. $eventDispatcher->addListener(
  192. NodeDeletedEvent::class,
  193. function (NodeDeletedEvent $event) use ($fileActions) {
  194. $fileActions->delete($event);
  195. }
  196. );
  197. }
  198. private function versionsHooks(IAuditLogger $logger): void {
  199. $versionsActions = new Versions($logger);
  200. Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback');
  201. Util::connectHook('\OCP\Versions', 'delete', $versionsActions, 'delete');
  202. }
  203. private function trashbinHooks(IAuditLogger $logger): void {
  204. $trashActions = new Trashbin($logger);
  205. Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
  206. Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
  207. }
  208. private function securityHooks(IAuditLogger $logger,
  209. IEventDispatcher $eventDispatcher): void {
  210. $eventDispatcher->addListener(TwoFactorProviderChallengePassed::class, function (TwoFactorProviderChallengePassed $event) use ($logger) {
  211. $security = new Security($logger);
  212. $security->twofactorSuccess($event->getUser(), $event->getProvider());
  213. });
  214. $eventDispatcher->addListener(TwoFactorProviderChallengeFailed::class, function (TwoFactorProviderChallengeFailed $event) use ($logger) {
  215. $security = new Security($logger);
  216. $security->twofactorFailed($event->getUser(), $event->getProvider());
  217. });
  218. }
  219. }