feat: new feature
This commit is contained in:
@@ -24,14 +24,15 @@ class LogLogin extends Model
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $email
|
||||
* @param ?string $email
|
||||
* @return LengthAwarePaginator
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function getUserLogBy(string $email): LengthAwarePaginator
|
||||
public function getUserLogBy(?string $email): LengthAwarePaginator
|
||||
{
|
||||
return self::query()->where('account', $email)
|
||||
->paginate(request()->get('limit', 10));
|
||||
return static::when($email, function ($query) use ($email){
|
||||
$query->where('account', $email);
|
||||
})->paginate(request()->get('limit', 10));
|
||||
}
|
||||
}
|
||||
|
90
modules/User/Models/LogOperate.php
Normal file
90
modules/User/Models/LogOperate.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Models;
|
||||
|
||||
use Catch\CatchAdmin;
|
||||
use Catch\Traits\DB\BaseOperate;
|
||||
use Catch\Traits\DB\ScopeTrait;
|
||||
use Catch\Traits\DB\Trans;
|
||||
use Illuminate\Contracts\Http\Kernel;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class LogOperate extends Model
|
||||
{
|
||||
use BaseOperate, Trans, ScopeTrait;
|
||||
|
||||
protected $table = 'log_operate';
|
||||
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'module',
|
||||
'action',
|
||||
'params',
|
||||
'ip',
|
||||
'http_method',
|
||||
'http_code',
|
||||
'start_at',
|
||||
'time_taken',
|
||||
'creator_id',
|
||||
'created_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @return void
|
||||
*/
|
||||
public function log(Request $request, Response $response): void
|
||||
{
|
||||
$user = Auth::guard(getGuardName())->user();
|
||||
|
||||
$userModel = getAuthUserModel();
|
||||
|
||||
if (! $user instanceof $userModel) {
|
||||
return;
|
||||
}
|
||||
|
||||
[$module, $controller, $action] = CatchAdmin::parseFromRouteAction();
|
||||
|
||||
$requestStartAt = app(Kernel::class)->requestStartedAt()->getPreciseTimestamp(3);
|
||||
|
||||
$params = $request->all();
|
||||
// 如果参数过长则不记录
|
||||
if (!empty($params)) {
|
||||
if (strlen(\json_encode($params, JSON_UNESCAPED_UNICODE)) > 5000) {
|
||||
$params = [];
|
||||
}
|
||||
}
|
||||
|
||||
$timeTaken = intval(microtime(true) * 1000 - $requestStartAt);
|
||||
|
||||
$this->storeBy([
|
||||
'module' => $module,
|
||||
'action' => $controller . '@' . $action,
|
||||
'creator_id' => $user->id,
|
||||
'http_method' => $request->method(),
|
||||
'http_code' => $response->getStatusCode(),
|
||||
'start_at' => $requestStartAt,
|
||||
'time_taken' => $timeTaken,
|
||||
'ip' => $request->ip(),
|
||||
'params' => \json_encode($params, JSON_UNESCAPED_UNICODE),
|
||||
'created_at' => time()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Attribute
|
||||
*/
|
||||
protected function timeTaken(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn ($value) => $value > 1000 ? intval($value/1000) . 's' : $value . 'ms',
|
||||
);
|
||||
}
|
||||
}
|
@@ -7,7 +7,7 @@ use Catch\Support\Module\ModuleRepository;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Modules\Permissions\Models\PermissionsModel;
|
||||
use Modules\Permissions\Models\Permissions;
|
||||
|
||||
trait UserRelations
|
||||
{
|
||||
@@ -51,7 +51,7 @@ trait UserRelations
|
||||
*/
|
||||
public function withPermissions(): self
|
||||
{
|
||||
/* @var PermissionsModel $permissionsModel */
|
||||
/* @var Permissions $permissionsModel */
|
||||
$permissionsModel = app($this->getPermissionsModel());
|
||||
|
||||
if ($this->isSuperAdmin()) {
|
||||
@@ -61,7 +61,7 @@ trait UserRelations
|
||||
|
||||
app($this->getRolesModel())->with(['permissions'])->get()
|
||||
|
||||
->each(function ($role) use (&$permissions){
|
||||
->each(function ($role) use (&$permissions) {
|
||||
$permissions = $permissions->concat($role->permissions);
|
||||
});
|
||||
|
||||
@@ -87,7 +87,7 @@ trait UserRelations
|
||||
}
|
||||
|
||||
if ($this->isSuperAdmin()) {
|
||||
// return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->withPermissions();
|
||||
@@ -108,34 +108,34 @@ trait UserRelations
|
||||
/**
|
||||
* get RolesModel
|
||||
*
|
||||
* @see \Modules\Permissions\Models\RolesModel
|
||||
* @see \Modules\Permissions\Models\Roles
|
||||
* @return string
|
||||
*/
|
||||
protected function getRolesModel(): string
|
||||
{
|
||||
return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'RolesModel';
|
||||
return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'Roles';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get JobsModel
|
||||
*
|
||||
* @see \Modules\Permissions\Models\JobsModel
|
||||
* @see \Modules\Permissions\Models\Jobs
|
||||
* @return string
|
||||
*/
|
||||
protected function getJobsModel(): string
|
||||
{
|
||||
return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'JobsModel';
|
||||
return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'Jobs';
|
||||
}
|
||||
|
||||
/**
|
||||
* get PermissionsModel
|
||||
*
|
||||
* @see \Modules\Permissions\Models\PermissionsModel
|
||||
* @return string
|
||||
*@see \Modules\Permissions\Models\Permissions
|
||||
*/
|
||||
protected function getPermissionsModel(): string
|
||||
{
|
||||
return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'PermissionsModel';
|
||||
return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'Permissions';
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user