[FEAT] 優化部署流程:加入 RoleSeeder 與 AdminUserSeeder,並實作權限系統基礎架構與多租戶隔離機制
All checks were successful
star-cloud-deploy-demo / deploy-demo (push) Successful in 48s
All checks were successful
star-cloud-deploy-demo / deploy-demo (push) Successful in 48s
This commit is contained in:
41
app/Traits/TenantScoped.php
Normal file
41
app/Traits/TenantScoped.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
trait TenantScoped
|
||||
{
|
||||
/**
|
||||
* Boot the trait.
|
||||
*/
|
||||
public static function bootTenantScoped(): void
|
||||
{
|
||||
static::addGlobalScope('tenant', function (Builder $query) {
|
||||
$user = auth()->user();
|
||||
|
||||
// 如果使用者已登入且有綁定公司,則自動注入過濾條件
|
||||
if ($user && $user->company_id) {
|
||||
$query->where((new static)->getTable() . '.company_id', $user->company_id);
|
||||
}
|
||||
});
|
||||
|
||||
// 建立資料時,自動填入當前使用者的 company_id
|
||||
static::creating(function ($model) {
|
||||
if (!$model->company_id) {
|
||||
$user = auth()->user();
|
||||
if ($user && $user->company_id) {
|
||||
$model->company_id = $user->company_id;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the company relationship.
|
||||
*/
|
||||
public function company()
|
||||
{
|
||||
return $this->belongsTo(\App\Models\System\Company::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user