Skip to content

Commit bd6db8d

Browse files
committed
Reverted @be4855f to fix bug.
1 parent 1113436 commit bd6db8d

File tree

3 files changed

+21
-44
lines changed

3 files changed

+21
-44
lines changed

README.md

-8
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,3 @@ Run `npm install`
2828
user1>> hubot user2 has jester role
2929
hubot>> OK, user2 has the jester role.
3030
```
31-
32-
## HUBOT_AUTH_ROLES
33-
34-
This can be used to give a default set of roles and **must** be used to set the admin role.
35-
36-
```sh
37-
HUBOT_AUTH_ROLES="admin=U12345678 mod=U87654321,U67856745"
38-
```

src/auth.coffee

+17-32
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Assign roles to users and restrict command access in other scripts.
33
#
44
# Configuration:
5-
# HUBOT_AUTH_ROLES - A list of roles with a comma delimited list of user ids
5+
# HUBOT_AUTH_ADMIN - A comma separate list of user IDs
66
#
77
# Commands:
88
# hubot <user> has <role> role - Assigns a role to a user
@@ -25,40 +25,20 @@
2525

2626
config =
2727
admin_list: process.env.HUBOT_AUTH_ADMIN
28-
role_list: process.env.HUBOT_AUTH_ROLES
2928

3029
module.exports = (robot) ->
3130

32-
# TODO: This has been deprecated so it needs to be removed at some point.
33-
if config.admin_list?
34-
robot.logger.warning 'The HUBOT_AUTH_ADMIN environment variable has been deprecated in favor of HUBOT_AUTH_ROLES'
35-
for id in config.admin_list.split ','
36-
user = robot.brain.userForId id
37-
38-
unless user?
39-
robot.logger.warning "#{id} does not exist"
40-
else
41-
user.roles or= []
42-
user.roles.push 'admin' unless 'admin' in user.roles
31+
unless config.admin_list?
32+
robot.logger.warning 'The HUBOT_AUTH_ADMIN environment variable not set'
4333

44-
unless config.role_list?
45-
robot.logger.warning 'The HUBOT_AUTH_ROLES environment variable not set'
34+
if config.admin_list?
35+
admins = config.admin_list.split ','
4636
else
47-
for role in config.role_list.split ' '
48-
[dummy, roleName, userIds] = role.match /(\w+)=([\w]+(?:,[\w]+)*)/
49-
for id in userIds.split ','
50-
user = robot.brain.userForId id
51-
52-
unless user?
53-
robot.logger.warning "#{id} does not exist"
54-
else
55-
user.roles or= []
56-
user.roles.push roleName unless roleName in user.roles
37+
admins = []
5738

5839
class Auth
5940
isAdmin: (user) ->
60-
roles = robot.brain.userForId(user.id).roles or []
61-
'admin' in roles
41+
user.id.toString() in admins
6242

6343
hasRole: (user, roles) ->
6444
userRoles = @userRoles(user)
@@ -70,13 +50,18 @@ module.exports = (robot) ->
7050

7151
usersWithRole: (role) ->
7252
users = []
73-
for own key, user of robot.brain.users()
53+
for own key, user of robot.brain.data.users
7454
if @hasRole(user, role)
7555
users.push(user.name)
7656
users
7757

7858
userRoles: (user) ->
79-
user.roles
59+
roles = []
60+
if user? and robot.auth.isAdmin user
61+
roles.push('admin')
62+
if user.roles?
63+
roles = roles.concat user.roles
64+
roles
8065

8166
robot.auth = new Auth
8267

@@ -99,7 +84,7 @@ module.exports = (robot) ->
9984
msg.reply "#{name} already has the '#{newRole}' role."
10085
else
10186
if newRole is 'admin'
102-
msg.reply "Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ROLES env variable."
87+
msg.reply "Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ADMIN env variable."
10388
else
10489
myRoles = msg.message.user.roles or []
10590
user.roles.push(newRole)
@@ -120,7 +105,7 @@ module.exports = (robot) ->
120105
user.roles or= []
121106

122107
if newRole is 'admin'
123-
msg.reply "Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ROLES env variable."
108+
msg.reply "Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ADMIN env variable."
124109
else
125110
myRoles = msg.message.user.roles or []
126111
user.roles = (role for role in user.roles when role isnt newRole)
@@ -152,7 +137,7 @@ module.exports = (robot) ->
152137
unless robot.auth.isAdmin msg.message.user
153138
msg.reply "Sorry, only admins can list assigned roles."
154139
else
155-
for i, user of robot.brain.users() when user.roles
140+
for i, user of robot.brain.data.users when user.roles
156141
roles.push role for role in user.roles when role not in roles
157142
if roles.length > 0
158143
msg.reply "The following roles are available: #{roles.join(', ')}"

test/auth-test.coffee

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ expect = require("chai").expect
66
describe "auth", ->
77

88
beforeEach ->
9-
process.env.HUBOT_AUTH_ROLES = "admin=alice"
9+
process.env.HUBOT_AUTH_ADMIN = "alice"
1010
@room = helper.createRoom()
1111
@room.robot.brain.userForId "alice",
1212
name: "alice"
@@ -42,7 +42,7 @@ describe "auth", ->
4242
@room.user.say("alice", "hubot: jimmy has admin role").then =>
4343
expect(@room.messages).to.eql [
4444
["alice", "hubot: jimmy has admin role"]
45-
["hubot", "@alice Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ROLES env variable."]
45+
["hubot", "@alice Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ADMIN env variable."]
4646
]
4747

4848
it "anon user fails to set role", ->
@@ -67,7 +67,7 @@ describe "auth", ->
6767
@room.user.say("alice", "hubot: jimmy doesn't have admin role").then =>
6868
expect(@room.messages).to.eql [
6969
["alice", "hubot: jimmy doesn't have admin role"]
70-
["hubot", "@alice Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ROLES env variable."]
70+
["hubot", "@alice Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ADMIN env variable."]
7171
]
7272

7373
context "what roles does <user> have", ->
@@ -114,5 +114,5 @@ describe "auth", ->
114114
["alice", "hubot: alice has test role"]
115115
["hubot", "@alice OK, alice has the 'test' role."]
116116
["alice", "hubot: list assigned roles"]
117-
["hubot", "@alice The following roles are available: admin, demo, test"]
117+
["hubot", "@alice The following roles are available: demo, test"]
118118
]

0 commit comments

Comments
 (0)