更新用户权限模块功能

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
+36
View File
@@ -324,4 +324,40 @@ private function isDescendant($id, $childId): bool
}
return $this->isDescendant($id, $child->parent_id);
}
/**
* 获取部门及所有子部门的ID列表
*
* @param int $departmentId 部门ID
* @param array $departments 所有部门数据
* @return array
*/
public function getDepartmentAndChildrenIds(int $departmentId, array $departments = null): array
{
if ($departments === null) {
$departments = Department::where('status', 1)->get()->keyBy('id')->toArray();
}
$ids = [$departmentId];
$this->collectChildrenIds($departmentId, $departments, $ids);
return $ids;
}
/**
* 递归收集子部门ID
*
* @param int $parentId 父部门ID
* @param array $departments 所有部门数据
* @param array &$ids ID收集数组
*/
private function collectChildrenIds(int $parentId, array $departments, array &$ids): void
{
foreach ($departments as $department) {
if ($department['parent_id'] == $parentId) {
$ids[] = $department['id'];
$this->collectChildrenIds($department['id'], $departments, $ids);
}
}
}
}
+36 -1
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 @@
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 @@ public function getList(array $params): array
}
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 @@ public function delete(int $id): void
*/
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 @@ public function batchDelete(array $ids): int
*/
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]);
}