- Permission 모델 및 PermissionService 생성 (Spatie Permission 확장) - HTMX 기반 권한 CRUD API 구현 - Blade 기반 권한 관리 화면 (index, create, edit) - 권한명 포맷팅 로직 추가 (menu:id.type 파싱) - 사이드바 메뉴 추가 - 멀티테넌트 지원 (tenant_id nullable) - 할당된 역할/부서 표시 기능
62 lines
1.5 KiB
PHP
62 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Tenants\Tenant;
|
|
use Spatie\Permission\Models\Permission as SpatiePermission;
|
|
|
|
class Permission extends SpatiePermission
|
|
{
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'name',
|
|
'guard_name',
|
|
'created_by',
|
|
'updated_by',
|
|
];
|
|
|
|
/**
|
|
* 테넌트 관계
|
|
*/
|
|
public function tenant()
|
|
{
|
|
return $this->belongsTo(Tenant::class);
|
|
}
|
|
|
|
/**
|
|
* 이 권한이 할당된 역할 목록 (현재 테넌트만)
|
|
*/
|
|
public function roles(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
{
|
|
$query = parent::roles();
|
|
|
|
$currentTenantId = session('selected_tenant_id');
|
|
if ($currentTenantId && $currentTenantId !== 'all') {
|
|
$query->where('roles.tenant_id', $currentTenantId);
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
/**
|
|
* 이 권한이 직접 할당된 부서 목록 (현재 테넌트만)
|
|
*/
|
|
public function departments(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
{
|
|
$query = $this->morphedByMany(
|
|
\App\Models\Tenants\Department::class,
|
|
'model',
|
|
config('permission.table_names.model_has_permissions'),
|
|
'permission_id',
|
|
'model_id'
|
|
);
|
|
|
|
$currentTenantId = session('selected_tenant_id');
|
|
if ($currentTenantId && $currentTenantId !== 'all') {
|
|
$query->where('departments.tenant_id', $currentTenantId);
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
}
|