]> BookStack Code Mirror - bookstack/blob - app/Actions/ActivityLogger.php
3a329387f7d19ab1bef05d689c4fbec1c58134d9
[bookstack] / app / Actions / ActivityLogger.php
1 <?php
2
3 namespace BookStack\Actions;
4
5 use BookStack\Auth\Permissions\PermissionService;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Interfaces\Loggable;
8 use Illuminate\Support\Facades\Log;
9
10 class ActivityLogger
11 {
12     protected $permissionService;
13
14     public function __construct(PermissionService $permissionService)
15     {
16         $this->permissionService = $permissionService;
17     }
18
19     /**
20      * Add a generic activity event to the database.
21      *
22      * @param string|Loggable $detail
23      */
24     public function add(string $type, $detail = '')
25     {
26         $detailToStore = ($detail instanceof Loggable) ? $detail->logDescriptor() : $detail;
27
28         $activity = $this->newActivityForUser($type);
29         $activity->detail = $detailToStore;
30
31         if ($detail instanceof Entity) {
32             $activity->entity_id = $detail->id;
33             $activity->entity_type = $detail->getMorphClass();
34         }
35
36         $activity->save();
37         $this->setNotification($type);
38     }
39
40     /**
41      * Get a new activity instance for the current user.
42      */
43     protected function newActivityForUser(string $type): Activity
44     {
45         $ip = request()->ip() ?? '';
46
47         return (new Activity())->forceFill([
48             'type'     => strtolower($type),
49             'user_id'  => user()->id,
50             'ip'       => config('app.env') === 'demo' ? '127.0.0.1' : $ip,
51         ]);
52     }
53
54     /**
55      * Removes the entity attachment from each of its activities
56      * and instead uses the 'extra' field with the entities name.
57      * Used when an entity is deleted.
58      */
59     public function removeEntity(Entity $entity)
60     {
61         $entity->activity()->update([
62             'detail'       => $entity->name,
63             'entity_id'    => null,
64             'entity_type'  => null,
65         ]);
66     }
67
68     /**
69      * Flashes a notification message to the session if an appropriate message is available.
70      */
71     protected function setNotification(string $type)
72     {
73         $notificationTextKey = 'activities.' . $type . '_notification';
74         if (trans()->has($notificationTextKey)) {
75             $message = trans($notificationTextKey);
76             session()->flash('success', $message);
77         }
78     }
79
80     /**
81      * Log out a failed login attempt, Providing the given username
82      * as part of the message if the '%u' string is used.
83      */
84     public function logFailedLogin(string $username)
85     {
86         $message = config('logging.failed_login.message');
87         if (!$message) {
88             return;
89         }
90
91         $message = str_replace('%u', $username, $message);
92         $channel = config('logging.failed_login.channel');
93         Log::channel($channel)->warning($message);
94     }
95 }