1
0

Files.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\BeforeNodeDeletedEvent;
  10. use OCP\Files\Events\Node\BeforeNodeReadEvent;
  11. use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
  12. use OCP\Files\Events\Node\NodeCopiedEvent;
  13. use OCP\Files\Events\Node\NodeCreatedEvent;
  14. use OCP\Files\Events\Node\NodeRenamedEvent;
  15. use OCP\Files\Events\Node\NodeWrittenEvent;
  16. use OCP\Files\InvalidPathException;
  17. use OCP\Files\NotFoundException;
  18. use OCP\Server;
  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. public function read(BeforeNodeReadEvent $event): void {
  31. try {
  32. $node = $event->getNode();
  33. $params = [
  34. 'id' => $node instanceof NonExistingFile ? null : $node->getId(),
  35. 'path' => mb_substr($node->getInternalPath(), 5),
  36. ];
  37. } catch (InvalidPathException|NotFoundException $e) {
  38. Server::get(LoggerInterface::class)->error(
  39. 'Exception thrown in file read: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  40. );
  41. return;
  42. }
  43. $this->log(
  44. 'File with id "%s" accessed: "%s"',
  45. $params,
  46. array_keys($params)
  47. );
  48. }
  49. /**
  50. * Logs rename actions of files
  51. */
  52. public function beforeRename(BeforeNodeRenamedEvent $event): void {
  53. try {
  54. $source = $event->getSource();
  55. $this->renamedNodes[$source->getId()] = $source;
  56. } catch (InvalidPathException|NotFoundException $e) {
  57. Server::get(LoggerInterface::class)->error(
  58. 'Exception thrown in file rename: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  59. );
  60. return;
  61. }
  62. }
  63. /**
  64. * Logs rename actions of files
  65. */
  66. public function afterRename(NodeRenamedEvent $event): void {
  67. try {
  68. $target = $event->getTarget();
  69. $originalSource = $this->renamedNodes[$target->getId()];
  70. $params = [
  71. 'newid' => $target->getId(),
  72. 'oldpath' => mb_substr($originalSource->getInternalPath(), 5),
  73. 'newpath' => mb_substr($target->getInternalPath(), 5),
  74. ];
  75. } catch (InvalidPathException|NotFoundException $e) {
  76. Server::get(LoggerInterface::class)->error(
  77. 'Exception thrown in file rename: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  78. );
  79. return;
  80. }
  81. $this->log(
  82. 'File renamed with id "%s" from "%s" to "%s"',
  83. $params,
  84. array_keys($params)
  85. );
  86. }
  87. /**
  88. * Logs creation of files
  89. */
  90. public function create(NodeCreatedEvent $event): void {
  91. try {
  92. $params = [
  93. 'id' => $event->getNode()->getId(),
  94. 'path' => mb_substr($event->getNode()->getInternalPath(), 5),
  95. ];
  96. } catch (InvalidPathException|NotFoundException $e) {
  97. Server::get(LoggerInterface::class)->error(
  98. 'Exception thrown in file create: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  99. );
  100. return;
  101. }
  102. if ($params['path'] === '/' || $params['path'] === '') {
  103. return;
  104. }
  105. $this->log(
  106. 'File with id "%s" created: "%s"',
  107. $params,
  108. array_keys($params)
  109. );
  110. }
  111. /**
  112. * Logs copying of files
  113. */
  114. public function copy(NodeCopiedEvent $event): void {
  115. try {
  116. $params = [
  117. 'oldid' => $event->getSource()->getId(),
  118. 'newid' => $event->getTarget()->getId(),
  119. 'oldpath' => mb_substr($event->getSource()->getInternalPath(), 5),
  120. 'newpath' => mb_substr($event->getTarget()->getInternalPath(), 5),
  121. ];
  122. } catch (InvalidPathException|NotFoundException $e) {
  123. Server::get(LoggerInterface::class)->error(
  124. 'Exception thrown in file copy: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  125. );
  126. return;
  127. }
  128. $this->log(
  129. 'File id copied from: "%s" to "%s", path from "%s" to "%s"',
  130. $params,
  131. array_keys($params)
  132. );
  133. }
  134. /**
  135. * Logs writing of files
  136. */
  137. public function write(NodeWrittenEvent $event): void {
  138. $node = $event->getNode();
  139. try {
  140. $params = [
  141. 'id' => $node->getId(),
  142. 'path' => mb_substr($node->getInternalPath(), 5),
  143. ];
  144. } catch (InvalidPathException|NotFoundException $e) {
  145. Server::get(LoggerInterface::class)->error(
  146. 'Exception thrown in file write: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  147. );
  148. return;
  149. }
  150. if ($params['path'] === '/' || $params['path'] === '') {
  151. return;
  152. }
  153. $this->log(
  154. 'File with id "%s" written to: "%s"',
  155. $params,
  156. array_keys($params)
  157. );
  158. }
  159. /**
  160. * Logs deletions of files
  161. */
  162. public function delete(BeforeNodeDeletedEvent $event): void {
  163. try {
  164. $params = [
  165. 'id' => $event->getNode()->getId(),
  166. 'path' => mb_substr($event->getNode()->getInternalPath(), 5),
  167. ];
  168. } catch (InvalidPathException|NotFoundException $e) {
  169. Server::get(LoggerInterface::class)->error(
  170. 'Exception thrown in file delete: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  171. );
  172. return;
  173. }
  174. $this->log(
  175. 'File with id "%s" deleted: "%s"',
  176. $params,
  177. array_keys($params)
  178. );
  179. }
  180. }