Application.php 9.4 KB

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