2
2
# Assign roles to users and restrict command access in other scripts.
3
3
#
4
4
# 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
6
6
#
7
7
# Commands:
8
8
# hubot <user> has <role> role - Assigns a role to a user
25
25
26
26
config =
27
27
admin_list : process .env .HUBOT_AUTH_ADMIN
28
+ role_list : process .env .HUBOT_AUTH_ROLES
28
29
29
30
module .exports = (robot ) ->
30
31
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.
34
33
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'
36
46
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
38
57
39
58
class Auth
40
59
isAdmin : (user ) ->
41
- user .id .toString () in admins
60
+ roles = robot .brain .userForId (user .id ).roles or []
61
+ ' admin' in roles
42
62
43
63
hasRole : (user , roles ) ->
44
64
userRoles = @ userRoles (user)
@@ -50,18 +70,13 @@ module.exports = (robot) ->
50
70
51
71
usersWithRole : (role ) ->
52
72
users = []
53
- for own key, user of robot .brain .data . users
73
+ for own key, user of robot .brain .users ()
54
74
if @ hasRole (user, role)
55
75
users .push (user .name )
56
76
users
57
77
58
78
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
65
80
66
81
robot .auth = new Auth
67
82
@@ -84,7 +99,7 @@ module.exports = (robot) ->
84
99
msg .reply " #{ name} already has the '#{ newRole} ' role."
85
100
else
86
101
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."
88
103
else
89
104
myRoles = msg .message .user .roles or []
90
105
user .roles .push (newRole)
@@ -105,7 +120,7 @@ module.exports = (robot) ->
105
120
user .roles or= []
106
121
107
122
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."
109
124
else
110
125
myRoles = msg .message .user .roles or []
111
126
user .roles = (role for role in user .roles when role isnt newRole)
@@ -137,7 +152,7 @@ module.exports = (robot) ->
137
152
unless robot .auth .isAdmin msg .message .user
138
153
msg .reply " Sorry, only admins can list assigned roles."
139
154
else
140
- for i, user of robot .brain .data . users when user .roles
155
+ for i, user of robot .brain .users () when user .roles
141
156
roles .push role for role in user .roles when role not in roles
142
157
if roles .length > 0
143
158
msg .reply " The following roles are available: #{ roles .join (' , ' )} "
0 commit comments