1
0

Application.php 10.0 KB

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