Skip to content

Commit 45ab9ed

Browse files
Nick Woodwardpatcon
Nick Woodward
authored andcommitted
Extra message when trying to ask for roles (#25)
* Fixed issue which caused "who has admin role" to also match adding a role * Added test scenario to ensure that this won't happen again
1 parent fa2a337 commit 45ab9ed

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/auth.coffee

+16-14
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ module.exports = (robot) ->
6565
robot.auth = new Auth
6666

6767
robot.respond /@?([^\s]+) ha(?:s|ve) (["'\w: -_]+) role/i, (msg) ->
68-
unless robot.auth.isAdmin msg.message.user
69-
msg.reply "Sorry, only admins can assign roles."
70-
else
71-
name = msg.match[1].trim()
72-
if name.toLowerCase() is 'i' then name = msg.message.user.name
73-
newRole = msg.match[2].trim().toLowerCase()
68+
name = msg.match[1].trim()
69+
if name.toLowerCase() is 'i' then name = msg.message.user.name
70+
71+
unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
72+
unless robot.auth.isAdmin msg.message.user
73+
msg.reply "Sorry, only admins can assign roles."
74+
else
75+
newRole = msg.match[2].trim().toLowerCase()
7476

75-
unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
7677
user = robot.brain.userForName(name)
7778
return msg.reply "#{name} does not exist" unless user?
7879
user.roles or= []
@@ -88,14 +89,15 @@ module.exports = (robot) ->
8889
msg.reply "OK, #{name} has the '#{newRole}' role."
8990

9091
robot.respond /@?([^\s]+) (?:don['’]t|doesn['’]t|do not) have (["'\w: -_]+) role/i, (msg) ->
91-
unless robot.auth.isAdmin msg.message.user
92-
msg.reply "Sorry, only admins can remove roles."
93-
else
94-
name = msg.match[1].trim()
95-
if name.toLowerCase() is 'i' then name = msg.message.user.name
96-
newRole = msg.match[2].trim().toLowerCase()
92+
name = msg.match[1].trim()
93+
if name.toLowerCase() is 'i' then name = msg.message.user.name
94+
95+
unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
96+
unless robot.auth.isAdmin msg.message.user
97+
msg.reply "Sorry, only admins can remove roles."
98+
else
99+
newRole = msg.match[2].trim().toLowerCase()
97100

98-
unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
99101
user = robot.brain.userForName(name)
100102
return msg.reply "#{name} does not exist" unless user?
101103
user.roles or= []

test/auth-test.coffee

+7
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ describe 'auth', ->
5555

5656
adapter.receive(new TextMessage admin_user, "hubot: who has admin role?")
5757

58+
it 'list admin users using non-admin user', (done) ->
59+
adapter.on "reply", (envelope, strings) ->
60+
expect(strings[0]).to.match /admin-user/i
61+
done()
62+
63+
adapter.receive(new TextMessage anon_user, "hubot: who has admin role?")
64+
5865
it 'anon user fails to set role', (done) ->
5966
adapter.on "reply", (envelope, strings) ->
6067
expect(strings[0]).to.match /only admins can assign roles/i

0 commit comments

Comments
 (0)