Files
sam-manage/app/Models/Permission.php

62 lines
1.5 KiB
PHP
Raw Normal View History

<?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;
}
}