-<?php namespace BookStack\Actions;
+<?php
+
+namespace BookStack\Actions;
use BookStack\Auth\Permissions\PermissionService;
use BookStack\Auth\User;
-use BookStack\Entities\Chapter;
-use BookStack\Entities\Entity;
-use BookStack\Entities\Page;
+use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Chapter;
+use BookStack\Entities\Models\Entity;
+use BookStack\Entities\Models\Page;
+use BookStack\Interfaces\Loggable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Log;
$this->setNotification($type);
}
+ /**
+ * Add a generic activity event to the database.
+ *
+ * @param string|Loggable $detail
+ */
+ public function add(string $type, $detail = '')
+ {
+ if ($detail instanceof Loggable) {
+ $detail = $detail->logDescriptor();
+ }
+
+ $activity = $this->newActivityForUser($type);
+ $activity->detail = $detail;
+ $activity->save();
+ $this->setNotification($type);
+ }
+
/**
* Get a new activity instance for the current user.
*/
protected function newActivityForUser(string $type): Activity
{
+ $ip = request()->ip() ?? '';
+
return $this->activity->newInstance()->forceFill([
'type' => strtolower($type),
- 'user_id' => user()->id,
+ 'user_id' => user()->id,
+ 'ip' => config('app.env') === 'demo' ? '127.0.0.1' : $ip,
]);
}
{
$entity->activity()->update([
'detail' => $entity->name,
- 'entity_id' => null,
- 'entity_type' => null,
+ 'entity_id' => null,
+ 'entity_type' => null,
]);
}
public function latest(int $count = 20, int $page = 0): array
{
$activityList = $this->permissionService
- ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
+ ->filterRestrictedEntityRelations($this->activity->newQuery(), 'activities', 'entity_id', 'entity_type')
->orderBy('created_at', 'desc')
->with(['user', 'entity'])
->skip($count * $page)
*/
public function entityActivity(Entity $entity, int $count = 20, int $page = 1): array
{
- /** @var [string => int[]] $queryIds */
+ /** @var array<string, int[]> $queryIds */
$queryIds = [$entity->getMorphClass() => [$entity->id]];
- if ($entity->isA('book')) {
- $queryIds[(new Chapter)->getMorphClass()] = $entity->chapters()->visible()->pluck('id');
+ if ($entity instanceof Book) {
+ $queryIds[(new Chapter())->getMorphClass()] = $entity->chapters()->visible()->pluck('id');
}
- if ($entity->isA('book') || $entity->isA('chapter')) {
- $queryIds[(new Page)->getMorphClass()] = $entity->pages()->visible()->pluck('id');
+ if ($entity instanceof Book || $entity instanceof Chapter) {
+ $queryIds[(new Page())->getMorphClass()] = $entity->pages()->visible()->pluck('id');
}
$query = $this->activity->newQuery();
public function userActivity(User $user, int $count = 20, int $page = 0): array
{
$activityList = $this->permissionService
- ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
+ ->filterRestrictedEntityRelations($this->activity->newQuery(), 'activities', 'entity_id', 'entity_type')
->orderBy('created_at', 'desc')
->where('user_id', '=', $user->id)
->skip($count * $page)
/**
* Filters out similar activity.
+ *
* @param Activity[] $activities
+ *
* @return array
*/
protected function filterSimilar(iterable $activities): array
return;
}
- $message = str_replace("%u", $username, $message);
+ $message = str_replace('%u', $username, $message);
$channel = config('logging.failed_login.channel');
Log::channel($channel)->warning($message);
}