Пример #1
0
        public async Task <DirectoryRoleTemplate> FindRoleTemplateByName(string name, CancellationToken token)
        {
            IGraphServiceDirectoryRoleTemplatesCollectionPage page = await client.DirectoryRoleTemplates.Request()
                                                                     .Filter($"DisplayName eq {name}")
                                                                     .GetAsync(token);

            return(page.FirstOrDefault());
        }
        /// <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);
        }
Пример #3
0
        public IEnumerable <List <DirectoryRoleTemplate> > GetRoleTemplates(CancellationToken token)
        {
            IGraphServiceDirectoryRoleTemplatesCollectionPage page = null;

            try
            {
                page = client.DirectoryRoleTemplates.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);
        }