protected static PrivilegeDepthExtended GetPrivilegeLevel( IEnumerable <SecurityPrivilegeMetadata> privilegesEnum , IEnumerable <RolePrivileges> rolePrivileges , Dictionary <PrivilegeType, SecurityPrivilegeMetadata> availablePrivilegesTypes , PrivilegeType privilegeType ) { if (privilegeType != PrivilegeType.None && privilegesEnum != null && privilegesEnum.Any() ) { var privilege = privilegesEnum.FirstOrDefault(p => p.PrivilegeType == privilegeType); if (privilege != null) { availablePrivilegesTypes.Add(privilegeType, privilege); var rolePrivilege = rolePrivileges.FirstOrDefault(p => p.PrivilegeId == privilege.PrivilegeId); if (rolePrivilege != null && rolePrivilege.PrivilegeDepthMask.HasValue) { var privilegeDepth = RolePrivilegesRepository.ConvertMaskToPrivilegeDepth(rolePrivilege.PrivilegeDepthMask.Value); if (privilegeDepth.HasValue) { return((PrivilegeDepthExtended)privilegeDepth.Value); } } } } return(PrivilegeDepthExtended.None); }
private PrivilegeDepthExtended GetPrivilegeLevel(IEnumerable <RolePrivileges> rolePrivileges, IEnumerable <SecurityPrivilegeMetadata> entityPrivileges, PrivilegeType privilegeType) { if (privilegeType != PrivilegeType.None) { var privilege = entityPrivileges.FirstOrDefault(p => p.PrivilegeType == privilegeType); if (privilege != null) { this._availablePrivilegesTypes.Add(privilegeType); var rolePrivilege = rolePrivileges.FirstOrDefault(p => p.PrivilegeId == privilege.PrivilegeId); if (rolePrivilege != null && rolePrivilege.PrivilegeDepthMask.HasValue) { var privilegeDepth = RolePrivilegesRepository.ConvertMaskToPrivilegeDepth(rolePrivilege.PrivilegeDepthMask.Value); if (privilegeDepth.HasValue) { return((PrivilegeDepthExtended)privilegeDepth.Value); } } } } return(PrivilegeDepthExtended.None); }
protected override List <string> GetDescriptionValues(Entity entityInput, string behavior, bool withManaged, bool withSolutionInfo, bool withUrls, Action <List <string>, Entity, bool, bool, bool> action) { var entity = entityInput.ToEntity <RolePrivileges>(); List <string> values = new List <string>(); var businessUnit = EntityDescriptionHandler.GetAttributeString(entity, "role.businessunitid"); if (!entity.Attributes.Contains("businessunit.parentbusinessunitid")) { businessUnit = "Root Organization"; } values.AddRange(new[] { EntityDescriptionHandler.GetAttributeString(entity, "role.name") , businessUnit , EntityDescriptionHandler.GetAttributeString(entity, "privilege.name") , RolePrivilegesRepository.GetPrivilegeDepthMaskName(entity.PrivilegeDepthMask.Value) , behavior }); action(values, entity, withUrls, withManaged, withSolutionInfo); return(values); }
private async void mISaveRoleChanges_Click(object sender, RoutedEventArgs e) { var changedRoles = _itemsSourceSecurityRoleList?.Where(en => en.IsChanged).ToList(); if (changedRoles == null || !changedRoles.Any()) { return; } string rolesName = string.Join(", ", changedRoles.Select(r => r.Role.Name).OrderBy(s => s)); string message = string.Format(Properties.MessageBoxStrings.SaveChangesToRolesFormat1, rolesName); if (MessageBox.Show(message, Properties.MessageBoxStrings.QuestionTitle, MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK) { return; } var service = await GetService(); if (service == null) { return; } string operationName = string.Format(Properties.OperationNames.SavingChangesInRolesFormat1, service.ConnectionData.Name); _iWriteToOutput.WriteToOutputStartOperation(service.ConnectionData, operationName); ToggleControls(service.ConnectionData, false, Properties.OutputStrings.InConnectionSavingChangesInRolesFormat1, service.ConnectionData.Name); var repositoryRolePrivileges = new RolePrivilegesRepository(service); foreach (var role in changedRoles) { try { List <RolePrivilege> privilegesAdd = role.GetAddRolePrivilege(); List <RolePrivilege> privilegesRemove = role.GetRemoveRolePrivilege(); _iWriteToOutput.WriteToOutput(service.ConnectionData, Properties.OutputStrings.SavingChangesInRoleFormat1, role.RoleName); _iWriteToOutput.WriteToOutputEntityInstance(service.ConnectionData, role.Role); await repositoryRolePrivileges.ModifyRolePrivilegesAsync(role.Role.Id, privilegesAdd, privilegesRemove); } catch (Exception ex) { this._iWriteToOutput.WriteErrorToOutput(service.ConnectionData, ex); } } ToggleControls(service.ConnectionData, true, Properties.OutputStrings.InConnectionSavingChangesInRolesCompletedFormat1, service.ConnectionData.Name); _iWriteToOutput.WriteToOutputEndOperation(service.ConnectionData, operationName); await ShowExistingSecurityRoles(); }
public override string GetDisplayName(SolutionComponent component) { var entity = GetEntity <RolePrivileges>(component.ObjectId.Value); if (entity != null) { return(RolePrivilegesRepository.GetPrivilegeDepthMaskName(entity.ToEntity <RolePrivileges>().PrivilegeDepthMask.Value)); } return(base.GetDisplayName(component)); }
protected static PrivilegeDepthExtended GetPrivilegeLevel(Guid privilegeId, IEnumerable <RolePrivileges> rolePrivileges) { var rolePrivilege = rolePrivileges.FirstOrDefault(p => p.PrivilegeId == privilegeId); if (rolePrivilege != null && rolePrivilege.PrivilegeDepthMask.HasValue) { var privilegeDepth = RolePrivilegesRepository.ConvertMaskToPrivilegeDepth(rolePrivilege.PrivilegeDepthMask.Value); if (privilegeDepth.HasValue) { return((PrivilegeDepthExtended)privilegeDepth.Value); } } return(PrivilegeDepthExtended.None); }
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() { var content = new StringBuilder(); var privilegeComparer = PrivilegeNameComparer.Comparer; var privilegeEquality = 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 taskRoles1 = _comparerSource.GetRole1Async(); var taskRoles2 = _comparerSource.GetRole2Async(); var listRoles1 = await taskRoles1; var taskPriv1 = new PrivilegeRepository(_comparerSource.Service1).GetListAsync(null); content.AppendLine(_iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.SecurityRolesInConnectionFormat2, Connection1.Name, listRoles1.Count())); var listRoles2 = await taskRoles2; var taskPriv2 = new PrivilegeRepository(_comparerSource.Service2).GetListAsync(null); content.AppendLine(_iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.SecurityRolesInConnectionFormat2, Connection2.Name, listRoles2.Count())); var dictPrivilege1 = (await taskPriv1).ToDictionary(p => p.Name, StringComparer.InvariantCultureIgnoreCase); var taskPrivRole1 = new RolePrivilegesRepository(_comparerSource.Service1).GetListAsync(listRoles1.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(listRoles2.Select(e => e.RoleId.Value)); content.AppendLine(_iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.PrivilegesInConnectionFormat2, Connection2.Name, dictPrivilege2.Count())); var commonPrivileges = dictPrivilege1.Values.Intersect(dictPrivilege2.Values, privilegeEquality).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 (!listRoles1.Any() && !listRoles2.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()); var rolesOnlyExistsIn1 = new FormatTextTableHandler("Name", "BusinessUnit", "IsManaged"); var rolesOnlyExistsIn2 = new FormatTextTableHandler("Name", "BusinessUnit", "IsManaged"); var privilegesOnlyExistsIn1 = new FormatTextTableHandler("PrivilegeName", "PrivilegeType", "Linked Entities"); var privilegesOnlyExistsIn2 = new FormatTextTableHandler("PrivilegeName", "PrivilegeType", "Linked Entities"); foreach (var item1 in dictPrivilege1.Values .Except(dictPrivilege2.Values, privilegeEquality) .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, privilegeEquality) .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 listRoles1) { var name1 = role1.Name; var businessUnit1 = role1.BusinessUnitId.Name; if (role1.BusinessUnitParentBusinessUnit == null) { businessUnit1 = "Root Organization"; } { Role role2 = null; if (role2 == null) { role2 = listRoles2.FirstOrDefault(role => role.Id == role1.Id); } if (role2 == null && role1.RoleTemplateId != null) { role2 = listRoles2.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 listRoles2) { var name2 = role2.Name; var businessUnit2 = role2.BusinessUnitId.Name; if (role2.BusinessUnitParentBusinessUnit == null) { businessUnit2 = "Root Organization"; } { Role role1 = null; if (role1 == null) { role1 = listRoles1.FirstOrDefault(role => role.Id == role2.Id); } if (role1 == null && role2.RoleTemplateId != null) { role1 = listRoles1.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 = CompareRolePrivileges(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("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); }
private async void mICreateSecurityRoleBackup_Click(object sender, RoutedEventArgs e) { if (!(e.OriginalSource is MenuItem menuItem)) { return; } if (menuItem.DataContext == null || !(menuItem.DataContext is OtherPrivilegeRolePrivilegeViewItem role) ) { return; } var service = await GetService(); if (service == null) { return; } string operationName = string.Format(Properties.OperationNames.CreatingRoleBackupFormat2, service.ConnectionData.Name, role.Role.Name); _iWriteToOutput.WriteToOutputStartOperation(service.ConnectionData, operationName); ToggleControls(service.ConnectionData, false, Properties.OutputStrings.InConnectionCreatingRoleBackupFormat2, service.ConnectionData.Name, role.Role.Name); var repositoryRolePrivileges = new RolePrivilegesRepository(service); var repositoryPrivileges = new PrivilegeRepository(service); var rolePrivileges = await repositoryRolePrivileges.GetRolePrivilegesAsync(role.Role.Id); var privileges = await repositoryPrivileges.GetListForRoleAsync(role.Role.Id); var temp = new List <Model.Backup.RolePrivilege>(); foreach (var rolePriv in rolePrivileges) { var priv = privileges.FirstOrDefault(p => p.Id == rolePriv.PrivilegeId); if (priv != null) { temp.Add(new Model.Backup.RolePrivilege() { Name = priv.Name, Level = rolePriv.Depth, }); } } var roleBackup = new Model.Backup.Role() { Id = role.Role.Id, TemplateId = role.Role.RoleTemplateId?.Id, Name = role.Role.Name, }; roleBackup.RolePrivileges.AddRange(temp.OrderBy(p => p.Name)); string fileName = EntityFileNameFormatter.GetRoleFileName(service.ConnectionData.Name, role.Role.Name, EntityFileNameFormatter.Headers.Backup, FileExtension.xml); string filePath = Path.Combine(_commonConfig.FolderForExport, FileOperations.RemoveWrongSymbols(fileName)); await roleBackup.SaveAsync(filePath); _iWriteToOutput.WriteToOutput(service.ConnectionData , Properties.OutputStrings.InConnectionExportedRoleBackupFormat3 , service.ConnectionData.Name , role.Role.Name , filePath ); _iWriteToOutput.PerformAction(service.ConnectionData, filePath); ToggleControls(service.ConnectionData, true, Properties.OutputStrings.InConnectionCreatingRoleBackupCompletedFormat2, service.ConnectionData.Name, role.Role.Name); _iWriteToOutput.WriteToOutputEndOperation(service.ConnectionData, operationName); }
private async Task ShowExistingSecurityRoles() { if (!this.IsControlsEnabled) { return; } Privilege privilege = null; this.Dispatcher.Invoke(() => { foreach (var entity in _itemsSourceSecurityRoleList) { entity.PropertyChanged -= rolePrivilege_PropertyChanged; entity.PropertyChanged -= rolePrivilege_PropertyChanged; } _itemsSourceSecurityRoleList.Clear(); privilege = GetSelectedOtherPrivilege()?.Privilege; foreach (var menuItem in _menuItemsSetPrivilegeDepths.Values) { menuItem.IsEnabled = false; menuItem.Visibility = Visibility.Collapsed; } }); if (privilege == null) { return; } var service = await GetService(); if (service == null) { return; } ToggleControls(service.ConnectionData, false, Properties.OutputStrings.LoadingEntityPrivileges); string textName = string.Empty; bool? isTemplate = null; bool? isManaged = null; bool? isCustomizable = null; txtBFilterSecurityRole.Dispatcher.Invoke(() => { textName = txtBFilterSecurityRole.Text.Trim().ToLower(); if (cmBRoleIsTemplate.SelectedItem is bool valueIsTemplate) { isTemplate = valueIsTemplate; } if (cmBRoleIsManaged.SelectedItem is bool valueIsManaged) { isManaged = valueIsManaged; } if (cmBRoleIsCustomizable.SelectedItem is bool valueIsCustomizable) { isCustomizable = valueIsCustomizable; } }); IEnumerable <OtherPrivilegeRolePrivilegeViewItem> list = Enumerable.Empty <OtherPrivilegeRolePrivilegeViewItem>(); try { if (service != null) { var repositoryRole = new RoleRepository(service); IEnumerable <Role> roles = await repositoryRole.GetListAsync(textName , isCustomizable , isManaged , isTemplate , new ColumnSet ( Role.Schema.Attributes.name , Role.Schema.Attributes.businessunitid , Role.Schema.Attributes.ismanaged , Role.Schema.Attributes.roletemplateid , Role.Schema.Attributes.iscustomizable ) ); var repositoryRolePrivileges = new RolePrivilegesRepository(service); var task = repositoryRolePrivileges.GetEntityPrivilegesAsync(roles.Select(r => r.RoleId.Value), new[] { privilege.Id }); ActivateMenuItemsSetPrivileges(privilege); var listRolePrivilege = await task; list = roles.Select(r => new OtherPrivilegeRolePrivilegeViewItem(r, listRolePrivilege.Where(rp => rp.RoleId == r.RoleId), privilege)).ToList(); } } catch (Exception ex) { this._iWriteToOutput.WriteErrorToOutput(service.ConnectionData, ex); } LoadSecurityRoles(list); ToggleControls(service.ConnectionData, true, Properties.OutputStrings.LoadingEntityPrivilegesCompletedFormat1, list.Count()); }
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); }