public static PSADObject ToPSADObject(this ADGroup group)
        {
            var adObj = new PSADObject()
            {
                DisplayName = group.DisplayName
            };

            return(AssignObjectId(adObj, group.ObjectId));
        }
        public static PSADObject ToPSADObject(this User user)
        {
            var adObj = new PSADObject()
            {
                DisplayName = user.DisplayName
            };

            return(AssignObjectId(adObj, user.ObjectId));
        }
        public static PSADObject ToPSADGroup(this AADObject obj)
        {
            var adObj = new PSADObject()
            {
                DisplayName = obj.DisplayName,
            };

            return(AssignObjectId(adObj, obj.ObjectId));
        }
        public static PSADObject ToPSADObject(this AADObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException();
            }

            if (obj.ObjectType == typeof(User).Name)
            {
                var adUser = new PSADUser()
                {
                    DisplayName       = obj.DisplayName,
                    Type              = obj.ObjectType,
                    UserPrincipalName = obj.UserPrincipalName
                };

                return(AssignObjectId(adUser, obj.ObjectId));
            }
            else if (obj.ObjectType == "Group")
            {
                var adGroup = new PSADGroup()
                {
                    DisplayName     = obj.DisplayName,
                    Type            = obj.ObjectType,
                    SecurityEnabled = obj.SecurityEnabled,
                    MailNickname    = obj.Mail
                };
                return(AssignObjectId(adGroup, obj.ObjectId));
            }
            else if (obj.ObjectType == typeof(ServicePrincipal).Name)
            {
                var adSp = new PSADServicePrincipal()
                {
                    DisplayName           = obj.DisplayName,
                    Type                  = obj.ObjectType,
                    ServicePrincipalNames = obj.ServicePrincipalNames.ToArray()
                };

                return(AssignObjectId(adSp, obj.ObjectId));
            }
            else
            {
                var adObj = new PSADObject()
                {
                    DisplayName = obj.DisplayName,
                    Type        = obj.ObjectType
                };

                return(AssignObjectId(adObj, obj.ObjectId));
            }
        }
        public static PSADObject AssignObjectId(PSADObject adObj, string objectId)
        {
            Guid objectIdGuid;

            if (Guid.TryParse(objectId, out objectIdGuid))
            {
                adObj.Id = objectIdGuid;
            }
            else
            {
                adObj.AdfsId = objectId;
            }

            return(adObj);
        }
        public List <PSADObject> ListUserGroups(string principal)
        {
            List <PSADObject> result = new List <PSADObject>();
            Guid objectId            = GetObjectId(new ADObjectFilterOptions {
                UPN = principal
            });
            PSADObject user = GetADObject(new ADObjectFilterOptions {
                Id = objectId.ToString()
            });
            var groupsIds    = GraphClient.Users.GetMemberGroups(objectId.ToString(), new UserGetMemberGroupsParameters());
            var groupsResult = GraphClient.Objects.GetObjectsByObjectIds(new GetObjectsParameters {
                ObjectIds = groupsIds.ToList()
            });

            result.AddRange(groupsResult.Select(g => g.ToPSADGroup()));

            return(result);
        }
        public string GetAdfsObjectId(ADObjectFilterOptions options)
        {
            string principalId = null;

            if (options != null && options.Id != null)
            {
                // 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.AdfsId;
            }

            return(principalId);
        }
        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);
        }