mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 12:30:49 +08:00
user-list
This commit is contained in:
18
app/Repositories/BaseRepository.php
Normal file
18
app/Repositories/BaseRepository.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class BaseRepository
|
||||
{
|
||||
protected function getSortFieldAndType(array $params)
|
||||
{
|
||||
$field = $params['sort_field'] ?? 'id';
|
||||
$type = 'desc';
|
||||
if (!empty($params['sort_type']) && Str::startsWith($params['sort_type'], 'asc')) {
|
||||
$type = 'asc';
|
||||
}
|
||||
return [$field, $type];
|
||||
}
|
||||
}
|
||||
56
app/Repositories/UserRepository.php
Normal file
56
app/Repositories/UserRepository.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
|
||||
class UserRepository extends BaseRepository
|
||||
{
|
||||
public function getList(array $params)
|
||||
{
|
||||
$query = User::query();
|
||||
list($sortField, $sortType) = $this->getSortFieldAndType($params);
|
||||
$query->orderBy($sortField, $sortType);
|
||||
return $query->paginate();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public function resetPassword($username, $password, $passwordConfirmation)
|
||||
{
|
||||
if ($password != $passwordConfirmation) {
|
||||
throw new \InvalidArgumentException("password confirmation != password");
|
||||
}
|
||||
$user = User::query()->where('username', $username)->firstOrFail();
|
||||
$secret = mksecret();
|
||||
$passhash = md5($secret . $password . $secret);
|
||||
$update = [
|
||||
'secret' => $secret,
|
||||
'passhash' => $passhash,
|
||||
];
|
||||
$user->update($update);
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user