Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Corrected possible double entry account (username, email)

Cette révision appartient à :
akuma06 2017-05-07 21:14:32 +02:00
Parent 9ec0ddab06
révision 3adcfb9a4c
2 fichiers modifiés avec 16 ajouts et 6 suppressions

Voir le fichier

@ -15,6 +15,7 @@ import (
"github.com/ewhal/nyaa/util/log"
"github.com/ewhal/nyaa/util/modelHelper"
"github.com/ewhal/nyaa/util/timeHelper"
"fmt"
)
var userFields []string = []string{"name", "email", "createdAt", "updatedAt"}
@ -42,7 +43,14 @@ func SuggestUsername(username string) string {
}
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.
func CreateUserFromForm(registrationForm formStruct.RegistrationForm) (model.User, error) {
var user model.User
@ -70,7 +78,13 @@ func CreateUser(w http.ResponseWriter, r *http.Request) (int, error) {
var err error
modelHelper.BindValueForm(&registrationForm, 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)
if err != nil {
return http.StatusInternalServerError, err

Voir le fichier

@ -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
}