laravel-vue-file-share/app/Http/Requests/ParentIDBaseRequest.php
2023-10-08 14:20:03 -07:00

52 lines
1.5 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Auth;
use App\Models\File;
class ParentIDBaseRequest extends FormRequest
{
public ?File $parent = null;
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
// gets first file (main folder) with parent_id as id
$this->parent = File::query()->where('id', $this->input('parent_id'))->first();
// if there is a a parent but it is not owned by the user
if ($this->parent && !$this->parent->isOwner(Auth::id())) {
// stop
return false;
}
// otherwise continue
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
// validate parent id
return [
'parent_id' => [
Rule::exists(File::class, 'id')
->where(function (Builder $query) {
return $query
->where('is_folder', '1') // must be a folder
->where('created_by', Auth::id()); // must be made by current user
}
)
]
];
}
}