Ejemplo n.º 1
0
        public async static Task <GroupRoleEntity> CreateTableEntry(GraphServiceClient graphServiceClient, string groupId, string roleId, List <string> members)
        {
            try
            {
                Console.WriteLine("In createtableentry beginning");

                CloudStorageAccount storageAccount    = CloudStorageAccount.Parse(storageConnectionString);
                CloudTableClient    tableClient       = storageAccount.CreateCloudTableClient();
                CloudTable          table             = tableClient.GetTableReference("groupRoles");
                TableOperation      retrieveOperation = TableOperation.Retrieve <GroupRoleEntity>(groupId, roleId);
                TableResult         retrievedResult   = table.Execute(retrieveOperation);
                GroupRoleEntity     test = (GroupRoleEntity)retrievedResult.Result;

                Console.WriteLine("Got result from table");
                if (test != null)
                {
                    await RemoveRoleFromOldUser(graphServiceClient, groupId, roleId, members, test);
                }

                string user = "";
                foreach (var member in members)
                {
                    user += $"{member},";
                }
                user = user.Remove(user.Length - 1);

                GroupRoleEntity groupRoleEntity = new GroupRoleEntity(groupId, roleId)
                {
                    ETag         = "*",
                    GroupMembers = user
                };

                return(groupRoleEntity);
            }
            catch (ArgumentException ex)
            {
                string errorMsg = "CreateTableEntry method failure.";
                string exMsg    = ex.Message;
                await ErrorHandling.ErrorEvent(errorMsg, exMsg);
            }
            return(null);
        }
        /// <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);
        }
Ejemplo n.º 3
0
        private async static Task RemoveRoleFromOldUser(GraphServiceClient graphServiceClient, string groupId, string roleId,
                                                        List <string> currentMembers, GroupRoleEntity groupRoleEntity)
        {
            try
            {
                List <string> groupMemberList = groupRoleEntity.GroupMembers.Split(',').ToList();

                var removeMembersList = groupMemberList.Except(currentMembers).ToList();

                var           test        = await graphServiceClient.DirectoryRoles[roleId].Members.Request().GetAsync();
                List <string> roleMembers = new List <string>();
                bool          notEmpty    = removeMembersList.Any();
                if (notEmpty)
                {
                    foreach (var user in test)
                    {
                        roleMembers.Add(user.Id);
                    }

                    foreach (var member in removeMembersList)
                    {
                        if (roleMembers.Contains(member))
                        {
                            await graphServiceClient.DirectoryRoles[roleId].Members[member].Reference.Request().DeleteAsync();
                        }
                    }
                }
            }
            catch (ArgumentException ex)
            {
                string errorMsg = "Remove roles from member method failure.";
                string exMsg    = ex.Message;
                await ErrorHandling.ErrorEvent(errorMsg, exMsg);
            }
        }