/// <summary> /// Enables a single user by his samAccountName. /// </summary> /// <param name="customerName">The name of the customer the user belongs to.</param> /// <param name="samAccountName">The samAccountName of the user to unblock.</param> public void EnableBySamAccountName(string customerName, string samAccountName) { // Get the Directory Searcher from the Foundation using (var activeDirectorySearcher = ActiveDirectoryConnector.GetDirectorySearcher(customerName)) { // Search for the user to activate. activeDirectorySearcher.Filter = "(&(sAMAccountName=" + samAccountName + "))"; activeDirectorySearcher.PropertiesToLoad.Add("userAccountControl"); var result = activeDirectorySearcher.FindOne(); if (result == null) { throw new UserNotFoundException("The user with the sAMAccountName " + samAccountName + " could not be found."); } // Get the DirectoryEntry that corresponds to the user. var entryToUpdate = result.GetDirectoryEntry(); // Perform the Activation var val = (int)entryToUpdate.Properties["userAccountControl"].Value; entryToUpdate.Properties["userAccountControl"].Value = val & (int)~UserAccountControl.UF_ACCOUNT_DISABLE; entryToUpdate.CommitChanges(); entryToUpdate.Close(); } }
/// <summary> /// Gets a user by his samAccountName. /// </summary> /// <param name="customerName"> The customer name. </param> /// <param name="samAccountName"> The samAccountName. </param> /// <returns> /// The retrieved user. /// </returns> public UserFromRepository GetBySamAccountName(string customerName, string samAccountName) { using (var activeDirectorySearcher = ActiveDirectoryConnector.GetDirectorySearcher(customerName)) { activeDirectorySearcher.Filter = $"(&(sAMAccountName={samAccountName}))"; var result = activeDirectorySearcher.FindOne(); if (result == null) { throw new UserNotFoundException($"The user with the sAMAccountName {samAccountName} could not be found."); } var userFromActiveDirectory = new UserFromActiveDirectory { DistinguishedName = result.Properties["distinguishedName"][0].ToString(), IsLocked = (bool)result.GetDirectoryEntry().InvokeGet("IsAccountLocked"), Attributes = result.Properties }; var userFromRepository = Mapper.Map <UserFromActiveDirectory, UserFromRepository>(userFromActiveDirectory); return(userFromRepository); } }
/// <summary> /// Updates a single user by his samAccountName. /// </summary> /// <param name="customerNo"> The customer No. </param> /// <param name="samAccountName">The samAccountName of the user to update.</param> /// <param name="userData">The updated user data.</param> public void UpdateBySamAccountName(string customerNo, string samAccountName, UserFromRepository userData) { using (var activeDirectorySearcher = ActiveDirectoryConnector.GetDirectorySearcher(customerNo)) { activeDirectorySearcher.Filter = $"(&(sAMAccountName={samAccountName}))"; var result = activeDirectorySearcher.FindOne(); if (result == null) { throw new UserNotFoundException($"The user with the sAMAccountName {samAccountName} could not be found."); } var userEntry = result.GetDirectoryEntry(); this.SetUserProperty(userEntry, ActiveDirectoryAttributeNames.AccountExpires, this.ParseDateToFileSystemTimeOrDefault(userData.ExpirationDate)); this.SetUserProperty(userEntry, ActiveDirectoryAttributeNames.FirstName, userData.ForeName); this.SetUserProperty(userEntry, ActiveDirectoryAttributeNames.LastName, userData.SurName); this.SetUserProperty(userEntry, ActiveDirectoryAttributeNames.Mail, userData.Email); this.SetUserProperty(userEntry, ActiveDirectoryAttributeNames.Description, userData.Description); this.SetUserProperty(userEntry, ActiveDirectoryAttributeNames.DisplayName, userData.DisplayName); userEntry.CommitChanges(); userEntry.Close(); } }
/// <summary> /// Gets all users for the given customer number. /// </summary> /// <param name="customerName">The customers name.</param> /// <param name="expression">The expression by which to filter the users. Empty String = Get all users.</param> /// <returns>A list of all retrieved users.</returns> public IEnumerable <UserFromRepository> GetUsers(string customerName, string expression) { // Get the DirectorySearcher from the Foundation using (var activeDirectorySearcher = ActiveDirectoryConnector.GetDirectorySearcher(customerName)) { activeDirectorySearcher.Filter = this.GetSearchFilter(expression); // Get all the users for the customer var results = activeDirectorySearcher.FindAll(); // Convert the search result into a viable dataset. var userList = (from SearchResult entry in results select new UserFromActiveDirectory { DistinguishedName = entry.Properties["distinguishedName"][0].ToString(), IsLocked = (bool)entry.GetDirectoryEntry().InvokeGet("IsAccountLocked"), Attributes = entry.Properties }).ToList(); // Map the Users into UserFromRepository Objects to make them actually readable. var usersFromRepository = Mapper.Map <List <UserFromActiveDirectory>, List <UserFromRepository> >(userList); return(usersFromRepository); } }