Esempio n. 1
0
        /// <summary>
        /// 修改当前域用户指定的属性信息(因为有些属性不需要修改的或者不能修改的)
        /// 选择属性名时,可以用类DomainUserPropertities中的对象来标示
        /// </summary>
        /// <param name="domainUserPropertityNames"></param>
        /// <param name="adminUser">拥有权限的用户(要有用户名和密码)</param>
        public void ModifyInfo(string[] domainUserPropertityNames, DomainUser adminUser)
        {
            if (domainUserPropertityNames == null)
            {
                throw new ArgumentNullException("domainUserPropertityNames");
            }

            using (DirectoryEntry userEntry = DomainUtility.GetUserEntry(LDAPCurrentUserPath, this, adminUser))
            {
                foreach (string propertityName in domainUserPropertityNames)
                {
                    string formatPropertyName = propertityName.ToUpper().Trim();

                    #region Address

                    if (formatPropertyName == DomainUserPropertities.Address.City.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Address.City))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Address.City].Value = Address.City.Trim();
                        continue;
                    }
                    if (formatPropertyName == DomainUserPropertities.Address.Country.CountryAB.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Address.Country.CountryAB))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Address.Country.CountryAB].Value = Address.Country.CountryAB.Trim();
                        continue;
                    }
                    if (formatPropertyName == DomainUserPropertities.Address.Country.CountryCode.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Address.Country.CountryCode))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Address.Country.CountryCode].Value = Address.Country.CountryCode.Trim();
                        continue;
                    }
                    if (formatPropertyName == DomainUserPropertities.Address.Country.CountryName.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Address.Country.CountryName))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Address.Country.CountryName].Value = Address.Country.CountryName.Trim();
                        continue;
                    }
                    if (formatPropertyName == DomainUserPropertities.Address.PostCode.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Address.PostCode))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Address.PostCode].Value = Address.PostCode.Trim();
                        continue;
                    }
                    if (formatPropertyName == DomainUserPropertities.Address.PostOfficeBox.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Address.PostOfficeBox))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Address.PostOfficeBox].Value = Address.PostOfficeBox.Trim();
                        continue;
                    }
                    if (formatPropertyName == DomainUserPropertities.Address.Province.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Address.Province))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Address.Province].Value = Address.Province.Trim();
                        continue;
                    }
                    if (formatPropertyName == DomainUserPropertities.Address.Street.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Address.Street))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Address.Street].Value = Address.Street.Trim();
                        continue;
                    }

                    #endregion

                    #region Company

                    if (formatPropertyName == DomainUserPropertities.Company.CompanyName.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Company.CompanyName))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Company.CompanyName].Value = Company.CompanyName.Trim();
                        continue;
                    }
                    if (formatPropertyName == DomainUserPropertities.Company.Department.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Company.Department))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Company.Department].Value = Company.Department.Trim();
                        continue;
                    }
                    if (formatPropertyName == DomainUserPropertities.Company.Manager.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Company.Manager))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Company.Manager].Value = Company.Manager.Trim();
                        continue;
                    }
                    if (formatPropertyName == DomainUserPropertities.Company.Title.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Company.Title))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Company.Title].Value = Company.Title.Trim();
                        continue;
                    }

                    #endregion

                    #region Telephone

                    if (formatPropertyName == DomainUserPropertities.Telephone.Fax.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Telephone.Fax))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Telephone.Fax].Value = Telephone.Fax.Trim();
                        continue;
                    }

                    if (formatPropertyName == DomainUserPropertities.Telephone.HomePhone.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Telephone.HomePhone))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Telephone.HomePhone].Value = Telephone.HomePhone.Trim();
                        continue;
                    }

                    if (formatPropertyName == DomainUserPropertities.Telephone.Mobile.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Telephone.Mobile))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Telephone.Mobile].Value = Telephone.Mobile.Trim();
                        continue;
                    }

                    if (formatPropertyName == DomainUserPropertities.Telephone.Pager.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Telephone.Pager))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Telephone.Pager].Value = Telephone.Pager.Trim();
                        continue;
                    }

                    if (formatPropertyName == DomainUserPropertities.Telephone.IPPhone.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(Telephone.IPPhone))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.Telephone.IPPhone].Value = Telephone.IPPhone.Trim();
                        continue;
                    }

                    #endregion

                    #region User

                    if (formatPropertyName == DomainUserPropertities.User.Description.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(User.Description))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.User.Description].Value = User.Description.Trim();
                        continue;
                    }

                    if (formatPropertyName == DomainUserPropertities.User.DisplayName.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(User.DisplayName))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.User.DisplayName].Value = User.DisplayName.Trim();
                        continue;
                    }

                    if (formatPropertyName == DomainUserPropertities.User.GivenName.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(User.GivenName))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.User.GivenName].Value = User.GivenName.Trim();
                        continue;
                    }

                    if (formatPropertyName == DomainUserPropertities.User.HomePage.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(User.HomePage))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.User.HomePage].Value = User.HomePage.Trim();
                        continue;
                    }

                    if (formatPropertyName == DomainUserPropertities.User.Initials.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(User.Initials))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.User.Initials].Value = User.Initials.Trim();
                        continue;
                    }

                    if (formatPropertyName == DomainUserPropertities.User.LastName.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(User.LastName))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.User.LastName].Value = User.LastName.Trim();
                        continue;
                    }

                    if (formatPropertyName == DomainUserPropertities.User.Mail.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(User.OfficeEMailAddress))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.User.Mail].Value = User.OfficeEMailAddress.Trim();
                        continue;
                    }

                    ////Name属性好像不能直接修改,它是RDN属性。
                    //if (formatPropertyName == DomainUserPropertities.User.Name.ToUpper().Trim())
                    //{
                    //    userEntry.Properties[DomainUserPropertities.User.Name].Value = User.Name;
                    //    continue;
                    //}

                    if (formatPropertyName == DomainUserPropertities.User.PhysicalDeliveryOfficeName.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(User.PhysicalDeliveryOfficeName))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.User.PhysicalDeliveryOfficeName].Value = User.PhysicalDeliveryOfficeName.Trim();
                        continue;
                    }

                    if (formatPropertyName == DomainUserPropertities.User.TelephoneNumber.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(User.OfficeTelephoneNumber))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.User.TelephoneNumber].Value = User.OfficeTelephoneNumber.Trim();
                        continue;
                    }


                    if (formatPropertyName == DomainUserPropertities.User.UserName.ToUpper().Trim())
                    {
                        if (string.IsNullOrEmpty(User.UserName))
                        {
                            continue;
                        }
                        userEntry.Properties[DomainUserPropertities.User.UserName].Value = User.UserName.Trim();
                        continue;
                    }

                    #endregion

                    throw new Exception(string.Format("不支持的属性更新:PropertityName = {0}.", propertityName));
                }

                userEntry.CommitChanges();
            }
        }
        /// <summary>
        /// 把当前组织单元和用户集合转换成数据表
        /// </summary>
        public DataTable ToDataTable()
        {
            DataTable dtOUSchema   = DomainOrganizationUnit.ToDataTableSchema();
            DataTable dtUserSchema = DomainUser.ToDataTableSchema();

            DataTable dtResult = new DataTable();

            dtResult.TableName = "组织单元用户表";
            foreach (DataColumn col in dtOUSchema.Columns)
            {
                col.ColumnName = "OU_" + col.ColumnName;
                dtResult.Columns.Add(new DataColumn()
                {
                    ColumnName = col.ColumnName, Caption = col.Caption
                });
            }
            foreach (DataColumn col in dtUserSchema.Columns)
            {
                col.ColumnName = "USER_" + col.ColumnName;
                dtResult.Columns.Add(new DataColumn()
                {
                    ColumnName = col.ColumnName, Caption = col.Caption
                });
            }


            foreach (DomainOrganizationUnitUser ouUser in List)
            {
                DataTable dtResultTemp = dtResult.Clone();

                DataTable dtOU    = ouUser.OU.ToDataTable();
                DataTable dtUsers = ouUser.Users.ToDataTable();

                if (dtUsers.Rows.Count == 0)
                {
                    continue;
                }

                foreach (DataRow drUser in dtUsers.Rows)
                {
                    DataRow drNew = dtResultTemp.NewRow();

                    foreach (DataRow drOU in dtOU.Rows)
                    {
                        foreach (DataColumn col in dtOU.Columns)
                        {
                            drNew["OU_" + col.ColumnName] = drOU[col];
                        }
                    }

                    foreach (DataColumn col in dtUsers.Columns)
                    {
                        drNew["USER_" + col.ColumnName] = drUser[col];
                    }

                    dtResultTemp.Rows.Add(drNew);
                }

                dtResult.Merge(dtResultTemp);
            }

            return(dtResult);
        }
Esempio n. 3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="user"></param>
 /// <returns></returns>
 public bool Contains(DomainUser user)
 {
     return(List.Contains(user));
 }