52 lines
1 KiB
Go
52 lines
1 KiB
Go
package orm
|
|
|
|
import (
|
|
"net/http"
|
|
"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
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func getModelIDFromToken(r *http.Request) string {
|
|
return getClaims(r)["model_id"].(string)
|
|
}
|
|
|
|
func getUserIDFromTokenAsUint(r *http.Request) uint {
|
|
id, _ := strconv.Atoi(getUserIDFromToken(r))
|
|
return uint(id)
|
|
}
|