user ban log

This commit is contained in:
xiaomlove
2021-05-12 13:45:00 +08:00
parent 02d7eb4e93
commit 70f1f31dcc
18 changed files with 107 additions and 217 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ class InviteResource extends JsonResource
return [ return [
'id' => $this->id, 'id' => $this->id,
'inviter' => $this->inviter, 'inviter' => $this->inviter,
'invitee' => $this->inviter, 'invitee' => $this->invitee,
'hash' => $this->hash, 'hash' => $this->hash,
'time_invited' => $this->time_invited, 'time_invited' => $this->time_invited,
'valid' => $this->valid, 'valid' => $this->valid,
+1 -1
View File
@@ -32,7 +32,7 @@ class UserResource extends JsonResource
'seedtime_text' => mkprettytime($this->seedtime), 'seedtime_text' => mkprettytime($this->seedtime),
'leechtime' => $this->leechtime, 'leechtime' => $this->leechtime,
'leechtime_text' => mkprettytime($this->leechtime), 'leechtime_text' => mkprettytime($this->leechtime),
'invitee_code' => new InviteResource($this->whenLoaded('invitee_code')), 'inviter' => new UserResource($this->whenLoaded('inviter')),
]; ];
} }
} }
+7 -1
View File
@@ -103,7 +103,8 @@ class User extends Authenticatable
public static $commonFields = [ public static $commonFields = [
'id', 'username', 'email', 'class', 'status', 'added', 'avatar', 'id', 'username', 'email', 'class', 'status', 'added', 'avatar',
'uploaded', 'downloaded', 'seedbonus', 'seedtime', 'leechtime' 'uploaded', 'downloaded', 'seedbonus', 'seedtime', 'leechtime',
'invited_by',
]; ];
public function checkIsNormal(array $fields = ['status', 'enabled']) public function checkIsNormal(array $fields = ['status', 'enabled'])
@@ -139,4 +140,9 @@ class User extends Authenticatable
return $this->hasOne(Invite::class, 'invitee_register_uid'); return $this->hasOne(Invite::class, 'invitee_register_uid');
} }
public function inviter()
{
return $this->belongsTo(User::class, 'invited_by');
}
} }
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace App\Models;
class UserBanLog extends NexusModel
{
protected $table = 'user_ban_logs';
}
+33 -4
View File
@@ -9,6 +9,7 @@ use App\Models\Message;
use App\Models\Setting; use App\Models\Setting;
use App\Models\Torrent; use App\Models\Torrent;
use App\Models\User; use App\Models\User;
use App\Models\UserBanLog;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
@@ -502,6 +503,7 @@ class ExamRepository extends BaseRepository
$now = Carbon::now()->toDateTimeString(); $now = Carbon::now()->toDateTimeString();
$examUserTable = (new ExamUser())->getTable(); $examUserTable = (new ExamUser())->getTable();
$examTable = (new Exam())->getTable(); $examTable = (new Exam())->getTable();
$userTable = (new User())->getTable();
$baseQuery = ExamUser::query() $baseQuery = ExamUser::query()
->join($examTable, "$examUserTable.exam_id", "=", "$examTable.id") ->join($examTable, "$examUserTable.exam_id", "=", "$examTable.id")
->where("$examUserTable.status", ExamUser::STATUS_NORMAL) ->where("$examUserTable.status", ExamUser::STATUS_NORMAL)
@@ -519,6 +521,7 @@ class ExamRepository extends BaseRepository
$size = 100; $size = 100;
$minId = 0; $minId = 0;
$result = 0; $result = 0;
while (true) { while (true) {
$logPrefix = sprintf('[%s], size: %s', __FUNCTION__, $size); $logPrefix = sprintf('[%s], size: %s', __FUNCTION__, $size);
$examUsers = (clone $baseQuery)->where("$examUserTable.id", ">", $minId)->limit($size)->get(); $examUsers = (clone $baseQuery)->where("$examUserTable.id", ">", $minId)->limit($size)->get();
@@ -530,11 +533,12 @@ class ExamRepository extends BaseRepository
} }
$result += $examUsers->count(); $result += $examUsers->count();
$now = Carbon::now()->toDateTimeString(); $now = Carbon::now()->toDateTimeString();
$examUserIdArr = $uidToDisable = $messageToSend = []; $examUserIdArr = $uidToDisable = $messageToSend = $userBanLog = $userModcommentUpdate = [];
foreach ($examUsers as $examUser) { foreach ($examUsers as $examUser) {
$minId = $examUser->id; $minId = $examUser->id;
$examUserIdArr[] = $examUser->id; $examUserIdArr[] = $examUser->id;
$uid = $examUser->uid; $uid = $examUser->uid;
$exam = $examUser->exam;
$currentLogPrefix = sprintf("$logPrefix, user: %s, exam: %s, examUser: %s", $uid, $examUser->exam_id, $examUser->id); $currentLogPrefix = sprintf("$logPrefix, user: %s, exam: %s, examUser: %s", $uid, $examUser->exam_id, $examUser->id);
$locale = $examUser->user->locale; $locale = $examUser->user->locale;
if ($examUser->is_done) { if ($examUser->is_done) {
@@ -547,10 +551,26 @@ class ExamRepository extends BaseRepository
$msgTransKey = 'exam.checkout_not_pass_message_content'; $msgTransKey = 'exam.checkout_not_pass_message_content';
//ban user //ban user
$uidToDisable[] = $uid; $uidToDisable[] = $uid;
$userModcomment = nexus_trans('exam.ban_user_modcomment', [
'exam_name' => $exam->name,
'begin' => $examUser->begin,
'end' => $examUser->end
], $locale);
$userModcommentUpdate[] = sprintf("when `id` = %s then concat_ws('\n', modcomment, '%s')", $uid, $userModcomment);
$banLogReason = nexus_trans('exam.ban_log_reason', [
'exam_name' => $exam->name,
'begin' => $exam->begin,
'end' => $exam->end,
], $locale);
$userBanLog[] = [
'uid' => $uid,
'username' => $examUser->user->username,
'reason' => $banLogReason,
];
} }
$subject = nexus_trans($subjectTransKey, [], $locale); $subject = nexus_trans($subjectTransKey, [], $locale);
$msg = nexus_trans($msgTransKey, [ $msg = nexus_trans($msgTransKey, [
'exam_name' => $examUser->exam->name, 'exam_name' => $exam->name,
'begin' => $examUser->begin, 'begin' => $examUser->begin,
'end' => $examUser->end 'end' => $examUser->end
], $locale); ], $locale);
@@ -561,11 +581,20 @@ class ExamRepository extends BaseRepository
'msg' => $msg 'msg' => $msg
]; ];
} }
DB::transaction(function () use ($uidToDisable, $messageToSend, $examUserIdArr) { DB::transaction(function () use ($uidToDisable, $messageToSend, $examUserIdArr, $userBanLog, $userModcommentUpdate, $userTable, $logPrefix) {
ExamUser::query()->whereIn('id', $examUserIdArr)->update(['status' => ExamUser::STATUS_FINISHED]); ExamUser::query()->whereIn('id', $examUserIdArr)->update(['status' => ExamUser::STATUS_FINISHED]);
Message::query()->insert($messageToSend); Message::query()->insert($messageToSend);
if (!empty($uidToDisable)) { if (!empty($uidToDisable)) {
User::query()->whereIn('id', $uidToDisable)->update(['enabled' => User::ENABLED_NO]); $uidStr = implode(', ', $uidToDisable);
$sql = sprintf(
'update %s set enabled = %s, set modcomment = case when %s end where id in (%s)',
$userTable, User::ENABLED_NO, implode(' ', $userModcommentUpdate), $uidStr
);
$updateResult = DB::update($sql);
do_log(sprintf("$logPrefix, disable %s users: %s, sql: %s, updateResult: %s", count($uidToDisable), $uidStr, $sql, $updateResult));
}
if (!empty($userBanLog)) {
UserBanLog::query()->insert($userBanLog);
} }
}); });
} }
+5 -1
View File
@@ -6,6 +6,7 @@ use App\Http\Resources\UserResource;
use App\Models\ExamUser; use App\Models\ExamUser;
use App\Models\Setting; use App\Models\Setting;
use App\Models\User; use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
class UserRepository extends BaseRepository class UserRepository extends BaseRepository
{ {
@@ -25,7 +26,10 @@ class UserRepository extends BaseRepository
public function getDetail($id) public function getDetail($id)
{ {
$user = User::query()->with(['invitee_code'])->findOrFail($id, User::$commonFields); $with = [
'inviter' => function (Builder $query) {return $query->select(User::$commonFields);}
];
$user = User::query()->with($with)->findOrFail($id, User::$commonFields);
$userResource = new UserResource($user); $userResource = new UserResource($user);
$baseInfo = $userResource->response()->getData(true)['data']; $baseInfo = $userResource->response()->getData(true)['data'];
@@ -1,36 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFailedJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('failed_jobs');
}
}
@@ -1,36 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePersonalAccessTokensTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->bigIncrements('id');
$table->morphs('tokenable');
$table->string('name');
$table->string('token', 64)->unique();
$table->text('abilities')->nullable();
$table->timestamp('last_used_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('personal_access_tokens');
}
}
@@ -1,40 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateExamsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('exams', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->dateTime('begin')->nullable();
$table->dateTime('end')->nullable();
$table->integer('duration')->default(0);
$table->text('filters')->nullable();
$table->text('indexes');
$table->tinyInteger('status')->default(0);
$table->tinyInteger('is_discovered')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('exams');
}
}
@@ -1,38 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateExamProgressTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('exam_progress', function (Blueprint $table) {
$table->id();
$table->integer('exam_user_id')->index();
$table->integer('exam_id')->index();
$table->integer('uid')->index();
$table->integer('torrent_id');
$table->integer('index');
$table->bigInteger('value');
$table->timestamps();
$table->index('created_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('exam_progress');
}
}
@@ -1,38 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateExamUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('exam_users', function (Blueprint $table) {
$table->id();
$table->integer('uid')->index();
$table->integer('exam_id')->index();
$table->integer('status')->default(0);
$table->dateTime('begin')->nullable();
$table->dateTime('end')->nullable();
$table->text('progress')->nullable();
$table->tinyInteger('is_done')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('exam_users');
}
}
+10 -1
View File
@@ -61,6 +61,7 @@ if ($action == "edituser")
stderr("Error", "You have no permission to change user's class to ".get_user_class_name($class,false,false,true).". BTW, how do you get here?"); stderr("Error", "You have no permission to change user's class to ".get_user_class_name($class,false,false,true).". BTW, how do you get here?");
$res = sql_query("SELECT * FROM users WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); $res = sql_query("SELECT * FROM users WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_assoc($res) or puke(); $arr = mysql_fetch_assoc($res) or puke();
$user = \App\Models\User::query()->findOrFail($userid);
$curenabled = $arr["enabled"]; $curenabled = $arr["enabled"];
$curparked = $arr["parked"]; $curparked = $arr["parked"];
@@ -83,6 +84,7 @@ if ($action == "edituser")
$updateset[] = "support = " . sqlesc($support); $updateset[] = "support = " . sqlesc($support);
$updateset[] = "supportfor = " . sqlesc($supportfor); $updateset[] = "supportfor = " . sqlesc($supportfor);
$updateset[] = "supportlang = ".sqlesc($supportlang); $updateset[] = "supportlang = ".sqlesc($supportlang);
$banLog = [];
if(get_user_class()<=$cruprfmanage_class) if(get_user_class()<=$cruprfmanage_class)
{ {
@@ -254,6 +256,11 @@ if ($action == "edituser")
} }
} else { } else {
$modcomment = date("Y-m-d") . " - Disabled by " . $CURUSER['username']. ".\n". $modcomment; $modcomment = date("Y-m-d") . " - Disabled by " . $CURUSER['username']. ".\n". $modcomment;
$banLog = [
'uid' => $userid,
'username' => $user->username,
'reason' => nexus_trans('user.edit_ban_reason', [], $user->locale),
];
} }
} }
if ($arr['noad'] != $noad){ if ($arr['noad'] != $noad){
@@ -333,7 +340,9 @@ if ($action == "edituser")
$updateset[] = "modcomment = " . sqlesc($modcomment); $updateset[] = "modcomment = " . sqlesc($modcomment);
sql_query("UPDATE users SET " . implode(", ", $updateset) . " WHERE id=$userid") or sqlerr(__FILE__, __LINE__); sql_query("UPDATE users SET " . implode(", ", $updateset) . " WHERE id=$userid") or sqlerr(__FILE__, __LINE__);
if (!empty($banLog)) {
\App\Models\UserBanLog::query()->insert($banLog);
}
$returnto = htmlspecialchars($_POST["returnto"]); $returnto = htmlspecialchars($_POST["returnto"]);
header("Location: " . get_protocol_prefix() . "$BASEURL/$returnto"); header("Location: " . get_protocol_prefix() . "$BASEURL/$returnto");
die; die;
+2
View File
@@ -17,4 +17,6 @@ return [
'checkout_pass_message_content' => 'Congratulation! You have complete the exam: :exam_name in time(:begin ~ :end)', 'checkout_pass_message_content' => 'Congratulation! You have complete the exam: :exam_name in time(:begin ~ :end)',
'checkout_not_pass_message_subject' => 'Exam not pass, and account is banned!', 'checkout_not_pass_message_subject' => 'Exam not pass, and account is banned!',
'checkout_not_pass_message_content' => 'You did not complete the exam: :exam_name in time(:begin ~ :end), and your account has be banned!', 'checkout_not_pass_message_content' => 'You did not complete the exam: :exam_name in time(:begin ~ :end), and your account has be banned!',
'ban_log_reason' => 'Not complete exam: :exam_name in time(:begin ~ :end)',
'ban_user_modcomment' => 'Due to not complete exam: :exam_name(:begin ~ :end), ban by system',
]; ];
+5
View File
@@ -0,0 +1,5 @@
<?php
return [
'edit_ban_reason' => 'Disable by administrator',
];
+2
View File
@@ -17,4 +17,6 @@ return [
'checkout_pass_message_content' => '恭喜!你在规定时间内(:begin ~ :end)顺利完成了考核::exam_name。', 'checkout_pass_message_content' => '恭喜!你在规定时间内(:begin ~ :end)顺利完成了考核::exam_name。',
'checkout_not_pass_message_subject' => '考核未通过,账号被禁用!', 'checkout_not_pass_message_subject' => '考核未通过,账号被禁用!',
'checkout_not_pass_message_content' => '你在规定时间内(:begin ~ :end)未完成考核::exam_name,账号已被禁用。', 'checkout_not_pass_message_content' => '你在规定时间内(:begin ~ :end)未完成考核::exam_name,账号已被禁用。',
'ban_log_reason' => '未完成考核::exam_name(:begin ~ :end)',
'ban_user_modcomment' => '未完成考核: :exam_name(:begin ~ :end), 被系统禁用',
]; ];
+5
View File
@@ -0,0 +1,5 @@
<?php
return [
'edit_ban_reason' => '被管理人员禁用',
];
+2
View File
@@ -17,4 +17,6 @@ return [
'checkout_pass_message_content' => '恭喜!你在規定時間內(:begin ~ :end)順利完成了考核::exam_name。', 'checkout_pass_message_content' => '恭喜!你在規定時間內(:begin ~ :end)順利完成了考核::exam_name。',
'checkout_not_pass_message_subject' => '考核未通過,賬號被禁用!', 'checkout_not_pass_message_subject' => '考核未通過,賬號被禁用!',
'checkout_not_pass_message_content' => '你在規定時間內(:begin ~ :end)未完成考核::exam_name,賬號已被禁用。', 'checkout_not_pass_message_content' => '你在規定時間內(:begin ~ :end)未完成考核::exam_name,賬號已被禁用。',
'ban_log_reason' => '未完成考核::exam_name(:begin ~ :end)',
'ban_user_modcomment' => '未完成考核: :exam_name(:begin ~ :end), 被系統禁用',
]; ];
+5
View File
@@ -0,0 +1,5 @@
<?php
return [
'edit_ban_reason' => '被管理人員禁用',
];