Пример #1
0
func (s *GroupServer) handleUser(r *http.Request) *server.Response {
	log.Println(r)
	vars := mux.Vars(r)
	groupId := vars["group"]
	userString := vars["user"]
	user, err := ParseUser(userString)
	if err != nil {
		return &server.Response{Error: err}
	}

	authUser, err := s.Authenticate(r)
	if err != nil {
		return &server.Response{
			Error:      fmt.Errorf("auth failed: %v", err),
			StatusCode: http.StatusUnauthorized,
		}
	}

	groupSrv := group.NewGroupService(s.Store, authUser)

	switch r.Method {
	case "GET":
		has, err := groupSrv.CheckMember(groupId, user)
		if err != nil {
			return &server.Response{Error: err}
		}
		if !has {
			return &server.Response{StatusCode: http.StatusNotFound}
		}
		return &server.Response{}
	case "PUT":
		err = groupSrv.AddMember(groupId, user)
		return &server.Response{Error: err}
	case "DELETE":
		err = groupSrv.RemoveMember(groupId, user)
		return &server.Response{Error: err}
	}
	return &server.Response{
		Error:      fmt.Errorf("unsupported HTTP method: %s", r.Method),
		StatusCode: http.StatusMethodNotAllowed,
	}
}
Пример #2
0
func (s *GroupServer) handleGroup(r *http.Request) *server.Response {
	log.Println(r)
	vars := mux.Vars(r)
	groupId := vars["group"]

	authUser, err := s.Authenticate(r)
	if err != nil {
		return &server.Response{
			Error:      fmt.Errorf("auth failed: %v", err),
			StatusCode: http.StatusUnauthorized,
		}
	}

	groupSrv := group.NewGroupService(s.Store, authUser)

	switch r.Method {
	case "PUT":
		err = groupSrv.AddGroup(groupId)
		return &server.Response{Error: err}
	case "GET":
		g, err := groupSrv.Group(groupId)
		if err != nil {
			return &server.Response{Error: err}
		}
		out, err := json.Marshal(g)
		resp := &server.Response{Error: err}
		resp.Write(out)
		return resp
	case "DELETE":
		err = groupSrv.RemoveGroup(groupId)
		return &server.Response{Error: err}
	}
	return &server.Response{
		Error:      fmt.Errorf("unsupported HTTP method: %v", r.Method),
		StatusCode: http.StatusMethodNotAllowed,
	}
}