user-list

This commit is contained in:
xiaomlove
2021-04-17 19:01:33 +08:00
parent d734788363
commit 736d42cd5c
18 changed files with 699 additions and 803 deletions

View 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];
}
}

View 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;
}
}