Пример #1
0
func (this *UserController) Detail(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("User Detail")
	this.Init(w, r)

	args := this.ParseURL(r.URL.String())
	uid := args["uid"]
	userModel := model.UserModel{}
	one, err := userModel.Detail(uid)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	this.Data["Detail"] = one

	solutionModle := model.SolutionModel{}
	solvedList, err := solutionModle.Achieve(uid)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	this.Data["List"] = solvedList
	//class.Logger.Debug(solvedList)
	this.Data["Title"] = "User Detail"
	if uid != "" && uid == this.Uid {
		this.Data["IsSettings"] = true
		this.Data["IsSettingsDetail"] = true
	}

	err = this.Execute(w, "view/layout.tpl", "view/user_detail.tpl")
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
}
Пример #2
0
func (this *UserController) Edit(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("User Edit")
	this.Init(w, r)

	if this.Privilege == config.PrivilegeNA {
		this.Data["Title"] = "Warning"
		this.Data["Info"] = "You must login!"
		err := this.Execute(w, "view/layout.tpl", "view/400.tpl")
		if err != nil {
			http.Error(w, "tpl error", 500)
			return
		}
		return
	}

	uid := this.Uid
	userModel := model.UserModel{}
	one, err := userModel.Detail(uid)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	this.Data["Detail"] = one

	this.Data["Title"] = "User Edit"
	this.Data["IsSettings"] = true
	this.Data["IsSettingsEdit"] = true

	err = this.Execute(w, "view/layout.tpl", "view/user_edit.tpl")
	if err != nil {
		http.Error(w, "tpl error", 500)
		return
	}
}
Пример #3
0
func (this *UserController) Privilegeset(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("User Privilege")
	this.Init(w, r)

	args := this.ParseURL(r.URL.String())
	uid := args["uid"]
	privilegeStr := args["type"]

	privilege := config.PrivilegeNA
	switch privilegeStr {
	case "Admin":
		privilege = config.PrivilegeAD
	case "TC":
		privilege = config.PrivilegeTC
	case "PU":
		privilege = config.PrivilegePU
	default:
		http.Error(w, "args error", 400)
	}

	ok := 1
	hint := make(map[string]string)

	if uid == "" {
		ok, hint["uid"] = 0, "Handle should not be empty."
	} else if uid == this.Uid {
		ok, hint["uid"] = 0, "You cannot delete yourself"
	} else {
		userModel := model.UserModel{}
		_, err := userModel.Detail(uid)
		if err == model.NotFoundErr {
			ok, hint["uid"] = 0, "This handle does not exist!"
		} else if err != nil {
			http.Error(w, err.Error(), 400)
			return
		}
	}

	if ok == 1 {
		userModel := model.UserModel{}
		err := userModel.Privilege(uid, privilege)
		if err != nil {
			http.Error(w, err.Error(), 400)
			return
		}

		w.WriteHeader(200)
	} else {
		b, err := json.Marshal(&hint)
		if err != nil {
			http.Error(w, "json error", 500)
			return
		}

		w.WriteHeader(400)
		w.Write(b)
	}
}
Пример #4
0
func (this *UserController) Password(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Admin Password")
	this.Init(w, r)

	ok := 1
	hint := make(map[string]string)

	data := make(map[string]string)
	data["userHandle"] = r.FormValue("user[Handle]")
	data["newPassword"] = r.FormValue("user[newPassword]")
	data["confirmPassword"] = r.FormValue("user[confirmPassword]")

	uid := r.FormValue("user[Handle]")

	if uid == "" {
		ok, hint["uid"] = 0, "Handle should not be empty"
	} else {
		userModel := model.UserModel{}
		_, err := userModel.Detail(uid)
		if err == model.NotFoundErr {
			ok, hint["uid"] = 0, "This handle does not exist!"
		} else if err != nil {
			http.Error(w, err.Error(), 400)
			return
		}

	}

	if len(data["newPassword"]) < 6 {
		ok, hint["newPassword"] = 0, "Password should contain at least six characters."
	}
	if data["newPassword"] != data["confirmPassword"] {
		ok, hint["confirmPassword"] = 0, "Confirmation mismatched."
	}

	if ok == 1 {
		pwd := data["newPassword"]
		userModel := model.UserModel{}
		err := userModel.Password(uid, pwd)
		if err != nil {
			http.Error(w, err.Error(), 400)
			return
		}

		w.WriteHeader(200)
	} else {
		w.WriteHeader(400)
	}
	b, err := json.Marshal(&hint)
	if err != nil {
		http.Error(w, "json error", 400)
		return
	}

	w.Write(b)
}
Пример #5
0
//@URL: /users @method:POST
func (uc *UserController) Register() {
	restweb.Logger.Debug("User Register")

	var one model.User
	userModel := model.UserModel{}

	uid := uc.Input.Get("user[handle]")
	nick := uc.Input.Get("user[nick]")
	pwd := uc.Input.Get("user[password]")
	pwdConfirm := uc.Input.Get("user[confirmPassword]")
	one.Mail = uc.Input.Get("user[mail]")
	one.School = uc.Input.Get("user[school]")
	one.Motto = uc.Input.Get("user[motto]")

	valid := restweb.Validation{}
	valid.MinSize(uid, 4, "uid")
	valid.Match(uid, "\\w+", "uid")

	if !valid.HasError {
		_, err := userModel.Detail(uid)
		if err != nil && err != model.NotFoundErr {
			http.Error(uc.W, err.Error(), 500)
			return
		} else if err == nil {
			valid.AppendError("uid", "Handle is currently in use.")
		}
	}

	valid.Required(nick, "nick")
	valid.MinSize(pwd, 6, "pwd")
	valid.Equal(pwd, pwdConfirm, "pwdConfirm")
	valid.Mail(one.Mail, "mail")

	if !valid.HasError {
		one.Uid = uid
		one.Nick = nick
		one.Pwd = pwd
		one.Privilege = config.PrivilegePU

		err := userModel.Insert(one)
		if err != nil {
			uc.Error(err.Error(), 500)
			return
		}

		uc.W.Header().Add("Location", "/users/"+uid)
		uc.W.WriteHeader(201)
	} else {
		hint := valid.RenderErrMap()
		b, _ := json.Marshal(&hint)
		uc.W.WriteHeader(400)
		uc.W.Write(b)
	}
}
Пример #6
0
// 设置用户权限
//@URL: /admin/privilegeset @method: POST
func (uc *AdminUser) Privilegeset() {
	restweb.Logger.Debug("User Privilege")

	uid := uc.Input.Get("uid")
	privilegeStr := uc.Input.Get("type")

	privilege := config.PrivilegeNA
	switch privilegeStr {
	case "Admin":
		privilege = config.PrivilegeAD
	case "TC":
		privilege = config.PrivilegeTC
	case "PU":
		privilege = config.PrivilegePU
	default:
		uc.Error("args error", 400)
	}

	ok := 1
	hint := make(map[string]string)

	if uid == "" {
		ok, hint["hint"] = 0, "Handle should not be empty."
	} else if uid == uc.Uid {
		ok, hint["hint"] = 0, "You cannot delete yourself!"
	} else {
		userModel := model.UserModel{}
		_, err := userModel.Detail(uid)
		if err == model.NotFoundErr {
			ok, hint["hint"] = 0, "uc handle does not exist!"
		} else if err != nil {
			uc.Error(err.Error(), 400)
			return
		}
	}

	if ok == 1 {
		userModel := model.UserModel{}
		err := userModel.Privilege(uid, privilege)
		if err != nil {
			uc.Error(err.Error(), 400)
			return
		}

		uc.W.WriteHeader(200)
	} else {
		b, _ := json.Marshal(&hint)

		uc.W.WriteHeader(400)
		uc.W.Write(b)
	}
}
Пример #7
0
//设置用户密码
//@URL: /admin/users/password @method: PUT
func (uc *AdminUser) Password() {
	restweb.Logger.Debug("Admin Password")

	ok := 1
	hint := make(map[string]string)
	data := make(map[string]string)

	data["userHandle"] = uc.Input.Get("user[Handle]")
	data["newPassword"] = uc.Input.Get("user[newPassword]")
	data["confirmPassword"] = uc.Input.Get("user[confirmPassword]")

	uid := uc.Input.Get("user[Handle]")

	if uid == "" {
		ok, hint["uid"] = 0, "Handle should not be empty"
	} else {
		userModel := model.UserModel{}
		_, err := userModel.Detail(uid)
		if err == model.NotFoundErr {
			ok, hint["uid"] = 0, "uc handle does not exist!"
		} else if err != nil {
			uc.Error(err.Error(), 400)
			return
		}

	}

	if len(data["newPassword"]) < 6 {
		ok, hint["newPassword"] = 0, "Password should contain at least six characters."
	}
	if data["newPassword"] != data["confirmPassword"] {
		ok, hint["confirmPassword"] = 0, "Confirmation mismatched."
	}

	if ok == 1 {
		pwd := data["newPassword"]
		userModel := model.UserModel{}
		err := userModel.Password(uid, pwd)
		if err != nil {
			uc.Error(err.Error(), 400)
			return
		}

		uc.W.WriteHeader(200)
	} else {
		uc.W.WriteHeader(400)
	}
	b, _ := json.Marshal(&hint)
	uc.W.Write(b)
}
Пример #8
0
//@URL: /profile @method: GET
func (uc *UserController) Edit() {
	restweb.Logger.Debug("User Edit")

	uid := uc.Uid
	userModel := model.UserModel{}
	one, err := userModel.Detail(uid)
	if err != nil {
		uc.Error(err.Error(), 400)
		return
	}
	uc.Output["Detail"] = one

	uc.Output["Title"] = "User Edit"
	uc.Output["IsSettings"] = true
	uc.Output["IsSettingsEdit"] = true

	uc.RenderTemplate("view/layout.tpl", "view/user_edit.tpl")
}
Пример #9
0
//@URL: /users/(.+) @method: GET
func (uc *UserController) Detail(uid string) {
	restweb.Logger.Debug("User Detail", uid)

	userModel := model.UserModel{}
	one, err := userModel.Detail(uid)
	if err != nil {
		uc.Error(err.Error(), 400)
		return
	}
	uc.Output["Detail"] = one

	solutionModle := model.SolutionModel{}
	solvedList, err := solutionModle.Achieve(uid, config.ModuleP, config.ModuleP)
	if err != nil {
		uc.Error(err.Error(), 400)
		return
	}

	type IPs struct {
		Time int64
		IP   string
	}
	var ips []IPs
	ipo := IPs{}

	for i, lenth := 0, len(one.IPRecord); i < lenth; i++ {
		ipo.Time = one.TimeRecord[i]
		ipo.IP = one.IPRecord[i]
		ips = append(ips, ipo)
	}

	achieveList := sort.IntSlice(solvedList)
	achieveList.Sort()
	uc.Output["List"] = achieveList
	uc.Output["IpList"] = ips
	uc.Output["Title"] = "User Detail"
	if uid != "" && uid == uc.Uid {
		uc.Output["IsSettings"] = true
		uc.Output["IsSettingsDetail"] = true
	}

	uc.RenderTemplate("view/layout.tpl", "view/user_detail.tpl")
}
Пример #10
0
func (this *UserController) Settings(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("User Settings")
	this.Init(w, r)

	if this.Privilege == config.PrivilegeNA {
		this.Data["Title"] = "Warning"
		this.Data["Info"] = "You must login!"
		err := this.Execute(w, "view/layout.tpl", "view/400.tpl")
		if err != nil {
			http.Error(w, "tpl error", 500)
			return
		}
		return
	}

	userModel := model.UserModel{}

	one, err := userModel.Detail(this.Uid)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	this.Data["Detail"] = one

	solutionModel := model.SolutionModel{}
	solvedList, err := solutionModel.Achieve(this.Uid)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	this.Data["List"] = solvedList

	this.Data["Title"] = "User Settings"
	this.Data["IsSettings"] = true
	this.Data["IsSettingsDetail"] = true

	err = this.Execute(w, "view/layout.tpl", "view/user_detail.tpl")
	if err != nil {
		http.Error(w, "tpl error", 500)
		return
	}
}
Пример #11
0
//提交某一问题的solution
//@URL: /problems/(\d+) @method: POST
func (pc *ProblemController) Submit(Pid string) {
	restweb.Logger.Debug("Problem Submit")

	pid, err := strconv.Atoi(Pid)
	if err != nil {
		pc.Error("args error", 400)
		return
	}

	var one model.Solution
	one.Pid = pid
	one.Uid = pc.Uid
	one.Module = config.ModuleP
	one.Mid = config.ModuleP // Todo use pid as mid

	problemModel := model.ProblemModel{}
	pro, err := problemModel.Detail(pid)
	if err != nil {
		pc.Error(err.Error(), 500)
		return
	}
	code := pc.Input.Get("code")

	one.Code = code
	one.Length = pc.GetCodeLen(len(pc.Input.Get("code")))
	one.Language, _ = strconv.Atoi(pc.Input.Get("compiler_id"))
	pc.SetSession("Compiler_id", pc.Input.Get("compiler_id")) //or set cookie?
	userModel := model.UserModel{}
	user, _ := userModel.Detail(pc.Uid)
	one.Share = user.ShareCode

	hint := make(map[string]string)
	errflag := true
	switch {
	case pro.Pid == 0:
		hint["info"] = "No such problem."
	case code == "":
		hint["info"] = "Your source code is too short."
	default:
		errflag = false
	}
	if errflag {
		b, _ := json.Marshal(&hint)
		pc.W.WriteHeader(400)
		pc.W.Write(b)
		return
	}

	one.Status = config.StatusAvailable
	one.Judge = config.JudgePD

	solutionModel := model.SolutionModel{}
	sid, err := solutionModel.Insert(one)
	if err != nil {
		pc.Error(err.Error(), 500)
		return
	}

	pc.W.WriteHeader(201)
	go func() { //编译运行solution
		one := make(map[string]interface{})
		one["Sid"] = sid
		one["Pid"] = pro.RPid
		one["OJ"] = pro.ROJ
		one["Rejudge"] = false
		reader, _ := pc.JsonReader(&one)
		restweb.Logger.Debug(reader)
		_, err := http.Post(config.JudgeHost, "application/json", reader)
		if err != nil {
			restweb.Logger.Debug("sid[", sid, "] submit post error")
		}
	}()
}