Login should allow a full fledged include filter
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
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:
- If I chained
UserModel.login()withUserModel.findById()... I can't use theincludefilter to get the related models as theincludefilter isn't supported forfindById - If I chain
loginwithfindOnewhich supportincludefilter, it will fail due to authorization reasons ... something about loopback not being able to apply ACLs unless anidis known in the REST url. - 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?
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 - 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 To get include: 'roles working you need to define the relationship: User.hasMany(Role, {through: RoleMapping, foreignKey: 'principalId'});
@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 :)
+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
+1
+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.
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1