public ActionResult CreateUser([FromBody] UserModel user) { try { UserUtil.CreateUser(user); return(Ok()); } catch (DuplicateNameException e) { Logger.Error(e); return(UnprocessableEntity("Duplicate")); } catch (Exception e) { Logger.Error(e); return(StatusCode(500)); } }
//Register a new user in the system protected void RegisterBtn_Click(object sender, EventArgs e) { UserCreateResult.Visible = false; EmailError.Visible = false; NameError.Visible = false; PasswordError.Visible = false; RoleCompanyError.Visible = false; string email = Email.Text; string firstName = FirstName.Text; string lastName = LastName.Text; string pass = Password.Text; string pass2 = PasswordRepeat.Text; int roleId = int.Parse(SelectedRole.Value); int companyId = int.Parse(SelectedCompany.Value); string displayName = ""; bool verificationEmail = true; //Validate that the logged in user has permissions to do this //Validate the new user's information //Create the new user account //Send an email to the new user //checks that a role was selected for the user if (!UserUtil.DoesUserExist(email)) { if (firstName.Length > 0 && lastName.Length > 0) { if (roleId != -1) { if (companyId != -1) { if (pass.Equals(pass2)) { if (pass.Length > 7) { int validPass = ValidatePasswordSecurity(pass); if (validPass == 0) { //creates the user in firebase Firebase.Auth.User fbUser = FirebaseUtil.CreateNewUser(email, pass, displayName, verificationEmail); //if the user already exists in firebase, try to log them in if (fbUser == null) { fbUser = FirebaseUtil.LoginUser(email, pass); } if (fbUser != null) { User u = UserUtil.CreateUser(roleId, companyId, email, firstName, lastName); User user = (User)Session["User"]; if (user != null) { Log.Info(user.Identity + " created a new " + RoleUtil.GetRole(roleId).RoleName + " account under " + CompanyUtil.GetCompanyName(companyId) + " assigned to " + firstName + " " + lastName + " - " + email); } else { Log.Info("System created a new " + RoleUtil.GetRole(roleId).RoleName + " account under " + CompanyUtil.GetCompanyName(companyId) + " assigned to " + firstName + " " + lastName + " - " + email); } u.FirebaseUser = fbUser; //display user created msg UserCreateResult.Visible = true; UserCreateResult.Text = "Successfully created user " + u.Identity; } else { UserCreateResult.CssClass = "error"; UserCreateResult.Visible = true; UserCreateResult.Text = "Error creating user in Firebase"; } } else { PasswordError.Visible = true; if (validPass == 1) { PasswordError.Text = "Password must contain at least 1 uppercase"; } else if (validPass == 2) { PasswordError.Text = "Password must contain at least 1 lowercase"; } else if (validPass == 3) { PasswordError.Text = "Password must contain at least 1 number"; } else { PasswordError.Text = "Unknown password error"; } } } else { //display user failed to be created msg PasswordError.Visible = true; PasswordError.Text = "Password must be at least 8 chars"; } } else { //throw error, passwords don't match PasswordError.Visible = true; PasswordError.Text = "Passwords don't match"; } } else { //throw error, please select company for user RoleCompanyError.Visible = true; RoleCompanyError.Text = "Please select a company"; } } else { //throw error, please select role for new user RoleCompanyError.Visible = true; RoleCompanyError.Text = "Please select a role"; } } else { NameError.Visible = true; NameError.Text = "Please enter a first and last name"; } } else { EmailError.Visible = true; EmailError.Text = "Email already in use"; } ClearFields(); }