Skip to content

Commit 743c649

Browse files
committed
restrict time entries create endpoints for employees to only projects where they have access to
1 parent de97d15 commit 743c649

File tree

4 files changed

+178
-3
lines changed

4 files changed

+178
-3
lines changed

app/Http/Requests/V1/TimeEntry/TimeEntryStoreRequest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use App\Models\Project;
1111
use App\Models\Tag;
1212
use App\Models\Task;
13+
use App\Service\PermissionStore;
1314
use Illuminate\Contracts\Validation\ValidationRule;
1415
use Illuminate\Database\Eloquent\Builder;
16+
use Illuminate\Support\Facades\Auth;
1517
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
1618

1719
/**
@@ -42,7 +44,16 @@ public function rules(): array
4244
'required_with:task_id',
4345
ExistsEloquent::make(Project::class, null, function (Builder $builder): Builder {
4446
/** @var Builder<Project> $builder */
45-
return $builder->whereBelongsTo($this->organization, 'organization');
47+
$builder = $builder->whereBelongsTo($this->organization, 'organization');
48+
49+
// If user doesn't have 'all' permission for time entries or projects, only allow access to public projects or projects they're a member of
50+
$permissionStore = app(PermissionStore::class);
51+
if (! $permissionStore->has($this->organization, 'time-entries:create:all')
52+
&& ! $permissionStore->has($this->organization, 'projects:view:all')) {
53+
$builder = $builder->visibleByEmployee(Auth::user());
54+
}
55+
56+
return $builder;
4657
})->uuid(),
4758
],
4859
// ID of the task that the time entry should belong to

app/Http/Requests/V1/TimeEntry/TimeEntryUpdateMultipleRequest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use App\Models\Project;
1111
use App\Models\Tag;
1212
use App\Models\Task;
13+
use App\Service\PermissionStore;
1314
use Illuminate\Contracts\Validation\ValidationRule;
1415
use Illuminate\Database\Eloquent\Builder;
16+
use Illuminate\Support\Facades\Auth;
1517
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
1618

1719
/**
@@ -54,7 +56,16 @@ public function rules(): array
5456
'required_with:task_id',
5557
ExistsEloquent::make(Project::class, null, function (Builder $builder): Builder {
5658
/** @var Builder<Project> $builder */
57-
return $builder->whereBelongsTo($this->organization, 'organization');
59+
$builder = $builder->whereBelongsTo($this->organization, 'organization');
60+
61+
// If user doesn't have 'all' permission for time entries or projects, only allow access to public projects or projects they're a member of
62+
$permissionStore = app(PermissionStore::class);
63+
if (! $permissionStore->has($this->organization, 'time-entries:update:all')
64+
&& ! $permissionStore->has($this->organization, 'projects:view:all')) {
65+
$builder = $builder->visibleByEmployee(Auth::user());
66+
}
67+
68+
return $builder;
5869
})->uuid(),
5970
],
6071
// ID of the task that the time entry should belong to

app/Http/Requests/V1/TimeEntry/TimeEntryUpdateRequest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use App\Models\Project;
1111
use App\Models\Tag;
1212
use App\Models\Task;
13+
use App\Service\PermissionStore;
1314
use Illuminate\Contracts\Validation\ValidationRule;
1415
use Illuminate\Database\Eloquent\Builder;
16+
use Illuminate\Support\Facades\Auth;
1517
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
1618

1719
/**
@@ -42,7 +44,16 @@ public function rules(): array
4244
'required_with:task_id',
4345
ExistsEloquent::make(Project::class, null, function (Builder $builder): Builder {
4446
/** @var Builder<Project> $builder */
45-
return $builder->whereBelongsTo($this->organization, 'organization');
47+
$builder = $builder->whereBelongsTo($this->organization, 'organization');
48+
49+
// If user doesn't have 'all' permission for time entries or projects, only allow access to public projects or projects they're a member of
50+
$permissionStore = app(PermissionStore::class);
51+
if (! $permissionStore->has($this->organization, 'time-entries:update:all')
52+
&& ! $permissionStore->has($this->organization, 'projects:view:all')) {
53+
$builder = $builder->visibleByEmployee(Auth::user());
54+
}
55+
56+
return $builder;
4657
})->uuid(),
4758
],
4859
// ID of the task that the time entry should belong to

0 commit comments

Comments
 (0)