rolehandler.go 678 B

123456789101112131415161718192021222324
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/go-chi/render"
  5. "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/dao"
  6. )
  7. //RoleCheck implements a simple middleware handler for adding a role check to a route.
  8. func RoleCheck(allowedRoles []string) func(next http.Handler) http.Handler {
  9. return func(next http.Handler) http.Handler {
  10. return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
  11. user, _, _ := request.BasicAuth()
  12. idm := dao.GetIDM()
  13. ok := idm.UserInRoles(user, allowedRoles)
  14. if !ok {
  15. render.Render(response, request, ErrForbidden)
  16. return
  17. }
  18. next.ServeHTTP(response, request)
  19. })
  20. }
  21. }