Пример #1
0
// register create user
func RegisterUser(user *models.User, username, email, password string, locale i18n.Locale) error {
	// use random salt encode password
	salt := models.GetUserSalt()
	pwd := utils.EncodePassword(password, salt)

	user.UserName = strings.ToLower(username)
	user.Email = strings.ToLower(email)

	// save salt and encode password, use $ as split char
	user.Password = fmt.Sprintf("%s$%s", salt, pwd)

	// save md5 email value for gravatar
	user.GrEmail = utils.EncodeMd5(user.Email)

	// Use username as default nickname.
	user.NickName = user.UserName

	//set default language
	if locale.Lang == "en-US" {
		user.Lang = setting.LangEnUS
	} else {
		user.Lang = setting.LangZhCN
	}

	//set default avatar
	user.AvatarType = setting.AvatarTypeGravatar
	return models.Insert(user)
}
Пример #2
0
func LoginUserFromRememberCookie(user *models.User, ctx *tango.Context, session *session.Session) (success bool) {
	userName := GetCookie(ctx.Req(), setting.CookieUserName)
	if len(userName) == 0 {
		return false
	}

	defer func() {
		if !success {
			DeleteRememberCookie(ctx)
		}
	}()

	var err error
	if user, err = models.GetUserByName(userName); err != nil {
		return false
	}

	secret := utils.EncodeMd5(user.Rands + user.Password)
	value, _ := GetSecureCookie(ctx.Req(), secret, setting.CookieRememberName)
	if value != userName {
		return false
	}

	LoginUser(user, ctx, session, true)

	return true
}
Пример #3
0
func (form *UserAdminForm) SetToUser(user *models.User) {
	// set md5 value if the value is an email
	if strings.IndexRune(form.GrEmail, '@') != -1 {
		form.GrEmail = utils.EncodeMd5(form.GrEmail)
	}

	utils.SetFormValues(form, user)
}
Пример #4
0
// compare raw password and encoded password
func VerifyPassword(rawPwd, encodedPwd string) bool {
	// for discuz accounts
	if len(encodedPwd) == 39 {
		salt := encodedPwd[:6]
		encoded := encodedPwd[7:]
		return encoded == utils.EncodeMd5(utils.EncodeMd5(rawPwd)+salt)
	}

	// split
	var salt, encoded string
	if len(encodedPwd) > 11 {
		salt = encodedPwd[:10]
		encoded = encodedPwd[11:]
	}

	return utils.EncodePassword(rawPwd, salt) == encoded
}
Пример #5
0
func (form *ProfileForm) SaveUserProfile(user *models.User) error {
	// set md5 value if the value is an email
	if strings.IndexRune(form.GrEmail, '@') != -1 {
		form.GrEmail = utils.EncodeMd5(form.GrEmail)
	}

	changes := utils.FormChanges(user, form)
	if len(changes) > 0 {
		// if email changed then need re-active
		if user.Email != form.Email {
			user.IsActive = false
			changes = append(changes, "IsActive")
		}

		utils.SetFormValues(form, user)
		return models.UpdateById(user.Id, user, changes...)
	}
	return nil
}
Пример #6
0
func WriteRememberCookie(user *models.User, ctx *tango.Context) {
	secret := utils.EncodeMd5(user.Rands + user.Password)
	days := 86400 * setting.LoginRememberDays
	SetCookie(ctx, setting.CookieUserName, user.UserName, days)
	SetSecureCookie(ctx, secret, setting.CookieRememberName, user.UserName, days)
}