3 namespace BookStack\Actions;
5 use BookStack\Auth\Permissions\PermissionService;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Interfaces\Loggable;
8 use Illuminate\Support\Facades\Log;
12 protected $permissionService;
14 public function __construct(PermissionService $permissionService)
16 $this->permissionService = $permissionService;
20 * Add a generic activity event to the database.
22 * @param string|Loggable $detail
24 public function add(string $type, $detail = '')
26 $detailToStore = ($detail instanceof Loggable) ? $detail->logDescriptor() : $detail;
28 $activity = $this->newActivityForUser($type);
29 $activity->detail = $detailToStore;
31 if ($detail instanceof Entity) {
32 $activity->entity_id = $detail->id;
33 $activity->entity_type = $detail->getMorphClass();
37 $this->setNotification($type);
41 * Get a new activity instance for the current user.
43 protected function newActivityForUser(string $type): Activity
45 $ip = request()->ip() ?? '';
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,
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.
59 public function removeEntity(Entity $entity)
61 $entity->activity()->update([
62 'detail' => $entity->name,
64 'entity_type' => null,
69 * Flashes a notification message to the session if an appropriate message is available.
71 protected function setNotification(string $type)
73 $notificationTextKey = 'activities.' . $type . '_notification';
74 if (trans()->has($notificationTextKey)) {
75 $message = trans($notificationTextKey);
76 session()->flash('success', $message);
81 * Log out a failed login attempt, Providing the given username
82 * as part of the message if the '%u' string is used.
84 public function logFailedLogin(string $username)
86 $message = config('logging.failed_login.message');
91 $message = str_replace('%u', $username, $message);
92 $channel = config('logging.failed_login.channel');
93 Log::channel($channel)->warning($message);