Exemplo n.º 1
0
Arquivo: db.go Projeto: oren/user
func (db Db) AddUser(r io.Reader) (string, error) {
	uuid := uuid.NewUUID()

	if uuid == nil {
		log.Fatal("uuid is nil")
	}

	fmt.Println(uuid)

	decoder := json.NewDecoder(r)

	var u User
	err := decoder.Decode(&u)

	if err != nil {
		return "", fmt.Errorf("invalid json")
	}

	fmt.Println(u)

	ok := validEmail(u.Email)

	if !ok {
		return "", fmt.Errorf("invalid email")
	}

	db.Store.AddQuad(cayley.Quad("person:"+uuid.String(), "type", "Person", ""))
	// store.AddQuad(cayley.Quad("person:UUID", "email", u.email, ""))
	// store.AddQuad(cayley.Quad("person:UUID", "password", u.password, ""))

	return db.location, nil
}
Exemplo n.º 2
0
// NewUser makes a new user in the database given the values from a HTTP POST request.
func NewUser(values url.Values) (u *User, err error) {
	username := values.Get("username")
	if username == "" {
		return nil, ErrUserCreateMissingValues
	}

	email := values.Get("email")
	if email == "" {
		return nil, ErrUserCreateMissingValues
	}

	// TODO: check for duplicate email addresses
	if !strings.Contains(email, "@") {
		return nil, ErrInvalidEmail
	}

	password := values.Get("password")
	if password == "" {
		return nil, ErrUserCreateMissingValues
	}

	confirm := values.Get("password_confirm")
	if confirm == "" {
		return nil, ErrUserCreateMissingValues
	}

	if password != confirm {
		return nil, ErrDifferentPasswords
	}

	salt := uuid.New()[0:14]
	result := hashPassword(password, salt)

	myUuid := uuid.NewUUID().String()

	u = &User{
		Email:        email,
		DisplayName:  username,
		ActualName:   url.QueryEscape(strings.ToLower(username)),
		Activated:    false,
		UUID:         myUuid,
		Salt:         salt,
		PasswordHash: result,
		AvatarURL:    "/img/avatar_default.jpg",
	}

	query := Db.Create(u)
	if query.Error != nil {
		return nil, query.Error
	}

	if Db.NewRecord(u) {
		return nil, errors.New("could not create user")
	}

	return
}