Exemplo n.º 1
0
        public async Task <DirectoryRole> FindRoleByName(string name, CancellationToken token)
        {
            IGraphServiceDirectoryRolesCollectionPage page = await client.DirectoryRoles.Request()
                                                             .Filter($"displayname eq {name}")
                                                             .GetAsync(token);

            return(page.FirstOrDefault());
        }
Exemplo n.º 2
0
        public static void DeviceOwners(Device _,
                                        List <DirectoryObject> ownerList,
                                        IGraphServiceDirectoryRolesCollectionPage directoryRoles)
        {
            try
            {
                var gremlinVertices   = new List <GremlinVertex>();
                var gremlinEdges      = new List <GremlinEdge>();
                var deviceOwnerGroups =
                    directoryRoles.Where(__ =>
                                         MicrosoftGraphApiHelper.DeviceOwnerGroupDisplayNames.Contains(__.DisplayName));
                var vertex = new GremlinVertex(_.Id, nameof(Computer));

                vertex.AddProperty(CosmosDbHelper.CollectionPartitionKey, _.Id.GetHashCode());
                vertex.AddProperty(nameof(_.DisplayName), _.DisplayName?.ToUpper() ?? string.Empty);
                gremlinVertices.Add(vertex);
                ownerList.ForEach(__ =>
                {
                    var user        = (User)__;
                    var gremlinEdge = new GremlinEdge(
                        user.Id + _.Id,
                        "AdminTo",
                        user.Id,
                        _.Id,
                        nameof(User),
                        nameof(Computer),
                        user.Id.GetHashCode(),
                        _.Id.GetHashCode());

                    gremlinEdges.Add(gremlinEdge);
                });
                deviceOwnerGroups.ForEach(directoryRole =>
                {
                    var gremlinEdge = new GremlinEdge(
                        directoryRole.Id + _.Id,
                        "AdminTo",
                        directoryRole.Id,
                        _.Id,
                        nameof(DirectoryRole),
                        nameof(Computer),
                        directoryRole.Id.GetHashCode(),
                        _.Id.GetHashCode());

                    gremlinEdges.Add(gremlinEdge);
                });

                CosmosDbHelper.RunImportVerticesBlock.Post(gremlinVertices);
                CosmosDbHelper.RunImportEdgesBlock.Post(gremlinEdges);
            }
            catch (ClientException ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// Retrieve the role name id string
        /// </summary>
        /// <param name="graphServiceClient"></param>
        /// <param name="roleName"></param>
        /// <param name="directoryRole"></param>
        /// <param name="directoryRoleTemplate"></param>
        /// <returns></returns>
        public async static Task <string> GetRoleByName(GraphServiceClient graphServiceClient, string roleName,
                                                        IGraphServiceDirectoryRolesCollectionPage directoryRole,
                                                        IGraphServiceDirectoryRoleTemplatesCollectionPage directoryRoleTemplate)
        {
            roleName = roleName.ToLower();
            try
            {
                foreach (var role in directoryRole)
                {
                    var directoryRoleName = role.DisplayName.ToLower();
                    if (directoryRoleName.Contains(roleName))
                    {
                        return(role.Id);
                    }
                }
                foreach (var roleTemplate in directoryRoleTemplate)
                {
                    var directoryRoleTemplateName = roleTemplate.DisplayName.ToLower();
                    if (directoryRoleTemplateName.Contains(roleName))
                    {
                        var directoryRoleCreate = new DirectoryRole()
                        {
                            DisplayName    = roleTemplate.DisplayName,
                            RoleTemplateId = roleTemplate.Id,
                        };

                        DirectoryRole newRole = await graphServiceClient.DirectoryRoles.Request().AddAsync(directoryRoleCreate);

                        if (newRole == null)
                        {
                            string errorMsg = "New role null. ";
                            await ErrorHandling.ErrorEvent(errorMsg, "N/A");
                        }

                        return(newRole.Id);
                    }
                }
            }
            catch (ArgumentException ex)
            {
                string errorMsg = "Get role by name method failure.";
                string exMsg    = ex.Message;
                await ErrorHandling.ErrorEvent(errorMsg, exMsg);
            }

            return(null);
        }
Exemplo n.º 4
0
        public IEnumerable <List <DirectoryRole> > GetRoles(CancellationToken token)
        {
            IGraphServiceDirectoryRolesCollectionPage page = null;

            try
            {
                page = client.DirectoryRoles.Request().GetAsync(token).Result;
            }
            catch (Exception ex)
            {
                HandleException(ex, null, messageOnlyExceptions, "");
            }

            while (page != null)
            {
                yield return(page.ToList());

                if (page.NextPageRequest == null)
                {
                    break;
                }
                page = page.NextPageRequest.GetAsync(token).Result;
            }
        }
        /// <summary>
        /// Gets all members from group and applies roles
        /// </summary>
        /// <param name="graphServiceClient"></param>
        /// <param name="groupId"></param>
        /// <param name="roleId"></param>
        /// <param name="groups"></param>
        /// <returns></returns>
        private async static Task <List <string> > GetMembersfromGroup(GraphServiceClient graphServiceClient, string groupId, string roleName,
                                                                       IGraphServiceGroupsCollectionPage groups, IGraphServiceDirectoryRolesCollectionPage directoryRole,
                                                                       IGraphServiceDirectoryRoleTemplatesCollectionPage directoryRoleTemplate)
        {
            List <string> groupRoles = new List <string>();

            try
            {
                string roleId = await GetRoleByName(graphServiceClient : graphServiceClient, roleName : roleName,
                                                    directoryRole : directoryRole, directoryRoleTemplate : directoryRoleTemplate);

                if (!string.IsNullOrEmpty(roleId))
                {
                    TableBatchOperation batchOperation = new TableBatchOperation();
                    do
                    {
                        foreach (var group in groups)
                        {
                            List <string>   members         = new List <string>();
                            GroupRoleEntity groupRoleEntity = new GroupRoleEntity();
                            var             users           = await graphServiceClient.Groups[group.Id].Members.Request().GetAsync();
                            var             groupInfo       = await graphServiceClient.Groups[group.Id].Request().GetAsync();
                            var             groupName       = groupInfo.DisplayName;
                            if (group.Id == groupId)
                            {
                                do
                                {
                                    Console.WriteLine($"\n{group.Id}, {group.DisplayName}");
                                    Console.WriteLine("------");

                                    foreach (var user in users)
                                    {
                                        members.Add(user.Id);
                                        try
                                        {
                                            await graphServiceClient.DirectoryRoles[roleId].Members.References.Request().AddAsync(user);
                                            Console.WriteLine($"Assigning role to: {user.Id}");
                                            groupRoles.Add($"Group: {groupName} - Role: {roleName} - UserId: {user.Id}");
                                        }
                                        catch
                                        {
                                            Console.WriteLine($"{user.Id} already contains role {roleId}");
                                        }
                                    }
                                }while (users.NextPageRequest != null && (users = await users.NextPageRequest.GetAsync()).Count > 0);
                                groupRoleEntity = await StorageHelper.CreateTableEntry(graphServiceClient, group.Id, roleId, members);

                                Console.WriteLine("CreateTableEntry workedS");
                                if (groupRoleEntity != null)
                                {
                                    batchOperation.InsertOrMerge(groupRoleEntity);
                                }
                            }
                        }
                    }while (groups.NextPageRequest != null && (groups = await groups.NextPageRequest.GetAsync()).Count > 0);
                    if (batchOperation != null)
                    {
                        await StorageHelper.TableBatchOperation(graphServiceClient, batchOperation);
                    }
                }
                return(groupRoles);
            }
            catch (ArgumentException ex)
            {
                string errorMsg = "Get members from group method failure.";
                string exMsg    = ex.Message;
                await ErrorHandling.ErrorEvent(errorMsg, exMsg);
            }
            return(null);
        }