public PSADObject GetADObject(ADObjectFilterOptions options)
        {
            PSADObject result = null;

            Debug.Assert(options != null);

            if (IsSet(options.Mail, options.UPN, options.Id))
            {
                result = FilterUsers(options).FirstOrDefault();
            }

            if (result == null && IsSet(options.SPN, options.Id))
            {
                result = FilterServicePrincipals(options).FirstOrDefault();
            }

            if (result == null && IsSet(options.Mail, options.Id))
            {
                result = FilterGroups(options).FirstOrDefault();
            }

            return(result);
        }
        public Guid GetObjectId(ADObjectFilterOptions options)
        {
            Guid principalId;

            if (options != null && options.Id != null &&
                Guid.TryParse(options.Id, out principalId))
            {
                // do nothing, we have parsed the guid
            }
            else
            {
                PSADObject adObj = GetADObject(options);

                if (adObj == null)
                {
                    throw new KeyNotFoundException("The provided information does not map to an AD object id.");
                }

                principalId = adObj.Id;
            }

            return(principalId);
        }
Beispiel #3
0
        public IEnumerable <PSADUser> FilterUsers(ADObjectFilterOptions options, ulong first = ulong.MaxValue, ulong skip = 0)
        {
            if (!string.IsNullOrEmpty(options.Id))
            {
                User user = null;
                try
                {
                    user = GraphClient.Users.Get(Normalize(options.Id));
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (user != null)
                {
                    return(new List <PSADUser> {
                        user.ToPSADUser()
                    });
                }
            }
            else if (!string.IsNullOrEmpty(options.UPN) || !string.IsNullOrEmpty(options.Mail))
            {
                IPage <User> result = null;
                try
                {
                    string upnOrMail  = Normalize(options.UPN) ?? Normalize(options.Mail);
                    var    odataQuery = new Rest.Azure.OData.ODataQuery <User>();
                    if (!string.IsNullOrEmpty(options.UPN))
                    {
                        odataQuery.SetFilter(u => u.UserPrincipalName == upnOrMail);
                    }
                    else
                    {
                        odataQuery.SetFilter(u => u.Mail == upnOrMail);
                    }
                    result = GraphClient.Users.List(odataQuery);
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (result != null)
                {
                    return(result.Select(u => u.ToPSADUser()));
                }
            }
            else
            {
                Rest.Azure.OData.ODataQuery <User> odataQuery = null;
                if (!string.IsNullOrEmpty(options.SearchString) && options.SearchString.EndsWith("*"))
                {
                    options.SearchString = options.SearchString.TrimEnd('*');
                    odataQuery           = new Rest.Azure.OData.ODataQuery <User>(u => u.DisplayName.StartsWith(options.SearchString));
                }
                else
                {
                    odataQuery = new Rest.Azure.OData.ODataQuery <User>(u => u.DisplayName == options.SearchString);
                }

                return(new GenericPageEnumerable <User>(
                           delegate()
                {
                    return GraphClient.Users.List(odataQuery.ToString());
                }, GraphClient.Users.ListNext, first, skip).Select(u => u.ToPSADUser()));
            }

            return(new List <PSADUser>());
        }
        public List <PSADServicePrincipal> FilterServicePrincipals(ADObjectFilterOptions options)
        {
            List <PSADServicePrincipal> servicePrincipals = new List <PSADServicePrincipal>();
            IPage <ServicePrincipal>    result            = null;
            ServicePrincipal            servicePrincipal  = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipals.Get(options.Id);
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else if (!string.IsNullOrEmpty(options.SPN))
            {
                try
                {
                    var odataQuery = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.ServicePrincipalNames.Contains(options.SPN));
                    servicePrincipal = GraphClient.ServicePrincipals.List(odataQuery.ToString()).FirstOrDefault();
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        var odataQuery = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString));
                        result = GraphClient.ServicePrincipals.List(odataQuery);
                    }
                    else
                    {
                        result = GraphClient.ServicePrincipals.ListNext(options.NextLink);
                    }

                    servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {
                    var odataQuery = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString));
                    result = GraphClient.ServicePrincipals.List(odataQuery.ToString());
                    servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.ServicePrincipals.ListNext(result.NextPageLink);
                        servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));
                    }
                }
            }

            return(servicePrincipals);
        }
        public List <PSADGroup> FilterGroups(ADObjectFilterOptions options)
        {
            List <PSADGroup> groups = new List <PSADGroup>();

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    // use GetObjectsByObjectId to handle Redirects in the CSP scenario
                    PSADGroup group = this.GetObjectsByObjectId(new List <string> {
                        options.Id
                    }).FirstOrDefault() as PSADGroup;
                    if (group != null)
                    {
                        groups.Add(group);
                    }
                }
                catch { /* The group does not exist, ignore the exception */ }
            }
            else
            {
                IPage <ADGroup> result = null;
                Rest.Azure.OData.ODataQuery <ADGroup> odataQuery = null;

                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        if (options.Mail != null)
                        {
                            odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.Mail == options.Mail);
                        }
                        else
                        {
                            odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
                        }

                        result = GraphClient.Groups.List(odataQuery);
                    }
                    else
                    {
                        result = GraphClient.Groups.ListNext(options.NextLink);
                    }

                    groups.AddRange(result.Select(g => g.ToPSADGroup()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {
                    if (options.Mail != null)
                    {
                        odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.Mail == options.Mail);
                    }
                    else
                    {
                        odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
                    }

                    result = GraphClient.Groups.List(odataQuery.ToString());
                    groups.AddRange(result.Select(g => g.ToPSADGroup()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.Groups.ListNext(result.NextPageLink);
                        groups.AddRange(result.Select(g => g.ToPSADGroup()));
                    }
                }
            }

            return(groups);
        }
        public List <PSADUser> FilterUsers(ADObjectFilterOptions options)
        {
            List <PSADUser> users  = new List <PSADUser>();
            IPage <User>    result = null;
            User            user   = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    user = GraphClient.Users.Get(Normalize(options.Id));
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (user != null)
                {
                    users.Add(user.ToPSADUser());
                }
            }
            else if (!string.IsNullOrEmpty(options.UPN) || !string.IsNullOrEmpty(options.Mail))
            {
                try
                {
                    string upnOrMail  = Normalize(options.UPN) ?? Normalize(options.Mail);
                    var    odataQuery = new Rest.Azure.OData.ODataQuery <User>(u => u.UserPrincipalName == upnOrMail);
                    result = GraphClient.Users.List(odataQuery);
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (result != null)
                {
                    users.AddRange(result.Select(u => u.ToPSADUser()));
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        var odataQuery = new Rest.Azure.OData.ODataQuery <User>(u => u.DisplayName.StartsWith(options.SearchString));
                        result = GraphClient.Users.List(odataQuery.ToString());
                    }
                    else
                    {
                        result = GraphClient.Users.ListNext(options.NextLink);
                    }

                    users.AddRange(result.Select(u => u.ToPSADUser()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {
                    var odataQuery = new Rest.Azure.OData.ODataQuery <User>(u => u.DisplayName.StartsWith(options.SearchString));
                    result = GraphClient.Users.List(odataQuery.ToString());
                    users.AddRange(result.Select(u => u.ToPSADUser()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.Users.ListNext(result.NextPageLink);
                        users.AddRange(result.Select(u => u.ToPSADUser()));
                    }
                }
            }

            return(users);
        }