Files.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\AdminAudit\Actions;
  8. use OC\Files\Node\NonExistingFile;
  9. use OCP\Files\Events\Node\BeforeNodeReadEvent;
  10. use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
  11. use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
  12. use OCP\Files\Events\Node\NodeCopiedEvent;
  13. use OCP\Files\Events\Node\NodeCreatedEvent;
  14. use OCP\Files\Events\Node\NodeDeletedEvent;
  15. use OCP\Files\Events\Node\NodeRenamedEvent;
  16. use OCP\Files\Events\Node\NodeWrittenEvent;
  17. use OCP\Files\InvalidPathException;
  18. use OCP\Files\NotFoundException;
  19. use Psr\Log\LoggerInterface;
  20. /**
  21. * Class Files logs the actions to files
  22. *
  23. * @package OCA\AdminAudit\Actions
  24. */
  25. class Files extends Action {
  26. private array $renamedNodes = [];
  27. /**
  28. * Logs file read actions
  29. *
  30. * @param BeforeNodeReadEvent $event
  31. */
  32. public function read(BeforeNodeReadEvent $event): void {
  33. try {
  34. $node = $event->getNode();
  35. $params = [
  36. 'id' => $node instanceof NonExistingFile ? null : $node->getId(),
  37. 'path' => mb_substr($node->getInternalPath(), 5),
  38. ];
  39. } catch (InvalidPathException|NotFoundException $e) {
  40. \OCP\Server::get(LoggerInterface::class)->error(
  41. 'Exception thrown in file read: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  42. );
  43. return;
  44. }
  45. $this->log(
  46. 'File with id "%s" accessed: "%s"',
  47. $params,
  48. array_keys($params)
  49. );
  50. }
  51. /**
  52. * Logs rename actions of files
  53. *
  54. * @param BeforeNodeRenamedEvent $event
  55. */
  56. public function beforeRename(BeforeNodeRenamedEvent $event): void {
  57. try {
  58. $source = $event->getSource();
  59. $this->renamedNodes[$source->getId()] = $source;
  60. } catch (InvalidPathException|NotFoundException $e) {
  61. \OCP\Server::get(LoggerInterface::class)->error(
  62. 'Exception thrown in file rename: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  63. );
  64. return;
  65. }
  66. }
  67. /**
  68. * Logs rename actions of files
  69. *
  70. * @param NodeRenamedEvent $event
  71. */
  72. public function afterRename(NodeRenamedEvent $event): void {
  73. try {
  74. $target = $event->getTarget();
  75. $originalSource = $this->renamedNodes[$target->getId()];
  76. $params = [
  77. 'newid' => $target->getId(),
  78. 'oldpath' => mb_substr($originalSource->getInternalPath(), 5),
  79. 'newpath' => mb_substr($target->getInternalPath(), 5),
  80. ];
  81. } catch (InvalidPathException|NotFoundException $e) {
  82. \OCP\Server::get(LoggerInterface::class)->error(
  83. 'Exception thrown in file rename: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  84. );
  85. return;
  86. }
  87. $this->log(
  88. 'File renamed with id "%s" from "%s" to "%s"',
  89. $params,
  90. array_keys($params)
  91. );
  92. }
  93. /**
  94. * Logs creation of files
  95. *
  96. * @param NodeCreatedEvent $event
  97. */
  98. public function create(NodeCreatedEvent $event): void {
  99. try {
  100. $params = [
  101. 'id' => $event->getNode()->getId(),
  102. 'path' => mb_substr($event->getNode()->getInternalPath(), 5),
  103. ];
  104. } catch (InvalidPathException|NotFoundException $e) {
  105. \OCP\Server::get(LoggerInterface::class)->error(
  106. 'Exception thrown in file create: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  107. );
  108. return;
  109. }
  110. if ($params['path'] === '/' || $params['path'] === '') {
  111. return;
  112. }
  113. $this->log(
  114. 'File with id "%s" created: "%s"',
  115. $params,
  116. array_keys($params)
  117. );
  118. }
  119. /**
  120. * Logs copying of files
  121. *
  122. * @param NodeCopiedEvent $event
  123. */
  124. public function copy(NodeCopiedEvent $event): void {
  125. try {
  126. $params = [
  127. 'oldid' => $event->getSource()->getId(),
  128. 'newid' => $event->getTarget()->getId(),
  129. 'oldpath' => mb_substr($event->getSource()->getInternalPath(), 5),
  130. 'newpath' => mb_substr($event->getTarget()->getInternalPath(), 5),
  131. ];
  132. } catch (InvalidPathException|NotFoundException $e) {
  133. \OCP\Server::get(LoggerInterface::class)->error(
  134. 'Exception thrown in file copy: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  135. );
  136. return;
  137. }
  138. $this->log(
  139. 'File id copied from: "%s" to "%s", path from "%s" to "%s"',
  140. $params,
  141. array_keys($params)
  142. );
  143. }
  144. /**
  145. * Logs writing of files
  146. *
  147. * @param BeforeNodeWrittenEvent $event
  148. */
  149. public function write(BeforeNodeWrittenEvent $event): void {
  150. $node = $event->getNode();
  151. try {
  152. $params = [
  153. 'id' => $node instanceof NonExistingFile ? null : $node->getId(),
  154. 'path' => mb_substr($node->getInternalPath(), 5),
  155. ];
  156. } catch (InvalidPathException|NotFoundException $e) {
  157. \OCP\Server::get(LoggerInterface::class)->error(
  158. 'Exception thrown in file write: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  159. );
  160. return;
  161. }
  162. if ($params['path'] === '/' || $params['path'] === '') {
  163. return;
  164. }
  165. $this->log(
  166. 'File with id "%s" written to: "%s"',
  167. $params,
  168. array_keys($params)
  169. );
  170. }
  171. /**
  172. * Logs update of files
  173. *
  174. * @param NodeWrittenEvent $event
  175. */
  176. public function update(NodeWrittenEvent $event): void {
  177. try {
  178. $params = [
  179. 'id' => $event->getNode()->getId(),
  180. 'path' => mb_substr($event->getNode()->getInternalPath(), 5),
  181. ];
  182. } catch (InvalidPathException|NotFoundException $e) {
  183. \OCP\Server::get(LoggerInterface::class)->error(
  184. 'Exception thrown in file update: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  185. );
  186. return;
  187. }
  188. $this->log(
  189. 'File with id "%s" updated: "%s"',
  190. $params,
  191. array_keys($params)
  192. );
  193. }
  194. /**
  195. * Logs deletions of files
  196. *
  197. * @param NodeDeletedEvent $event
  198. */
  199. public function delete(NodeDeletedEvent $event): void {
  200. try {
  201. $params = [
  202. 'id' => $event->getNode()->getId(),
  203. 'path' => mb_substr($event->getNode()->getInternalPath(), 5),
  204. ];
  205. } catch (InvalidPathException|NotFoundException $e) {
  206. \OCP\Server::get(LoggerInterface::class)->error(
  207. 'Exception thrown in file delete: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  208. );
  209. return;
  210. }
  211. $this->log(
  212. 'File with id "%s" deleted: "%s"',
  213. $params,
  214. array_keys($params)
  215. );
  216. }
  217. }