4

I would like to add a new role for a user, when he is able to give the right password to Hubot.

Here is the script i have for now :

module.exports = (robot) ->
  robot.respond /PasswOrd (.*)/, (res) ->
    paSS = res.match[1]
    if paSS is "1234"
      role = 'h1'
      user = robot.brain.userForName(res.message.user.name)
      #CODE TO ADD ROLE h1 FOR THIS USER ???
      res.reply "Correct." + user.id
    else
      res.reply "Incorrect password."

It's launching correctly with @bot PasswOrd 1234

But i don't know how to add the role.
Thanks.

bob dylan
  • 989
  • 2
  • 10
  • 26

1 Answers1

0

Seeing no other way, i was able to "solve" this using the REST API. Is it a bad idea ?

I've simplified my actual code to answer the original question.

user = robot.brain.userForName(res.message.user.name)
robot.http("https://xxx.xxx.xxx/api/v1/users.info?userId=" + user['id'])
    .headers("X-Auth-Token":"xxxxxxxxxxxxxxx", "X-User-Id":"xxxxxxx")
    .get() (err, response, body) ->
        Info = JSON.parse(body)
        Info.user.roles.push("newRole")
        robot.http("https://xxx.xxx.xxx/api/v1/users.update")
            .headers("X-Auth-Token":"xxxxxxxxxxxxxxx", "X-User-Id":"xxxxxxx", "Content-type":"application/json")
            .post('{"userId": "' + user['id'] + '", "data": { "roles": ' + JSON.stringify(Info.user.roles) + ' }}')
bob dylan
  • 989
  • 2
  • 10
  • 26