コード例 #1
0
ファイル: login.go プロジェクト: kameleon83/managIncident
func (this *LoginController) Login() {
	flash := beego.ReadFromRequest(&this.Controller)
	if _, ok := flash.Data["notice"]; ok {
	}

	this.Ctx.Request.ParseForm()
	username := this.Ctx.Request.Form.Get("username")
	password := this.Ctx.Request.Form.Get("password")

	newPass := admin.Md5Pass(password)

	fmt.Println(username, newPass)

	if this.Ctx.Input.Method() == "POST" {
		o := orm.NewOrm()
		o.Using("default")

		var users []*models.User

		qs := o.QueryTable("user")
		err := qs.Filter("mail__iexact", username).Filter("pass__iexact", newPass).One(&users)
		if err == orm.ErrNoRows {
			// No result
			flash := beego.NewFlash()
			flash.Error("Vérifie tes informations car apparemment elles ne sont pas bonnes.")
			flash.Store(&this.Controller)
		} else {
			v := this.GetSession("IncidentManager")
			if v == nil {
				this.SetSession("IncidentID", int(1))
				for _, data := range users {
					this.SetSession("uid", data.Id)
					this.SetSession("mail", data.Mail)
					this.SetSession("role", data.Role)
				}
				this.Data["num"] = 0

			} else {
				this.SetSession("IncidentID", v.(int)+1)
				this.Data["num"] = v.(int)
			}
			flash := beego.NewFlash()

			flash.Notice("Bienvenue : " + username)
			flash.Store(&this.Controller)
			if this.GetSession("role") == "admin" {
				this.Redirect("/incident-manager/admin", 302)
			} else if this.GetSession("role") == "user" {
				this.Redirect("/incident-manager", 302)
			}

		}

	}

	this.Layout = "layout.tpl"
	this.TplNames = "login.tpl"
	this.Data["title2"] = "Se connecter"
	this.Data["role"] = this.GetSession("role")
	this.Data["mail"] = this.GetSession("mail")
	this.LayoutSections = make(map[string]string)
	this.LayoutSections["navbar"] = "index/navbar.tpl"
	this.LayoutSections["footer"] = "index/footer.tpl"

}
コード例 #2
0
ファイル: register.go プロジェクト: kameleon83/managIncident
func (this *RegisterController) Password() {

	flash := beego.NewFlash()
	o := orm.NewOrm()

	v := this.GetSession("uid")
	if v != nil {
		flash.Error("Une session existe déjà sur cette Ordinateur. Déconnectes toi afin d'éviter tout problème")
		flash.Store(&this.Controller)
		this.Redirect("/", 302)
	}

	o.Using("default")
	mail := this.Ctx.Input.Param(":mail")

	user := models.User{Md5Mail: mail}
	err := o.Read(&user, "Md5Mail")
	this.Data["mail"] = user.Mail
	this.Data["md5Mail"] = mail
	// Three return values:Is Created,Object Id,Error
	if err == nil && user.Pass == "" {
		if this.Ctx.Input.Method() == "POST" {
			this.Ctx.Request.ParseForm()
			password := this.Ctx.Request.Form.Get("password")
			repassword := this.Ctx.Request.Form.Get("repassword")

			if repassword == password {

				newPass := admin.Md5Pass(password)

				user := models.User{Id: user.Id, Mail: user.Mail, Role: user.Role, Pass: newPass, Created: time.Now()}

				if _, err := o.Update(&user); err == nil {
					flash.Success("Bienvenue " + user.Mail)
					flash.Store(&this.Controller)
					v := this.GetSession("IncidentManager")
					if v == nil {
						this.SetSession("IncidentID", int(1))
						this.SetSession("uid", user.Id)
						this.SetSession("mail", user.Mail)
						this.SetSession("role", user.Role)
						this.Data["num"] = 0

					} else {
						this.SetSession("IncidentID", v.(int)+1)
						this.Data["num"] = v.(int)
					}
					this.Redirect("/incident-manager/", 302)
				} else {
					fmt.Println("update", err)
				}
			}

		}

	} else {
		flash.Error("Dommage mais tu ne peux accéder à cette page")
		flash.Store(&this.Controller)
		this.Redirect("/", 302)

	}
	Template(this, "user", "password", "Enregistre ton mot de passe")

}