Ejemplo n.º 1
0
//-----------------------------------------------------------------------------
func (this *UserController) Login(userId string) {
	if !this.isAdmin() {
		http.Redirect(this.Response, this.Request, "/", http.StatusForbidden)
		return
	}

	id, err := strconv.Atoi(userId)
	if utils.HandleErr("[UserController::Login] user_id Atoi: ", err, this.Response) {
		return
	}

	if !db.IsExists("users", []string{"id"}, []interface{}{id}) {
		http.Error(this.Response, "Have not such user with the id", http.StatusInternalServerError)
		return
	}

	sid := utils.GetRandSeq(HASH_SIZE)
	params := map[string]interface{}{"sid": sid, "enabled": true}
	where := map[string]interface{}{"id": id}

	this.GetModel("users").Update(this.isAdmin(), id, params, where)
	sessions.SetSession(this.Response, map[string]interface{}{"sid": sid})

	http.Redirect(this.Response, this.Request, "/usercontroller/showcabinet", 200)
}
Ejemplo n.º 2
0
func (this *RegistrationController) Login() {
	data, err := utils.ParseJS(this.Request, this.Response)
	if utils.HandleErr("[RegistrationController::Login]: ", err, this.Response) {
		utils.SendJSReply(map[string]interface{}{"result": err.Error()}, this.Response)

		return
	}

	login := data["login"].(string)
	pass := data["password"].(string)

	var id int
	var enabled bool
	var passHash, salt string
	result := make(map[string]interface{}, 1)

	if err = this.GetModel("users").
		LoadWherePart(map[string]interface{}{"login": login}).
		SelectRow([]string{"id", "pass", "salt", "enabled"}).
		Scan(&id, &passHash, &salt, &enabled); err != nil {
		result["result"] = "invalidCredentials"

	} else if enabled == false {
		result["result"] = "notEnabled"

	} else if passHash != utils.GetMD5Hash(pass+salt) {
		result["result"] = "badPassword"

	} else {
		result["result"] = "ok"

		sid := utils.GetRandSeq(HASH_SIZE)
		params := map[string]interface{}{"sid": sid, "enabled": true}
		where := map[string]interface{}{"id": id}
		this.GetModel("users").Update(this.isAdmin(), id, params, where)
		sessions.SetSession(this.Response, map[string]interface{}{"sid": sid})
	}

	utils.SendJSReply(result, this.Response)
}
Ejemplo n.º 3
0
func WellcomeToProfile(w http.ResponseWriter, r *http.Request) {
	newContreoller := new(BaseController).Handler()

	parts := strings.Split(r.URL.Path, "/")
	token := parts[len(parts)-1]

	var id int
	err := newContreoller.GetModel("users").
		LoadWherePart(map[string]interface{}{"token": token}).
		SelectRow([]string{"id"}).
		Scan(&id)
	if utils.HandleErr("[WellcomeToProfile]: ", err, w) || id == 0 {
		return
	}

	sid := utils.GetRandSeq(HASH_SIZE)
	params := map[string]interface{}{"sid": sid, "enabled": true}
	where := map[string]interface{}{"id": id}
	newContreoller.GetModel("users").Update(false, -1, params, where)
	sessions.SetSession(w, map[string]interface{}{"sid": sid})
	http.Redirect(w, r, "/usercontroller/showcabinet", 200)
}