Skip to content

Commit be4855f

Browse files
author
Nick
committed
Add a more general environment variable for adding a static set of roles
* Added the env variable to the README * Deprecated HUBOT_AUTH_ADMIN
1 parent 7f42c1a commit be4855f

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ 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

+32-17
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_ADMIN - A comma separate list of user IDs
5+
# HUBOT_AUTH_ROLES - A list of roles with a comma delimited list of user ids
66
#
77
# Commands:
88
# hubot <user> has <role> role - Assigns a role to a user
@@ -25,20 +25,40 @@
2525

2626
config =
2727
admin_list: process.env.HUBOT_AUTH_ADMIN
28+
role_list: process.env.HUBOT_AUTH_ROLES
2829

2930
module.exports = (robot) ->
3031

31-
unless config.admin_list?
32-
robot.logger.warning 'The HUBOT_AUTH_ADMIN environment variable not set'
33-
32+
# TODO: This has been deprecated so it needs to be removed at some point.
3433
if config.admin_list?
35-
admins = config.admin_list.split ','
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
43+
44+
unless config.role_list?
45+
robot.logger.warning 'The HUBOT_AUTH_ROLES environment variable not set'
3646
else
37-
admins = []
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
3857

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

4363
hasRole: (user, roles) ->
4464
userRoles = @userRoles(user)
@@ -50,18 +70,13 @@ module.exports = (robot) ->
5070

5171
usersWithRole: (role) ->
5272
users = []
53-
for own key, user of robot.brain.data.users
73+
for own key, user of robot.brain.users()
5474
if @hasRole(user, role)
5575
users.push(user.name)
5676
users
5777

5878
userRoles: (user) ->
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
79+
user.roles
6580

6681
robot.auth = new Auth
6782

@@ -84,7 +99,7 @@ module.exports = (robot) ->
8499
msg.reply "#{name} already has the '#{newRole}' role."
85100
else
86101
if newRole is 'admin'
87-
msg.reply "Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ADMIN env variable."
102+
msg.reply "Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ROLES env variable."
88103
else
89104
myRoles = msg.message.user.roles or []
90105
user.roles.push(newRole)
@@ -105,7 +120,7 @@ module.exports = (robot) ->
105120
user.roles or= []
106121

107122
if newRole is 'admin'
108-
msg.reply "Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ADMIN env variable."
123+
msg.reply "Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ROLES env variable."
109124
else
110125
myRoles = msg.message.user.roles or []
111126
user.roles = (role for role in user.roles when role isnt newRole)
@@ -137,7 +152,7 @@ module.exports = (robot) ->
137152
unless robot.auth.isAdmin msg.message.user
138153
msg.reply "Sorry, only admins can list assigned roles."
139154
else
140-
for i, user of robot.brain.data.users when user.roles
155+
for i, user of robot.brain.users() when user.roles
141156
roles.push role for role in user.roles when role not in roles
142157
if roles.length > 0
143158
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_ADMIN = "alice"
9+
process.env.HUBOT_AUTH_ROLES = "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_ADMIN env variable."]
45+
["hubot", "@alice Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ROLES 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_ADMIN env variable."]
70+
["hubot", "@alice Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ROLES 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: demo, test"]
117+
["hubot", "@alice The following roles are available: admin, demo, test"]
118118
]

0 commit comments

Comments
 (0)