swang/node-stormpath

Working example / test of createGroupMembership?

Closed this issue · 4 comments

Hello,

I am having difficulty getting the createGroupMembership method working. I am passing in the appropriate acct and group ids, but the method doesn't seem to use these to construct the post.

I have got it working using the below hack (account and group checking removed), but wonder if I am simply not using it correctly, or if indeed there may be a bug.

Application.prototype.createGroupMembership = function(acct, group, callback) {

  return new GroupMemberships(this.options).setData({
    account: {
      href: c.BASE_URL + '/' + acct
    },
    group: {
      href: c.BASE_URL + '/' + group
    }
  }).create(function(err, body) {
    callback(err, body)
  })
}

Thanks so much for your time on this library, it is very useful to me!

Regards, Rob

Hi Rob

The use case I had for Stormpath made it easier if I passed email for the account, and the name of the group I wanted to add.

So if your account email was rob@iotaweb, and you wanted to be added to the group, Developers you would write it as such:

var a = (new Application(appId, options))

a.createGroupMembership({ email: "rob@iotaweb" }, { name: "Developers" },  function(err, result) {
  console.log(err, result)
})

Tx Shuan, that makes sense :)

I'll try that method and see if it works for me.

Just one more question if you don't mind - is there a convenient way to remove a Group Membership? I'm using this feature for account upgrades/downgrades and have a hacky way of doing it, i.e.

Account.prototype.deleteGroupMembership = function(membershipId, callback) {
    this.request('DELETE', ['groupMemberships', membershipId], function(err, body) {
      if (callback) {
        if (err) return callback(err)
        callback(null, body)
      }
    })
}

It is working for me, but I'd be interested in any suggestions for a better method that is more in keeping with your approach.

If membershipId is the ID for that groupMembership then you can do

;(new GroupMembership(options).setId(membershipId).delete())

You can also pass a callback into the delete function if you need to check for success.

Right now if you pass two arguments to the constructor, it assumes the first one is a URL that contains the id that you want, e.g. http://api.stormpath.com/<appId>/accounts/<accountId> but you can't pass the id directly ATM.

Thanks for the explanation - I'll try the method you suggest!