/// <summary> /// Updates an existing user /// </summary> protected void btnEditSaveUser_Click(object sender, EventArgs e) { ADUsers users = new ADUsers(Config.Username, Config.Password, Config.PrimaryDC); try { // Create our user object ADUser user = new ADUser(); user.UserPrincipalName = txtEditLoginName.Text; user.SamAccountName = txtEditSamAccountName.Text; user.CompanyCode = CPContext.SelectedCompanyCode; user.Firstname = txtEditFirstName.Text; user.Middlename = txtEditMiddleName.Text; user.Lastname = txtEditLastName.Text; user.DisplayName = txtEditDisplayName.Text; user.Department = txtEditDepartment.Text; user.IsCompanyAdmin = cbEditCompanyAdmin.Checked; user.IsResellerAdmin = cbEditResellerAdmin.Checked; user.IsEnabled = cbEnableUser.Checked; user.PasswordNeverExpires = cbEditPwdNeverExpires.Checked; if (!string.IsNullOrEmpty(txtEditPwd1.Text)) user.Password = txtEditPwd1.Text; // Update our user in Active Directory users.Edit(user); // Update SQL DbSql.Update_User(user); // Do not save if they are a super admin or reseller admin or company admin permissions were removed if (Master.IsSuperAdmin || Master.IsResellerAdmin || !cbEditCompanyAdmin.Checked) { // Modify the users permissions if they are a company admin ModifyAdminPermissions(user.UserPrincipalName, cbEditCompanyAdmin.Checked, false); } // Update notification notification1.SetMessage(controls.notification.MessageType.Success, Resources.LocalizedText.NotificationSuccessUpdateUser + user.DisplayName + "!"); // Refresh View GetUsers(); } catch (Exception ex) { notification1.SetMessage(controls.notification.MessageType.Error, ex.Message); // Log Error // this.logger.Error("There was an error updating a user for company " + CPContext.SelectedCompanyName, ex); } finally { if (users != null) users.Dispose(); } }
/// <summary> /// Used for the import feature /// </summary> /// <param name="user"></param> /// <param name="plan"></param> public static void UpdateUserMailbox(ADUser user, MailboxPlan plan) { SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString); SqlCommand cmd = new SqlCommand(@"UPDATE Users SET MailboxPlan=@MailboxPlan, AdditionalMB=@AdditionalMB WHERE UserPrincipalName=@UserPrincipalName", sql); try { // Add our parameters cmd.Parameters.AddWithValue("@MailboxPlan", plan.PlanID); cmd.Parameters.AddWithValue("@AdditionalMB", plan.AdditionalMBAdded); cmd.Parameters.AddWithValue("@UserPrincipalName", user.UserPrincipalName); // Open connection sql.Open(); // Insert data cmd.ExecuteNonQuery(); // Close sql.Close(); } catch (Exception) { throw; } finally { cmd.Dispose(); sql.Dispose(); } }
/// <summary> /// Create new user /// </summary> protected void btnSaveUser_Click(object sender, EventArgs e) { ADUsers users = new ADUsers(Config.Username, Config.Password, Config.PrimaryDC); try { // Create our user object ADUser user = new ADUser(); user.CompanyCode = CPContext.SelectedCompanyCode; user.Firstname = txtFirstName.Text.Trim(); user.Middlename = txtMiddleName.Text.Trim(); user.Lastname = txtLastName.Text.Trim(); user.DisplayName = txtDisplayName.Text.Trim(); user.Department = txtDepartment.Text.Trim(); user.UserPrincipalName = txtLoginName.Text.Replace(" ", string.Empty) + "@" + ddlLoginDomain.SelectedValue; user.IsCompanyAdmin = cbIsCompanyAdministrator.Checked; user.IsResellerAdmin = cbIsResellerAdministrator.Checked; user.PasswordNeverExpires = cbPasswordNeverExpires.Checked; // Check if we are using custom name attribute if (Config.CustomNameAttribute) user.Name = txtFullName.Text.Trim(); else user.Name = string.Empty; // Set to empty so our class knows to use UPN instead // Create our user ADUser returnedUser = users.Create(user, Retrieve.GetCompanyUsersOU, txtPassword1.Text); // Insert into SQL SQLUsers.AddUser(returnedUser); // Modify the users permissions if they are a company admin ModifyAdminPermissions(user.UserPrincipalName, cbIsCompanyAdministrator.Checked, true); // Update notification notification1.SetMessage(controls.notification.MessageType.Success, Resources.LocalizedText.NotificationSuccessCreateUser + " " + user.DisplayName + "!"); // Refresh View GetUsers(); } catch (Exception ex) { notification1.SetMessage(controls.notification.MessageType.Error, ex.Message); // Log Error // this.logger.Error("There was an error creating an new user for company " + CPContext.SelectedCompanyName, ex); } finally { users.Dispose(); } }
/// <summary> /// Adds a new user to SQL /// </summary> /// <param name="user"></param> public static void AddUser(ADUser user) { SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString); SqlCommand cmd = new SqlCommand(@"INSERT INTO Users (UserGuid, CompanyCode, sAMAccountName, UserPrincipalName, DistinguishedName, DisplayName, Firstname, Middlename, Lastname, Email, Department, IsResellerAdmin, IsCompanyAdmin, MailboxPlan, TSPlan, LyncPlan, Created, AdditionalMB) VALUES (@UserGuid, @CompanyCode, @sAMAccountName, @UserPrincipalName, @DistinguishedName, @DisplayName, @Firstname, @Middlename, @Lastname, @Email, @Department, @IsResellerAdmin, @IsCompanyAdmin, @MailboxPlan, @TSPlan, @LyncPlan, GETDATE(), 0)", sql); try { // Add our parameters cmd.Parameters.AddWithValue("@UserGuid", user.UserGuid); cmd.Parameters.AddWithValue("@CompanyCode", user.CompanyCode); cmd.Parameters.AddWithValue("@sAMAccountName", user.SamAccountName); cmd.Parameters.AddWithValue("@UserPrincipalName", user.UserPrincipalName); cmd.Parameters.AddWithValue("@DistinguishedName", user.DistinguishedName); cmd.Parameters.AddWithValue("@DisplayName", user.DisplayName); cmd.Parameters.AddWithValue("@Firstname", user.Firstname); cmd.Parameters.AddWithValue("@Middlename", string.IsNullOrEmpty(user.Middlename) ? "" : user.Middlename); cmd.Parameters.AddWithValue("@Lastname", string.IsNullOrEmpty(user.Lastname) ? "" : user.Lastname); cmd.Parameters.AddWithValue("@Email", string.Empty); cmd.Parameters.AddWithValue("@Department", string.IsNullOrEmpty(user.Department) ? "" : user.Department); cmd.Parameters.AddWithValue("@IsResellerAdmin", user.IsResellerAdmin); cmd.Parameters.AddWithValue("@IsCompanyAdmin", user.IsCompanyAdmin); cmd.Parameters.AddWithValue("@MailboxPlan", 0); cmd.Parameters.AddWithValue("@TSPlan", 0); cmd.Parameters.AddWithValue("@LyncPlan", 0); // Open connection sql.Open(); // Insert data cmd.ExecuteNonQuery(); // Close sql.Close(); } catch (Exception) { throw; } finally { cmd.Dispose(); sql.Dispose(); } }