Exemplo n.º 1
0
func DeleteAccount(w http.ResponseWriter, r *http.Request) {
	me, _ := context.Get(r, "me").(*data.Account)
	if me == nil || me.Level != data.Administrator {
		http.Error(w, "", http.StatusForbidden)
		return
	}

	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	acc, err := data.GetAccount(id)
	catch(err)

	if acc.Id == me.Id || acc.Id == 1 {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	err = acc.Del()
	catch(err)

	json.NewEncoder(w).Encode(&struct {
		Id int64 `json:"id"`
	}{
		Id: acc.Id,
	})
	hub.Send([]interface{}{"SYNC", "accounts"})

	err = data.NewActivity(me, fmt.Sprintf("deleted account %d", acc.Id)).Put()
	catch(err)
	hub.Send([]interface{}{"SYNC", "activities"})
}
Exemplo n.º 2
0
func UpdateAccountPart(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	acc, err := data.GetAccount(id)
	catch(err)

	me, _ := context.Get(r, "me").(*data.Account)
	if me == nil || me.Id != acc.Id {
		http.Error(w, "", http.StatusForbidden)
		return
	}

	body := struct {
		Notified time.Time `json:"notified"`
	}{}
	err = json.NewDecoder(r.Body).Decode(&body)
	catch(err)

	acc.Notified = body.Notified
	err = acc.Put()
	catch(err)

	json.NewEncoder(w).Encode(acc)
	hub.Send([]interface{}{"SYNC", "accounts", acc.Id})
}
Exemplo n.º 3
0
func ServeSubmissionTestOutput(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	subm, err := data.GetSubmission(id)
	catch(err)

	no, err := strconv.ParseInt(vars["no"], 10, 64)
	catch(err)
	test := subm.Tests[no-1]

	w.Header().Add("Content-Type", "text/plain")

	if r.FormValue("download") == "yes" {
		acc, err := data.GetAccount(subm.AuthorId)
		catch(err)

		prob, err := data.GetProblem(subm.ProblemId)
		catch(err)

		w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%d-%s-%s-%d.out"`, subm.Id, acc.Handle, strings.ToLower(prob.Char), no))
	}

	blob, err := data.Blobs.Get(test.OutputKey)
	catch(err)
	_, err = io.Copy(w, blob)
	catch(err)
	err = blob.Close()
	catch(err)
}
Exemplo n.º 4
0
func handleIdentity(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, err := api.Store.Get(r, "s")
		if err != nil {
			r.Header.Del("Cookie")
			context.Clear(r)
		}

		sess, err := api.Store.Get(r, "s")
		catch(err)

		id, _ := sess.Values["me.id"].(int64)
		acc, err := data.GetAccount(id)
		catch(err)
		context.Set(r, "me", acc)

		h.ServeHTTP(w, r)
	})
}
Exemplo n.º 5
0
func ServeAccount(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	if err != nil {
		http.Error(w, "Bad Request", http.StatusBadRequest)
		return
	}
	acc, err := data.GetAccount(id)
	catch(err)

	if acc == nil {
		http.Error(w, "Not Found", http.StatusNotFound)
		return
	}

	err = json.NewEncoder(w).Encode(acc)
	catch(err)
}
Exemplo n.º 6
0
func UpdateAccount(w http.ResponseWriter, r *http.Request) {
	me, _ := context.Get(r, "me").(*data.Account)
	if me == nil || me.Level != data.Administrator {
		http.Error(w, "", http.StatusForbidden)
		return
	}

	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	acc, err := data.GetAccount(id)
	catch(err)

	body := struct {
		Handle   string     `json:"handle"`
		Password string     `json:"password"`
		Level    data.Level `json:"level"`
		Name     string     `json:"name"`
	}{}
	err = json.NewDecoder(r.Body).Decode(&body)
	catch(err)

	acc.Handle = body.Handle
	if body.Password != "" {
		err = acc.SetPassword(body.Password)
		catch(err)
	}
	acc.Level = body.Level
	acc.Name = body.Name
	err = acc.Put()
	catch(err)

	json.NewEncoder(w).Encode(acc)
	hub.Send([]interface{}{"SYNC", "accounts", acc.Id})

	err = data.NewActivity(me, fmt.Sprintf("updated account %d", acc.Id)).Put()
	catch(err)
	hub.Send([]interface{}{"SYNC", "activities"})
}
Exemplo n.º 7
0
func ServeExecutionTestOutput(w http.ResponseWriter, r *http.Request) {
	me, _ := context.Get(r, "me").(*data.Account)
	if me == nil || (me.Level != data.Judge && me.Level != data.Administrator) {
		http.Error(w, "", http.StatusForbidden)
		return
	}

	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	exec, err := data.GetExecution(id)
	catch(err)

	no, err := strconv.ParseInt(vars["no"], 10, 64)
	catch(err)
	test := exec.Tests[no-1]

	w.Header().Add("Content-Type", "text/plain")

	if r.FormValue("download") == "yes" {
		subm, err := exec.Submission()
		catch(err)
		acc, err := data.GetAccount(subm.AuthorId)
		catch(err)
		prob, err := data.GetProblem(subm.ProblemId)
		catch(err)
		w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%d-%s-%s-%d-%d.out"`, subm.Id, acc.Handle, strings.ToLower(prob.Char), exec.Id, no))
	}

	blob, err := data.Blobs.Get(test.OutputKey)
	catch(err)
	_, err = io.Copy(w, blob)
	catch(err)
	err = blob.Close()
	catch(err)
}