Corrected possible double entry account (username, email)
Cette révision appartient à :
Parent
9ec0ddab06
révision
3adcfb9a4c
2 fichiers modifiés avec 16 ajouts et 6 suppressions
|
@ -15,6 +15,7 @@ import (
|
||||||
"github.com/ewhal/nyaa/util/log"
|
"github.com/ewhal/nyaa/util/log"
|
||||||
"github.com/ewhal/nyaa/util/modelHelper"
|
"github.com/ewhal/nyaa/util/modelHelper"
|
||||||
"github.com/ewhal/nyaa/util/timeHelper"
|
"github.com/ewhal/nyaa/util/timeHelper"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var userFields []string = []string{"name", "email", "createdAt", "updatedAt"}
|
var userFields []string = []string{"name", "email", "createdAt", "updatedAt"}
|
||||||
|
@ -42,7 +43,14 @@ func SuggestUsername(username string) string {
|
||||||
}
|
}
|
||||||
return usernameCandidate
|
return usernameCandidate
|
||||||
}
|
}
|
||||||
|
func CheckEmail(email string) bool {
|
||||||
|
var count int
|
||||||
|
db.ORM.Model(model.User{}).Where(&model.User{Email: email}).Count(&count)
|
||||||
|
if count == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
// CreateUserFromForm creates a user from a registration form.
|
// CreateUserFromForm creates a user from a registration form.
|
||||||
func CreateUserFromForm(registrationForm formStruct.RegistrationForm) (model.User, error) {
|
func CreateUserFromForm(registrationForm formStruct.RegistrationForm) (model.User, error) {
|
||||||
var user model.User
|
var user model.User
|
||||||
|
@ -70,7 +78,13 @@ func CreateUser(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
modelHelper.BindValueForm(®istrationForm, r)
|
modelHelper.BindValueForm(®istrationForm, r)
|
||||||
|
usernameCandidate := SuggestUsername(registrationForm.Username)
|
||||||
|
if (usernameCandidate != registrationForm.Username) {
|
||||||
|
return http.StatusInternalServerError, fmt.Errorf("Username already taken, you can choose: %s", usernameCandidate)
|
||||||
|
}
|
||||||
|
if CheckEmail(registrationForm.Email) {
|
||||||
|
return http.StatusInternalServerError, errors.New("Email Address already in our database!")
|
||||||
|
}
|
||||||
password, err := bcrypt.GenerateFromPassword([]byte(registrationForm.Password), 10)
|
password, err := bcrypt.GenerateFromPassword([]byte(registrationForm.Password), 10)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
|
|
|
@ -120,9 +120,5 @@ func ValidateForm(form interface{}, errorForm map[string][]string) (map[string][
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len(errorForm) == 0) { // If no error, return nil
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return errorForm
|
return errorForm
|
||||||
}
|
}
|
Référencer dans un nouveau ticket