public List <string> CompareRolePrivileges(
            IEnumerable <RolePrivilege> enumerableRolePriv1
            , IEnumerable <RolePrivilege> enumerableRolePriv2
            , IEnumerable <Privilege> commonPrivileges
            , PrivilegeNameComparer privilegeNameComparer
            )
        {
            List <string> result = new List <string>();

            FormatTextTableHandler tableOnlyIn1 = new FormatTextTableHandler();

            tableOnlyIn1.SetHeader("PrivilegeName", "PrivilegeType", "Depth", "Linked Entities");

            FormatTextTableHandler tableOnlyIn2 = new FormatTextTableHandler();

            tableOnlyIn2.SetHeader("PrivilegeName", "PrivilegeType", "Depth", "Linked Entities");

            FormatTextTableHandler tableDifferent = new FormatTextTableHandler();

            tableDifferent.SetHeader("PrivilegeName", "PrivilegeType", Entity1Name, Entity2Name, "Linked Entities");

            foreach (var priv in commonPrivileges.OrderBy(s => s.LinkedEntitiesSorted).OrderBy(s => s.Name, privilegeNameComparer))
            {
                RolePrivilege rolePriv1 = null;
                RolePrivilege rolePriv2 = null;

                if (enumerableRolePriv1 != null)
                {
                    rolePriv1 = enumerableRolePriv1.FirstOrDefault(i => i.PrivilegeId == priv.PrivilegeId);
                }

                if (enumerableRolePriv2 != null)
                {
                    rolePriv2 = enumerableRolePriv2.FirstOrDefault(i => i.PrivilegeId == priv.PrivilegeId);
                }

                if (rolePriv1 != null && rolePriv2 == null)
                {
                    var privilegedepthmask = rolePriv1.Depth;

                    tableOnlyIn1.AddLine(priv.Name
                                         , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                         , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask)
                                         , priv.LinkedEntitiesSorted
                                         );
                    tableOnlyIn2.CalculateLineLengths(priv.Name
                                                      , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                                      , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask)
                                                      , priv.LinkedEntitiesSorted
                                                      );
                }
                else if (rolePriv1 == null && rolePriv2 != null)
                {
                    var privilegedepthmask = rolePriv2.Depth;

                    tableOnlyIn2.AddLine(priv.Name
                                         , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                         , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask)
                                         , priv.LinkedEntitiesSorted
                                         );
                    tableOnlyIn1.CalculateLineLengths(priv.Name
                                                      , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                                      , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask)
                                                      , priv.LinkedEntitiesSorted
                                                      );
                }
                else if (rolePriv1 != null && rolePriv2 != null)
                {
                    var privilegedepthmask1 = rolePriv1.Depth;
                    var privilegedepthmask2 = rolePriv2.Depth;

                    if (privilegedepthmask1 != privilegedepthmask2)
                    {
                        tableDifferent.AddLine(priv.Name
                                               , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                               , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask1)
                                               , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask2)
                                               , priv.LinkedEntitiesSorted
                                               );
                    }
                }
            }

            if (tableOnlyIn1.Count > 0)
            {
                if (result.Count > 0)
                {
                    result.Add(string.Empty);
                }

                result.Add(string.Format("RolePrivileges ONLY in {0}: {1}", Entity1Name, tableOnlyIn1.Count));
                tableOnlyIn1.GetFormatedLines(false).ForEach(s => result.Add(_tabSpacer + s));
            }

            if (tableOnlyIn2.Count > 0)
            {
                if (result.Count > 0)
                {
                    result.Add(string.Empty);
                }

                result.Add(string.Format("RolePrivileges ONLY in {0}: {1}", Entity2Name, tableOnlyIn2.Count));
                tableOnlyIn2.GetFormatedLines(false).ForEach(s => result.Add(_tabSpacer + s));
            }

            if (tableDifferent.Count > 0)
            {
                if (result.Count > 0)
                {
                    result.Add(string.Empty);
                }

                result.Add(string.Format("Different RolePrivileges in {0} and {1}: {2}", Entity1Name, Entity2Name, tableDifferent.Count));
                tableDifferent.GetFormatedLines(false).ForEach(s => result.Add(_tabSpacer + s));
            }

            if (tableOnlyIn1.Count == 0 &&
                tableOnlyIn2.Count == 0 &&
                tableDifferent.Count == 0
                )
            {
                result.Add(string.Format("No difference RolePrivileges in {0} and {1}", Entity1Name, Entity2Name));
            }

            return(result);
        }
