protected void btnCreateCarrierAccount_Click(object sender, EventArgs e) { int clientID = 0; int contactID = 0; Contact contact = null; string newUserName = null; string password = null; CRM.Data.Entities.SecUser newUserAccount = null; CRM.Data.Entities.SecUser user = null; lblMessage.Text = string.Empty; lblMessage.CssClass = string.Empty; Page.Validate("Account"); if (!Page.IsValid) { return; } clientID = SessionHelper.getClientId(); newUserName = txtUserName.Text.Trim(); // check username is not taken if (SecUserManager.IsUserNameExist(newUserName)) { lblMessage.Text = "User Name " + newUserName + " is already taken."; lblMessage.CssClass = "error"; return; } // initialize user object user = new CRM.Data.Entities.SecUser(); user.UserName = newUserName; user.FirstName = txtFirstName.Text; user.LastName = txtLastName.Text; user.Status = true; user.isSSL = true; // assign client for this user user.ClientID = clientID; // attach carrier to user user.CarrierID = this.carrierID; user.CreatedBy = Core.SessionHelper.getUserId(); user.CreatedOn = DateTime.Now; user.Email = txtContactEmail.Text.Trim(); // assign selected role user.RoleId = Convert.ToInt32(ddlClientRoles.SelectedValue); // random password password = Guid.NewGuid().ToString().Substring(0, 8); // encrypt password user.Password = Core.SecurityManager.Encrypt(password); try { using (TransactionScope scope = new TransactionScope()) { // add new user account newUserAccount = SecUserManager.Save(user); // associate new user account with contact contactID = Convert.ToInt32(ViewState["ContactID"]); contact = ContactManager.Get(contactID); contact.UserID = newUserAccount.UserId; ContactManager.Save(contact); scope.Complete(); } // email adjuster about newly created account Core.EmailHelper.emailUserCredentials(newUserAccount); lblMessage.Text = string.Format("User Account '{0}' was created and credentials were emailed to the user.", newUserName); lblMessage.CssClass = "ok"; // hide create account button this.btnShowCreateAccount.Visible = false; this.btnCreateCarrierAccount.Visible = false; } catch (Exception ex) { lblMessage.Text = "Unable to create user account."; lblMessage.CssClass = "error"; Core.EmailHelper.emailError(ex); } }
protected void btnSave_Click(object sender, EventArgs e) { lblError.Text = string.Empty; lblSave.Text = string.Empty; lblMessage.Text = string.Empty; lblError.Visible = false; lblSave.Visible = false; lblMessage.Visible = false; bool isnew = false; bool isError = false; CRM.Data.Entities.SecUser user = null; Page.Validate("register"); if (!Page.IsValid) { return; } // 2013-08-06 tortega int clientID = Core.SessionHelper.getClientId(); int roleID = Core.SessionHelper.getUserRoleId(); try { using (TransactionScope scope = new TransactionScope()) { if (hfUserId.Value == "0") { isnew = true; user = new CRM.Data.Entities.SecUser(); user.ClientID = clientID; } else { user = SecUserManager.GetByUserId(Convert.ToInt32(hfUserId.Value)); } // ** Add User ** // if (isnew) { bool userExist = SecUserManager.IsUserNameExist(txtUserName.Text.Trim()); //bool emailExist = SecUserManager.IsEmailExist(txtEmail.Text.Trim()); // 2013-08-06 tortega if (clientID > 0 && ClientManager.UsersLimitReached(clientID)) { throw new Exception("Maximum number of users reached."); } if (userExist) { throw new Exception("User Name already exists."); } } user.FirstName = txtFirstName.Text.Trim(); user.LastName = txtLastName.Text.Trim(); user.Email = txtEmail.Text; user.UserName = txtUserName.Text.Trim(); //user.Password = SecurityManager.Encrypt(txtPassWord.Text); user.RoleId = Convert.ToInt32(ddlRole.SelectedValue); user.Status = ddlStatus.SelectedValue == "1" ? true : false; user.Blocked = false; user.isViewAllClaims = cbxViewAllClaims.Checked; if (chkPassword.Checked == true || isnew) { user.Password = SecurityManager.Encrypt(txtPassWord.Text); } user.UserName = txtUserName.Text.Trim(); user.FirstName = txtFirstName.Text.Trim(); user.LastName = txtLastName.Text.Trim(); user.Email = txtEmail.Text; user.Status = ddlStatus.SelectedValue == "1" ? true : false; user.Blocked = user.Blocked; user.RoleId = Convert.ToInt32(ddlRole.SelectedValue); user = SecUserManager.Save(user); //clearControls(); scope.Complete(); if (isnew) { lblSave.Text = "User account has been created. User credentials sent via email."; } else { lblSave.Text = "User account saved."; } lblSave.Visible = true; btnResetPassword.Visible = true; } } catch (Exception ex) { isError = true; lblError.Text = ex.Message; lblError.Visible = true; Core.EmailHelper.emailError(ex); } finally { if (isnew && isError == false) { // email user account info Core.EmailHelper.emailUserCredentials(user); } } }