39 lines
746 B
Go
39 lines
746 B
Go
|
package orm
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"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 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)
|
||
|
}
|