ADRole.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WF / Activities / Role / ADRole.cs / 1305376 / ADRole.cs

                            #region Using directives 

using System;
using System.Collections.Generic;
using System.Text; 
using System.DirectoryServices;
using System.Security.Permissions; 
using System.Security.Principal; 
using System.Runtime.Serialization;
 
using System.Workflow.ComponentModel;
using System.Diagnostics;

#endregion 

namespace System.Workflow.Activities 
{ 
    [Serializable]
 	public abstract class WorkflowRole 
	{
        public abstract String Name { set; get; }

        public abstract IList GetIdentities(); 

        public abstract bool IncludesIdentity(String identity); 
	} 

	[Serializable] 
 	sealed public class WorkflowRoleCollection : List
	{
 		public WorkflowRoleCollection()
 			: base() 
		{
 		} 
 
		public bool IncludesIdentity(String identity)
		{ 
            if (identity == null)
                return false;

			foreach (WorkflowRole role in this) 
 			{
                if (role != null) 
                { 
                    if (role.IncludesIdentity(identity))
                        return true; 
                }
			}

 			return false; 
 		}
	} 
 	 
	[Serializable]
    sealed public class ActiveDirectoryRole : WorkflowRole, ISerializable, IDisposable 
    {
        private String m_name;
        private DirectoryEntry m_root;
        private List m_operations; 

        internal ActiveDirectoryRole(DirectoryEntry rootEntry, IDirectoryOperation operation) 
        { 
            if (rootEntry == null)
                throw new ArgumentNullException("rootEntry"); 

            this.m_root = rootEntry;

            this.m_operations = new List(); 
            if (operation != null)
                this.m_operations.Add(operation); 
        } 

        internal ActiveDirectoryRole(DirectoryEntry rootEntry, ICollection operations) 
        {
            if (rootEntry == null)
                throw new ArgumentNullException("rootEntry");
 
            this.m_root = rootEntry;
 
            if (operations == null) 
                this.m_operations = new List();
            else 
                this.m_operations = new List(operations);
        }

        private ActiveDirectoryRole(SerializationInfo info, StreamingContext context) 
        {
            this.m_name = info.GetString("m_name"); 
            this.m_operations = (List)info.GetValue("m_operations", typeof(List)); 

            String path = info.GetString("m_root\\path"); 

            this.m_root = new DirectoryEntry( path );
        }
 
        [SecurityPermission( SecurityAction.Demand, SerializationFormatter = true)]
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) 
        { 
            info.AddValue("m_name", this.m_name);
            info.AddValue("m_operations", this.m_operations); 

            info.AddValue("m_root\\path", this.m_root.Path);
        }
 
        public override String Name
        { 
            get 
            {
                return this.m_name; 
            }

            set
            { 
                this.m_name = value;
            } 
        } 

        public DirectoryEntry RootEntry 
        {
            get
            {
                return this.m_root; 
            }
        } 
 
        internal ICollection Operations
        { 
            get
            {
                return this.m_operations;
            } 
        }
 
