Example #1
0
        static void Main(string[] args)
        {
            var startSettings = new RepositoryStartSettings
            {
                Console = Console.Out,
                StartLuceneManager = true
            };
            using (var repo = Repository.Start(startSettings))
            {
                using (var traceOperation = Logger.TraceOperation("SyncAD2Portal", string.Empty, AdLog.AdSyncLogCategory))
                {
                    SyncAD2Portal directoryServices = new SyncAD2Portal();
                    directoryServices.SyncFromAD();

                    traceOperation.IsSuccessful = true;
                }
            }
        }
 /* ==================================================================================== Event handlers */
 protected void _btnCheck_Click(object sender, EventArgs e)
 {
     var syncAD2Portal = new SyncAD2Portal();
     var syncInfo = syncAD2Portal.GetSyncInfo(_tbLdapPath.Text);
     
     string syncInfoStr;
     if (!syncInfo.SyncTreeFound) {
         syncInfoStr = "Configured SyncTree could not be found for this path. Check the <a href='/Explore.html#/Root/System/SystemPlugins/Tools/DirectoryServices/AD2PortalConfig.xml' target='_blank'>configuration</a>!";
     }
     else 
     {
         syncInfoStr = string.Format("Configured synctree: ({0}, {1}) -> ({2}) (<a href='/Explore.html#/Root/System/SystemPlugins/Tools/DirectoryServices/AD2PortalConfig.xml' target='_blank'>configuration</a>)<br/>Target portal path: <a href='/Explore.html#{3}' target='_blank'>{3}</a><br/>{4}<br/>{5}",
             syncInfo.SyncTreeADIPAddress,
             syncInfo.SyncTreeADPath,
             syncInfo.SyncTreePortalPath,
             syncInfo.TargetPortalPath,
             syncInfo.PortalNodeExists ? "Target path exists" : "Target path does not exist",
             syncInfo.PortalParentExists ? "Target parent path exists" : "Target parent path does not exist"
             );
     }
     this.Controls.Add(new Literal { Text = "<hr/><strong>Results:</strong><br/>" });
     this.Controls.Add(new Literal { Text = syncInfoStr });
 }
        protected void _btnSyncObject_Click(object sender, EventArgs e)
        {
            this.Controls.Add(new Literal { Text = "<hr/><strong>Results:</strong><br/>" });
            
            try
            {
                var syncAD2Portal = new SyncAD2Portal();

                // impersonate to currently logged on windows user, to use its credentials to connect to AD
                WindowsImpersonationContext impersonationContext = null;
                if (this.UseImpersonate)
                {
                    var windowsIdentity = ((User)User.Current).WindowsIdentity;
                    if (windowsIdentity == null)
                    {
                        this.Controls.Add(new Literal { Text = "Windows identity impersonation failed for current user." });
                        return;
                    }
                    impersonationContext = windowsIdentity.Impersonate();
                }

                int? logid = null;
                bool noerrors = false;
                try
                {
                    logid = AdLog.SubscribeToLog();
                    syncAD2Portal.SyncObjectFromAD(_tbLdapPath.Text);
                    noerrors = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteException(ex);
                    this.Controls.Add(new Literal { Text = string.Format("An error occurred during execution ({0}).<br/>See adsync log below and eventlog for details.<br/><br/>", ex.Message) });
                }
                finally
                {
                    if (impersonationContext != null)
                        impersonationContext.Undo();

                    if (logid.HasValue)
                    {
                        var logStr = AdLog.GetLogAndRemoveSubscription(logid.Value);
                        this.Controls.Add(new Literal { Text = logStr.Replace(Environment.NewLine, "<br/>") });
                    }

                    // add link to object to bottom
                    if (noerrors)
                    {
                        var syncInfo = syncAD2Portal.GetSyncInfo(_tbLdapPath.Text);
                        if (syncInfo.PortalNodeExists)
                            this.Controls.Add(new Literal { Text = string.Format("<hr/>Check the results: <a href='/Explore.html#{0}' target='_blank'>{0}</a>", syncInfo.TargetPortalPath) });
                    }
                }
            }
            catch (SecurityException ex)
            {
                Logger.WriteException(ex);
                this.Controls.Add(new Literal { Text = string.Format("A security exception occurred ({0}).<br/>See eventlog for details.", ex.Message) });
            }
            catch (Exception ex)
            {
                Logger.WriteException(ex);
                this.Controls.Add(new Literal { Text = string.Format("An exception occurred ({0}).<br/>See eventlog for details.", ex.Message) });
            }
        }