X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/90797001701017bb7ecce11ae39bff1411fbdc35..refs/pull/3918/head:/app/Actions/ActivityLogger.php diff --git a/app/Actions/ActivityLogger.php b/app/Actions/ActivityLogger.php index 3a329387f..6ece47fd5 100644 --- a/app/Actions/ActivityLogger.php +++ b/app/Actions/ActivityLogger.php @@ -2,20 +2,15 @@ namespace BookStack\Actions; -use BookStack\Auth\Permissions\PermissionService; use BookStack\Entities\Models\Entity; +use BookStack\Facades\Theme; use BookStack\Interfaces\Loggable; +use BookStack\Theming\ThemeEvents; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Log; class ActivityLogger { - protected $permissionService; - - public function __construct(PermissionService $permissionService) - { - $this->permissionService = $permissionService; - } - /** * Add a generic activity event to the database. * @@ -34,7 +29,10 @@ class ActivityLogger } $activity->save(); + $this->setNotification($type); + $this->dispatchWebhooks($type, $detail); + Theme::dispatch(ThemeEvents::ACTIVITY_LOGGED, $type, $detail); } /** @@ -42,12 +40,10 @@ class ActivityLogger */ protected function newActivityForUser(string $type): Activity { - $ip = request()->ip() ?? ''; - return (new Activity())->forceFill([ 'type' => strtolower($type), 'user_id' => user()->id, - 'ip' => config('app.env') === 'demo' ? '127.0.0.1' : $ip, + 'ip' => IpFormatter::fromCurrentRequest()->format(), ]); } @@ -68,7 +64,7 @@ class ActivityLogger /** * Flashes a notification message to the session if an appropriate message is available. */ - protected function setNotification(string $type) + protected function setNotification(string $type): void { $notificationTextKey = 'activities.' . $type . '_notification'; if (trans()->has($notificationTextKey)) { @@ -77,6 +73,24 @@ class ActivityLogger } } + /** + * @param string|Loggable $detail + */ + protected function dispatchWebhooks(string $type, $detail): void + { + $webhooks = Webhook::query() + ->whereHas('trackedEvents', function (Builder $query) use ($type) { + $query->where('event', '=', $type) + ->orWhere('event', '=', 'all'); + }) + ->where('active', '=', true) + ->get(); + + foreach ($webhooks as $webhook) { + dispatch(new DispatchWebhookJob($webhook, $type, $detail)); + } + } + /** * Log out a failed login attempt, Providing the given username * as part of the message if the '%u' string is used.