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