예제 #1
0
        /// <summary>
        /// Optionally called during [list construction](@ref AcUtils#AcUsers#AcUsers) to initialize the
        /// <a href="class_ac_utils_1_1_ac_user.html#properties">default set</a> of regular user properties
        /// and others from Active Directory. This method is called internally and not by user code.
        /// </summary>
        /// <param name="dc">List of DomainElement objects as defined in <tt>\<prog_name\>.exe.config</tt> \e domains section. Used to initialize
        /// the user properties <a href="class_ac_utils_1_1_ac_user.html#properties">default set</a> from Active Directory.</param>
        /// <param name="pc">List of PropElement objects as defined in <tt>\<prog_name\>.exe.config</tt> \e properties section. Used to add
        /// and initialize user properties from Active Directory that are outside the regular <a href="class_ac_utils_1_1_ac_user.html#properties">default set</a>.</param>
        /// <returns>\e true if no exception was thrown and operation succeeded, \e false otherwise.</returns>
        /// <exception cref="Exception">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
        /// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on failure to handle a range of exceptions.</exception>
        /*! \sa DomainCollection, PropCollection, Other */
        /*! \attention User properties require that AccuRev principal names match login names stored on the LDAP server. */
        internal async Task <bool> initFromADAsync(DomainCollection dc, PropCollection pc = null)
        {
            return(await Task.Run(() =>
            {
                bool ret = true; // assume success
                foreach (DomainElement de in dc)
                {
                    PrincipalContext ad = null;
                    try
                    {
                        ad = new PrincipalContext(ContextType.Domain, de.Host.Trim(), de.Path.Trim());
                        UserPrincipal up = new UserPrincipal(ad);
                        up.SamAccountName = Principal.Name;
                        using (PrincipalSearcher ps = new PrincipalSearcher(up))
                        {
                            UserPrincipal rs = (UserPrincipal)ps.FindOne();
                            if (rs != null)
                            {
                                _givenName = rs.GivenName;
                                _middleName = rs.MiddleName;
                                _surname = rs.Surname;
                                _displayName = rs.DisplayName;
                                _business = rs.VoiceTelephoneNumber;
                                _emailAddress = rs.EmailAddress;
                                _description = rs.Description;
                                _distinguishedName = rs.DistinguishedName;

                                if (pc != null)
                                {
                                    DirectoryEntry lowerLdap = (DirectoryEntry)rs.GetUnderlyingObject();
                                    foreach (PropElement pe in pc)
                                    {
                                        PropertyValueCollection pvc = lowerLdap.Properties[pe.Field];
                                        if (pvc != null)
                                        {
                                            _other.Add(pe.Title.Trim(), pvc.Value);
                                        }
                                    }
                                }

                                break;
                            }
                        }
                    }

                    catch (Exception ecx)
                    {
                        ret = false;
                        AcDebug.Log($"Exception caught and logged in AcUser.initFromADAsync{Environment.NewLine}{ecx.Message}");
                    }

                    // avoid CA2202: Do not dispose objects multiple times
                    finally { if (ad != null)
                              {
                                  ad.Dispose();
                              }
                    }
                }

                return ret;
            }).ConfigureAwait(false));
        }