Feat v0.9.3 team user roles (#77)
All checks were successful
CI/CD / detect-changes (push) Successful in 4s
CI/CD / e2e-smoke (push) Has been skipped
CI/CD / test-runners (push) Has been skipped
CI/CD / test-frontend (push) Successful in 6s
CI/CD / test-go-pg (push) Successful in 2m51s
CI/CD / test-sqlite (push) Successful in 3m5s
CI/CD / build-and-deploy (push) Successful in 27s
All checks were successful
CI/CD / detect-changes (push) Successful in 4s
CI/CD / e2e-smoke (push) Has been skipped
CI/CD / test-runners (push) Has been skipped
CI/CD / test-frontend (push) Successful in 6s
CI/CD / test-go-pg (push) Successful in 2m51s
CI/CD / test-sqlite (push) Successful in 3m5s
CI/CD / build-and-deploy (push) Successful in 27s
Co-authored-by: Jeffrey Smith <jasafpro@gmail.com> Co-committed-by: Jeffrey Smith <jasafpro@gmail.com>
This commit was merged in pull request #77.
This commit is contained in:
@@ -54,6 +54,51 @@ func RequireTeamAdmin(teams store.TeamStore, allStores ...store.Stores) gin.Hand
|
||||
}
|
||||
}
|
||||
|
||||
// RequireRole returns middleware that restricts access to users holding at least
|
||||
// one of the specified roles in the team identified by :teamId.
|
||||
// Checks both the primary role (team_members.role) and additional roles
|
||||
// (team_user_roles). System admins are always allowed through.
|
||||
func RequireRole(teams store.TeamStore, roles []string, allStores ...store.Stores) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if len(allStores) > 0 && isSystemAdmin(c, allStores[0]) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
userID := c.GetString("user_id")
|
||||
teamID := c.Param("teamId")
|
||||
if teamID == "" {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"error": "team ID required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
memberRoles, err := teams.GetMemberRoles(c.Request.Context(), teamID, userID)
|
||||
if err != nil || len(memberRoles) == 0 {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
|
||||
"error": "required role not held",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
roleSet := make(map[string]bool, len(memberRoles))
|
||||
for _, r := range memberRoles {
|
||||
roleSet[r] = true
|
||||
}
|
||||
for _, required := range roles {
|
||||
if roleSet[required] {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
|
||||
"error": "required role not held",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// RequireTeamMember returns middleware that restricts access to users who
|
||||
// belong to the team identified by :teamId (any role).
|
||||
// System admins are always allowed through.
|
||||
|
||||
Reference in New Issue
Block a user