Added check if the emailaddress is already used when creating a new user

This commit is contained in:
konrad 2018-01-26 16:00:18 +01:00 committed by kolaente
parent 63a74ee7ac
commit f09e5c6f84
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 38 additions and 2 deletions

View File

@ -22,6 +22,22 @@ func (err ErrUsernameExists) Error() string {
return fmt.Sprintf("a user with this username does already exist [user id: %d, username: %s]", err.UserID, err.Username)
}
// ErrUserEmailExists represents a "UserEmailExists" kind of error.
type ErrUserEmailExists struct {
UserID int64
Email string
}
// IsErrUserEmailExists checks if an error is a ErrUserEmailExists.
func IsErrUserEmailExists(err error) bool {
_, ok := err.(ErrUserEmailExists)
return ok
}
func (err ErrUserEmailExists) Error() string {
return fmt.Sprintf("a user with this email does already exist [user id: %d, email: %s]", err.UserID, err.Email)
}
// ErrNoUsername represents a "UsernameAlreadyExists" kind of error.
type ErrNoUsername struct {
UserID int64

View File

@ -14,7 +14,7 @@ func CreateUser(user User) (newUser User, err error) {
return User{}, ErrNoUsernamePassword{}
}
// Check if the user already existst
// Check if the user already existst with that username
existingUser, exists, err := GetUser(User{Username: newUser.Username})
if err != nil {
return User{}, err
@ -23,6 +23,15 @@ func CreateUser(user User) (newUser User, err error) {
return User{}, ErrUsernameExists{existingUser.ID, existingUser.Username}
}
// Check if the user already existst with that username
existingUser, exists, err = GetUser(User{Email:newUser.Email})
if err != nil {
return User{}, err
}
if exists {
return User{}, ErrUserEmailExists{existingUser.ID, existingUser.Email}
}
// Hash the password
newUser.Password, err = hashPassword(user.Password)
if err != nil {

View File

@ -37,8 +37,14 @@ func TestCreateUser(t *testing.T) {
assert.Error(t, err)
// Check if it fails to create a user with just the same username
_, err = CreateUser(User{Username:dummyuser.Username})
_, err = CreateUser(User{Username:dummyuser.Username, Password: "1234"})
assert.Error(t, err)
assert.True(t, IsErrUsernameExists(err))
// Check if it fails to create one with the same email
_, err = CreateUser(User{Username: "noone", Password: "1234", Email:dummyuser.Email})
assert.Error(t, err)
assert.True(t, IsErrUserEmailExists(err))
// Check if it fails to create a user without password and username
_, err = CreateUser(User{})

View File

@ -68,6 +68,11 @@ func UserAddOrUpdate(c echo.Context) error {
return c.JSON(http.StatusBadRequest, models.Message{"A user with this username already exists."})
}
// Check for user with that email already exists
if models.IsErrUserEmailExists(err) {
return c.JSON(http.StatusBadRequest, models.Message{"A user with this email address already exists."})
}
// Check for no username provided
if models.IsErrNoUsername(err) {
return c.JSON(http.StatusBadRequest, models.Message{"Please specify a username."})