mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-22 02:47:27 +08:00
[admin] add username change log
This commit is contained in:
@@ -86,9 +86,7 @@ class Test extends Command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$a = Carbon::parse('2022-08-06 23:08:03');
|
||||
$b = $a->clone()->addHours(1);
|
||||
dd($a, $b);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\System;
|
||||
|
||||
use App\Filament\Resources\System\UsernameChangeLogResource\Pages;
|
||||
use App\Filament\Resources\System\UsernameChangeLogResource\RelationManagers;
|
||||
use App\Models\UsernameChangeLog;
|
||||
use Filament\Forms;
|
||||
use Filament\Resources\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Resources\Table;
|
||||
use Filament\Tables;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
|
||||
class UsernameChangeLogResource extends Resource
|
||||
{
|
||||
protected static ?string $model = UsernameChangeLog::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-pencil-alt';
|
||||
|
||||
protected static ?string $navigationGroup = 'User';
|
||||
|
||||
protected static ?int $navigationSort = 8;
|
||||
|
||||
protected static function getNavigationLabel(): string
|
||||
{
|
||||
return __('admin.sidebar.username_change_log');
|
||||
}
|
||||
|
||||
public static function getBreadcrumb(): string
|
||||
{
|
||||
return self::getNavigationLabel();
|
||||
}
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
//
|
||||
]);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('id')->sortable(),
|
||||
Tables\Columns\TextColumn::make('changeTypeText')->label(__('username-change-log.labels.change_type')),
|
||||
Tables\Columns\TextColumn::make('uid')->searchable(),
|
||||
Tables\Columns\TextColumn::make('user.username')->searchable()->label(__('label.username')),
|
||||
Tables\Columns\TextColumn::make('username_old')->searchable()->label(__('username-change-log.labels.username_old')),
|
||||
Tables\Columns\TextColumn::make('username_new')->searchable()->label(__('username-change-log.labels.username_new')),
|
||||
Tables\Columns\TextColumn::make('operator')->searchable()->label(__('label.operator')),
|
||||
Tables\Columns\TextColumn::make('created_at')->label(__('label.created_at'))->formatStateUsing(fn ($state) => format_datetime($state)),
|
||||
|
||||
])
|
||||
->defaultSort('id', 'desc')
|
||||
->filters([
|
||||
Tables\Filters\SelectFilter::make('change_type')->options(UsernameChangeLog::listChangeType())->label(__('username-change-log.labels.change_type')),
|
||||
])
|
||||
->actions([
|
||||
// Tables\Actions\EditAction::make(),
|
||||
// Tables\Actions\DeleteAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
// Tables\Actions\DeleteBulkAction::make(),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ManageUsernameChangeLogs::route('/'),
|
||||
];
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\System\UsernameChangeLogResource\Pages;
|
||||
|
||||
use App\Filament\Resources\System\UsernameChangeLogResource;
|
||||
use Filament\Pages\Actions;
|
||||
use Filament\Resources\Pages\ManageRecords;
|
||||
|
||||
class ManageUsernameChangeLogs extends ManageRecords
|
||||
{
|
||||
protected ?string $maxContentWidth = 'full';
|
||||
|
||||
protected static string $resource = UsernameChangeLogResource::class;
|
||||
|
||||
protected function getActions(): array
|
||||
{
|
||||
return [
|
||||
// Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,35 @@ namespace App\Models;
|
||||
|
||||
class UsernameChangeLog extends NexusModel
|
||||
{
|
||||
protected $fillable = ['uid', 'username_old', 'username_new', ];
|
||||
protected $fillable = ['uid', 'username_old', 'username_new', 'operator', 'change_type'];
|
||||
|
||||
public $timestamps = true;
|
||||
|
||||
const CHANGE_TYPE_USER = 1;
|
||||
const CHANGE_TYPE_ADMIN = 2;
|
||||
|
||||
public static array $changeTypes = [
|
||||
self::CHANGE_TYPE_USER => ['text' => 'User'],
|
||||
self::CHANGE_TYPE_ADMIN => ['text' => 'Administrator'],
|
||||
];
|
||||
|
||||
public function getChangeTypeTextAttribute()
|
||||
{
|
||||
return nexus_trans('username-change-log.change_type.' . $this->change_type);
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'uid');
|
||||
}
|
||||
|
||||
public static function listChangeType()
|
||||
{
|
||||
$result = [];
|
||||
foreach (self::$changeTypes as $type => $info) {
|
||||
$result[$type] = nexus_trans('username-change-log.change_type.' . $type);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use App\Models\UserBanLog;
|
||||
use App\Models\UserMeta;
|
||||
use App\Models\UsernameChangeLog;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Arr;
|
||||
@@ -359,6 +360,9 @@ class UserRepository extends BaseRepository
|
||||
private function checkPermission($operator, User $user, $minAuthClass = 'authority.prfmanage')
|
||||
{
|
||||
$operator = $this->getUser($operator);
|
||||
if ($operator->id == $user->id) {
|
||||
return;
|
||||
}
|
||||
$classRequire = Setting::get($minAuthClass);
|
||||
if ($operator->class < $classRequire || $operator->class <= $user->class) {
|
||||
throw new InsufficientPermissionException();
|
||||
@@ -396,7 +400,7 @@ class UserRepository extends BaseRepository
|
||||
$user = User::query()->findOrFail($uid, User::$commonFields);
|
||||
if ($metaKey == UserMeta::META_KEY_CHANGE_USERNAME) {
|
||||
NexusDB::transaction(function () use ($user, $meta, $params) {
|
||||
$this->changeUsername($user, $params['username']);
|
||||
$this->changeUsername($user, UsernameChangeLog::CHANGE_TYPE_USER, $user, $params['username']);
|
||||
$meta->delete();
|
||||
clear_user_cache($user->id, $user->passkey);
|
||||
});
|
||||
@@ -406,22 +410,32 @@ class UserRepository extends BaseRepository
|
||||
throw new \InvalidArgumentException("Invalid meta_key: $metaKey");
|
||||
}
|
||||
|
||||
private function changeUsername(User $user, $newUsername): bool
|
||||
private function changeUsername($operator, $changeType, $targetUser, $newUsername): bool
|
||||
{
|
||||
if ($user->username == $newUsername) {
|
||||
$operator = $this->getUser($operator);
|
||||
$targetUser = $this->getUser($targetUser);
|
||||
$this->checkPermission($operator, $targetUser);
|
||||
if ($targetUser->username == $newUsername) {
|
||||
throw new \RuntimeException("New username can not be the same with current username !");
|
||||
}
|
||||
if (!validusername($newUsername)) {
|
||||
throw new \InvalidArgumentException("Invalid username, length must between 4 and 20 characters");
|
||||
$strWidth = mb_strwidth($newUsername);
|
||||
if ($strWidth < 4 || $strWidth > 20) {
|
||||
throw new \InvalidArgumentException("Invalid username, maybe too long or too short");
|
||||
}
|
||||
if (User::query()->where('username', $newUsername)->where('id', '!=', $user->id)->exists()) {
|
||||
if (User::query()->where('username', $newUsername)->where('id', '!=', $targetUser->id)->exists()) {
|
||||
throw new \RuntimeException("Username: $newUsername already exists !");
|
||||
}
|
||||
NexusDB::transaction(function () use ($user, $newUsername) {
|
||||
$oldUsername = $user->username;
|
||||
$user->usernameChangeLogs()->create(['username_old' => $oldUsername, 'username_new' => $newUsername]);
|
||||
$user->username = $newUsername;
|
||||
$user->save();
|
||||
$changeLog = [
|
||||
'uid' => $targetUser->id,
|
||||
'operator' => $operator->username,
|
||||
'change_type' => $changeType,
|
||||
'username_old' => $targetUser->username,
|
||||
'username_new' => $newUsername
|
||||
];
|
||||
NexusDB::transaction(function () use ($operator, $changeType,$targetUser, $changeLog) {
|
||||
$targetUser->usernameChangeLogs()->create($changeLog);
|
||||
$targetUser->username = $changeLog['username_new'];
|
||||
$targetUser->save();
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ return new class extends Migration
|
||||
Schema::create('username_change_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->integer('uid');
|
||||
$table->string('operator');
|
||||
$table->integer('change_type')->nullable(false)->default(0);
|
||||
$table->string('username_old');
|
||||
$table->string('username_new');
|
||||
$table->timestamps();
|
||||
|
||||
@@ -117,6 +117,14 @@ if ($action == "edituser")
|
||||
$subject = sqlesc($lang_modtask_target[get_user_lang($userid)]['msg_username_change']);
|
||||
$msg = sqlesc($lang_modtask_target[get_user_lang($userid)]['msg_your_username_changed_from'].$arr['username'].$lang_modtask_target[get_user_lang($userid)]['msg_to_new'] . $username .$lang_modtask_target[get_user_lang($userid)]['msg_by'].$CURUSER['username']);
|
||||
sql_query("INSERT INTO messages (sender, receiver, subject, msg, added) VALUES(0, $userid, $subject, $msg, $added)") or sqlerr(__FILE__, __LINE__);
|
||||
$changeLog = [
|
||||
'uid' => $arr['id'],
|
||||
'operator' => $CURUSER['username'],
|
||||
'change_type' => \App\Models\UsernameChangeLog::CHANGE_TYPE_ADMIN,
|
||||
'username_old' => $arr['username'],
|
||||
'username_new' => $username,
|
||||
];
|
||||
\App\Models\UsernameChangeLog::query()->create($changeLog);
|
||||
}
|
||||
if ($ori_downloaded != $downloaded){
|
||||
$updateset[] = "downloaded = " . sqlesc($downloaded);
|
||||
|
||||
@@ -22,6 +22,7 @@ return [
|
||||
'download_speed' => 'Download speed',
|
||||
'isp' => 'ISP',
|
||||
'menu' => 'Custom menu',
|
||||
'username_change_log' => 'Username change log',
|
||||
],
|
||||
'resources' => [
|
||||
'agent_allow' => [
|
||||
|
||||
@@ -27,6 +27,7 @@ return [
|
||||
'description' => 'Description',
|
||||
'price' => 'Price',
|
||||
'deadline' => 'Deadline',
|
||||
'operator' => 'Operator',
|
||||
'setting' => [
|
||||
'nav_text' => 'Setting',
|
||||
'backup' => [
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'change_type' => [
|
||||
\App\Models\UsernameChangeLog::CHANGE_TYPE_USER => 'User',
|
||||
\App\Models\UsernameChangeLog::CHANGE_TYPE_ADMIN => 'Administrator',
|
||||
],
|
||||
'labels' => [
|
||||
'username_old' => 'Old username',
|
||||
'username_new' => 'New username',
|
||||
'change_type' => 'Change type',
|
||||
],
|
||||
];
|
||||
@@ -22,6 +22,7 @@ return [
|
||||
'download_speed' => '下行带宽',
|
||||
'isp' => 'ISP',
|
||||
'menu' => '自定义菜单',
|
||||
'username_change_log' => '改名记录',
|
||||
],
|
||||
'resources' => [
|
||||
'agent_allow' => [
|
||||
|
||||
@@ -27,6 +27,7 @@ return [
|
||||
'description' => '描述',
|
||||
'price' => '价格',
|
||||
'deadline' => '截止时间',
|
||||
'operator' => '操作者',
|
||||
'setting' => [
|
||||
'nav_text' => '设置',
|
||||
'backup' => [
|
||||
@@ -213,7 +214,7 @@ return [
|
||||
],
|
||||
'user_meta' => [
|
||||
'meta_keys' => [
|
||||
\App\Models\UserMeta::META_KEY_CHANGE_USERNAME => '更名卡',
|
||||
\App\Models\UserMeta::META_KEY_CHANGE_USERNAME => '改名卡',
|
||||
\App\Models\UserMeta::META_KEY_PERSONALIZED_USERNAME => '彩虹 ID',
|
||||
],
|
||||
],
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'change_type' => [
|
||||
\App\Models\UsernameChangeLog::CHANGE_TYPE_USER => '用户',
|
||||
\App\Models\UsernameChangeLog::CHANGE_TYPE_ADMIN => '管理员',
|
||||
],
|
||||
'labels' => [
|
||||
'username_old' => '旧用户名',
|
||||
'username_new' => '新用户名',
|
||||
'change_type' => '修改类型',
|
||||
],
|
||||
];
|
||||
@@ -22,6 +22,7 @@ return [
|
||||
'download_speed' => '下行帶寬',
|
||||
'isp' => 'ISP',
|
||||
'menu' => '自定義菜單',
|
||||
'username_change_log' => '改名記錄',
|
||||
],
|
||||
'resources' => [
|
||||
'agent_allow' => [
|
||||
|
||||
@@ -27,6 +27,7 @@ return [
|
||||
'description' => '描述',
|
||||
'price' => '價格',
|
||||
'deadline' => '截止時間',
|
||||
'operator' => '操作者',
|
||||
'setting' => [
|
||||
'nav_text' => '設置',
|
||||
'backup' => [
|
||||
@@ -210,7 +211,7 @@ return [
|
||||
],
|
||||
'user_meta' => [
|
||||
'meta_keys' => [
|
||||
\App\Models\UserMeta::META_KEY_CHANGE_USERNAME => '更名卡',
|
||||
\App\Models\UserMeta::META_KEY_CHANGE_USERNAME => '改名卡',
|
||||
\App\Models\UserMeta::META_KEY_PERSONALIZED_USERNAME => '彩虹 ID',
|
||||
],
|
||||
],
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'change_type' => [
|
||||
\App\Models\UsernameChangeLog::CHANGE_TYPE_USER => '用戶',
|
||||
\App\Models\UsernameChangeLog::CHANGE_TYPE_ADMIN => '管理員',
|
||||
],
|
||||
'labels' => [
|
||||
'username_old' => '舊用戶名',
|
||||
'username_new' => '新用戶名',
|
||||
'change_type' => '修改類型',
|
||||
],
|
||||
];
|
||||
Reference in New Issue
Block a user