Skip to content

"shared" property on User model not working #410

Closed

Description

@coodoo

I'm having a look at loopback-example-app and playing around location.js, trying to block one method from being accessed via REST API:

  1. Setting RentalLocation.find.shared = false;
  2. Try to access that method in explorer and expect it to be hidden or not accessible, but it still returns result.

Thought that was the correct way to hide a method from showing up in REST end point?

Activity

  1. sam-artuso commented on Jul 29, 2014

    @sam-artuso

    same problem here.

  2. ritch commented on Jul 29, 2014

    @ritch
    Member

    What version of loopback?

  3. sam-artuso commented on Jul 29, 2014

    @sam-artuso
  4. coodoo commented on Jul 30, 2014

    @coodoo
    Author

    v2.

  5. kontza commented on Jul 30, 2014

    @kontza

    Ditto here, running version 2. Any workarounds available?

  6. raymondfeng commented on Jul 30, 2014

    @raymondfeng
    Member

    @ritch is working on a fix.

    Thanks,


    Raymond Feng
    Co-Founder and Architect @ StrongLoop, Inc.

    StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

    On Jul 30, 2014, at 3:12 AM, Juha Ruotsalainen notifications@github.com wrote:

    Ditto here, running version 2. Any workarounds available?

    —
    Reply to this email directly or view it on GitHub.

  7. ritch commented on Jul 30, 2014

    @ritch
    Member

    In 2.0, I'm trying to encourage people to use the strong remoting objects directly. Here is an example.

    module.exports = function(MyModel) {
      var isStatic = true;
      MyModel
        .sharedClass
        .find('find', isStatic)
        .shared = false;
    }

    Take a look at the SharedClass documentation for more.

    Edit: find is a static method

  8. coodoo commented on Jul 31, 2014

    @coodoo
    Author

    This would work, thanks for the tips.

    Btw, should this go into documentation somewhere?

  9. johnsoftek commented on Jul 31, 2014

    @johnsoftek

    I am a little confused by the example. Isn't 'find' a static method? What does isStatic = false mean?

  10. raymondfeng commented on Jul 31, 2014

    @raymondfeng
    Member

    Each remote model has a sharedClass property representing the remoting metadata. The find method allows you to look up the SharedMethod by name so that you can change the shared flag. The isStatic controls if the remote method is a static or prototype method.

    IMO, we should provide a helper method, such as:

    Model.disableRemoteMethod(name, isStatic)

  11. nvdnkpr commented on Jul 31, 2014

    @nvdnkpr

    What about the shared status of related models ? How can I (taking the mentioned User model as example) disable the find/deleteById/update method for the user's access tokens ??

  12. raymondfeng commented on Jul 31, 2014

    @raymondfeng
    Member

    At this moment, relation methods are internally added to the model prototype, for example,

    User.prototype.__get__accessTokens
    User.prototype.__create__accessTokens
    User.prototype.__delete__accessTokens
    

    You can use these names to disable the remoting.

  13. lchenay commented on Jul 31, 2014

    @lchenay
    Contributor

    I'm still unable to disable shared method with sharedClass:

        var method = User
            .sharedClass
            .find('login', true);
    
        method.shared = false;
    

    Method is still accessible.

  14. nvdnkpr commented on Jul 31, 2014

    @nvdnkpr

    Hello Raymond,
    I know that they are internally added and that I can disable them by setting this methods to .shared = false. Problem is : There aren't any on that Models prototype 😄 . If you wan't to reproduce, try it in the loopback-example app: Add a customer.js and log out the Prototype.
    e.g.

    module.exports = function(Customer){
      console.log(Customer.prototype);
      console.log(Customer.super_.prototype);
    };
    
  15. lchenay commented on Jul 31, 2014

    @lchenay
    Contributor

    Try it inside a
    Customer.on('dataSourceAttached', function (obj) {});
    to be sure that all data are attached.

  16. 16 remaining items

  17. lchenay commented on Jul 31, 2014

    @lchenay
    Contributor

    For some method, the shared property are not set correctly.

        User.on('dataSourceAttached', function (obj) {
            User.sharedClass.find('__findById__accessTokens').shared = false;
            if (User.sharedClass.find('__findById__accessTokens').shared) {
                throw new Error('Shared property have not be set correctly');
            }
        });
    
  18. coodoo commented on Aug 1, 2014

    @coodoo
    Author

    [edited]

    False alarm, some how I can't reproduce the issue again after restarting the server and browser.

  19. raymondfeng commented on Aug 1, 2014

    @raymondfeng
    Member

    I'll create a different event to avoid racing conditions.

    Sent from my iPhone 5

    On Jul 31, 2014, at 11:22 PM, Jeremy Lu notifications@github.com wrote:

    Found another issue.

    There is indeed racing condition happening as Feng suggested, at least for custom model.

    I have a ResetKey model extending PersistedModel.

    When I try to hide the "find" method inside "dataSourceAttached" event handler with code suggested above, it won't work, the method is still accessible.

    If I move the code outside of 'dataSourceAttached' event, it worked.

    But for built-in models, like User and AccessToken, one has to put the code inside 'dataSourceAttached' to make the disabling work.

    It's a bit confusing and looks like a bug to me, thought?

    Ps. I've upgraded to the latest version of everything.

    —
    Reply to this email directly or view it on GitHub.

  20. coodoo commented on Aug 1, 2014

    @coodoo
    Author

    @raymondfeng I just edited the comment, seemed to be a false alarm, my apology.

  21. coodoo commented on Aug 1, 2014

    @coodoo
    Author

    I did more tests and seems one needs to wait a bit before setting those value, here's what I got so far, confirmed to work on both custom and built-in models.

    The most interesting part is, if I run setup() before banMethods(), it won't work.

    ResetKey.on('dataSourceAttached', function(obj) {
            setTimeout(function(){
              promisifyMethods();
              banMethods(); // set shared to false here
              setup();
              modelDidAttach();
            },0)
        })
    
    function banMethods(){
          ResetKey.sharedClass.find('find', true).shared = false;
        }
    
    function setup() {
            ResetKey.base.setup.apply(ResetKey, arguments);
        }
  22. raymondfeng commented on Aug 1, 2014

    @raymondfeng
    Member

    In node, events are processed synchronously. Your listener and the loopback internal ones to set up relation methods are invoked by the order of registration. setTimeout defers your logic to future ticks and it sure helps. But the root problem is still there - dataSourceAttached event does not guarantee the model is fully configured.

    Sent from my iPhone 5

    On Jul 31, 2014, at 11:41 PM, Jeremy Lu notifications@github.com wrote:

    I did more tests and seems one needs to wait a bit before setting those value, here's what I got so far, confirmed to work on both custom and built-in models.

    ResetKey.on('dataSourceAttached', function(obj) {
    setTimeout(function(){
    promisifyMethods();
    banMethods(); // set shared to false here
    setup();
    modelDidAttach();
    },0)

    })
    

    —
    Reply to this email directly or view it on GitHub.

  23. coodoo commented on Aug 1, 2014

    @coodoo
    Author

    Agreed, there should be a better event, maybe "initialized", which guarantee the model is fully configured.

  24. ritch commented on Aug 1, 2014

    @ritch
    Member

    @lchenay Can you open a new issue with your circumstances? I have a feeling it is because you are extending a built in model.

  25. clark0x commented on Aug 9, 2014

    @clark0x
    Contributor

    I have the same problem. shared = false doesn't work for all scope methods.

  26. clark0x commented on Aug 9, 2014

    @clark0x
    Contributor

    check https://github.com/strongloop/strong-remoting/blob/master/lib/shared-class.js#L96

    sharedClass.find() calls sharedClass.methods(), which calls resolver() (https://github.com/strongloop/loopback/blob/master/lib/models/model.js#L187).

    While resolver() will eventually create a new branch of SharedMethods for each relation and scope everytime. That's why User.sharedClass.find('__destroyById__accessTokens', false).shared = false won't work.

  27. nvdnkpr commented on Aug 18, 2014

    @nvdnkpr

    is there any update on this ??

  28. clark0x commented on Sep 1, 2014

    @clark0x
    Contributor

    @nvdnkpr @coodoo @lchenay I have made a PR to fix this issue, could you check if that works in your case? strongloop/strong-remoting#95

  29. bajtos commented on Nov 26, 2014

    @bajtos
    Member

    Hey everybody, the issue has been fixed since loopback 2.4.0. Now you can do:

    var isStaticMethod = true;
    RentalLocation.disableRemoteMethod('find', isStaticMethod);

    See also #563, #595 and strongloop/strong-remoting#109.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions