Skip to content

Commit 337f011

Browse files
feat(api-server): support assigning multiple roles
Split role assignments on comma to grant a module multiple roles on the same target. For example a nethvoice-proxy application can be granted both fwadm and portsadm on node/1. The resulting list of authorized actions will be the union of all granted roles.
1 parent f58e302 commit 337f011

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

core/api-server/methods/auth.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,33 @@ func RedisAuthorization(username string, c *gin.Context) (models.UserAuthorizati
115115
pathScan = "module/" + c.Param("module_id") + "/roles/"
116116
}
117117

118-
// get roles of current user: HGET roles/<username>.<entity> -> <role>
119-
role, errRedisRoleGet := redisConnection.HGet(ctx, "roles/"+username, pathGet).Result()
118+
// get roles of current user: HGET roles/<username> -> <role(s)>
119+
roles, errRedisRoleGet := redisConnection.HGet(ctx, "roles/"+username, pathGet).Result()
120120

121121
// handle redis error
122122
if errRedisRoleGet != nil {
123123
return userAuthorizationsRedis, errRedisRoleGet
124124
}
125125

126-
// get action for current role and entity: SMEMBERS <entity>/<reference>/roles/<role>
127-
actions, errRedisRoleScan := redisConnection.SMembers(ctx, pathScan+role).Result()
128-
129-
// handle redis error
130-
if errRedisRoleScan != nil {
131-
return userAuthorizationsRedis, errRedisRoleScan
126+
// get actions for each role and entity: SMEMBERS <entity>/<reference>/roles/<role>
127+
var actions []string
128+
roleList := strings.Split(roles, ",")
129+
for _, r := range roleList {
130+
r = strings.TrimSpace(r)
131+
if r == "" {
132+
continue
133+
}
134+
a, errRedisRoleScan := redisConnection.SMembers(ctx, pathScan+r).Result()
135+
if errRedisRoleScan != nil {
136+
return userAuthorizationsRedis, errRedisRoleScan
137+
}
138+
// duplicated values are allowed, since Authorizator checks only existence
139+
actions = append(actions, a...)
132140
}
133141

134142
// compose user authorizations
135143
userAuthorizationsRedis.Username = username
136-
userAuthorizationsRedis.Role = role
144+
userAuthorizationsRedis.Role = roles // keep original string of raw role list
137145
userAuthorizationsRedis.Actions = actions
138146

139147
// close redis connection

0 commit comments

Comments
 (0)