Preserva base local aceite de identidade e acessos
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Shared\Context;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
|
||||
trait BelongsToCompany
|
||||
{
|
||||
protected static function bootBelongsToCompany(): void
|
||||
{
|
||||
static::addGlobalScope('company', function (Builder $query): void {
|
||||
foreach (app(CompanyContext::class)->identifiers() as $column => $value) {
|
||||
$query->where($query->getModel()->qualifyColumn($column), $value);
|
||||
}
|
||||
});
|
||||
|
||||
static::saving(function (Model $model): void {
|
||||
foreach (app(CompanyContext::class)->identifiers() as $column => $value) {
|
||||
if ($model->getAttribute($column) !== null && (int) $model->getAttribute($column) !== $value) {
|
||||
throw new AccessDeniedHttpException('Contexto de empresa incompatível.');
|
||||
}
|
||||
$model->setAttribute($column, $value);
|
||||
}
|
||||
});
|
||||
|
||||
static::deleting(function (Model $model): void {
|
||||
foreach (app(CompanyContext::class)->identifiers() as $column => $value) {
|
||||
if ((int) $model->getAttribute($column) !== $value) {
|
||||
throw new AccessDeniedHttpException('Contexto de empresa incompatível.');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user