示例#1
0
// AuthRequired run function when user logged in.
func AuthRequired(f func(c *gin.Context)) gin.HandlerFunc {
	return func(c *gin.Context) {
		_, err := userService.CurrentUser(c)
		if err != nil {
			log.Error("Auth failed.")
			response.KnownErrorJSON(c, http.StatusUnauthorized, "error.loginPlease", errors.New("Auth failed."))
			return
		}
		f(c)
		return
	}
}
示例#2
0
// AdminRequired run function when user logged in and user has an admin role.
func AdminRequired(f func(c *gin.Context)) gin.HandlerFunc {
	return func(c *gin.Context) {
		user, err := userService.CurrentUser(c)
		if err == nil {
			if HasAdmin(&user) {
				f(c)
				log.Debug("User has admin role.")
				return
			}
		}
		log.Error("Admin role required.")
		response.KnownErrorJSON(c, http.StatusUnauthorized, "error.adminRequired", errors.New("Admin role required."))
		return
	}
}