역할 관리 페이지 개선

- 역할 목록: 테넌트/Guard 컬럼 추가, Guard 필터 드롭다운 추가
- 역할 등록/수정: Guard 선택 기능 추가 (API/Web)
- 권한 선택 UI를 메뉴 기반 매트릭스로 변경 (7가지 권한 유형)
- 테넌트 미선택 시 역할 등록 차단
- pagination.js 디버그 로그 제거
This commit is contained in:
2025-11-26 17:11:17 +09:00
parent f029d78614
commit ee9e645290
10 changed files with 365 additions and 124 deletions

View File

@@ -2,9 +2,12 @@
namespace App\Services;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role as SpatieRole;
class RoleService
{
@@ -15,12 +18,20 @@ public function getRoles(array $filters = [], int $perPage = 15): LengthAwarePag
{
$tenantId = session('selected_tenant_id');
$query = Role::query()
->where('guard_name', 'web')
->withCount('permissions');
$query = Role::query()->withCount('permissions');
// Guard 필터링 (선택된 경우에만)
if (! empty($filters['guard_name'])) {
$query->where('guard_name', $filters['guard_name']);
}
// 전체 보기일 때 tenant 정보 로드
if (! $tenantId || $tenantId === 'all') {
$query->with('tenant');
}
// Tenant 필터링 (선택된 경우에만)
if ($tenantId) {
if ($tenantId && $tenantId !== 'all') {
$query->where('tenant_id', $tenantId);
}
@@ -44,16 +55,14 @@ public function getRoles(array $filters = [], int $perPage = 15): LengthAwarePag
/**
* 특정 역할 조회
*/
public function getRoleById(int $id): ?Role
public function getRoleById(int $id): ?SpatieRole
{
$tenantId = session('selected_tenant_id');
$query = Role::query()
->where('guard_name', 'web')
->with('permissions');
$query = SpatieRole::query()->with('permissions');
// Tenant 필터링 (선택된 경우에만)
if ($tenantId) {
if ($tenantId && $tenantId !== 'all') {
$query->where('tenant_id', $tenantId);
}
@@ -63,20 +72,22 @@ public function getRoleById(int $id): ?Role
/**
* 역할 생성
*/
public function createRole(array $data): Role
public function createRole(array $data): SpatieRole
{
$tenantId = session('selected_tenant_id');
$effectiveTenantId = ($tenantId && $tenantId !== 'all') ? $tenantId : null;
$guardName = $data['guard_name'] ?? 'api';
$role = Role::create([
'tenant_id' => $tenantId,
'guard_name' => 'web',
$role = SpatieRole::create([
'tenant_id' => $effectiveTenantId,
'guard_name' => $guardName,
'name' => $data['name'],
'description' => $data['description'] ?? null,
]);
// 권한 동기화 (있는 경우)
if (! empty($data['permissions'])) {
$role->syncPermissions($data['permissions']);
// 메뉴 권한 동기화 (있는 경우)
if (! empty($data['menu_permissions'])) {
$this->syncMenuPermissions($role, $data['menu_permissions'], $effectiveTenantId, $guardName);
}
return $role->fresh(['permissions']);
@@ -93,19 +104,71 @@ public function updateRole(int $id, array $data): bool
return false;
}
$tenantId = session('selected_tenant_id');
$effectiveTenantId = ($tenantId && $tenantId !== 'all') ? $tenantId : null;
$guardName = $data['guard_name'] ?? $role->guard_name;
$updated = $role->update([
'name' => $data['name'] ?? $role->name,
'guard_name' => $guardName,
'description' => $data['description'] ?? $role->description,
]);
// 권한 동기화 (있는 경우)
if (isset($data['permissions'])) {
$role->syncPermissions($data['permissions']);
// 메뉴 권한 동기화 (있는 경우)
if (isset($data['menu_permissions'])) {
$this->syncMenuPermissions($role, $data['menu_permissions'], $effectiveTenantId, $guardName);
}
return $updated;
}
/**
* 메뉴 권한 동기화
*/
protected function syncMenuPermissions(SpatieRole $role, array $menuPermissions, ?int $tenantId, string $guardName): void
{
// 기존 메뉴 권한 모두 제거
DB::table('role_has_permissions')
->where('role_id', $role->id)
->whereIn('permission_id', function ($query) use ($guardName) {
$query->select('id')
->from('permissions')
->where('guard_name', $guardName)
->where('name', 'like', 'menu:%');
})
->delete();
// 새로운 권한 부여
foreach ($menuPermissions as $menuId => $types) {
if (! is_array($types)) {
continue;
}
foreach ($types as $type) {
$permissionName = "menu:{$menuId}.{$type}";
// 권한 생성 또는 조회
$permission = Permission::firstOrCreate(
['name' => $permissionName, 'guard_name' => $guardName, 'tenant_id' => $tenantId],
['created_by' => auth()->id()]
);
// 권한 부여
$exists = DB::table('role_has_permissions')
->where('role_id', $role->id)
->where('permission_id', $permission->id)
->exists();
if (! $exists) {
DB::table('role_has_permissions')->insert([
'role_id' => $role->id,
'permission_id' => $permission->id,
]);
}
}
}
}
/**
* 역할 삭제
*/
@@ -133,10 +196,13 @@ public function isNameExists(string $name, ?int $excludeId = null): bool
{
$tenantId = session('selected_tenant_id');
$query = Role::where('tenant_id', $tenantId)
->where('guard_name', 'web')
$query = SpatieRole::where('guard_name', 'web')
->where('name', $name);
if ($tenantId && $tenantId !== 'all') {
$query->where('tenant_id', $tenantId);
}
if ($excludeId) {
$query->where('id', '!=', $excludeId);
}
@@ -151,10 +217,10 @@ public function getActiveRoles(): Collection
{
$tenantId = session('selected_tenant_id');
$query = Role::query()->where('guard_name', 'web');
$query = SpatieRole::query()->where('guard_name', 'web');
// Tenant 필터링 (선택된 경우에만)
if ($tenantId) {
if ($tenantId && $tenantId !== 'all') {
$query->where('tenant_id', $tenantId);
}
@@ -168,10 +234,10 @@ public function getRoleStats(): array
{
$tenantId = session('selected_tenant_id');
$baseQuery = Role::query()->where('guard_name', 'web');
$baseQuery = SpatieRole::query()->where('guard_name', 'web');
// Tenant 필터링 (선택된 경우에만)
if ($tenantId) {
if ($tenantId && $tenantId !== 'all') {
$baseQuery->where('tenant_id', $tenantId);
}