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.6KB

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