41 lines
1.3 KiB
PHP
41 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Scopes;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Scope;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class ScopeProjectManagerProjects implements Scope
|
|
{
|
|
/**
|
|
* Apply the scope to a given Eloquent query builder.
|
|
* Restricts Project Managers to only view specifically assigned projects.
|
|
*/
|
|
public function apply(Builder $builder, Model $model): void
|
|
{
|
|
if (!Auth::check()) {
|
|
return;
|
|
}
|
|
|
|
$user = Auth::user();
|
|
|
|
// Platform Admin / Admin role bypasses project assignment restriction
|
|
if ($user->hasRole('admin') || $user->hasRole('Super Admin') || is_null($user->contractor_id)) {
|
|
return;
|
|
}
|
|
|
|
// Restrict Project Managers to assigned projects
|
|
if ($user->hasRole('Project Manager')) {
|
|
$assignedProjectIds = $user->projects()->pluck('projects.id')->toArray();
|
|
|
|
if ($model instanceof \Modules\ProjectManagement\Models\Project) {
|
|
$builder->whereIn('id', $assignedProjectIds);
|
|
} else if (Schema::hasColumn($model->getTable(), 'project_id')) {
|
|
$builder->whereIn('project_id', $assignedProjectIds);
|
|
}
|
|
}
|
|
}
|
|
}
|