- Permission 모델 및 PermissionService 생성 (Spatie Permission 확장) - HTMX 기반 권한 CRUD API 구현 - Blade 기반 권한 관리 화면 (index, create, edit) - 권한명 포맷팅 로직 추가 (menu:id.type 파싱) - 사이드바 메뉴 추가 - 멀티테넌트 지원 (tenant_id nullable) - 할당된 역할/부서 표시 기능
143 lines
3.7 KiB
PHP
143 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Permission;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
class PermissionService
|
|
{
|
|
/**
|
|
* 권한 목록 조회
|
|
*/
|
|
public function getPermissions(array $filters = [], int $perPage = 20): LengthAwarePaginator
|
|
{
|
|
$query = Permission::query()->with('tenant', 'roles', 'departments');
|
|
|
|
// 검색
|
|
if (! empty($filters['search'])) {
|
|
$search = $filters['search'];
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
// 테넌트 필터
|
|
$selectedTenantId = session('selected_tenant_id');
|
|
if ($selectedTenantId && $selectedTenantId !== 'all') {
|
|
$query->where('tenant_id', $selectedTenantId);
|
|
} elseif (isset($filters['tenant_id'])) {
|
|
$query->where('tenant_id', $filters['tenant_id']);
|
|
}
|
|
|
|
// guard_name 필터
|
|
if (! empty($filters['guard_name'])) {
|
|
$query->where('guard_name', $filters['guard_name']);
|
|
}
|
|
|
|
return $query->orderBy('id', 'desc')->paginate($perPage);
|
|
}
|
|
|
|
/**
|
|
* 특정 권한 조회
|
|
*/
|
|
public function getPermissionById(int $id): ?Permission
|
|
{
|
|
return Permission::with('tenant', 'roles')->find($id);
|
|
}
|
|
|
|
/**
|
|
* 권한 생성
|
|
*/
|
|
public function createPermission(array $data): Permission
|
|
{
|
|
$data['created_by'] = auth()->id();
|
|
$data['guard_name'] = $data['guard_name'] ?? 'web';
|
|
|
|
return Permission::create($data);
|
|
}
|
|
|
|
/**
|
|
* 권한 수정
|
|
*/
|
|
public function updatePermission(int $id, array $data): Permission
|
|
{
|
|
$permission = Permission::findOrFail($id);
|
|
|
|
$data['updated_by'] = auth()->id();
|
|
|
|
$permission->update($data);
|
|
|
|
return $permission->fresh();
|
|
}
|
|
|
|
/**
|
|
* 권한 삭제
|
|
*/
|
|
public function deletePermission(int $id): bool
|
|
{
|
|
$permission = Permission::findOrFail($id);
|
|
|
|
// 할당된 역할이 있는지 확인
|
|
if ($permission->roles()->count() > 0) {
|
|
throw new \Exception('이 권한이 할당된 역할이 있어 삭제할 수 없습니다.');
|
|
}
|
|
|
|
return $permission->delete();
|
|
}
|
|
|
|
/**
|
|
* 권한 이름 중복 체크
|
|
*/
|
|
public function isNameExists(string $name, string $guardName, ?int $excludeId = null, ?int $tenantId = null): bool
|
|
{
|
|
$query = Permission::where('name', $name)
|
|
->where('guard_name', $guardName);
|
|
|
|
if ($tenantId !== null) {
|
|
$query->where('tenant_id', $tenantId);
|
|
}
|
|
|
|
if ($excludeId) {
|
|
$query->where('id', '!=', $excludeId);
|
|
}
|
|
|
|
return $query->exists();
|
|
}
|
|
|
|
/**
|
|
* 활성 권한 목록 (드롭다운용)
|
|
*/
|
|
public function getActivePermissions(?int $tenantId = null): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
$query = Permission::query();
|
|
|
|
if ($tenantId) {
|
|
$query->where('tenant_id', $tenantId);
|
|
}
|
|
|
|
return $query->orderBy('name')->get();
|
|
}
|
|
|
|
/**
|
|
* 권한 통계
|
|
*/
|
|
public function getPermissionStats(): array
|
|
{
|
|
$selectedTenantId = session('selected_tenant_id');
|
|
$query = Permission::query();
|
|
|
|
if ($selectedTenantId && $selectedTenantId !== 'all') {
|
|
$query->where('tenant_id', $selectedTenantId);
|
|
}
|
|
|
|
return [
|
|
'total' => $query->count(),
|
|
'by_guard' => $query->select('guard_name', \DB::raw('count(*) as count'))
|
|
->groupBy('guard_name')
|
|
->pluck('count', 'guard_name')
|
|
->toArray(),
|
|
];
|
|
}
|
|
}
|