|
| 1 | +# Description |
| 2 | +# Assign roles to users and restrict command access in other scripts. |
| 3 | +# |
| 4 | +# Configuration: |
| 5 | +# HUBOT_AUTH_ADMIN - A comma separate list of user IDs |
| 6 | +# |
| 7 | +# Commands: |
| 8 | +# hubot <user> has <role> role - Assigns a role to a user |
| 9 | +# hubot <user> doesn't have <role> role - Removes a role from a user |
| 10 | +# hubot what role does <user> have - Find out what roles are assigned to a specific user |
| 11 | +# hubot who has admin role - Find out who's an admin and can assign roles |
| 12 | +# |
| 13 | +# Notes: |
| 14 | +# * Call the method: robot.auth.hasRole(msg.envelope.user,'<role>') |
| 15 | +# * returns bool true or false |
| 16 | +# |
| 17 | +# * the 'admin' role can only be assigned through the environment variable |
| 18 | +# * roles are all transformed to lower case |
| 19 | +# |
| 20 | +# * The script assumes that user IDs will be unique on the service end as to |
| 21 | +# correctly identify a user. Names were insecure as a user could impersonate |
| 22 | +# a user |
| 23 | + |
| 24 | +module.exports = (robot) -> |
| 25 | + |
| 26 | + unless process.env.HUBOT_AUTH_ADMIN? |
| 27 | + robot.logger.warning 'The HUBOT_AUTH_ADMIN environment variable not set' |
| 28 | + |
| 29 | + if process.env.HUBOT_AUTH_ADMIN? |
| 30 | + admins = process.env.HUBOT_AUTH_ADMIN.split ',' |
| 31 | + else |
| 32 | + admins = [] |
| 33 | + |
| 34 | + class Auth |
| 35 | + hasRole: (user, roles) -> |
| 36 | + user = robot.brain.userForId(user.id) |
| 37 | + if user? and user.roles? |
| 38 | + roles = [roles] if typeof roles is 'string' |
| 39 | + for role in roles |
| 40 | + return true if role in user.roles |
| 41 | + return false |
| 42 | + |
| 43 | + usersWithRole: (role) -> |
| 44 | + users = [] |
| 45 | + for own key, user of robot.brain.data.users |
| 46 | + if robot.auth.hasRole(msg.envelope.user, role) |
| 47 | + users.push(user) |
| 48 | + users |
| 49 | + |
| 50 | + robot.auth = new Auth |
| 51 | + |
| 52 | + robot.respond /@?(.+) (has) (["'\w: -_]+) (role)/i, (msg) -> |
| 53 | + name = msg.match[1].trim() |
| 54 | + newRole = msg.match[3].trim().toLowerCase() |
| 55 | + |
| 56 | + unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why'] |
| 57 | + user = robot.brain.userForName(name) |
| 58 | + return msg.reply "#{name} does not exist" unless user? |
| 59 | + user.roles or= [] |
| 60 | + |
| 61 | + if newRole in user.roles |
| 62 | + msg.reply "#{name} already has the '#{newRole}' role." |
| 63 | + else |
| 64 | + if newRole is 'admin' |
| 65 | + msg.reply "Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ADMIN env variable." |
| 66 | + else |
| 67 | + myRoles = msg.message.user.roles or [] |
| 68 | + if msg.message.user.id.toString() in admins |
| 69 | + user.roles.push(newRole) |
| 70 | + msg.reply "Ok, #{name} has the '#{newRole}' role." |
| 71 | + |
| 72 | + robot.respond /@?(.+) (doesn't have|does not have) (["'\w: -_]+) (role)/i, (msg) -> |
| 73 | + name = msg.match[1].trim() |
| 74 | + newRole = msg.match[3].trim().toLowerCase() |
| 75 | + |
| 76 | + unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why'] |
| 77 | + user = robot.brain.userForName(name) |
| 78 | + return msg.reply "#{name} does not exist" unless user? |
| 79 | + user.roles or= [] |
| 80 | + |
| 81 | + if newRole is 'admin' |
| 82 | + msg.reply "Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ADMIN env variable." |
| 83 | + else |
| 84 | + myRoles = msg.message.user.roles or [] |
| 85 | + if msg.message.user.id.toString() in admins |
| 86 | + user.roles = (role for role in user.roles when role isnt newRole) |
| 87 | + msg.reply "Ok, #{name} doesn't have the '#{newRole}' role." |
| 88 | + |
| 89 | + robot.respond /(what role does|what roles does) @?(.+) (have)\?*$/i, (msg) -> |
| 90 | + name = msg.match[2].trim() |
| 91 | + user = robot.brain.userForName(name) |
| 92 | + return msg.reply "#{name} does not exist" unless user? |
| 93 | + user.roles or= [] |
| 94 | + displayRoles = user.roles |
| 95 | + |
| 96 | + if user.id.toString() in admins |
| 97 | + displayRoles.push('admin') |
| 98 | + |
| 99 | + if displayRoles.length == 0 |
| 100 | + msg.reply "#{name} has no roles." |
| 101 | + else |
| 102 | + msg.reply "#{name} has the following roles: #{displayRoles.join(', ')}." |
| 103 | + |
| 104 | + robot.respond /who has admin role\?*$/i, (msg) -> |
| 105 | + adminNames = [] |
| 106 | + for admin in admins |
| 107 | + user = robot.brain.userForId(admin) |
| 108 | + adminNames.push user.name if user? |
| 109 | + |
| 110 | + if adminNames.length > 0 |
| 111 | + msg.reply "The following people have the 'admin' role: #{adminNames.join(', ')}" |
| 112 | + else |
| 113 | + msg.reply "There are no people that have the 'admin' role." |
0 commit comments