Replies: 2 comments 1 reply
-
You could instruct your developers to extend their FormRequests your OrganisationFormRequest and implement/override the methods they need. If you test your routes to always use a controller that extends OrganisationController, the ide will warn the developer if hey are not calling the parent constructor, and the tests will fail and block other ci steps if a controller is not using the OrganisationController. |
Beta Was this translation helpful? Give feedback.
-
You don't want to see users hijacking your Thus you will have the public function rules(): void
{
return [
'limit' => 'nullable|integer|min:1|max:20',
'keyword' => 'nullable|string',
];
} Additionally, within the FormRequest's scope, you can create multiple getter methods to get the related Models of the Request, eg: public function getOption(): ?Option
{
return Option::find($this->input('option_id'));
} So from your controller, simply hit the method to get the Model with the exact public function index(PostIndexRequest $request): JsonResponse
{
$option = $request->getOption(); // Option type here, use it, IDE loves it.
// ...
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have an application where I have to deal with some sloppy developers. We can't get rid of them, so I've been tasked with making the development process as idiot-proof as possible.
One problem these developers have is that they often forget to do input validation and user authorization. In the past, this has resulted in major problems with methods that allow junk input (e.g. invalid email addresses) and/or allow users to do things they do not actually have permission to do.
In order to make it harder for them to make these mistakes, I suggested that we use
FormRequest
as much as possible because that way all requests have automatic authorization and validation. We plan to use this structure:It feels a little weird to do this for
GET
requests (i.e.index
andshow
) andDELETE
requests because those requests have no body. Validation can still be useful for these requests (query parameters forGET
to include relations and for whetherDELETE
should be hard or soft), but it feels like there should be a nicer way to do it.Also, when using model binding for a resource that doesn't have query parameters, the
$request
parameter and model parameter both need to be defined on theshow
method, but only one of them will be used. For example:Organizations
ControllerThis uses both the injected model and the request:
Addresses
ControllerThis uses only the injected model, so IDEs will mark the
$request
as unused and prompt the developer to remove it:If developers follow that recommendation from the IDE, it will cause the authorization to be bypassed, which is what I'm trying to prevent.
I supposed I could say to use policies for authorization instead of
FormRequest
s and use the$this->authorizeResource(...)
method in the controller constructor, but that has two issues:FormRequest
injection, but they are not included when callingauthorizeResource
so they need to have manually-added validation.FormRequest
for validation ofPOST
/PUT
/PATCH
requests, and it will be confusing for the developers to have justreturn true
in theauthorize
method of theFormRequest
.I know the best option would be "either educate the developers better or get rid of them and get good ones," but that is not currently a practical solution (and I'm not in charge of hiring anyway).
Given these constraints, is my original approach (using
FormRequest
everywhere) my best option? Or is there something else I should be doing?Beta Was this translation helpful? Give feedback.
All reactions