當前位置: 首頁>>代碼示例>>Golang>>正文


Golang models.User類代碼示例

本文整理匯總了Golang中gorevel/app/models.User的典型用法代碼示例。如果您正苦於以下問題:Golang User類的具體用法?Golang User怎麽用?Golang User使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了User類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: ForgotPasswordPost

func (c User) ForgotPasswordPost(email string) revel.Result {
	c.Validation.Required(email).Message("請填寫Email")
	c.Validation.Email(email).Message("Email格式不正確")
	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(routes.User.ForgotPassword())
	}

	var user models.User
	has, _ := engine.Where("email = ?", email).Get(&user)
	if !has {
		return c.NotFound("用戶不存在")
	}

	user.ValidateCode = uuidName()
	engine.Where("email = ?", email).Cols("validate_code").Update(&user)

	subject := "重設密碼 —— Revel社區"
	content := `<h2><a href="http://gorevel.cn/reset_password/` + user.ValidateCode + `">重設密碼</a></h2>`

	go sendMail(subject, content, []string{user.Email})

	c.Flash.Success(fmt.Sprintf("你好,重設密碼的鏈接已經發送到您的郵箱,請到您的郵箱 %s 重設密碼!", user.Email))

	return c.Redirect(routes.User.Signin())
}
開發者ID:zeuson,項目名稱:gorevel,代碼行數:27,代碼來源:user.go

示例2: SigninPost

func (c User) SigninPost(name, password string) revel.Result {
	c.Validation.Required(name).Message("請輸入用戶名")
	c.Validation.Required(password).Message("請輸入密碼")
	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(routes.User.Signin())
	}

	var user models.User
	var count int64
	var hashedPassword string
	has, _ := engine.Where("name = ?", name).Get(&user)
	if has {
		// 密碼加鹽兼容舊密碼
		if user.Salt == "" {
			hashedPassword = models.EncryptPassword(password, "")
		} else {
			hashedPassword = models.EncryptPassword(password, user.Salt)
		}

		count, _ = engine.Where("name = ? AND hashed_password = ?", name, hashedPassword).Count(&models.User{})

		// 密碼加鹽兼容舊密碼
		if count > 0 && user.Salt == "" {
			salt := uuidName()
			hashedPassword = models.EncryptPassword(password, salt)
			engine.Id(user.Id).Update(&models.User{
				Salt:           salt,
				HashedPassword: hashedPassword,
			})
		}
	}

	if !has || count == 0 {
		c.Validation.Keep()
		c.FlashParams()
		c.Flash.Out["user"] = name
		c.Flash.Error("用戶名或密碼錯誤")
		return c.Redirect(routes.User.Signin())
	}

	if !user.IsActive() {
		c.Flash.Error(fmt.Sprintf("您的賬號 %s 尚未激活,請到您的郵箱 %s 激活賬號!", user.Name, user.Email))
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(routes.User.Signin())
	}

	c.Session["user"] = name

	if preUrl, ok := c.Session["preUrl"]; ok {
		return c.Redirect(preUrl)
	}

	return c.Redirect(routes.App.Index())
}
開發者ID:zeuson,項目名稱:gorevel,代碼行數:57,代碼來源:user.go

示例3: SignupPost

func (c User) SignupPost(user models.User) revel.Result {
	user.Validate(c.Validation)
	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(routes.User.Signup())
	}

	salt := uuidName()
	user.Type = MEMBER_GROUP
	user.Avatar = models.DefaultAvatar
	user.ValidateCode = uuidName()
	user.Salt = salt
	user.HashedPassword = models.EncryptPassword(user.Password, salt)

	aff, _ := engine.Insert(&user)
	if aff == 0 {
		c.Flash.Error("注冊用戶失敗")
		return c.Redirect(routes.User.Signup())
	}

	subject := "激活賬號 —— Revel中文社區"
	content := `<h2><a href="http://gorevel.cn/user/validate/` + user.ValidateCode + `">激活賬號</a></h2>`
	go sendMail(subject, content, []string{user.Email})

	c.Flash.Success(fmt.Sprintf("%s 注冊成功,請到您的郵箱 %s 激活賬號!", user.Name, user.Email))

	engine.Insert(&models.Permissions{
		UserId: user.Id,
		Perm:   MEMBER_GROUP,
	})

	return c.Redirect(routes.User.Signin())
}
開發者ID:zeuson,項目名稱:gorevel,代碼行數:34,代碼來源:user.go

示例4: Validate

func (c User) Validate(code string) revel.Result {
	var user models.User
	has, _ := engine.Where("validate_code = ?", code).Get(&user)
	if !has {
		return c.NotFound("用戶不存在或校驗碼錯誤")
	}

	user.Status = models.USER_STATUS_ACTIVATED
	user.ValidateCode = ""
	aff, _ := engine.Cols("status", "validate_code").Update(&user)
	if aff > 0 {
		c.Flash.Success("您的賬號成功激活,請登錄!")
	} else {
		c.Flash.Error("抱歉,您的賬號未能激活,請與管理員聯係!")
	}

	return c.Redirect(routes.User.Signin())
}
開發者ID:zeuson,項目名稱:gorevel,代碼行數:18,代碼來源:user.go

示例5: EditPost

func (c User) EditPost(avatar string) revel.Result {
	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(routes.User.Edit())
	}

	var user models.User
	has, _ := engine.Id(c.user().Id).Get(&user)
	if !has {
		return c.NotFound("用戶不存在")
	}

	file, header, err := c.Request.FormFile("image")
	if err == nil {
		defer file.Close()
		if ok := checkImageExt(c.Validation, &file, header, IMAGE_EXTS); ok {
			fileName := uuidFileName(header.Filename)
			err, ret := qiniuUploadImage(&file, fileName)
			if err != nil {
				c.Flash.Error("上傳頭像到七牛出錯,請檢查七牛配置。")
				return c.Redirect(routes.User.Edit())
			} else {
				if user.IsCustomAvatar() {
					qiniuDeleteImage(user.Avatar)
				}
				user.Avatar = ret.Key
			}
		}
	} else if avatar != "" {
		if user.IsCustomAvatar() {
			qiniuDeleteImage(user.Avatar)
		}
		user.Avatar = avatar
	}

	aff, _ := engine.Id(c.user().Id).Cols("avatar").Update(&user)
	if aff > 0 {
		c.Flash.Success("保存成功")
	} else {
		c.Flash.Error("保存失敗")
	}

	return c.Redirect(routes.User.Edit())
}
開發者ID:zeuson,項目名稱:gorevel,代碼行數:45,代碼來源:user.go


注:本文中的gorevel/app/models.User類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。