Ejemplo n.º 1
0
        /// <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();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a user with the given profile data.
        /// </summary>
        /// <param name="customerName"> The requesting users customer name. </param>
        /// <param name="userData">The data for the new account.</param>
        public void Create(string customerName, UserFromRepository userData)
        {
            using (var directoryRootEntry = ActiveDirectoryConnector.GetDirectorEntry(customerName))
            {
                userData.Name = $"{userData.ForeName} {userData.SurName}";
                userData.Cn   = userData.SamAccountName;

                try
                {
                    if (this.DirectoryEntryContainsUser(directoryRootEntry, userData.Cn))
                    {
                        throw new UserAlreadyExistsException($"A user with the CN '{userData.Cn}' already exists.");
                    }

                    if (this.DirectoryEntryContainsAccount(directoryRootEntry, userData.SamAccountName))
                    {
                        throw new UserAlreadyExistsException($"A user with the sAMAccountName '{userData.SamAccountName}' already exists.");
                    }

                    var newUser = directoryRootEntry.Children.Add($"CN={userData.Cn}", "user");

                    var userPrincipalName = $"{userData.SamAccountName}@Blueprint.local";
                    this.SetUserProperty(newUser, ActiveDirectoryAttributeNames.UserPrincipalName, userPrincipalName);
                    this.SetUserProperty(newUser, ActiveDirectoryAttributeNames.SamAccountName, userData.SamAccountName);
                    this.SetUserProperty(newUser, ActiveDirectoryAttributeNames.AccountExpires, this.ParseDateToFileSystemTimeOrDefault(userData.ExpirationDate));
                    this.SetUserProperty(newUser, ActiveDirectoryAttributeNames.Name, userData.Name);
                    this.SetUserProperty(newUser, ActiveDirectoryAttributeNames.FirstName, userData.ForeName);
                    this.SetUserProperty(newUser, ActiveDirectoryAttributeNames.LastName, userData.SurName);
                    this.SetUserProperty(newUser, ActiveDirectoryAttributeNames.Mail, userData.Email);
                    this.SetUserProperty(newUser, ActiveDirectoryAttributeNames.Description, userData.Description);
                    this.SetUserProperty(newUser, ActiveDirectoryAttributeNames.DisplayName, userData.DisplayName);

                    // Password and Group Memberships cannot be set during an initial registration.
                    // Both can only be set on already existing users, therefore the user must be saved, before we can proceed.
                    try
                    {
                        newUser.CommitChanges();
                    }
                    catch (Exception ex)
                    {
                        newUser.Close();
                        throw new UserNotCreatedException("Failed to create user!", ex);
                    }

                    // Set the new users password to the given value.
                    this.SetUserPassword(newUser, userData.Password);

                    // Add the User to some groups.
                    this.AddUserToSampleGroups(directoryRootEntry, newUser, customerName);
                }
                finally
                {
                    directoryRootEntry.Close();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Maps the given UserFromRepository object into a UserFromService object.
        /// </summary>
        /// <param name="userFromRepository">The user from repository.</param>
        /// <returns>The mapped object.</returns>
        private static UserFromService MapUser(UserFromRepository userFromRepository)
        {
            if (userFromRepository == null)
            {
                return(null);
            }

            var userFromService = Mapper.Map <UserFromRepository, UserFromService>(userFromRepository);

            return(userFromService);
        }