first commit
This commit is contained in:
36
app/Scopes/ProjectAccessScope.php
Normal file
36
app/Scopes/ProjectAccessScope.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Scopes;
|
||||
|
||||
use App\Enums\Role;
|
||||
use App\Exceptions\Entities\AuthorizationException;
|
||||
use Illuminate\Contracts\Database\Query\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Scope;
|
||||
use Throwable;
|
||||
|
||||
class ProjectAccessScope implements Scope
|
||||
{
|
||||
/**
|
||||
* @param Builder $builder
|
||||
* @param Model $model
|
||||
* @return Builder
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function apply(Builder $builder, Model $model): Builder
|
||||
{
|
||||
if (app()->runningInConsole()) {
|
||||
return $builder;
|
||||
}
|
||||
|
||||
$user = optional(request())->user();
|
||||
|
||||
throw_unless($user, new AuthorizationException);
|
||||
|
||||
if ($user->hasRole([Role::ADMIN, Role::MANAGER, Role::AUDITOR])) {
|
||||
return $builder;
|
||||
}
|
||||
|
||||
return $builder->whereHas('users', static fn(Builder $query) => $query->where('user_id', $user->id));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user