        public ActiveDirectoryRole GetManager() 
        {
            List queries = new List(this.Operations); 
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.DirectReports));

            return new ActiveDirectoryRole(this.RootEntry, queries);
        } 

        public ActiveDirectoryRole GetManagerialChain() 
        { 
            List queries = new List(this.Operations);
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.DirectReports, true)); 

            return new ActiveDirectoryRole(this.RootEntry, queries);
        }
 
        public ActiveDirectoryRole GetDirectReports()
        { 
            List queries = new List(this.Operations); 
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.Manager));
 
            return new ActiveDirectoryRole(this.RootEntry, queries);
        }

        public ActiveDirectoryRole GetAllReports() 
        {
            List queries = new List(this.Operations); 
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.Manager, true)); 

            return new ActiveDirectoryRole(this.RootEntry, queries); 
        }

        public ActiveDirectoryRole GetPeers()
        { 
            ICollection entries = this.GetEntries();
 
            List queries = new List(this.Operations); 
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.DirectReports));
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.Manager)); 

            foreach (DirectoryEntry entry in entries)
            {
                queries.Add(new DirectoryLocalQuery(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, (String)entry.Properties[ActiveDirectoryRoleFactory.Configuration.DistinguishedName][0], DirectoryQueryOperation.NotEqual)); 
            }
 
            return new ActiveDirectoryRole(this.RootEntry, queries); 
        }
 

        public ICollection GetEntries()
        {
            List currentEntries = new List(); 
            currentEntries.Add(this.m_root);
            List newEntries = new List(); 
 
            for (int i = 0; i < this.m_operations.Count; ++i)
            { 
                for (int j = 0; j < currentEntries.Count; ++j)
                {
                    this.m_operations[i].GetResult(this.m_root, currentEntries[j], newEntries);
                } 

                // Swap between new and current, as the for the new iteration the 'new' of 
                // now will be the current.  After the swap we clear out the 'new' list as to 
                // reuse it.
 
                List tempEntries = currentEntries;
                currentEntries = newEntries;
                newEntries = tempEntries;
                newEntries.Clear(); 
            }
 
            // Remove duplicates 

            Dictionary dFinal = new Dictionary(); 
            for (int i = 0; i < currentEntries.Count; ++i)
            {
                if (!dFinal.ContainsKey(currentEntries[i].Guid))
                    dFinal.Add(currentEntries[i].Guid, currentEntries[i]); 
            }
 
            return dFinal.Values; 
        }
 
        public IList GetSecurityIdentifiers()
        {
            List identifiers = new List();
 
            foreach (DirectoryEntry entry in this.GetEntries())
            { 
                if (entry.Properties["objectSid"] != null && 
                    entry.Properties["objectSid"].Count != 0)
                { 
                    identifiers.Add(new SecurityIdentifier((byte[])(entry.Properties["objectSid"][0]), 0));
                }
                else
                { 
                    WorkflowActivityTrace.Activity.TraceEvent(TraceEventType.Information, 0, "Unable to find 'objectSid' property for directory entry = {0}.", entry.Path);
                } 
            } 

            return identifiers; 
        }

        public override IList GetIdentities()
        { 
            List identityRefs = new List();
            foreach (SecurityIdentifier entrySid in this.GetSecurityIdentifiers()) 
            { 
                identityRefs.Add(entrySid.Translate(typeof(NTAccount)).ToString());
            } 
            return identityRefs;
        }

        public override bool IncludesIdentity(String identity) 
        {
            if (identity == null) 
                return false; 

            foreach (String roleIdentity in this.GetIdentities()) 
            {
                if (String.Compare(identity, roleIdentity, StringComparison.Ordinal) == 0)
                    return true;
            } 

            return false; 
        } 

        void IDisposable.Dispose() 
        {
            this.m_root.Dispose();
        }
 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
#region Using directives 

using System;
using System.Collections.Generic;
using System.Text; 
using System.DirectoryServices;
using System.Security.Permissions; 
using System.Security.Principal; 
using System.Runtime.Serialization;
 
using System.Workflow.ComponentModel;
using System.Diagnostics;

#endregion 

namespace System.Workflow.Activities 
{ 
    [Serializable]
 	public abstract class WorkflowRole 
	{
        public abstract String Name { set; get; }

        public abstract IList GetIdentities(); 

        public abstract bool IncludesIdentity(String identity); 
	} 

	[Serializable] 
 	sealed public class WorkflowRoleCollection : List
	{
 		public WorkflowRoleCollection()
 			: base() 
		{
 		} 
 
		public bool IncludesIdentity(String identity)
		{ 
            if (identity == null)
                return false;

			foreach (WorkflowRole role in this) 
 			{
                if (role != null) 
                { 
                    if (role.IncludesIdentity(identity))
                        return true; 
                }
			}

 			return false; 
 		}
	} 
 	 
	[Serializable]
    sealed public class ActiveDirectoryRole : WorkflowRole, ISerializable, IDisposable 
    {
        private String m_name;
        private DirectoryEntry m_root;
        private List m_operations; 

        internal ActiveDirectoryRole(DirectoryEntry rootEntry, IDirectoryOperation operation) 
        { 
            if (rootEntry == null)
                throw new ArgumentNullException("rootEntry"); 

            this.m_root = rootEntry;

            this.m_operations = new List(); 
            if (operation != null)
                this.m_operations.Add(operation); 
        } 

        internal ActiveDirectoryRole(DirectoryEntry rootEntry, ICollection operations) 
        {
            if (rootEntry == null)
                throw new ArgumentNullException("rootEntry");
 
            this.m_root = rootEntry;
 
            if (operations == null) 
                this.m_operations = new List();
            else 
                this.m_operations = new List(operations);
        }

        private ActiveDirectoryRole(SerializationInfo info, StreamingContext context) 
        {
            this.m_name = info.GetString("m_name"); 
            this.m_operations = (List)info.GetValue("m_operations", typeof(List)); 

            String path = info.GetString("m_root\\path"); 

            this.m_root = new DirectoryEntry( path );
        }
 
