loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Login should allow a full fledged include filter

Open pulkitsinghal opened this issue 11 years ago • 34 comments

Developers should be able to capture a complete picture of a User model upon login from angular like so:

        UserModel.login({
          realm: 'myRealm',
          username: username,
          password: password,
          include: {user: ['roles', ..., 'otherModels']}
        });

What are your thoughts on this enhancement request?

Also not sure what this test teaches us: https://github.com/strongloop/loopback/blob/553a3b208386a0e946cccfcfdba1d83eae8a6f15/test/user.test.js#L401

pulkitsinghal avatar Jun 23 '15 00:06 pulkitsinghal

The workaround (it may be a lapse in my understanding) seems a bit wasteful/slow when retrieving user's related models via promise chaining after login.

For example:

  1. If I chained UserModel.login() with UserModel.findById() ... I can't use the include filter to get the related models as the include filter isn't supported for findById
  2. If I chain login with findOne which support include filter, it will fail due to authorization reasons ... something about loopback not being able to apply ACLs unless an id is known in the REST url.
  3. So I'm left with one call per related model being chained like so:
UserModel.login()
  > UserModel.storeModels({id:xxx})
    > UserModel.prototype$__get__roles({id:xxx})

silly no?

pulkitsinghal avatar Jun 23 '15 13:06 pulkitsinghal

You should be using approach 2, login chained with findOne w/ include. I'm not sure why it's failing. Show us how you were doing it?

doublemarked avatar Jun 23 '15 14:06 doublemarked

@doublemarked - sure, here's what I tried for approach 2:

This works but does not have roles: UserModel.findOne({id: $sessionStorage.currentUser.userId})

It doesn't work when I try to include roles:

                UserModel.findOne({
                  filter: {
                    where: {
                      id: $sessionStorage.currentUser.userId
                    },
                    include: [
                      'roles'
                    ]
                  }
                })

I think @beeman uses it here: https://github.com/beeman/loopback-angular-playground/blob/498f39b9c48c4984cedf5c574ce692c7a114247f/client/js/services/auth.js#L7-L10

I get an error:

{
   "error":{
      "name":"Error",
      "status":403,
      "message":"Access Denied",
      "statusCode":403,
      "code":"ACCESS_DENIED",
      "stack":"Error: Access Denied
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/lib/application.js:348:21
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/lib/model.js:313:7
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/common/models/acl.js:465:23
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:251:17
    at done (/Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:132:19)
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:32:16
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:248:21
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:572:34
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/common/models/acl.js:447:17
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/common/models/role.js:319:23
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:368:13
    at done (/Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:132:19)
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:32:16
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/async/lib/async.js:365:17
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/common/models/role.js:310:15
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback-datasource-juggler/lib/dao.js:1442:62
    at allCb (/Users/pulkitsinghal/dev/w2/node_modules/loopback-datasource-juggler/lib/dao.js:1374:7)
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback-datasource-juggler/lib/connectors/memory.js:371:7
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:188:31
    at /Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:188:31
    at process._tickDomainCallback (node.js:492:13)
    at process.<anonymous> (/Users/pulkitsinghal/dev/w2/node_modules/loopback/node_modules/continuation-local-storage/node_modules/async-listener/index.js:19:15)"
   }
}

pulkitsinghal avatar Jun 23 '15 17:06 pulkitsinghal

@pulkitsinghal To get include: 'roles working you need to define the relationship: User.hasMany(Role, {through: RoleMapping, foreignKey: 'principalId'});

faridnsh avatar Aug 19 '15 05:08 faridnsh

@alFReD-NSH - Thanks! I've had that for a while: https://github.com/ShoppinPal/warehouse/blob/develop/common/models/user-model.json#L43-L48

I think it is just the way login is implemented, it doesn't do certain things. So I'd like it enhanced :)

pulkitsinghal avatar Aug 19 '15 13:08 pulkitsinghal

+1, would like to see the ability to include more than just the "user". it is strange that it is limited ONLY to the user and no sub relations

stringbeans avatar Aug 26 '15 23:08 stringbeans

+1

mamartins avatar Feb 08 '16 10:02 mamartins

+1 I should be able to log in and have related objects returned (using include) in a single REST call. As I understand it, I can only get the user object returned in a single call.

lee-40square avatar Jul 25 '16 20:07 lee-40square

+1

dongmai avatar Aug 12 '16 03:08 dongmai

+1

dongmai avatar Aug 31 '16 09:08 dongmai

+1

acabreragnz avatar Sep 07 '16 21:09 acabreragnz

+1

tatianacmh avatar Sep 07 '16 21:09 tatianacmh

+1

mradzinski avatar Sep 07 '16 21:09 mradzinski

+1

rkmax avatar Sep 07 '16 21:09 rkmax

+1

luckita avatar Sep 07 '16 22:09 luckita

+1

carlos-jovel avatar Sep 07 '16 22:09 carlos-jovel

+1

ghost avatar Sep 07 '16 23:09 ghost

+1

EddSuarez avatar Sep 07 '16 23:09 EddSuarez

+1

alfredooo avatar Sep 08 '16 01:09 alfredooo

+1

brusherru avatar Sep 08 '16 14:09 brusherru