Skip to content

Commit 8c268a1

Browse files
committed
Fix update request did not go through global polciies
1 parent e1805e3 commit 8c268a1

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ npm install --save sails-json-api-blueprints
1717
````
1818

1919
Please note the following :
20+
- Default policies (`'*': 'somePolicy'`) will not work by default for update requests. See *Usage:Policies are not applied on custom actions for updates (PATCH method)* for details on how to fix it.
2021
- Being a set of blueprints this only works if `sails.config.blueprints.rest` is set to true (is it by default)
2122
- `sails.config.blueprints.pluralize` will be set to true to match the JSON API specification
2223
- Default responses will be overridden to respond with valid JSON API errors
@@ -47,6 +48,29 @@ As shown in [tests/dummy/api/controllers/UserController.js:24](https://github.co
4748
- `destroyOneRecord` DELETE /{model}
4849
- `updateOneRecord` PATCH /{model}/{id}
4950

51+
## Policies are not applied on custom actions for updates (PATCH method)
52+
53+
By default, due to the fact updates are handled with PUT methods and not PATCH methods in sails, *sails-json-api-blueprints* have to inject a route redirecting all incoming PATCH request to the update action. This is transparent for the user, but this means requests do no go though default policies if any.
54+
55+
To fix this, create a new route in `config/routes.js`:
56+
57+
````
58+
'PATCH /api/:model/:id': {
59+
controller: 'App', // replace with an actual controller
60+
action: 'update' // This can be any name
61+
}
62+
````
63+
64+
Then in `AppController.js`, add the following `update` method:
65+
66+
````
67+
update: function(req, res) {
68+
return JsonApiService.updateMethod(req, res);
69+
}
70+
````
71+
72+
This way you are guaranteed incoming requests go through default policies before being redirected to the update blueprint.
73+
5074
## Data validation
5175

5276
This module is compatible with default Sails.js waterline validations and *sails-hook-validation*. It will produce a JSON API error compliant object.

lib/api/services/JsonApiService.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,27 @@ module.exports = {
142142
return converted;
143143
},
144144

145+
/*
146+
* Function to call to process a generic PATCH request
147+
*
148+
* @param {Object} Sails request object
149+
* @param {Object} Sails response object
150+
*/
151+
updateMethod: function(req, res) {
152+
let id = req.allParams()['id'];
153+
let model = pluralize.singular(req.param('model'));
154+
155+
req.options.controller = model;
156+
req.options.model = model;
157+
158+
if (sails.controllers[model].update !== undefined) {
159+
return sails.controllers[model].update(req, res);
160+
} else {
161+
req.body = JsonApiService.deserialize(req.body);
162+
return JsonApiService.updateOneRecord(req, res);
163+
}
164+
},
165+
145166
deserialize: function(data) {
146167

147168
var caseSetting = this.getAttributesDeserializedCaseSetting();

lib/hook.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,7 @@ module.exports = function(sails) {
6262
path = 'PATCH /:model/:id';
6363
}
6464
patchRoute[path] = function(req, res) {
65-
let id = req.allParams()['id'];
66-
let model = pluralize.singular(req.param('model'));
67-
68-
req.options.controller = model;
69-
req.options.model = model;
70-
71-
if (sails.controllers[model].update !== undefined) {
72-
return sails.controllers[model].update(req, res);
73-
} else {
74-
req.body = JsonApiService.deserialize(req.body);
75-
return BlueprintController.update(req, res);
76-
}
65+
return JsonApiService.updateMethod(req, res);
7766
};
7867

7968
/**

0 commit comments

Comments
 (0)