You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Application.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\AdminAudit\AppInfo;
  29. use OC\Files\Filesystem;
  30. use OC\Files\Node\File;
  31. use OC\Group\Manager;
  32. use OC\User\Session;
  33. use OCA\AdminAudit\Actions\AppManagement;
  34. use OCA\AdminAudit\Actions\Auth;
  35. use OCA\AdminAudit\Actions\Console;
  36. use OCA\AdminAudit\Actions\Files;
  37. use OCA\AdminAudit\Actions\GroupManagement;
  38. use OCA\AdminAudit\Actions\Security;
  39. use OCA\AdminAudit\Actions\Sharing;
  40. use OCA\AdminAudit\Actions\Trashbin;
  41. use OCA\AdminAudit\Actions\UserManagement;
  42. use OCA\AdminAudit\Actions\Versions;
  43. use OCP\App\ManagerEvent;
  44. use OCP\AppFramework\App;
  45. use OCP\Authentication\TwoFactorAuth\IProvider;
  46. use OCP\Console\ConsoleEvent;
  47. use OCP\IGroupManager;
  48. use OCP\ILogger;
  49. use OCP\IPreview;
  50. use OCP\IUserSession;
  51. use OCP\Share;
  52. use OCP\Util;
  53. use Symfony\Component\EventDispatcher\GenericEvent;
  54. class Application extends App {
  55. /** @var ILogger */
  56. protected $logger;
  57. public function __construct() {
  58. parent::__construct('admin_audit');
  59. $this->initLogger();
  60. }
  61. public function initLogger() {
  62. $c = $this->getContainer()->getServer();
  63. $config = $c->getConfig();
  64. $default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
  65. $logFile = $config->getAppValue('admin_audit', 'logfile', $default);
  66. if($logFile === null) {
  67. $this->logger = $c->getLogger();
  68. return;
  69. }
  70. $this->logger = $c->getLogFactory()->getCustomLogger($logFile);
  71. }
  72. public function register() {
  73. $this->registerHooks();
  74. }
  75. /**
  76. * Register hooks in order to log them
  77. */
  78. protected function registerHooks() {
  79. $this->userManagementHooks();
  80. $this->groupHooks();
  81. $this->authHooks();
  82. $this->consoleHooks();
  83. $this->appHooks();
  84. $this->sharingHooks();
  85. $this->fileHooks();
  86. $this->trashbinHooks();
  87. $this->versionsHooks();
  88. $this->securityHooks();
  89. }
  90. protected function userManagementHooks() {
  91. $userActions = new UserManagement($this->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. /** @var IUserSession|Session $userSession */
  96. $userSession = $this->getContainer()->getServer()->getUserSession();
  97. $userSession->listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']);
  98. $userSession->listen('\OC\User', 'assignedUserId', [$userActions, 'assign']);
  99. $userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']);
  100. }
  101. protected function groupHooks() {
  102. $groupActions = new GroupManagement($this->logger);
  103. /** @var IGroupManager|Manager $groupManager */
  104. $groupManager = $this->getContainer()->getServer()->getGroupManager();
  105. $groupManager->listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']);
  106. $groupManager->listen('\OC\Group', 'postAddUser', [$groupActions, 'addUser']);
  107. $groupManager->listen('\OC\Group', 'postDelete', [$groupActions, 'deleteGroup']);
  108. $groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']);
  109. }
  110. protected function sharingHooks() {
  111. $shareActions = new Sharing($this->logger);
  112. Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared');
  113. Util::connectHook(Share::class, 'post_unshare', $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. protected function authHooks() {
  120. $authActions = new Auth($this->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. protected function appHooks() {
  126. $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
  127. $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) {
  128. $appActions = new AppManagement($this->logger);
  129. $appActions->enableApp($event->getAppID());
  130. });
  131. $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function(ManagerEvent $event) {
  132. $appActions = new AppManagement($this->logger);
  133. $appActions->enableAppForGroups($event->getAppID(), $event->getGroups());
  134. });
  135. $eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function(ManagerEvent $event) {
  136. $appActions = new AppManagement($this->logger);
  137. $appActions->disableApp($event->getAppID());
  138. });
  139. }
  140. protected function consoleHooks() {
  141. $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
  142. $eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function(ConsoleEvent $event) {
  143. $appActions = new Console($this->logger);
  144. $appActions->runCommand($event->getArguments());
  145. });
  146. }
  147. protected function fileHooks() {
  148. $fileActions = new Files($this->logger);
  149. $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
  150. $eventDispatcher->addListener(
  151. IPreview::EVENT,
  152. function(GenericEvent $event) use ($fileActions) {
  153. /** @var File $file */
  154. $file = $event->getSubject();
  155. $fileActions->preview([
  156. 'path' => substr($file->getInternalPath(), 5),
  157. 'width' => $event->getArguments()['width'],
  158. 'height' => $event->getArguments()['height'],
  159. 'crop' => $event->getArguments()['crop'],
  160. 'mode' => $event->getArguments()['mode']
  161. ]);
  162. }
  163. );
  164. Util::connectHook(
  165. Filesystem::CLASSNAME,
  166. Filesystem::signal_post_rename,
  167. $fileActions,
  168. 'rename'
  169. );
  170. Util::connectHook(
  171. Filesystem::CLASSNAME,
  172. Filesystem::signal_post_create,
  173. $fileActions,
  174. 'create'
  175. );
  176. Util::connectHook(
  177. Filesystem::CLASSNAME,
  178. Filesystem::signal_post_copy,
  179. $fileActions,
  180. 'copy'
  181. );
  182. Util::connectHook(
  183. Filesystem::CLASSNAME,
  184. Filesystem::signal_post_write,
  185. $fileActions,
  186. 'write'
  187. );
  188. Util::connectHook(
  189. Filesystem::CLASSNAME,
  190. Filesystem::signal_post_update,
  191. $fileActions,
  192. 'update'
  193. );
  194. Util::connectHook(
  195. Filesystem::CLASSNAME,
  196. Filesystem::signal_read,
  197. $fileActions,
  198. 'read'
  199. );
  200. Util::connectHook(
  201. Filesystem::CLASSNAME,
  202. Filesystem::signal_delete,
  203. $fileActions,
  204. 'delete'
  205. );
  206. }
  207. protected function versionsHooks() {
  208. $versionsActions = new Versions($this->logger);
  209. Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback');
  210. Util::connectHook('\OCP\Versions', 'delete',$versionsActions, 'delete');
  211. }
  212. protected function trashbinHooks() {
  213. $trashActions = new Trashbin($this->logger);
  214. Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
  215. Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
  216. }
  217. protected function securityHooks() {
  218. $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
  219. $eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function(GenericEvent $event) {
  220. $security = new Security($this->logger);
  221. $security->twofactorSuccess($event->getSubject(), $event->getArguments());
  222. });
  223. $eventDispatcher->addListener(IProvider::EVENT_FAILED, function(GenericEvent $event) {
  224. $security = new Security($this->logger);
  225. $security->twofactorFailed($event->getSubject(), $event->getArguments());
  226. });
  227. }
  228. }