Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting group name from account in Stormpath-Express

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!

like image 237
Alejandro Avatar asked Dec 04 '25 10:12

Alejandro


2 Answers

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
like image 68
user3345580 Avatar answered Dec 07 '25 14:12

user3345580


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
like image 30
rdegges Avatar answered Dec 07 '25 15:12

rdegges