Example #1
0
func (u *User) RegisterUser(arg *RegisterUserArg, ukey *string) (err error) {
	arg.Email = strings.Replace(arg.Email, " ", "", -1)
	arg.Nickname = strings.Replace(arg.Nickname, " ", "", -1)
	arg.Password = strings.Replace(arg.Password, " ", "", -1)

	if len(arg.Email) > 50 {
		err = errors.New("EmailError:too long")
		return
	}
	if len(arg.Nickname) > 50 {
		err = errors.New("NicknameError:too long")
		return
	}
	if len(arg.Password) > 40 {
		err = errors.New("PasswordError:too long")
		return
	}

	h := sha1.New()
	salt := utils.GenSalt(10)
	io.WriteString(h, salt+arg.Password) // salt
	arg.Password = fmt.Sprintf("%#x", string(h.Sum(nil)))

	if !u.checkNicknameLower(arg.Nickname) {
		err = errors.New("NicknameError:nickname dup")
		return
	}

	for {
		*ukey = utils.GenUkey()
		r, err := u.DB.Exec(SQL_REGISTER_USER, *ukey, arg.Email, salt, arg.Password)
		if err != nil {
			if strings.Contains(err.Error(), "email_key") {
				err = errors.New("EmailError:" + err.Error())
			} else if strings.Contains(err.Error(), "ukey_key") {
				continue
			} else {
				err = errors.New("InternalError:" + err.Error())
			}
			return err
		}
		if n, err := r.RowsAffected(); n != 1 {
			err = errors.New("InternalError:insert user error")
			return err
		}
		break
	}
	_, err = u.DB.Exec(SQL_REGISTER_INSERT_NICKNAME, *ukey, arg.Nickname)
	if err != nil {
		err = errors.New("InternalError:" + err.Error())
		return
	}
	return
}
Example #2
0
func (u *User) UpdatePassword(arg *UpdatePasswordArg, ukey *string) (err error) {
	if len(arg.Ukey) != 6 {
		err = errors.New("UkeyError:" + arg.Ukey)
		return
	}
	if len(arg.Password) == 0 {
		err = errors.New("PasswordError: unvalid password")
		return
	}
	salt := utils.GenSalt(10)
	h := sha1.New()
	io.WriteString(h, salt+arg.Password)
	pass := fmt.Sprintf("%#x", string(h.Sum(nil)))
	_, err = u.DB.Exec(SQL_UPDATE_PASSWORD, salt, pass, arg.Ukey)
	if err != nil {
		err = errors.New("InternalError:" + err.Error())
		return
	}
	*ukey = arg.Ukey
	return
}