- 新增角色菜单关联模型、控制器、请求验证和业务逻辑 - 新增用户角色关联模型、控制器、请求验证和业务逻辑 - 更新系统角色模型,增加与用户和菜单的关联 - 更新用户模型,增加与角色的关联和相关方法 - 在路由文件中添加新功能的路由定义
193 lines
5.2 KiB
PHP
193 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\System;
|
|
|
|
use App\Models\System\SystemUserRole;
|
|
use App\Models\System\SystemRole;
|
|
use App\Models\User;
|
|
use App\Services\BaseService;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* 系统用户角色关联服务类
|
|
*/
|
|
class SystemUserRoleService extends BaseService
|
|
{
|
|
protected string $modelClass = SystemUserRole::class;
|
|
|
|
/**
|
|
* 获取用户角色关联列表
|
|
*/
|
|
public function getList(array $params): LengthAwarePaginator
|
|
{
|
|
$query = SystemUserRole::query()
|
|
->with(['user:id,username,nickname', 'role:id,name,code']);
|
|
|
|
// 用户筛选
|
|
if (isset($params['user_id'])) {
|
|
$query->where('user_id', $params['user_id']);
|
|
}
|
|
|
|
// 角色筛选
|
|
if (isset($params['role_id'])) {
|
|
$query->where('role_id', $params['role_id']);
|
|
}
|
|
|
|
// 排序
|
|
$query->orderBy('user_id', 'asc')
|
|
->orderBy('role_id', 'asc');
|
|
|
|
return $query->paginate($params['page_size'] ?? 15);
|
|
}
|
|
|
|
/**
|
|
* 获取用户的角色列表
|
|
*/
|
|
public function getUserRoles(int $userId): array
|
|
{
|
|
return SystemUserRole::where('user_id', $userId)
|
|
->pluck('role_id')
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* 获取角色的用户列表
|
|
*/
|
|
public function getRoleUsers(int $roleId): array
|
|
{
|
|
return SystemUserRole::where('role_id', $roleId)
|
|
->pluck('user_id')
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* 设置用户角色
|
|
*/
|
|
public function setUserRoles(int $userId, array $roleIds): bool
|
|
{
|
|
return DB::transaction(function () use ($userId, $roleIds) {
|
|
// 删除现有关联
|
|
SystemUserRole::where('user_id', $userId)->delete();
|
|
|
|
// 批量插入新关联
|
|
if (!empty($roleIds)) {
|
|
$data = [];
|
|
foreach ($roleIds as $roleId) {
|
|
$data[] = [
|
|
'user_id' => $userId,
|
|
'role_id' => $roleId,
|
|
'creator' => auth('admin')->id() ?? 0,
|
|
'create_time' => now(),
|
|
'tenant_id' => $this->getCurrentTenantId() ?? 0,
|
|
'deleted' => 0,
|
|
];
|
|
}
|
|
SystemUserRole::insert($data);
|
|
}
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 获取用户的角色详情(包含角色信息)
|
|
*/
|
|
public function getUserRoleDetails(int $userId): array
|
|
{
|
|
return SystemUserRole::where('user_id', $userId)
|
|
->with('role:id,name,code,data_scope,status')
|
|
->get()
|
|
->pluck('role')
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* 检查用户是否拥有指定角色
|
|
*/
|
|
public function hasRole(int $userId, int $roleId): bool
|
|
{
|
|
return SystemUserRole::where('user_id', $userId)
|
|
->where('role_id', $roleId)
|
|
->exists();
|
|
}
|
|
|
|
/**
|
|
* 检查用户是否拥有指定角色编码
|
|
*/
|
|
public function hasRoleCode(int $userId, string $roleCode): bool
|
|
{
|
|
return SystemUserRole::where('user_id', $userId)
|
|
->whereHas('role', function ($query) use ($roleCode) {
|
|
$query->where('code', $roleCode)->where('status', 1);
|
|
})
|
|
->exists();
|
|
}
|
|
|
|
/**
|
|
* 批量设置多个用户的角色
|
|
*/
|
|
public function batchSetUserRoles(array $userRoleData): bool
|
|
{
|
|
return DB::transaction(function () use ($userRoleData) {
|
|
foreach ($userRoleData as $data) {
|
|
$this->setUserRoles($data['user_id'], $data['role_ids'] ?? []);
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 为角色批量分配用户
|
|
*/
|
|
public function setRoleUsers(int $roleId, array $userIds): bool
|
|
{
|
|
return DB::transaction(function () use ($roleId, $userIds) {
|
|
// 删除现有关联
|
|
SystemUserRole::where('role_id', $roleId)->delete();
|
|
|
|
// 批量插入新关联
|
|
if (!empty($userIds)) {
|
|
$data = [];
|
|
foreach ($userIds as $userId) {
|
|
$data[] = [
|
|
'user_id' => $userId,
|
|
'role_id' => $roleId,
|
|
'creator' => auth('admin')->id() ?? 0,
|
|
'create_time' => now(),
|
|
'tenant_id' => $this->getCurrentTenantId() ?? 0,
|
|
'deleted' => 0,
|
|
];
|
|
}
|
|
SystemUserRole::insert($data);
|
|
}
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 删除用户的所有角色
|
|
*/
|
|
public function clearUserRoles(int $userId): bool
|
|
{
|
|
return SystemUserRole::where('user_id', $userId)->delete();
|
|
}
|
|
|
|
/**
|
|
* 删除角色的所有用户关联
|
|
*/
|
|
public function clearRoleUsers(int $roleId): bool
|
|
{
|
|
return SystemUserRole::where('role_id', $roleId)->delete();
|
|
}
|
|
|
|
/**
|
|
* 获取当前租户ID
|
|
*/
|
|
protected function getCurrentTenantId(): ?int
|
|
{
|
|
$user = auth('admin')->user();
|
|
return $user ? $user->tenant_id : null;
|
|
}
|
|
}
|