Ejemplo n.º 2
0
        private List <string> CompareRolePrivileges(
            IEnumerable <RolePrivileges> enumerableRolePriv1
            , IEnumerable <RolePrivileges> enumerableRolePriv2
            , IEnumerable <Privilege> commonPrivileges
            , Dictionary <string, Privilege> listPrivilege1
            , Dictionary <string, Privilege> listPrivilege2
            , PrivilegeNameComparer privilegeNameComparer
            )
        {
            var result = new List <string>();

            var tableOnlyIn1 = new FormatTextTableHandler("PrivilegeName", "PrivilegeType", "Depth", "Linked Entities");

            var tableOnlyIn2 = new FormatTextTableHandler("PrivilegeName", "PrivilegeType", "Depth", "Linked Entities");

            var tableDifferent = new FormatTextTableHandler("PrivilegeName", "PrivilegeType", Connection1.Name, Connection2.Name, "Linked Entities");

            var tableFullDifferences = new FormatTextTableHandler("PrivilegeName", "PrivilegeType", Connection1.Name, Connection2.Name, "Linked Entities");

            foreach (var priv in commonPrivileges.OrderBy(s => s.LinkedEntitiesSorted).OrderBy(s => s.Name, privilegeNameComparer))
            {
                var priv1 = listPrivilege1[priv.Name];
                var priv2 = listPrivilege2[priv.Name];

                RolePrivileges rolePriv1 = null;
                RolePrivileges rolePriv2 = null;

                if (enumerableRolePriv1 != null)
                {
                    rolePriv1 = enumerableRolePriv1.FirstOrDefault(i => i.PrivilegeId == priv1?.PrivilegeId);
                }

                if (enumerableRolePriv2 != null)
                {
                    rolePriv2 = enumerableRolePriv2.FirstOrDefault(i => i.PrivilegeId == priv2?.PrivilegeId);
                }

                if (rolePriv1 != null && rolePriv2 == null)
                {
                    var privilegedepthmask = rolePriv1.PrivilegeDepthMask.GetValueOrDefault();

                    tableOnlyIn1.AddLine(priv.Name
                                         , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                         , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask)
                                         , priv.LinkedEntitiesSorted
                                         );
                    tableOnlyIn2.CalculateLineLengths(priv.Name
                                                      , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                                      , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask)
                                                      , priv.LinkedEntitiesSorted
                                                      );
                }
                else if (rolePriv1 == null && rolePriv2 != null)
                {
                    var privilegedepthmask = rolePriv2.PrivilegeDepthMask.GetValueOrDefault();

                    tableOnlyIn2.AddLine(priv.Name
                                         , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                         , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask)
                                         , priv.LinkedEntitiesSorted
                                         );
                    tableOnlyIn1.CalculateLineLengths(priv.Name
                                                      , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                                      , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask)
                                                      , priv.LinkedEntitiesSorted
                                                      );
                }
                else if (rolePriv1 != null && rolePriv2 != null)
                {
                    var privilegedepthmask1 = rolePriv1.PrivilegeDepthMask.GetValueOrDefault();
                    var privilegedepthmask2 = rolePriv2.PrivilegeDepthMask.GetValueOrDefault();

                    if (privilegedepthmask1 != privilegedepthmask2)
                    {
                        tableDifferent.AddLine(priv.Name
                                               , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                               , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask1)
                                               , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask2)
                                               , priv.LinkedEntitiesSorted
                                               );

                        tableFullDifferences.CalculateLineLengths(priv.Name
                                                                  , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                                                  , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask1)
                                                                  , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmask2)
                                                                  , priv.LinkedEntitiesSorted
                                                                  );
                    }
                }

                var privilegedepthmaskValue1 = rolePriv1?.PrivilegeDepthMask;
                var privilegedepthmaskValue2 = rolePriv2?.PrivilegeDepthMask;

                if (privilegedepthmaskValue1 != privilegedepthmaskValue2)
                {
                    tableFullDifferences.AddLine(priv.Name
                                                 , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                                 , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmaskValue1)
                                                 , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmaskValue2)
                                                 , priv.LinkedEntitiesSorted
                                                 );

                    tableDifferent.CalculateLineLengths(priv.Name
                                                        , priv.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)priv.AccessRight.Value).ToString() : string.Empty
                                                        , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmaskValue1)
                                                        , RolePrivilegesRepository.GetPrivilegeDepthMaskName(privilegedepthmaskValue2)
                                                        , priv.LinkedEntitiesSorted
                                                        );
                }
            }

            if (tableFullDifferences.Count > 0)
            {
                if (result.Count > 0)
                {
                    result.Add(string.Empty);
                }

                result.Add(string.Format("Full Differences privileges in {0} and {1}: {2}", Connection1.Name, Connection2.Name, tableFullDifferences.Count));
                tableFullDifferences.GetFormatedLines(false).ForEach(s => result.Add(tabSpacer + s));
            }

            if (tableOnlyIn1.Count > 0)
            {
                if (result.Count > 0)
                {
                    result.Add(string.Empty);
                }

                result.Add(string.Format("Privileges ONLY in {0}: {1}", Connection1.Name, tableOnlyIn1.Count));
                tableOnlyIn1.GetFormatedLines(false).ForEach(s => result.Add(tabSpacer + s));
            }

            if (tableOnlyIn2.Count > 0)
            {
                if (result.Count > 0)
                {
                    result.Add(string.Empty);
                }

                result.Add(string.Format("Privileges ONLY in {0}: {1}", Connection2.Name, tableOnlyIn2.Count));
                tableOnlyIn2.GetFormatedLines(false).ForEach(s => result.Add(tabSpacer + s));
            }

            if (tableDifferent.Count > 0)
            {
                if (result.Count > 0)
                {
                    result.Add(string.Empty);
                }

                result.Add(string.Format("Different privileges in {0} and {1}: {2}", Connection1.Name, Connection2.Name, tableDifferent.Count));
                tableDifferent.GetFormatedLines(false).ForEach(s => result.Add(tabSpacer + s));
            }

            return(result);
        }
        private async Task <string> CheckSecurityRoles()
        {
            StringBuilder content = new StringBuilder();

            var privilegeComparer = new PrivilegeNameComparer();
            var privileteEquality = new PrivilegeEqualityComparer();

            await _comparerSource.InitializeConnection(_iWriteToOutput, content);

            string operation = string.Format(Properties.OperationNames.CheckingSecurityRolesFormat2, Connection1.Name, Connection2.Name);

            content.AppendLine(_iWriteToOutput.WriteToOutputStartOperation(null, operation));

            var task1 = _comparerSource.GetRole1Async();
            var task2 = _comparerSource.GetRole2Async();

            var list1 = await task1;

            var taskPriv1 = new PrivilegeRepository(_comparerSource.Service1).GetListAsync(null);

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.SecurityRolesInConnectionFormat2, Connection1.Name, list1.Count()));



            var list2 = await task2;

            var taskPriv2 = new PrivilegeRepository(_comparerSource.Service2).GetListAsync(null);

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.SecurityRolesInConnectionFormat2, Connection2.Name, list2.Count()));



            var dictPrivilege1 = (await taskPriv1).ToDictionary(p => p.Name, StringComparer.InvariantCultureIgnoreCase);

            var taskPrivRole1 = new RolePrivilegesRepository(_comparerSource.Service1).GetListAsync(list1.Select(e => e.RoleId.Value));

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.PrivilegesInConnectionFormat2, Connection1.Name, dictPrivilege1.Count()));



            var dictPrivilege2 = (await taskPriv2).ToDictionary(p => p.Name, StringComparer.InvariantCultureIgnoreCase);

            var taskPrivRole2 = new RolePrivilegesRepository(_comparerSource.Service2).GetListAsync(list2.Select(e => e.RoleId.Value));

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.PrivilegesInConnectionFormat2, Connection2.Name, dictPrivilege2.Count()));



            var commonPrivileges = dictPrivilege1.Values.Intersect(dictPrivilege2.Values, privileteEquality).ToList();

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, "Common Privileges in {0} and {1}: {2}", Connection1.Name, Connection2.Name, commonPrivileges.Count()));



            var listRolePrivilege1 = await taskPrivRole1;

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.RolePrivilegesInConnectionFormat2, Connection1.Name, listRolePrivilege1.Count()));

            var listRolePrivilege2 = await taskPrivRole2;

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.RolePrivilegesInConnectionFormat2, Connection2.Name, listRolePrivilege2.Count()));

            if (!list1.Any() && !list2.Any())
            {
                _iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.ThereIsNothingToCompare);
                _iWriteToOutput.WriteToOutputEndOperation(null, operation);
                return(null);
            }

            var groupByRole1 = listRolePrivilege1.GroupBy(e => e.RoleId.Value).ToDictionary(g => g.Key, g => g.AsEnumerable());
            var groupByRole2 = listRolePrivilege2.GroupBy(e => e.RoleId.Value).ToDictionary(g => g.Key, g => g.AsEnumerable());

            FormatTextTableHandler rolesOnlyExistsIn1 = new FormatTextTableHandler();

            rolesOnlyExistsIn1.SetHeader("Name", "BusinessUnit", "IsManaged");

            FormatTextTableHandler rolesOnlyExistsIn2 = new FormatTextTableHandler();

            rolesOnlyExistsIn2.SetHeader("Name", "BusinessUnit", "IsManaged");

            FormatTextTableHandler privilegesOnlyExistsIn1 = new FormatTextTableHandler();

            privilegesOnlyExistsIn1.SetHeader("PrivilegeName", "PrivilegeType", "Linked Entities");

            FormatTextTableHandler privilegesOnlyExistsIn2 = new FormatTextTableHandler();

            privilegesOnlyExistsIn2.SetHeader("PrivilegeName", "PrivilegeType", "Linked Entities");

            foreach (var item1 in dictPrivilege1.Values
                     .Except(dictPrivilege2.Values, privileteEquality)
                     .ToList()
                     .OrderBy(p => p.LinkedEntitiesSorted)
                     .ThenBy(p => p.Name, privilegeComparer)
                     )
            {
                privilegesOnlyExistsIn1.AddLine(item1.Name
                                                , item1.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)item1.AccessRight.Value).ToString() : string.Empty
                                                , item1.LinkedEntitiesSorted
                                                );
            }

            foreach (var item2 in dictPrivilege2.Values
                     .Except(dictPrivilege1.Values, privileteEquality)
                     .ToList()
                     .OrderBy(p => p.LinkedEntitiesSorted)
                     .ThenBy(p => p.Name, privilegeComparer)
                     )
            {
                privilegesOnlyExistsIn2.AddLine(item2.Name
                                                , item2.AccessRight.HasValue ? ((Microsoft.Crm.Sdk.Messages.AccessRights)item2.AccessRight.Value).ToString() : string.Empty
                                                , item2.LinkedEntitiesSorted
                                                );
            }

            var commonList = new List <LinkedEntities <Role> >();

            foreach (var role1 in list1)
            {
                var name1         = role1.Name;
                var businessUnit1 = role1.BusinessUnitId.Name;

                if (role1.BusinessUnitParentBusinessUnit == null)
                {
                    businessUnit1 = "Root Organization";
                }

                {
                    Role role2 = null;

                    if (role2 == null)
                    {
                        role2 = list2.FirstOrDefault(role => role.Id == role1.Id);
                    }

                    if (role2 == null && role1.RoleTemplateId != null)
                    {
                        role2 = list2.FirstOrDefault(role => role.RoleTemplateId != null && role.RoleTemplateId.Id == role1.RoleTemplateId.Id);
                    }

                    if (role2 != null)
                    {
                        commonList.Add(new LinkedEntities <Role>(role1, role2));
                        continue;
                    }
                }

                string state = role1.FormattedValues[Role.Schema.Attributes.ismanaged];

                rolesOnlyExistsIn1.AddLine(name1, businessUnit1, state);

                this.ImageBuilder.AddComponentSolution1((int)ComponentType.Role, role1.Id);
            }

            foreach (var role2 in list2)
            {
                var name2         = role2.Name;
                var businessUnit2 = role2.BusinessUnitId.Name;

                if (role2.BusinessUnitParentBusinessUnit == null)
                {
                    businessUnit2 = "Root Organization";
                }

                {
                    Role role1 = null;

                    if (role1 == null)
                    {
                        role1 = list1.FirstOrDefault(role => role.Id == role2.Id);
                    }

                    if (role1 == null && role2.RoleTemplateId != null)
                    {
                        role1 = list1.FirstOrDefault(role => role.RoleTemplateId != null && role.RoleTemplateId.Id == role2.RoleTemplateId.Id);
                    }

                    if (role1 != null)
                    {
                        continue;
                    }
                }

                string state = role2.FormattedValues[Role.Schema.Attributes.ismanaged];

                rolesOnlyExistsIn2.AddLine(name2, businessUnit2, state);

                this.ImageBuilder.AddComponentSolution2((int)ComponentType.Role, role2.Id);
            }

            var dictDifference = new Dictionary <LinkedEntities <Role>, List <string> >();

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.RolesCommonFormat3, Connection1.Name, Connection2.Name, commonList.Count()));

            foreach (var commonRole in commonList)
            {
                groupByRole1.TryGetValue(commonRole.Entity1.Id, out IEnumerable <RolePrivileges> enumerable1);
                groupByRole2.TryGetValue(commonRole.Entity2.Id, out IEnumerable <RolePrivileges> enumerable2);

                List <string> diff = ComparePrivileges(enumerable1, enumerable2, commonPrivileges, dictPrivilege1, dictPrivilege2, privilegeComparer);

                if (diff.Count > 0)
                {
                    dictDifference.Add(commonRole, diff);

                    this.ImageBuilder.AddComponentDifferent((int)ComponentType.Role, commonRole.Entity1.Id, commonRole.Entity2.Id, string.Join(Environment.NewLine, diff));
                }
            }

            if (privilegesOnlyExistsIn1.Count > 0)
            {
                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                content.AppendLine().AppendLine().AppendFormat("Security Privileges ONLY EXISTS in {0}: {1}", Connection1.Name, privilegesOnlyExistsIn1.Count);

                privilegesOnlyExistsIn1.GetFormatedLines(false).ForEach(e => content.AppendLine().Append(tabSpacer + e.TrimEnd()));
            }

            if (privilegesOnlyExistsIn2.Count > 0)
            {
                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                content.AppendLine().AppendLine().AppendFormat("Security Privileges ONLY EXISTS in {0}: {1}", Connection2.Name, privilegesOnlyExistsIn2.Count);

                privilegesOnlyExistsIn2.GetFormatedLines(false).ForEach(e => content.AppendLine().Append(tabSpacer + e.TrimEnd()));
            }

            if (rolesOnlyExistsIn1.Count > 0)
            {
                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                content.AppendLine().AppendLine().AppendFormat("Security Roles ONLY EXISTS in {0}: {1}", Connection1.Name, rolesOnlyExistsIn1.Count);

                rolesOnlyExistsIn1.GetFormatedLines(true).ForEach(e => content.AppendLine().Append(tabSpacer + e.TrimEnd()));
            }

            if (rolesOnlyExistsIn2.Count > 0)
            {
                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                content.AppendLine().AppendLine().AppendFormat("Security Roles ONLY EXISTS in {0}: {1}", Connection2.Name, rolesOnlyExistsIn2.Count);

                rolesOnlyExistsIn2.GetFormatedLines(true).ForEach(e => content.AppendLine().Append(tabSpacer + e.TrimEnd()));
            }

            if (dictDifference.Count > 0)
            {
                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                var order = dictDifference.OrderBy(s => s.Key.Entity1.Name).ThenBy(s => s.Key.Entity1.BusinessUnitParentBusinessUnit == null ? "Root Organization" : s.Key.Entity1.BusinessUnitId.Name);

                content.AppendLine().AppendLine().AppendFormat("Security Roles DIFFERENT in {0} and {1}: {2}", Connection1.Name, Connection2.Name, dictDifference.Count);

                {
                    var table = new FormatTextTableHandler();
                    table.SetHeader("Name", "BusinessUnit");

                    foreach (var item in order)
                    {
                        table.AddLine(item.Key.Entity1.Name, item.Key.Entity1.BusinessUnitParentBusinessUnit == null ? "Root Organization" : item.Key.Entity1.BusinessUnitId.Name);
                    }

                    table.GetFormatedLines(true).ForEach(e => content.AppendLine().Append(tabSpacer + e.TrimEnd()));
                }

                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                content.AppendFormat("Security Roles DIFFERENT Details in {0} and {1}: {2}", Connection1.Name, Connection2.Name, dictDifference.Count);

                foreach (var item in order)
                {
                    content
                    .AppendLine()
                    .AppendLine()
                    .Append((tabSpacer + string.Format("Role: {0}         Business Unit: {1}", item.Key.Entity1.Name, item.Key.Entity1.BusinessUnitParentBusinessUnit == null ? "Root Organization" : item.Key.Entity1.BusinessUnitId.Name)).TrimEnd());

                    foreach (var str in item.Value)
                    {
                        content.AppendLine().Append((tabSpacer + tabSpacer + str).TrimEnd());
                    }

                    content
                    .AppendLine()
                    .AppendLine()
                    .AppendLine()
                    .AppendLine(new string('-', 150));
                }
            }

            if (rolesOnlyExistsIn2.Count == 0 &&
                rolesOnlyExistsIn1.Count == 0 &&
                dictDifference.Count == 0
                )
            {
                content.AppendLine("No difference in Security Roles.");
            }

            content.AppendLine().AppendLine().AppendLine(_iWriteToOutput.WriteToOutputEndOperation(null, operation));

            string fileName = EntityFileNameFormatter.GetDifferenceConnectionsForFieldFileName(_OrgOrgName, "Security Roles");

            string filePath = Path.Combine(_folder, FileOperations.RemoveWrongSymbols(fileName));

            File.WriteAllText(filePath, content.ToString(), new UTF8Encoding(false));

            await SaveOrganizationDifferenceImage();

            return(filePath);
        }