Beispiel #1
0
func login(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")

	type LoginRequest struct {
		Username string
		Password string
	}

	loginReq := new(LoginRequest)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(loginReq); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if !players.AuthUser(loginReq.Username, loginReq.Password) {
		return &appError{errors.New("Forbidden"), "Invalid username/password", 403}
	}

	authUser, err := players.UserByName(loginReq.Username)
	if err != nil {
		return &appError{err, "Failed to fetch user data", 500}
	}
	if authUser.Locked {
		return &appError{errors.New("Locked"), "User locked", 403}
	}
	encoder := json.NewEncoder(w)
	encoder.Encode(authUser)
	return nil
}
func setUserForPlayer(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	if !c.Env["authIsAdmin"].(bool) {
		return &appError{errors.New("Unauthorized"), "Admins only", 403}
	}
	uuid, err := uuid.FromString(c.URLParams["uuid"])
	player, err := players.PlayerByUUID(uuid)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}
	tempUser := new(players.User)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(tempUser); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}
	user, err := players.UserByName(tempUser.Username)
	if err != nil {
		return &appError{err, "Cant find user", 400}
	}

	if err := player.SetUser(*user); err != nil {
		return &appError{err, "Failed to set user for player", 500}
	}
	w.WriteHeader(204)
	return nil
}