更新用户权限模块功能

This commit is contained in:
2026-02-18 16:17:11 +08:00
parent 790b3140a7
commit 5450777bd7
7 changed files with 404 additions and 96 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Services\Auth;
use App\Models\Auth\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
@@ -14,6 +15,20 @@ use App\Jobs\Auth\UserExportJob;
class UserService
{
protected $departmentService;
public function __construct(DepartmentService $departmentService)
{
$this->departmentService = $departmentService;
}
/**
* 获取当前登录用户ID
*/
protected function getCurrentUserId(): int
{
return Auth::guard('admin')->id();
}
/**
* 获取用户列表
*/
@@ -32,7 +47,9 @@ class UserService
}
if (!empty($params['department_id'])) {
$query->where('department_id', $params['department_id']);
// 获取部门及所有子部门的ID
$departmentIds = $this->departmentService->getDepartmentAndChildrenIds($params['department_id']);
$query->whereIn('department_id', $departmentIds);
}
if (isset($params['status']) && $params['status'] !== '') {
@@ -226,6 +243,15 @@ class UserService
*/
public function batchDelete(array $ids): int
{
$currentUserId = $this->getCurrentUserId();
// 检查是否包含当前用户
if (in_array($currentUserId, $ids)) {
throw ValidationException::withMessages([
'ids' => ['不能删除当前登录用户'],
]);
}
return User::whereIn('id', $ids)->delete();
}
@@ -234,6 +260,15 @@ class UserService
*/
public function batchUpdateStatus(array $ids, int $status): int
{
$currentUserId = $this->getCurrentUserId();
// 如果是禁用操作,检查是否包含当前用户
if ($status === 0 && in_array($currentUserId, $ids)) {
throw ValidationException::withMessages([
'ids' => ['不能禁用当前登录用户'],
]);
}
return User::whereIn('id', $ids)->update(['status' => $status]);
}