Files
nexusphp/app/Repositories/UserRepository.php

178 lines
5.6 KiB
PHP
Raw Normal View History

2021-04-17 19:01:33 +08:00
<?php
namespace App\Repositories;
2021-05-14 20:41:43 +08:00
use App\Exceptions\NexusException;
2021-04-27 19:13:32 +08:00
use App\Http\Resources\ExamUserResource;
use App\Http\Resources\UserResource;
use App\Models\ExamUser;
2021-04-17 19:01:33 +08:00
use App\Models\Setting;
use App\Models\User;
2021-05-14 02:11:57 +08:00
use App\Models\UserBanLog;
2021-05-12 13:45:00 +08:00
use Illuminate\Database\Eloquent\Builder;
2021-05-14 02:11:57 +08:00
use Illuminate\Support\Facades\DB;
2021-04-17 19:01:33 +08:00
class UserRepository extends BaseRepository
{
public function getList(array $params)
{
$query = User::query();
2022-02-25 18:09:31 +08:00
if (!empty($params['id'])) {
$query->where('id', $params['id']);
}
if (!empty($params['username'])) {
$query->where('username', 'like',"%{$params['username']}%");
}
if (!empty($params['email'])) {
$query->where('email', 'like',"%{$params['email']}%");
}
2022-03-04 16:16:56 +08:00
if (!empty($params['class'])) {
$query->where('class', $params['class']);
}
2021-04-17 19:01:33 +08:00
list($sortField, $sortType) = $this->getSortFieldAndType($params);
$query->orderBy($sortField, $sortType);
return $query->paginate();
}
2021-04-23 01:28:41 +08:00
public function getBase($id)
{
$user = User::query()->findOrFail($id, ['id', 'username', 'email', 'avatar']);
return $user;
}
2021-04-27 19:13:32 +08:00
public function getDetail($id)
{
2021-05-12 13:45:00 +08:00
$with = [
2022-01-19 23:54:55 +08:00
'inviter' => function ($query) {return $query->select(User::$commonFields);},
'valid_medals'
2021-05-12 13:45:00 +08:00
];
$user = User::query()->with($with)->findOrFail($id, User::$commonFields);
2021-04-27 19:13:32 +08:00
$userResource = new UserResource($user);
$baseInfo = $userResource->response()->getData(true)['data'];
$examRep = new ExamRepository();
2022-01-19 23:54:55 +08:00
$examProgress = $examRep->getUserExamProgress($id, null);
2021-04-27 19:13:32 +08:00
if ($examProgress) {
$examResource = new ExamUserResource($examProgress);
$examInfo = $examResource->response()->getData(true)['data'];
} else {
$examInfo = null;
}
2022-01-19 23:54:55 +08:00
2021-04-27 19:13:32 +08:00
return [
'base_info' => $baseInfo,
'exam_info' => $examInfo,
];
}
2021-04-17 19:01:33 +08:00
public function store(array $params)
{
$password = $params['password'];
if ($password != $params['password_confirmation']) {
throw new \InvalidArgumentException("password confirmation != password");
}
$setting = Setting::get('main');
$secret = mksecret();
$passhash = md5($secret . $password . $secret);
$data = [
'username' => $params['username'],
'email' => $params['email'],
'secret' => $secret,
'editsecret' => '',
'passhash' => $passhash,
'stylesheet' => $setting['defstylesheet'],
'added' => now()->toDateTimeString(),
'status' => User::STATUS_CONFIRMED,
];
$user = User::query()->create($data);
return $user;
}
2021-05-14 20:41:43 +08:00
public function resetPassword($id, $password, $passwordConfirmation)
2021-04-17 19:01:33 +08:00
{
if ($password != $passwordConfirmation) {
throw new \InvalidArgumentException("password confirmation != password");
}
2021-05-14 20:41:43 +08:00
$user = User::query()->findOrFail($id, ['id', 'username']);
2021-04-17 19:01:33 +08:00
$secret = mksecret();
$passhash = md5($secret . $password . $secret);
$update = [
'secret' => $secret,
'passhash' => $passhash,
];
$user->update($update);
2021-05-14 20:41:43 +08:00
return true;
2021-04-17 19:01:33 +08:00
}
2021-04-19 20:13:21 +08:00
public function listClass()
{
$out = [];
foreach(User::$classes as $key => $value) {
$out[(string)$key] = $value['text'];
}
return $out;
}
2021-05-14 02:11:57 +08:00
public function disableUser(User $operator, $uid, $reason)
{
2021-05-14 20:41:43 +08:00
$targetUser = User::query()->findOrFail($uid, ['id', 'enabled', 'username']);
if ($targetUser->enabled == User::ENABLED_NO) {
throw new NexusException('Already disabled!');
}
2021-05-14 02:11:57 +08:00
$banLog = [
'uid' => $uid,
'username' => $targetUser->username,
'reason' => $reason,
'operator' => $operator->id,
];
$modCommentText = sprintf("Disable by %s, reason: %s.", $operator->username, $reason);
DB::transaction(function () use ($targetUser, $banLog, $modCommentText) {
2021-05-14 20:41:43 +08:00
$targetUser->updateWithModComment(['enabled' => User::ENABLED_NO], $modCommentText);
2021-05-14 02:11:57 +08:00
UserBanLog::query()->insert($banLog);
});
2021-05-14 20:41:43 +08:00
do_log("user: $uid, $modCommentText");
return true;
}
public function enableUser(User $operator, $uid)
{
2021-05-15 12:59:59 +08:00
$targetUser = User::query()->findOrFail($uid, ['id', 'enabled', 'username', 'class']);
2021-05-14 20:41:43 +08:00
if ($targetUser->enabled == User::ENABLED_YES) {
throw new NexusException('Already enabled!');
}
2021-05-15 12:59:59 +08:00
$update = [
'enabled' => User::ENABLED_YES
];
if ($targetUser->class == User::CLASS_PEASANT) {
// warn users until 30 days
$until = now()->addDays(30)->toDateTimeString();
$update['leechwarn'] = 'yes';
$update['leechwarnuntil'] = $until;
} else {
$update['leechwarn'] = 'no';
$update['leechwarnuntil'] = null;
}
2021-05-14 20:41:43 +08:00
$modCommentText = sprintf("Enable by %s.", $operator->username);
2021-05-15 12:59:59 +08:00
$targetUser->updateWithModComment($update, $modCommentText);
do_log("user: $uid, $modCommentText, update: " . nexus_json_encode($update));
2021-05-14 02:11:57 +08:00
return true;
}
2021-05-14 20:41:43 +08:00
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;
}
2022-01-19 23:54:55 +08:00
2021-04-17 19:01:33 +08:00
}