mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-16 05:50:55 +08:00
[admin] add setting backup
This commit is contained in:
@@ -57,9 +57,9 @@ class Test extends Command
|
||||
// dd($r);
|
||||
// $rep->assignCronjob();
|
||||
// $r = $rep->cronjobCheckout();
|
||||
$disk = Storage::disk('google_dirve');
|
||||
$r = $disk->put('/', base_path('composer.json'));
|
||||
dd($r);
|
||||
// $disk = Storage::disk('google_dirve');
|
||||
// $r = $disk->put('/', base_path('composer.json'));
|
||||
// $r = DB::table('users')->where('id', 1)->update(['modcomment' => DB::raw("concat_ws(',', 'ddddd', modcomment)")]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,14 +12,14 @@ class UserResetPassword extends Command
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'user:reset_password {username} {password} {password_confirmation}';
|
||||
protected $signature = 'user:reset_password {uid} {password} {password_confirmation}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Reset user password, arguments: username password password_comfirmation';
|
||||
protected $description = 'Reset user password, arguments: uid password password_comfirmation';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
@@ -38,15 +38,15 @@ class UserResetPassword extends Command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$username = $this->argument('username');
|
||||
$uid = $this->argument('uid');
|
||||
$password = $this->argument('password');
|
||||
$passwordConfirmation = $this->argument('password_confirmation');
|
||||
$log = "username: $username, password: $password, passwordConfirmation: $passwordConfirmation";
|
||||
$log = "uid: $uid, password: $password, passwordConfirmation: $passwordConfirmation";
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
|
||||
$rep = new UserRepository();
|
||||
$result = $rep->resetPassword($username, $password, $passwordConfirmation);
|
||||
$result = $rep->resetPassword($uid, $password, $passwordConfirmation);
|
||||
$log = sprintf('[%s], %s, result: %s', REQUEST_ID, __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
|
||||
77
app/Http/Controllers/SettingController.php
Normal file
77
app/Http/Controllers/SettingController.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Repositories\SettingRepository;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class SettingController extends Controller
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct(SettingRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$result = $this->repository->getList($request->all());
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$result = $this->repository->store($request->all());
|
||||
return $this->success($result, 'Save setting success!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\ExamResource;
|
||||
use App\Http\Resources\InviteResource;
|
||||
use App\Http\Resources\UserResource;
|
||||
use App\Repositories\ExamRepository;
|
||||
use App\Repositories\UserRepository;
|
||||
@@ -90,14 +91,13 @@ class UserController extends Controller
|
||||
public function resetPassword(Request $request)
|
||||
{
|
||||
$rules = [
|
||||
'username' => 'required|string|exists:users',
|
||||
'uid' => 'required',
|
||||
'password' => 'required|string|min:6|max:40',
|
||||
'password_confirmation' => 'required|same:password',
|
||||
];
|
||||
$request->validate($rules);
|
||||
$result = $this->repository->resetPassword($request->repositoryname, $request->password, $request->password_confirmation);
|
||||
$resource = new UserResource($result);
|
||||
return $this->success($resource);
|
||||
$result = $this->repository->resetPassword($request->uid, $request->password, $request->password_confirmation);
|
||||
return $this->success($result, 'Reset password success!');
|
||||
}
|
||||
|
||||
public function classes()
|
||||
@@ -132,6 +132,35 @@ class UserController extends Controller
|
||||
'reason' => 'required',
|
||||
]);
|
||||
$result = $this->repository->disableUser(Auth::user(), $request->uid, $request->reason);
|
||||
return $this->success($result, 'Disable user success!');
|
||||
}
|
||||
|
||||
public function enable(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'uid' => 'required',
|
||||
]);
|
||||
$result = $this->repository->enableUser(Auth::user(), $request->uid);
|
||||
return $this->success($result, 'Enable user success!');
|
||||
}
|
||||
|
||||
public function inviteInfo(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'uid' => 'required',
|
||||
]);
|
||||
$result = $this->repository->getInviteInfo($request->uid);
|
||||
$resource = $result ? (new InviteResource($result)) : null;
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
public function modComment(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'uid' => 'required',
|
||||
]);
|
||||
$result = $this->repository->getModComment($request->uid);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ class InviteResource extends JsonResource
|
||||
'invitee_register_uid' => $this->invitee_register_uid,
|
||||
'invitee_register_email' => $this->invitee_register_email,
|
||||
'invitee_register_username' => $this->invitee_register_username,
|
||||
'inviter_user' => new UserResource($this->whenLoaded('inviter_user')),
|
||||
'invitee_user' => new UserResource($this->whenLoaded('invitee_user')),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ class UserResource extends JsonResource
|
||||
'email' => $this->email,
|
||||
'username' => $this->username,
|
||||
'status' => $this->status,
|
||||
'enabled' => $this->enabled,
|
||||
'added' => format_datetime($this->added),
|
||||
'class' => $this->class,
|
||||
'class_text' => $this->class_text,
|
||||
|
||||
@@ -24,4 +24,14 @@ class Invite extends NexusModel
|
||||
return self::$validInfo[$this->valid]['text'] ?? '';
|
||||
}
|
||||
|
||||
public function inviter_user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'inviter');
|
||||
}
|
||||
|
||||
public function invitee_user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'invitee_register_uid');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -65,7 +65,6 @@ class User extends Authenticatable
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 为数组 / JSON 序列化准备日期。
|
||||
*
|
||||
@@ -82,7 +81,9 @@ class User extends Authenticatable
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['username', 'email', 'passhash', 'secret', 'editsecret', 'added'];
|
||||
protected $fillable = [
|
||||
'username', 'email', 'passhash', 'secret', 'stylesheet', 'editsecret', 'added', 'modcomment', 'enabled', 'status'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
@@ -105,7 +106,7 @@ class User extends Authenticatable
|
||||
public static $commonFields = [
|
||||
'id', 'username', 'email', 'class', 'status', 'added', 'avatar',
|
||||
'uploaded', 'downloaded', 'seedbonus', 'seedtime', 'leechtime',
|
||||
'invited_by',
|
||||
'invited_by', 'enabled',
|
||||
];
|
||||
|
||||
public function checkIsNormal(array $fields = ['status', 'enabled'])
|
||||
@@ -160,7 +161,9 @@ class User extends Authenticatable
|
||||
if (!$this->exists) {
|
||||
throw new \RuntimeException('This mehtod only works when user exists!');
|
||||
}
|
||||
$update['modcomment'] = DB::raw("concat_ws('\n', $modComment, modcomment)");
|
||||
//@todo how to do prepare bindings here ?
|
||||
$modComment = addslashes($modComment);
|
||||
$update['modcomment'] = DB::raw("concat_ws('\n', '$modComment', modcomment)");
|
||||
return $this->update($update);
|
||||
}
|
||||
|
||||
|
||||
64
app/Repositories/SettingRepository.php
Normal file
64
app/Repositories/SettingRepository.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Exceptions\NexusException;
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class SettingRepository extends BaseRepository
|
||||
{
|
||||
public function getList(array $params)
|
||||
{
|
||||
$query = Setting::query();
|
||||
if (!empty($params['prefix'])) {
|
||||
$query->where('name', 'like', "{$params['prefix']}%");
|
||||
}
|
||||
$settings = $query->get();
|
||||
$results = [];
|
||||
foreach ($settings as $setting) {
|
||||
$value = $setting->value;
|
||||
$arr = json_decode($value, true);
|
||||
if (is_array($arr)) {
|
||||
$value = $arr;
|
||||
}
|
||||
Arr::set($results, $setting->name, $value);
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function store(array $params)
|
||||
{
|
||||
$settingModel = new Setting();
|
||||
$values = [];
|
||||
foreach ($params as $prefix => $nameValues) {
|
||||
if (!is_array($nameValues)) {
|
||||
throw new \InvalidArgumentException("Unsupported parameter format.");
|
||||
}
|
||||
foreach ($nameValues as $name => $value) {
|
||||
$valueArr = Arr::wrap($value);
|
||||
array_walk_recursive($valueArr, function ($item) {return addslashes($item);});
|
||||
if (is_array($value)) {
|
||||
$valueStr = json_encode($valueArr);
|
||||
} else {
|
||||
$valueStr = Arr::first($valueArr);
|
||||
}
|
||||
$values[] = sprintf("('%s', '%s')", addslashes("$prefix.$name"), addslashes($valueStr));
|
||||
}
|
||||
}
|
||||
if (empty($values)) {
|
||||
do_log("no values");
|
||||
return true;
|
||||
}
|
||||
$sql = sprintf(
|
||||
"insert into `%s` (`name`, `value`) values %s on duplicate key update `value` = values(`value`)",
|
||||
$settingModel->getTable(), implode(', ', $values)
|
||||
);
|
||||
$result = DB::insert($sql);
|
||||
do_log("sql: $sql, result: $result");
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Exceptions\NexusException;
|
||||
use App\Http\Resources\ExamUserResource;
|
||||
use App\Http\Resources\UserResource;
|
||||
use App\Models\ExamUser;
|
||||
@@ -74,12 +75,12 @@ class UserRepository extends BaseRepository
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function resetPassword($username, $password, $passwordConfirmation)
|
||||
public function resetPassword($id, $password, $passwordConfirmation)
|
||||
{
|
||||
if ($password != $passwordConfirmation) {
|
||||
throw new \InvalidArgumentException("password confirmation != password");
|
||||
}
|
||||
$user = User::query()->where('username', $username)->firstOrFail(['id', 'username']);
|
||||
$user = User::query()->findOrFail($id, ['id', 'username']);
|
||||
$secret = mksecret();
|
||||
$passhash = md5($secret . $password . $secret);
|
||||
$update = [
|
||||
@@ -87,7 +88,7 @@ class UserRepository extends BaseRepository
|
||||
'passhash' => $passhash,
|
||||
];
|
||||
$user->update($update);
|
||||
return $user;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function listClass()
|
||||
@@ -101,7 +102,10 @@ class UserRepository extends BaseRepository
|
||||
|
||||
public function disableUser(User $operator, $uid, $reason)
|
||||
{
|
||||
$targetUser = User::query()->findOrFail(['id', 'username']);
|
||||
$targetUser = User::query()->findOrFail($uid, ['id', 'enabled', 'username']);
|
||||
if ($targetUser->enabled == User::ENABLED_NO) {
|
||||
throw new NexusException('Already disabled!');
|
||||
}
|
||||
$banLog = [
|
||||
'uid' => $uid,
|
||||
'username' => $targetUser->username,
|
||||
@@ -110,9 +114,34 @@ class UserRepository extends BaseRepository
|
||||
];
|
||||
$modCommentText = sprintf("Disable by %s, reason: %s.", $operator->username, $reason);
|
||||
DB::transaction(function () use ($targetUser, $banLog, $modCommentText) {
|
||||
$targetUser->updateWithModComment(['enable' => User::ENABLED_NO], $modCommentText);
|
||||
$targetUser->updateWithModComment(['enabled' => User::ENABLED_NO], $modCommentText);
|
||||
UserBanLog::query()->insert($banLog);
|
||||
});
|
||||
do_log("user: $uid, $modCommentText");
|
||||
return true;
|
||||
}
|
||||
|
||||
public function enableUser(User $operator, $uid)
|
||||
{
|
||||
$targetUser = User::query()->findOrFail($uid, ['id', 'enabled', 'username']);
|
||||
if ($targetUser->enabled == User::ENABLED_YES) {
|
||||
throw new NexusException('Already enabled!');
|
||||
}
|
||||
$modCommentText = sprintf("Enable by %s.", $operator->username);
|
||||
$targetUser->updateWithModComment(['enabled' => User::ENABLED_YES], $modCommentText);
|
||||
do_log("user: $uid, $modCommentText");
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getInviteInfo($id)
|
||||
{
|
||||
$user = User::query()->findOrFail($id, ['id']);
|
||||
return $user->invitee_code()->with('inviter_user')->first();
|
||||
}
|
||||
|
||||
public function getModComment($id)
|
||||
{
|
||||
$user = User::query()->findOrFail($id, ['modcomment']);
|
||||
return $user->modcomment;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user