Beispiel #1
0
        private IList<UserRowData> InternalSelect(int startRowIndex, int maximumRows, out int resultCount)
        {
            if (maximumRows == 0)
            {
                resultCount = 0;
                return new List<UserRowData>();
            }

            List<UserRowData> users;
            using (var service = new UserManagement())
            {
                var filter = new ListUsersRequest
                                 {
                                     UserName = UserName.Replace("*", "%").Replace("?", "_"),
                                     DisplayName = DisplayName.Replace("*", "%").Replace("?", "_"),
                                     Page = {FirstRow = startRowIndex},
                                     ExactMatchOnly = false
                                 };

                users = CollectionUtils.Map(
                    service.FindUsers(filter),
                    (UserSummary summary) => new UserRowData(summary, service.GetUserDetail(summary.UserName)));
            }
            resultCount = users.Count;

            return users;
        }
        public List<UserRowData> GetAllUsers()
        {
            List<UserRowData> data;
            
            using(UserManagement service = new UserManagement())
            {
                data = CollectionUtils.Map(
                    service.FindUsers(new ListUsersRequest()),
                    delegate(UserSummary summary)
                    {
                        UserRowData user = new UserRowData(summary, null);
                        return user;
                    });
            }

            return data;
        }
        public bool AddUser(UserRowData user)
        {
            bool success = false;

            using(var service = new UserManagement())
            {
                try
                {
	                var newUser = new UserDetail
		                {
			                UserName = user.UserName,
			                DisplayName = user.DisplayName,
			                Enabled = user.Enabled,
			                CreationTime = Platform.Time,
			                PasswordExpiryTime = Platform.Time,
			                EmailAddress = user.EmailAddress,
			                ResetPassword = true // TODO: Why do we need to reset password here?
		                };


                    var groups = new List<AuthorityGroupSummary>();

                    foreach (UserGroup userGroup in user.UserGroups)
                    {
                        groups.Add(new AuthorityGroupSummary(new EntityRef(userGroup.UserGroupRef), userGroup.Name,userGroup.Name, false, false));
                    }

                    newUser.AuthorityGroups = groups;
                    service.AddUser(newUser);
                    success = true;

                }
                catch (Exception ex)
                {
                	Platform.Log(LogLevel.Error, ex, "Unexpected exception adding user: {0}", user.DisplayName);
                }
            }

            return success;
        }
        public bool UpdateUser(UserRowData user)
        {
            bool success = false;

            using(UserManagement service = new UserManagement())
            {
                try
                {
                    UserDetail updateUser = new UserDetail
                                                {
                                                    UserName = user.UserName,
                                                    DisplayName = user.DisplayName,
                                                    EmailAddress = user.EmailAddress,
                                                    Enabled = user.Enabled
                                                };

                    List<AuthorityGroupSummary> groups = new List<AuthorityGroupSummary>();

                    foreach(UserGroup userGroup in user.UserGroups)
                    {
                        groups.Add(new AuthorityGroupSummary(new EntityRef(userGroup.UserGroupRef), userGroup.Name, userGroup.Name,false));
                    }

                    updateUser.AuthorityGroups = groups;

                    service.UpdateUserDetail(updateUser);
                    success = true;
                }
                catch (Exception ex)
                {
                	Platform.Log(LogLevel.Error, ex, "Unexpected exception updating user: {0}", user.DisplayName);
                }
            }

            return success;
        }
        public bool DeleteUser(UserRowData user)
        {
            bool success = false;

            using(UserManagement service = new UserManagement())
            {
                try
                {
                    service.DeleteUser(user.UserName);
                    success = true;
                }
                catch (Exception ex)
                {
					Platform.Log(LogLevel.Error, ex, "Unexpected exception deleting user: {0}",
								 user.DisplayName);
                }
            }

            return success;
        }
        public bool ResetPassword(UserRowData user)
        {
            bool success = false;

            using(UserManagement service = new UserManagement())
            {
                try
                {
                    service.ResetPassword(user.UserName);
                    success = true;
                }
                catch (Exception ex)
                {
                	Platform.Log(LogLevel.Error, ex, "Unexpected exception resetting password for user: {0}",
                	             user.DisplayName);
                }
            }

            return success;
        }
        public bool ExistsUsername(string username)
        {
            bool exists = false;

            using(UserManagement service = new UserManagement())
            {
                ListUsersRequest filter = new ListUsersRequest();
                filter.ExactMatchOnly = true;
                filter.UserName = username;

                IList<UserSummary> users = service.FindUsers(filter);

                if (users != null && users.Count > 0)
                {
                    exists = true;
                }
            }

            return exists;
        }