|
|
@@ -1397,29 +1397,30 @@ func deleteUser(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
|
|
|
|
|
// Checks if a user's entries are reasonable before database insertion. |
|
|
|
// This function is very important because it is the only thing preventing |
|
|
|
// anyone from creating an admin user. |
|
|
|
// anyone from creating an admin user. These error messages are displayed to |
|
|
|
// the user. |
|
|
|
func (user *User) validate() error { |
|
|
|
_, err := mail.ParseAddress(user.Email) |
|
|
|
if err != nil { errors.New("Invalid email.") } |
|
|
|
if err != nil { return errors.New("Invalid email.") } |
|
|
|
|
|
|
|
if roles[user.Role] == 0 { |
|
|
|
errors.New("Invalid role.") |
|
|
|
return errors.New("Invalid role.") |
|
|
|
} |
|
|
|
|
|
|
|
if roles[user.Role] == roles["Admin"] { |
|
|
|
errors.New("New user cannot be an Admin.") |
|
|
|
return errors.New("New user cannot be an Admin.") |
|
|
|
} |
|
|
|
|
|
|
|
if user.FirstName == "" { |
|
|
|
errors.New("Given name cannot be empty.") |
|
|
|
return errors.New("Given name cannot be empty.") |
|
|
|
} |
|
|
|
|
|
|
|
if user.LastName == "" { |
|
|
|
errors.New("Surname cannot be empty.") |
|
|
|
return errors.New("Surname cannot be empty.") |
|
|
|
} |
|
|
|
|
|
|
|
if user.Password == "" { |
|
|
|
errors.New("User must have a password.") |
|
|
|
return errors.New("Empty password") |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
@@ -1430,11 +1431,13 @@ func createUser(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
|
err := json.NewDecoder(r.Body).Decode(&user) |
|
|
|
if err != nil { http.Error(w, "Invalid fields.", 422); return } |
|
|
|
|
|
|
|
user.Role = "User" |
|
|
|
user.Status = "Trial" |
|
|
|
err = user.validate() |
|
|
|
if err != nil { http.Error(w, err.Error(), 422); return } |
|
|
|
|
|
|
|
user, err = insertUser(db, user) |
|
|
|
if err != nil { http.Error(w, "Error creating user.", 422); return } |
|
|
|
if err != nil { http.Error(w, err.Error(), 422); return } |
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(user) |
|
|
|
} |
|
|
|