Skip to content

Commit e330f35

Browse files
zGravpatcon
authored andcommitted
Add add command to list assigned roles [#28]
Original commits: * initial commit * only admins can list roles + auth-test fix? * indentation issue? * No roles have been assigned reply * cleanup * removal of comments * moved command to bottom of file
1 parent 45ab9ed commit e330f35

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/auth.coffee

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# hubot what roles does <user> have - Find out what roles a user has
1111
# hubot what roles do I have - Find out what roles you have
1212
# hubot who has <role> role - Find out who has the given role
13+
# hubot list assigned roles - List all roles and users assigned to them
1314
#
1415
# Notes:
1516
# * Call the method: robot.auth.hasRole(msg.envelope.user,'<role>')
@@ -64,6 +65,7 @@ module.exports = (robot) ->
6465

6566
robot.auth = new Auth
6667

68+
6769
robot.respond /@?([^\s]+) ha(?:s|ve) (["'\w: -_]+) role/i, (msg) ->
6870
name = msg.match[1].trim()
6971
if name.toLowerCase() is 'i' then name = msg.message.user.name
@@ -129,3 +131,15 @@ module.exports = (robot) ->
129131
msg.reply "The following people have the '#{role}' role: #{userNames.join(', ')}"
130132
else
131133
msg.reply "There are no people that have the '#{role}' role."
134+
135+
robot.respond /list assigned roles/i, (msg) ->
136+
roles = []
137+
unless robot.auth.isAdmin msg.message.user
138+
msg.reply "Sorry, only admins can list assigned roles."
139+
else
140+
for i, user of robot.brain.data.users when user.roles
141+
roles.push role for role in user.roles when role not in roles
142+
if roles.length > 0
143+
msg.reply "The following roles are available: #{roles.join(', ')}"
144+
else
145+
msg.reply "No roles to list."

test/auth-test.coffee

+15
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,18 @@ describe 'auth', ->
121121
done()
122122

123123
adapter.receive(new TextMessage anon_user, "hubot: what roles does admin-user have?")
124+
125+
it 'successfully list assigned roles', (done) ->
126+
adapter.receive(new TextMessage admin_user, "hubot: admin-user has demo role")
127+
adapter.receive(new TextMessage admin_user, "hubot: anon-user has test role")
128+
adapter.receive(new TextMessage admin_user, "hubot: admin-user has test role")
129+
130+
adapter.on "reply", (envelope, strings) ->
131+
if strings[0].match /OK, .* has .* role/i
132+
return
133+
134+
135+
expect(strings[0]).to.match(/following roles .*:.*demo.*test/)
136+
done()
137+
138+
adapter.receive(new TextMessage admin_user, "hubot: list assigned roles")

0 commit comments

Comments
 (0)