]> BookStack Code Mirror - bookstack/blob - app/Activity/Tools/ActivityLogger.php
Comments: Added text for new activity types
[bookstack] / app / Activity / Tools / ActivityLogger.php
1 <?php
2
3 namespace BookStack\Activity\Tools;
4
5 use BookStack\Activity\DispatchWebhookJob;
6 use BookStack\Activity\Models\Activity;
7 use BookStack\Activity\Models\Loggable;
8 use BookStack\Activity\Models\Webhook;
9 use BookStack\App\Model;
10 use BookStack\Entities\Models\Entity;
11 use BookStack\Facades\Theme;
12 use BookStack\Theming\ThemeEvents;
13 use Illuminate\Database\Eloquent\Builder;
14 use Illuminate\Support\Facades\Log;
15
16 class ActivityLogger
17 {
18     /**
19      * Add a generic activity event to the database.
20      */
21     public function add(string $type, $detail = '')
22     {
23         $detailToStore = ($detail instanceof Loggable) ? $detail->logDescriptor() : $detail;
24
25         $activity = $this->newActivityForUser($type);
26         $activity->detail = $detailToStore;
27
28         if ($detail instanceof Entity) {
29             $activity->entity_id = $detail->id;
30             $activity->entity_type = $detail->getMorphClass();
31         }
32
33         $activity->save();
34
35         $this->setNotification($type);
36         $this->dispatchWebhooks($type, $detail);
37         Theme::dispatch(ThemeEvents::ACTIVITY_LOGGED, $type, $detail);
38     }
39
40     /**
41      * Get a new activity instance for the current user.
42      */
43     protected function newActivityForUser(string $type): Activity
44     {
45         return (new Activity())->forceFill([
46             'type'     => strtolower($type),
47             'user_id'  => user()->id,
48             'ip'       => IpFormatter::fromCurrentRequest()->format(),
49         ]);
50     }
51
52     /**
53      * Removes the entity attachment from each of its activities
54      * and instead uses the 'extra' field with the entities name.
55      * Used when an entity is deleted.
56      */
57     public function removeEntity(Entity $entity): void
58     {
59         $entity->activity()->update([
60             'detail'       => $entity->name,
61             'entity_id'    => null,
62             'entity_type'  => null,
63         ]);
64     }
65
66     /**
67      * Flashes a notification message to the session if an appropriate message is available.
68      */
69     protected function setNotification(string $type): void
70     {
71         $notificationTextKey = 'activities.' . $type . '_notification';
72         if (trans()->has($notificationTextKey)) {
73             $message = trans($notificationTextKey);
74             session()->flash('success', $message);
75         }
76     }
77
78     protected function dispatchWebhooks(string $type, string|Loggable $detail): void
79     {
80         $webhooks = Webhook::query()
81             ->whereHas('trackedEvents', function (Builder $query) use ($type) {
82                 $query->where('event', '=', $type)
83                     ->orWhere('event', '=', 'all');
84             })
85             ->where('active', '=', true)
86             ->get();
87
88         foreach ($webhooks as $webhook) {
89             dispatch(new DispatchWebhookJob($webhook, $type, $detail));
90         }
91     }
92
93     /**
94      * Log out a failed login attempt, Providing the given username
95      * as part of the message if the '%u' string is used.
96      */
97     public function logFailedLogin(string $username): void
98     {
99         $message = config('logging.failed_login.message');
100         if (!$message) {
101             return;
102         }
103
104         $message = str_replace('%u', $username, $message);
105         $channel = config('logging.failed_login.channel');
106         Log::channel($channel)->warning($message);
107     }
108 }