You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Disable / Force / Limit filters on model when find #3640
Hi! Starting with loopback, I'd like to expose a REST api. I'd like to be able to customise the filters.
I don't want users to be able to query whatever they want from my database.
I can't find anything in the doc about that:
How to disable a specific filters ?
How to enable only a subset of filters on certains properties ?
How to force filters ?
How to force limit the results ?
What is the best practice about this use case ?
Completely redefine the find method ?
Thanks !
On Oct 4, 2017, at 1:39 PM, Vincent ***@***.***> wrote:
Hi! Starting with loopback, I'd like to expose a REST api. I'd like to be able to customise the filters.
I don't want users to be able to query whatever they want from my database.
I can't find anything in the doc about that:
How to disable a specific filters ?
How to enable only a subset of filters on certains properties ?
How to force filters ?
How to force limit the results ?
What is the best practice about this use case ?
Completely redefine the find method ?
Thanks !
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub <#3640>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAhA3FlJvddc1iNvtyPle1IWBdykTq2vks5so-0egaJpZM4PuNGX>.
@raymondfeng So to disable filters for a method, we need to implement a beforeRemote hook, and modify the context to delete the "query string" entry I'm guessing. Is this correct ? Is this an issue for ACL if the access token was passed in query string as well ?
@Overdrivr The access_token is passed via query string, so you would have to iterate through and pull out just the access token, and then ensure that you are handling authentication correctly.
I would not go that approach as it can potentially lead to some hard to maintain & understand code.
I would instead say that you should explicitly define the scope for the particular endpoints in the model's JSON file whereyou can explicitly define the following:
the properties should be sent to the client
the properties that should never be sent to the client
the filter (or lack of filter that should be used)
what related should also be included (and an additional scope for what should be returned in that)
Without wanting to hijack, some of the points @Vincz has mentioned are kind of related to my use case where I wish to achieve the following:
A GET to / (i.e. a find operation to list all objects) needs to return objects that are owned by the authenticated user, and also those objects which are owned by other users but have a shared property set. (In other words, when listing the entire collection, you can see all of your own objects and also objects not owned by you which their respective owners have opted to share publicly.)
It is important that the endpoint to / can still accept filter arguments, so that the client can use pagination features (top, limit) and use where to filter returned types.
Initially I looked into using a custom role resolver, such as having one named $ownerOrShared, but this can't work because the role resolvers only operate on endpoints that return one object (i.e. those which take an id in the path).
Next, I looked at enforcing a where filter. This is related to @SippieCup 's point #3 I think, because I believe what @SippieCup is referring to in #3 is the use of Scopes (but please correct me if I am wrong). However, the problems I see with Scopes are:
A named scope cannot take arguments. So, as far as I can tell, a named scope is no use if you want to merge in filter arguments from a HTTP request.
A default scope can be used in my use case, except: (1) I'm unsure if the default scope can get hold of the user ID to filter with (I haven't properly checked this, actually) and (2) to me it seems messy to have to suppress (in code) the use of the default scope by endpoints which do not want that default scope filter applied.
So, in my use case, it seems that the good old remote hook is the only way to go. That is, I intercept the call to /, then I would use the Juggler's mergeQuery function to merge whatever filter the client sends with a where filter that enforces (user owns object || object is shared) on every returned row.
Going back to @Vincz 's question, I struggle to see how the following four points he makes can be addressed in the model JSON alone. To the best of my understanding and knowledge, all of these things would have to be coded manually in a remote hook:
"How to disable a specific filters?" In the JSON file we can define a default scope which seems sort of related to this, but I can't see a mechanism that allows specific filters to be disabled, or which allows specific properties not to be filtered on.
"How to enable only a subset of filters on certains properties?" Comment as above.
"How to force filters?" This is the one that brought me here, because it's applicable to my use case, where I basically want to merge a mandatory where filter with whatever filter the client provides, at a specific endpoint.
"How to force limit the results?" Again the only way I can see is to enforce top and limit properties in a remote hook, which is exactly how I've implemented pagination. See under the "one last thing" header here: https://strongloop.com/strongblog/working-with-pagination-and-loopback/
Hard-coding these kind of requirements into remote hooks (or of course override find() as @raymondfeng suggests) is absolutely no problem, but it would just be interesting to know if I've overlooked a way of achieving any of the above using built-in functionality.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.
Activity
raymondfeng commentedon Oct 4, 2017
SippieCup commentedon Oct 4, 2017
You can also setup the ACL & scope of each method to do so.
Overdrivr commentedon Nov 20, 2017
@raymondfeng So to disable filters for a method, we need to implement a beforeRemote hook, and modify the context to delete the "query string" entry I'm guessing. Is this correct ? Is this an issue for ACL if the access token was passed in query string as well ?
SippieCup commentedon Nov 21, 2017
@Overdrivr The access_token is passed via query string, so you would have to iterate through and pull out just the access token, and then ensure that you are handling authentication correctly.
I would not go that approach as it can potentially lead to some hard to maintain & understand code.
I would instead say that you should explicitly define the scope for the particular endpoints in the model's JSON file whereyou can explicitly define the following:
https://loopback.io/doc/en/lb2/Model-definition-JSON-file.html
TrevorPage commentedon Nov 22, 2017
Without wanting to hijack, some of the points @Vincz has mentioned are kind of related to my use case where I wish to achieve the following:
/(i.e. afindoperation to list all objects) needs to return objects that are owned by the authenticated user, and also those objects which are owned by other users but have asharedproperty set. (In other words, when listing the entire collection, you can see all of your own objects and also objects not owned by you which their respective owners have opted to share publicly.)whereto filter returned types.Initially I looked into using a custom role resolver, such as having one named $ownerOrShared, but this can't work because the role resolvers only operate on endpoints that return one object (i.e. those which take an
idin the path).Next, I looked at enforcing a
wherefilter. This is related to @SippieCup 's point #3 I think, because I believe what @SippieCup is referring to in #3 is the use of Scopes (but please correct me if I am wrong). However, the problems I see with Scopes are:So, in my use case, it seems that the good old remote hook is the only way to go. That is, I intercept the call to
/, then I would use the Juggler'smergeQueryfunction to merge whatever filter the client sends with awherefilter that enforces (user owns object || object is shared) on every returned row.Going back to @Vincz 's question, I struggle to see how the following four points he makes can be addressed in the model JSON alone. To the best of my understanding and knowledge, all of these things would have to be coded manually in a remote hook:
"How to disable a specific filters?" In the JSON file we can define a default scope which seems sort of related to this, but I can't see a mechanism that allows specific filters to be disabled, or which allows specific properties not to be filtered on.
"How to enable only a subset of filters on certains properties?" Comment as above.
"How to force filters?" This is the one that brought me here, because it's applicable to my use case, where I basically want to merge a mandatory
wherefilter with whatever filter the client provides, at a specific endpoint."How to force limit the results?" Again the only way I can see is to enforce top and limit properties in a remote hook, which is exactly how I've implemented pagination. See under the "one last thing" header here: https://strongloop.com/strongblog/working-with-pagination-and-loopback/
Hard-coding these kind of requirements into remote hooks (or of course override find() as @raymondfeng suggests) is absolutely no problem, but it would just be interesting to know if I've overlooked a way of achieving any of the above using built-in functionality.
stale commentedon Jan 21, 2018
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
stale commentedon Feb 4, 2018
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the
CODEOWNERSfile at the top-level of this repository.