2019-12-07 11:44:19 +01:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-01-27 08:35:37 +01:00
|
|
|
"strconv"
|
2019-12-07 11:44:19 +01:00
|
|
|
|
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:44:19 +01:00
|
|
|
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 getUserIDFromTokenAsUint(r *http.Request) uint {
|
|
|
|
id, _ := strconv.Atoi(getUserIDFromToken(r))
|
|
|
|
return uint(id)
|
|
|
|
}
|