I am making a Stormpath-Express application for a company configured with many groups. Each group can only see certain elements on the page (ie. admins can delete and approve posts, users can only create and edit them). Is it possible to use Jade to create a conditional that will display the component by getting the user's group membership?
Here's a rough example to show what I mean:
if user.admin === true
a.btn.submit Approve Post
else
p Please wait for management to approve
I have noticed I can use user.fullName, user.email, etc. but not user.directory.
I have also tried this through the server side using getDirectory method
client.getDirectory(href, function(err, dir) {
console.log(dir);
});
However, this is from the Node docs, and breaks when applied to my Express project. Any help with this is greatly appreciated since I'm in a deadline!
Much thanks to rdegges answer, I had to modify with the version 1.0.5 express-stormpath because the groups property is used to store the uri to retrieve the groups...
app.get('/page', stormpath.loginRequired, function(req, res) {
req.user.groupsNames = [];
req.user.getGroups(function(err, groups) {
if (err) return next(err);
groups.each(function(group, cb) {
req.user.groupsNames.push(group.name);
cb();
}, function(err) {
if (err) return next(err);
return res.render('myview');
});
});
});
And the jade template looks like...
- if (user.groupNames.indexOf('admin') > -1)
a.btn.submit Approve Post
- else
p Please wait for management to approve
Heyo -- I'm the author of the express-stormpath library, figured I'd hop in here.
I'm actually going to be adding in some new ways to do this in the near future -- but for now, what you can do is pre-load a user's groups into an array, then check membership. Here's an example:
app.get('/page', stormpath.loginRequired, function(req, res) {
req.user.groups = [];
req.user.getGroups(function(err, groups) {
if (err) return next(err);
groups.each(function(group, cb) {
req.user.groups.push(group);
cb();
}, function(err) {
if (err) return next(err);
return res.render('myview');
});
});
});
Now that you've defined req.user.groups, you can then use this in your Jade template, like so:
- if (user.groups.indexOf('admin') > -1)
a.btn.submit Approve Post
- else
p Please wait for management to approve
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With