Skip to content

Disable / Force / Limit filters on model when find #3640

Closed

Description

@Vincz

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 !

Activity

  1. raymondfeng commented on Oct 4, 2017

    @raymondfeng
    Member
  2. SippieCup commented on Oct 4, 2017

    @SippieCup

    You can also setup the ACL & scope of each method to do so.

  3. Overdrivr commented on Nov 20, 2017

    @Overdrivr

    @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 ?

  4. SippieCup commented on Nov 21, 2017

    @SippieCup

    @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:

    1. the properties should be sent to the client
    2. the properties that should never be sent to the client
    3. the filter (or lack of filter that should be used)
    4. what related should also be included (and an additional scope for what should be returned in that)

    https://loopback.io/doc/en/lb2/Model-definition-JSON-file.html

  5. TrevorPage commented on Nov 22, 2017

    @TrevorPage

    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.

  6. stale commented on Jan 21, 2018

    @stale

    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.

  7. stale commented on Feb 4, 2018

    @stale

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions