oef/orm/role.go

53 lines
1 KiB
Go
Raw Normal View History

package orm
import (
"net/http"
2020-01-27 08:35:37 +01:00
"strconv"
"github.com/dgrijalva/jwt-go"
)
func getClaims(r *http.Request) jwt.MapClaims {
return r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
}
func isRole(role string, r *http.Request) bool {
if r.Context().Value("user") != nil {
return getClaims(r)["role"].(string) == role
}
return false
}
2020-01-27 08:35:37 +01:00
func getUserRole(r *http.Request) string {
return getClaims(r)["role"].(string)
}
func isAdministrator(r *http.Request) bool {
return isRole("administrator", r)
}
func isParticipant(r *http.Request) bool {
return isRole("participant", r)
}
func isSchool(r *http.Request) bool {
return isRole("school", r)
}
func isSubscriber(r *http.Request) bool {
return isRole("subscriber", r)
}
func getUserIDFromToken(r *http.Request) string {
return getClaims(r)["user_id"].(string)
}
2020-01-27 08:35:37 +01:00
func getModelIDFromToken(r *http.Request) string {
return getClaims(r)["model_id"].(string)
}
2020-01-27 08:35:37 +01:00
func getUserIDFromTokenAsUint(r *http.Request) uint {
id, _ := strconv.Atoi(getUserIDFromToken(r))
return uint(id)
}