Skip to content

include for role mappings to different principalTypes #436

Closed

Description

@nvdnkpr

I read through the issues, the tests and the docs but I couldn't find a word about the possibility to e.g. include all users (but not applications or roles) of a role.

I found issue #332 but

  1. the code described there only works if we change the return call to callback (as described by @haohello)

  2. even if it is working, what would be the best next step to embed user objects in the roles ?

Activity

  1. raymondfeng commented on Aug 4, 2014

    @raymondfeng
    Member
  2. nvdnkpr commented on Aug 4, 2014

    @nvdnkpr
    Author

    I'm currently building my app with LB1 but I have an on LB2 with migration in my mind. I already read about the polymorphic relations, but their aren't used yet in the Role -> RoleMapping -> User/Application/Role Model. Any ETA ?

  3. nvdnkpr commented on Aug 6, 2014

    @nvdnkpr
    Author

    If I extend Role and RoleMapping (I already have an extended User model) can I then already model it with the polymorphic relations ?

  4. raymondfeng commented on Aug 6, 2014

    @raymondfeng
    Member

    You should be able to override the relations. Can you give a try and let us know?

  5. nvdnkpr commented on Aug 6, 2014

    @nvdnkpr
    Author

    It works... I managed to extend Application, User, Role and RoleMapping in the following way:
    (Class with "My" prefix are the extension)

        MyApplication.hasAndBelongsToMany(MyRole, { through: MyRoleMapping, polymorphic: 'principal' });
        MyUser.hasAndBelongsToMany(MyRole, { through: MyRoleMapping, polymorphic: 'principal' });
    
        // optional inverted relations
        MyRole.hasMany(MyApplication, {through: MyRoleMapping, polymorphic: 'principal', invert: true});
        MyRole.hasMany(MyUser, {through: MyRoleMapping, polymorphic: 'principal', invert: true});
    

    and it worked since I got the following methods on the following classes:
    on MyUser: __[findById, destroyById, ...]__roles, …
    on MyApplication: __[findById, destroyById, ...]__roles, …
    on MyRole: __[__findById, destroyById]__applications, …

    so it seem to work on the model level.
    But they don't show up in the Explorer, although e.g.
    MyApplication.sharedClass.find('__findById__roles', false).shared is true.

    I guess that comes from the fact that the classes' relations were added "after" they were published.

    Is there a way to republish all published classes ?

  6. raymondfeng commented on Aug 6, 2014

    @raymondfeng
    Member

    Cool.

    1. I think we'll probably remodel role/rolemapping using ploymorphic relations.
    2. Where did you set up the relations?
    3. Expose new models created after the explorer is initiated loopback-component-explorer#22
  7. nvdnkpr commented on Aug 6, 2014

    @nvdnkpr
    Author

    I extended Role, Application, User and RoleMapping, prefixed the new models with "My" and set up the relations entirely in MyRole using the code below (it's my MyRole.js)

    /*
      Module dependencies
    */
    var _ = require('underscore');
    var util = require('util')
    var debug = require('debug')('myapp:models:MyRole');
    
    module.exports = function(MyRole){
      MyRole.on('dataAccessConfigured',
        function(MyRole){
          process.nextTick(function(){
            // retrieve app model registry
            var models = MyRole.app.models;
            // get MyApplication and MyUser
            var MyRoleMapping = models.MyRoleMapping;
            var MyApplication = models.MyApplication;
            var MyUser = models.MyUser;
    
            //
            MyApplication.hasAndBelongsToMany(MyRole, { through: MyRoleMapping, polymorphic: 'principal' });
            MyUser.hasAndBelongsToMany(MyRole, { through: MyRoleMapping, polymorphic: 'principal' });
    
            // optional inverted relations
            MyRole.hasMany(MyApplication, {through: MyRoleMapping, polymorphic: 'principal', invert: true});
            MyRole.hasMany(MyUser, {through: MyRoleMapping, polymorphic: 'principal', invert: true});
          });
        });
    };

    By the way:
    As you can see I still had the problem that was reported was by coodoo in #410 and by me in #425. The relations on the show up in explorer but not on the model when listening for 'dataAccessConfigured' (I guess that was the "new" event to listen to to be notified that all models are ready to go.). I solved that by using process.nextTick().

    This could possibly be my problem for the current situation that it works on the model level but is not shown in the loopback-explorer.

  8. nvdnkpr commented on Aug 6, 2014

    @nvdnkpr
    Author

    Regarding 3.
    How can I get that to work ? Your PR seemed to have failed, so what is the event to listen on with the loopback-explorer ?

  9. raymondfeng commented on Aug 6, 2014

    @raymondfeng
    Member

    You should listen dataSourceAttached which will be emitted after dataAccessConfigured.

  10. nvdnkpr commented on Aug 6, 2014

    @nvdnkpr
    Author

    I changed the listener to dataSourceAttached, but I still need the process.nextTick to access the methods. The code I wrote for the polymorphic relations does not work without the process.nextTick. Without it I get a TypeError: Cannot read property 'models' of undefined Error

  11. nvdnkpr commented on Aug 7, 2014

    @nvdnkpr
    Author

    It seems the polymorphic relations arent that stable after all. I posted a new issue, see #446

  12. nvdnkpr commented on Aug 8, 2014

    @nvdnkpr
    Author

    After #446 being closed, I managed (succesfully) to model the polymorphic relation (which btw still needed a process.nextTick to work:

    MyRole.on('dataSourceAttached',function(){process.nextTick(function(){
        var app = MyRole.app;
        // get MyApplication and MyUser
        var RoleMapping = app.models.MyRoleMapping;
        var Application = app.models.MyApplication;
        var User = app.models.MyUser;
    
        //
        Application.hasAndBelongsToMany(MyRole, { through: RoleMapping, polymorphic: 'principal'});
        User.hasAndBelongsToMany(MyRole, { through: RoleMapping, polymorphic: 'principal'});
    
        // optional inverted relations
        MyRole.hasMany(Application, {through: RoleMapping, polymorphic: 'principal', invert:true});
        MyRole.hasMany(User, {through: RoleMapping, polymorphic: 'principal', invert:true});
    });});

    Then I tried to model the same relationship via the JSON files using

    {
      "name": "[MyUser|MyApplication]",
      "base": "[User|Application]",
      "relations":{
        "roles":{
          "model": "MyRole",
          "type": "hasAndBelongsToMany",
          "through": "MyRoleMapping",
          "polymorphic": "principal",
        }
      }
    }

    and the appropriate relation modeling on MyRole:

    {
      "name": "MyRole",
      "plural": "roles",
      "base": "Role",
      "relations": {
        "applications":{
          "model": "MyApplication",
          "type": "hasMany",
          "through": "MyRoleMapping",
          "polymorphic": "principal",
          "invert":true
        },
        "users":{
          "model": "MyUser",
          "type": "hasMany",
          "through": "MyRoleMapping",
          "polymorphic": "principal",
          "invert":true
        },
      },
    }

    but that created a wrong data model for MyRoleMapping (id, principalType, principalId, applicationId, userId, roleId).

    Did I model it wrong or is this just another bug ? If so tell me, I'm going to open another issue ;)

  13. superkhau commented on Feb 26, 2015

    @superkhau
    Contributor

    @raymondfeng Is this still relevant?

  14. mike-aungsan commented on Nov 5, 2015

    @mike-aungsan

    I still have similar issue extending RoleMapping, AccessToken and ACL

  15. behrad commented on Dec 14, 2015

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions