Exemple #1
0
func ParseAllTask(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
	ctx = ParseAllTaskData(ctx, w, r)
	if ctx == nil {
		return nil
	}
	td := GetAllTaskData(ctx)
	res := make([]*task.Task, 0)
	for _, v := range td {
		t, err := manager.GetTask(v.ID)
		if err != nil {
			log.Warnf("Frontend: Task %d vanished when retriving.", v.ID)
			continue
		}
		res = append(res, t)
	}
	return context.WithValue(ctx, keyAllTask, res)
}
Exemple #2
0
func ParseTaskID(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
	var ID int
	err := GetJSONKeyAs(ctx, "id", &ID)
	if err != nil {
		WriteJSON(w, http.StatusBadRequest, BadField("id"))
		return nil
	}

	// authenticate
	taskdata, err := manager.GetTaskData(ID)
	if err != nil {
		WriteJSON(w, http.StatusNotFound, NotFound)
		return nil
	}
	session, err := store.Get(r, SessionName)
	if err != nil {
		log.Errorf("Frontend: Failed to save session: %s", err.Error())
		WriteJSON(w, http.StatusInternalServerError, InternalError)
		return nil
	}
	userid, ok := session.Values["user"]
	if !ok {
		WriteJSON(w, http.StatusUnauthorized, Unauthorized)
		return nil
	}
	if userid != *flagEdgeUser {
		handle, ok := session.Values["handle"]
		if !ok || userid != taskdata.User.UserID || handle != taskdata.Handle {
			WriteJSON(w, http.StatusForbidden, Forbidden)
			return nil
		}
	}

	// get real task
	t, err := manager.GetTask(ID)
	if err != nil {
		WriteJSON(w, http.StatusNotFound, NotFound)
		return nil
	}
	newCtx := context.WithValue(ctx, keyTaskID, ID)
	return context.WithValue(newCtx, keyTask, t)
}