        [SecurityPermission( SecurityAction.Demand, SerializationFormatter = true)]
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) 
        { 
            info.AddValue("m_name", this.m_name);
            info.AddValue("m_operations", this.m_operations); 

            info.AddValue("m_root\\path", this.m_root.Path);
        }
 
        public override String Name
        { 
            get 
            {
                return this.m_name; 
            }

            set
            { 
                this.m_name = value;
            } 
        } 

        public DirectoryEntry RootEntry 
        {
            get
            {
                return this.m_root; 
            }
        } 
 
        internal ICollection Operations
        { 
            get
            {
                return this.m_operations;
            } 
        }
 
        public ActiveDirectoryRole GetManager() 
        {
            List queries = new List(this.Operations); 
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.DirectReports));

            return new ActiveDirectoryRole(this.RootEntry, queries);
        } 

        public ActiveDirectoryRole GetManagerialChain() 
        { 
            List queries = new List(this.Operations);
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.DirectReports, true)); 

            return new ActiveDirectoryRole(this.RootEntry, queries);
        }
 
        public ActiveDirectoryRole GetDirectReports()
        { 
            List queries = new List(this.Operations); 
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.Manager));
 
            return new ActiveDirectoryRole(this.RootEntry, queries);
        }

        public ActiveDirectoryRole GetAllReports() 
        {
            List queries = new List(this.Operations); 
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.Manager, true)); 

            return new ActiveDirectoryRole(this.RootEntry, queries); 
        }

        public ActiveDirectoryRole GetPeers()
        { 
            ICollection entries = this.GetEntries();
 
            List queries = new List(this.Operations); 
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.DirectReports));
            queries.Add(new DirectoryRedirect(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, ActiveDirectoryRoleFactory.Configuration.Manager)); 

            foreach (DirectoryEntry entry in entries)
            {
                queries.Add(new DirectoryLocalQuery(ActiveDirectoryRoleFactory.Configuration.DistinguishedName, (String)entry.Properties[ActiveDirectoryRoleFactory.Configuration.DistinguishedName][0], DirectoryQueryOperation.NotEqual)); 
            }
 
            return new ActiveDirectoryRole(this.RootEntry, queries); 
        }
 

        public ICollection GetEntries()
        {
            List currentEntries = new List(); 
            currentEntries.Add(this.m_root);
            List newEntries = new List(); 
 
            for (int i = 0; i < this.m_operations.Count; ++i)
            { 
                for (int j = 0; j < currentEntries.Count; ++j)
                {
                    this.m_operations[i].GetResult(this.m_root, currentEntries[j], newEntries);
                } 

                // Swap between new and current, as the for the new iteration the 'new' of 
                // now will be the current.  After the swap we clear out the 'new' list as to 
                // reuse it.
 
                List tempEntries = currentEntries;
                currentEntries = newEntries;
                newEntries = tempEntries;
                newEntries.Clear(); 
            }
 
            // Remove duplicates 

            Dictionary dFinal = new Dictionary(); 
            for (int i = 0; i < currentEntries.Count; ++i)
            {
                if (!dFinal.ContainsKey(currentEntries[i].Guid))
                    dFinal.Add(currentEntries[i].Guid, currentEntries[i]); 
            }
 
            return dFinal.Values; 
        }
 
        public IList GetSecurityIdentifiers()
        {
            List identifiers = new List();
 
            foreach (DirectoryEntry entry in this.GetEntries())
            { 
                if (entry.Properties["objectSid"] != null && 
                    entry.Properties["objectSid"].Count != 0)
                { 
                    identifiers.Add(new SecurityIdentifier((byte[])(entry.Properties["objectSid"][0]), 0));
                }
                else
                { 
                    WorkflowActivityTrace.Activity.TraceEvent(TraceEventType.Information, 0, "Unable to find 'objectSid' property for directory entry = {0}.", entry.Path);
                } 
            } 

            return identifiers; 
        }

        public override IList GetIdentities()
        { 
            List identityRefs = new List();
            foreach (SecurityIdentifier entrySid in this.GetSecurityIdentifiers()) 
            { 
                identityRefs.Add(entrySid.Translate(typeof(NTAccount)).ToString());
            } 
            return identityRefs;
        }

        public override bool IncludesIdentity(String identity) 
        {
            if (identity == null) 
                return false; 

            foreach (String roleIdentity in this.GetIdentities()) 
            {
                if (String.Compare(identity, roleIdentity, StringComparison.Ordinal) == 0)
                    return true;
            } 

            return false; 
        } 

        void IDisposable.Dispose() 
        {
            this.m_root.Dispose();
        }
 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK