Checking Group Membership of UID
-
We're currently building our own authentication plugin and are now stuck at checking the group membership of a user.
Snippet that doesn't work so far:
var Groups = Groups = module.parent.require('./groups');
var isAdmin = Groups.isMember(uid, 'administrators', function() {});
if (isAdmin) { ... }
Another not-working idea:
var isAdmin = Groups.isMember(uid, 'administrators', function(err, isMember) {
if (isMember) { return true; }
});
Any ideas how to solve this?
-
You can't return values from async functions like that you need to check the isMember value inside the callback.
Groups.isMember(uid, 'administrators', function(err, isAdmin) { if (err) { return console.error(err); } if (isAdmin) { console.log('user ' + uid + ' is an admin'); } else { console.log('user ' + uid + ' is not an admin'); } });
-
@alexschomb Does it throw an error?
If you want to check if a user is an admin then you can probably use
User.isAdministrator
-
Thanks to both of you for your quick help!
Both solution do work indeed. As baris pointed out, my failure was returning values from async functions.
Here is a working example for each solution:
var Groups = module.parent.require('./groups'); Groups.isMember(uid, 'administrators', function(err, isMember) { if (err) { next(err); } if (isMember) { winston.info('user is an administrator'); Groups.join('administrators', uid); } else { winston.info('user is no administrator'); Groups.leave('administrators', uid); } });
var User = module.parent.require('./user'); var Groups = module.parent.require('./groups'); User.isAdministrator(uid, function(err, isAdmin) { if (err) { next(err); } if (isAdmin) { winston.info('user is an administrator'); Groups.join('administrators', uid); } else { winston.info('user is no administrator'); Groups.leave('administrators', uid); } });