Application.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author GrayFix <grayfix@gmail.com>
  11. * @author Joas Schilling <coding@schilljs.com>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Tiago Flores <tiago.flores@yahoo.com.br>
  15. *
  16. * @license GNU AGPL version 3 or any later version
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License as
  20. * published by the Free Software Foundation, either version 3 of the
  21. * License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. */
  32. namespace OCA\AdminAudit\AppInfo;
  33. use OC\Files\Filesystem;
  34. use OC\Group\Manager as GroupManager;
  35. use OC\User\Session as UserSession;
  36. use OCA\AdminAudit\Actions\AppManagement;
  37. use OCA\AdminAudit\Actions\Auth;
  38. use OCA\AdminAudit\Actions\Console;
  39. use OCA\AdminAudit\Actions\Files;
  40. use OCA\AdminAudit\Actions\GroupManagement;
  41. use OCA\AdminAudit\Actions\Security;
  42. use OCA\AdminAudit\Actions\Sharing;
  43. use OCA\AdminAudit\Actions\Trashbin;
  44. use OCA\AdminAudit\Actions\UserManagement;
  45. use OCA\AdminAudit\Actions\Versions;
  46. use OCA\AdminAudit\AuditLogger;
  47. use OCA\AdminAudit\IAuditLogger;
  48. use OCA\AdminAudit\Listener\CriticalActionPerformedEventListener;
  49. use OCP\App\ManagerEvent;
  50. use OCP\AppFramework\App;
  51. use OCP\AppFramework\Bootstrap\IBootContext;
  52. use OCP\AppFramework\Bootstrap\IBootstrap;
  53. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  54. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengeFailed;
  55. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengePassed;
  56. use OCP\Console\ConsoleEvent;
  57. use OCP\EventDispatcher\IEventDispatcher;
  58. use OCP\IConfig;
  59. use OCP\IGroupManager;
  60. use OCP\IUserSession;
  61. use OCP\Log\Audit\CriticalActionPerformedEvent;
  62. use OCP\Log\ILogFactory;
  63. use OCP\Preview\BeforePreviewFetchedEvent;
  64. use OCP\Share;
  65. use OCP\Util;
  66. use Psr\Container\ContainerInterface;
  67. use Psr\Log\LoggerInterface;
  68. class Application extends App implements IBootstrap {
  69. /** @var LoggerInterface */
  70. protected $logger;
  71. public function __construct() {
  72. parent::__construct('admin_audit');
  73. }
  74. public function register(IRegistrationContext $context): void {
  75. $context->registerService(IAuditLogger::class, function (ContainerInterface $c) {
  76. return new AuditLogger($c->get(ILogFactory::class), $c->get(IConfig::class));
  77. });
  78. $context->registerEventListener(CriticalActionPerformedEvent::class, CriticalActionPerformedEventListener::class);
  79. }
  80. public function boot(IBootContext $context): void {
  81. /** @var IAuditLogger $logger */
  82. $logger = $context->getAppContainer()->get(IAuditLogger::class);
  83. /*
  84. * TODO: once the hooks are migrated to lazy events, this should be done
  85. * in \OCA\AdminAudit\AppInfo\Application::register
  86. */
  87. $this->registerHooks($logger, $context->getServerContainer());
  88. }
  89. /**
  90. * Register hooks in order to log them
  91. */
  92. private function registerHooks(IAuditLogger $logger,
  93. ContainerInterface $serverContainer): void {
  94. $this->userManagementHooks($logger, $serverContainer->get(IUserSession::class));
  95. $this->groupHooks($logger, $serverContainer->get(IGroupManager::class));
  96. $this->authHooks($logger);
  97. /** @var IEventDispatcher $eventDispatcher */
  98. $eventDispatcher = $serverContainer->get(IEventDispatcher::class);
  99. $this->consoleHooks($logger, $eventDispatcher);
  100. $this->appHooks($logger, $eventDispatcher);
  101. $this->sharingHooks($logger);
  102. $this->fileHooks($logger, $eventDispatcher);
  103. $this->trashbinHooks($logger);
  104. $this->versionsHooks($logger);
  105. $this->securityHooks($logger, $eventDispatcher);
  106. }
  107. private function userManagementHooks(IAuditLogger $logger,
  108. IUserSession $userSession): void {
  109. $userActions = new UserManagement($logger);
  110. Util::connectHook('OC_User', 'post_createUser', $userActions, 'create');
  111. Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete');
  112. Util::connectHook('OC_User', 'changeUser', $userActions, 'change');
  113. assert($userSession instanceof UserSession);
  114. $userSession->listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']);
  115. $userSession->listen('\OC\User', 'assignedUserId', [$userActions, 'assign']);
  116. $userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']);
  117. }
  118. private function groupHooks(IAuditLogger $logger,
  119. IGroupManager $groupManager): void {
  120. $groupActions = new GroupManagement($logger);
  121. assert($groupManager instanceof GroupManager);
  122. $groupManager->listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']);
  123. $groupManager->listen('\OC\Group', 'postAddUser', [$groupActions, 'addUser']);
  124. $groupManager->listen('\OC\Group', 'postDelete', [$groupActions, 'deleteGroup']);
  125. $groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']);
  126. }
  127. private function sharingHooks(IAuditLogger $logger): void {
  128. $shareActions = new Sharing($logger);
  129. Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared');
  130. Util::connectHook(Share::class, 'post_unshare', $shareActions, 'unshare');
  131. Util::connectHook(Share::class, 'post_unshareFromSelf', $shareActions, 'unshare');
  132. Util::connectHook(Share::class, 'post_update_permissions', $shareActions, 'updatePermissions');
  133. Util::connectHook(Share::class, 'post_update_password', $shareActions, 'updatePassword');
  134. Util::connectHook(Share::class, 'post_set_expiration_date', $shareActions, 'updateExpirationDate');
  135. Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed');
  136. }
  137. private function authHooks(IAuditLogger $logger): void {
  138. $authActions = new Auth($logger);
  139. Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt');
  140. Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful');
  141. Util::connectHook('OC_User', 'logout', $authActions, 'logout');
  142. }
  143. private function appHooks(IAuditLogger $logger,
  144. IEventDispatcher $eventDispatcher): void {
  145. $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function (ManagerEvent $event) use ($logger) {
  146. $appActions = new AppManagement($logger);
  147. $appActions->enableApp($event->getAppID());
  148. });
  149. $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function (ManagerEvent $event) use ($logger) {
  150. $appActions = new AppManagement($logger);
  151. $appActions->enableAppForGroups($event->getAppID(), $event->getGroups());
  152. });
  153. $eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function (ManagerEvent $event) use ($logger) {
  154. $appActions = new AppManagement($logger);
  155. $appActions->disableApp($event->getAppID());
  156. });
  157. }
  158. private function consoleHooks(IAuditLogger $logger,
  159. IEventDispatcher $eventDispatcher): void {
  160. $eventDispatcher->addListener(ConsoleEvent::class, function (ConsoleEvent $event) use ($logger) {
  161. $appActions = new Console($logger);
  162. $appActions->runCommand($event->getArguments());
  163. });
  164. }
  165. private function fileHooks(IAuditLogger $logger,
  166. IEventDispatcher $eventDispatcher): void {
  167. $fileActions = new Files($logger);
  168. $eventDispatcher->addListener(
  169. BeforePreviewFetchedEvent::class,
  170. function (BeforePreviewFetchedEvent $event) use ($fileActions) {
  171. $file = $event->getNode();
  172. $fileActions->preview([
  173. 'path' => mb_substr($file->getInternalPath(), 5),
  174. 'width' => $event->getWidth(),
  175. 'height' => $event->getHeight(),
  176. 'crop' => $event->isCrop(),
  177. 'mode' => $event->getMode()
  178. ]);
  179. }
  180. );
  181. Util::connectHook(
  182. Filesystem::CLASSNAME,
  183. Filesystem::signal_post_rename,
  184. $fileActions,
  185. 'rename'
  186. );
  187. Util::connectHook(
  188. Filesystem::CLASSNAME,
  189. Filesystem::signal_post_create,
  190. $fileActions,
  191. 'create'
  192. );
  193. Util::connectHook(
  194. Filesystem::CLASSNAME,
  195. Filesystem::signal_post_copy,
  196. $fileActions,
  197. 'copy'
  198. );
  199. Util::connectHook(
  200. Filesystem::CLASSNAME,
  201. Filesystem::signal_post_write,
  202. $fileActions,
  203. 'write'
  204. );
  205. Util::connectHook(
  206. Filesystem::CLASSNAME,
  207. Filesystem::signal_post_update,
  208. $fileActions,
  209. 'update'
  210. );
  211. Util::connectHook(
  212. Filesystem::CLASSNAME,
  213. Filesystem::signal_read,
  214. $fileActions,
  215. 'read'
  216. );
  217. Util::connectHook(
  218. Filesystem::CLASSNAME,
  219. Filesystem::signal_delete,
  220. $fileActions,
  221. 'delete'
  222. );
  223. }
  224. private function versionsHooks(IAuditLogger $logger): void {
  225. $versionsActions = new Versions($logger);
  226. Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback');
  227. Util::connectHook('\OCP\Versions', 'delete', $versionsActions, 'delete');
  228. }
  229. private function trashbinHooks(IAuditLogger $logger): void {
  230. $trashActions = new Trashbin($logger);
  231. Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
  232. Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
  233. }
  234. private function securityHooks(IAuditLogger $logger,
  235. IEventDispatcher $eventDispatcher): void {
  236. $eventDispatcher->addListener(TwoFactorProviderChallengePassed::class, function (TwoFactorProviderChallengePassed $event) use ($logger) {
  237. $security = new Security($logger);
  238. $security->twofactorSuccess($event->getUser(), $event->getProvider());
  239. });
  240. $eventDispatcher->addListener(TwoFactorProviderChallengeFailed::class, function (TwoFactorProviderChallengeFailed $event) use ($logger) {
  241. $security = new Security($logger);
  242. $security->twofactorFailed($event->getUser(), $event->getProvider());
  243. });
  244. }
  245. }