Phase 3 of S2b. Six policies and fifteen form requests for the universal
form builder. Every exists: rule is scoped to the route's organisation
or form_schema to close the A01-5..18 findings from SECURITY_AUDIT.md.
Policies (api/app/Policies/FormBuilder/):
- FormSchemaPolicy, FormFieldPolicy, FormFieldLibraryPolicy,
FormTemplatePolicy, FormSubmissionPolicy, FormSchemaWebhookPolicy.
- FormSubmissionPolicy honours subject-self (user / person.user_id
match / submitted_by_user_id) and active delegations, per §18.3.
- No `return true` placeholders — each method checks org membership and
role via Spatie's hasRole().
Form Requests (api/app/Http/Requests/Api/V1/FormBuilder/):
- Schema: Store/UpdateFormSchemaRequest, RotatePublicTokenRequest.
- Fields: Store/UpdateFormFieldRequest, ReorderFormFieldsRequest (field
ids scoped to the route schema), InsertLibraryFieldRequest (library
scoped to the route organisation).
- Templates: Store/UpdateFormTemplateRequest.
- Field library: Store/UpdateFormFieldLibraryRequest.
- Submissions: CreateFormSubmissionRequest, UpsertFormValuesRequest
(slug allow-list derived from schema), SubmitFormSubmissionRequest,
ReviewFormSubmissionRequest, DelegateFormSubmissionRequest (delegatee
scoped to organisation pivot).
- Webhooks: Store/UpdateFormSchemaWebhookRequest.
- Public: PublicSubmissionRequest (captcha_token collected here,
enforcement in controller per config('form_builder.captcha')).
All enum validation routes through the existing PHP enums from S1.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
79 lines
2.0 KiB
PHP
79 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Policies\FormBuilder;
|
|
|
|
use App\Models\FormBuilder\FormSchema;
|
|
use App\Models\FormBuilder\FormTemplate;
|
|
use App\Models\Organisation;
|
|
use App\Models\User;
|
|
|
|
final class FormTemplatePolicy
|
|
{
|
|
public function viewAny(User $user, Organisation $organisation): bool
|
|
{
|
|
return $this->belongsToOrg($user, $organisation);
|
|
}
|
|
|
|
public function view(User $user, FormTemplate $template): bool
|
|
{
|
|
return $this->belongsToOrg($user, $template->organisation);
|
|
}
|
|
|
|
public function create(User $user, Organisation $organisation): bool
|
|
{
|
|
return $this->canManage($user, $organisation);
|
|
}
|
|
|
|
public function update(User $user, FormTemplate $template): bool
|
|
{
|
|
if ($template->is_system && ! $user->hasRole('super_admin')) {
|
|
return false;
|
|
}
|
|
|
|
return $this->canManage($user, $template->organisation);
|
|
}
|
|
|
|
public function deactivate(User $user, FormTemplate $template): bool
|
|
{
|
|
return $this->canManage($user, $template->organisation);
|
|
}
|
|
|
|
public function applyToSchema(User $user, FormTemplate $template, FormSchema $schema): bool
|
|
{
|
|
if ($template->organisation_id !== $schema->organisation_id) {
|
|
return false;
|
|
}
|
|
|
|
return app(FormSchemaPolicy::class)->update($user, $schema);
|
|
}
|
|
|
|
private function belongsToOrg(User $user, ?Organisation $organisation): bool
|
|
{
|
|
if ($user->hasRole('super_admin')) {
|
|
return true;
|
|
}
|
|
if ($organisation === null) {
|
|
return false;
|
|
}
|
|
|
|
return $organisation->users()->where('user_id', $user->id)->exists();
|
|
}
|
|
|
|
private function canManage(User $user, ?Organisation $organisation): bool
|
|
{
|
|
if ($user->hasRole('super_admin')) {
|
|
return true;
|
|
}
|
|
if ($organisation === null) {
|
|
return false;
|
|
}
|
|
|
|
return $organisation->users()
|
|
->where('user_id', $user->id)
|
|
->wherePivot('role', 'org_admin')
|
|
->exists();
|
|
}
|
|
}
|