auditlogger.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Admin_Audit;
  24. use OC\Files\Filesystem;
  25. use OCA\Admin_Audit\Actions\Auth;
  26. use OCA\Admin_Audit\Actions\Files;
  27. use OCA\Admin_Audit\Actions\GroupManagement;
  28. use OCA\Admin_Audit\Actions\Sharing;
  29. use OCA\Admin_Audit\Actions\Trashbin;
  30. use OCA\Admin_Audit\Actions\UserManagement;
  31. use OCA\Admin_Audit\Actions\Versions;
  32. use OCP\IGroupManager;
  33. use OCP\ILogger;
  34. use OCP\IUserSession;
  35. use OCP\Util;
  36. class AuditLogger {
  37. /** @var ILogger */
  38. private $logger;
  39. /** @var IUserSession */
  40. private $userSession;
  41. /** @var IGroupManager */
  42. private $groupManager;
  43. /**
  44. * AuditLogger constructor.
  45. *
  46. * @param ILogger $logger
  47. * @param IUserSession $userSession
  48. * @param IGroupManager $groupManager
  49. */
  50. public function __construct(ILogger $logger,
  51. IUserSession $userSession,
  52. IGroupManager $groupManager) {
  53. $this->logger = $logger;
  54. $this->userSession = $userSession;
  55. $this->groupManager = $groupManager;
  56. }
  57. /**
  58. * register hooks in order to log them
  59. */
  60. public function registerHooks() {
  61. $this->userManagementHooks();
  62. $this->groupHooks();
  63. $this->sharingHooks();
  64. $this->authHooks();
  65. $this->fileHooks();
  66. $this->trashbinHooks();
  67. $this->versionsHooks();
  68. }
  69. /**
  70. * connect to user management hooks
  71. */
  72. private function userManagementHooks() {
  73. $userActions = new UserManagement($this->logger);
  74. Util::connectHook('OC_User', 'post_createUser', $userActions, 'create');
  75. Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete');
  76. $this->userSession->listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']);
  77. }
  78. private function groupHooks() {
  79. $groupActions = new GroupManagement($this->logger);
  80. $this->groupManager->listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']);
  81. $this->groupManager->listen('\OC\Group', 'postAddUser', [$groupActions, 'addUser']);
  82. }
  83. /**
  84. * connect to sharing events
  85. */
  86. private function sharingHooks() {
  87. $shareActions = new Sharing($this->logger);
  88. Util::connectHook('OCP\Share', 'post_shared', $shareActions, 'shared');
  89. Util::connectHook('OCP\Share', 'post_unshare', $shareActions, 'unshare');
  90. Util::connectHook('OCP\Share', 'post_update_permissions', $shareActions, 'updatePermissions');
  91. Util::connectHook('OCP\Share', 'post_update_password', $shareActions, 'updatePassword');
  92. Util::connectHook('OCP\Share', 'post_set_expiration_date', $shareActions, 'updateExpirationDate');
  93. Util::connectHook('OCP\Share', 'share_link_access', $shareActions, 'shareAccessed');
  94. }
  95. /**
  96. * connect to authentication event and related actions
  97. */
  98. private function authHooks() {
  99. $authActions = new Auth($this->logger);
  100. Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt');
  101. Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful');
  102. Util::connectHook('OC_User', 'logout', $authActions, 'logout');
  103. }
  104. /**
  105. * connect to file hooks
  106. */
  107. private function fileHooks() {
  108. $fileActions = new Files($this->logger);
  109. Util::connectHook(
  110. Filesystem::CLASSNAME,
  111. Filesystem::signal_post_rename,
  112. $fileActions,
  113. 'rename'
  114. );
  115. Util::connectHook(
  116. Filesystem::CLASSNAME,
  117. Filesystem::signal_post_create,
  118. $fileActions,
  119. 'create'
  120. );
  121. Util::connectHook(
  122. Filesystem::CLASSNAME,
  123. Filesystem::signal_post_copy,
  124. $fileActions,
  125. 'copy'
  126. );
  127. Util::connectHook(
  128. Filesystem::CLASSNAME,
  129. Filesystem::signal_post_write,
  130. $fileActions,
  131. 'write'
  132. );
  133. Util::connectHook(
  134. Filesystem::CLASSNAME,
  135. Filesystem::signal_post_update,
  136. $fileActions,
  137. 'update'
  138. );
  139. Util::connectHook(
  140. Filesystem::CLASSNAME,
  141. Filesystem::signal_read,
  142. $fileActions,
  143. 'read'
  144. );
  145. Util::connectHook(
  146. Filesystem::CLASSNAME,
  147. Filesystem::signal_delete,
  148. $fileActions,
  149. 'delete'
  150. );
  151. }
  152. public function versionsHooks() {
  153. $versionsActions = new Versions($this->logger);
  154. Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback');
  155. Util::connectHook('\OCP\Versions', 'delete',$versionsActions, 'delete');
  156. }
  157. /**
  158. * connect to trash bin hooks
  159. */
  160. private function trashbinHooks() {
  161. $trashActions = new Trashbin($this->logger);
  162. Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
  163. Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
  164. }
  165. }