-
Notifications
You must be signed in to change notification settings - Fork 548
Unify wording of standard permission names #738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
72287de
Unify wording of standard permission names
KrzysztofPajak 86ae569
Drop leftover "Area. " prefix duplication from permission names
KrzysztofPajak d8757c6
Potential fix for pull request finding 'Missed opportunity to use Where'
KrzysztofPajak a36d47b
Potential fix for pull request finding 'Generic catch clause'
KrzysztofPajak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/Modules/Grand.Module.Migration/Migrations/2.4/MigrationUpdateStandardPermissionNames.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| using System.Reflection; | ||
| using Grand.Data; | ||
| using Grand.Domain.Localization; | ||
| using Grand.Domain.Permissions; | ||
| using Grand.Infrastructure.Migrations; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Grand.Module.Migration.Migrations._2._4; | ||
|
|
||
| /// <summary> | ||
| /// Unifies the wording of standard permission display names in already-installed databases. | ||
| /// Two problems existed side by side: | ||
| /// 1) a handful of names had inconsistent casing/singular-plural wording (e.g. "Manage Order status", | ||
| /// "Manage Contact Attribute" vs. "Manage Address Attributes") - now corrected on <see cref="StandardPermission" />. | ||
| /// 2) about 40 permissions additionally had a duplicate translation resource row with an | ||
| /// "{Area}. " prefix (e.g. "Admin area. Manage Brands") left over from an old resource export. The | ||
| /// Area field on TranslationResource is never actually used to filter which row is loaded, so whichever | ||
| /// row was inserted first simply won - meaning some permissions showed the prefixed text and others | ||
| /// (with no duplicate) didn't, for no functional reason. This drops the prefix everywhere so every | ||
| /// permission consistently shows its plain name. | ||
| /// </summary> | ||
| public class MigrationUpdateStandardPermissionNames : IMigration | ||
| { | ||
| public int Priority => 2; | ||
| public DbVersion Version => new(2, 4); | ||
| public Guid Identity => new("DAEE04E0-573A-4001-9613-E682BE45BCE3"); | ||
| public string Name => "Unify wording of standard permission names and drop leftover 'Area. ' prefixes"; | ||
|
|
||
| public bool UpgradeProcess(IServiceProvider serviceProvider) | ||
| { | ||
| var permissionRepository = serviceProvider.GetRequiredService<IRepository<Permission>>(); | ||
| var translationRepository = serviceProvider.GetRequiredService<IRepository<TranslationResource>>(); | ||
| var logService = serviceProvider.GetRequiredService<ILogger<MigrationUpdateStandardPermissionNames>>(); | ||
|
|
||
| var standardPermissions = typeof(StandardPermission) | ||
| .GetFields(BindingFlags.Public | BindingFlags.Static) | ||
| .Where(f => f.FieldType == typeof(Permission)) | ||
| .Select(f => (Permission)f.GetValue(null)!); | ||
|
|
||
| foreach (var standard in standardPermissions) | ||
| try | ||
| { | ||
| var permission = permissionRepository.Table.FirstOrDefault(x => x.SystemName == standard.SystemName); | ||
| if (permission != null && permission.Name != standard.Name) | ||
| { | ||
| permission.Name = standard.Name; | ||
| permissionRepository.Update(permission); | ||
| } | ||
|
|
||
| //the display text is also cached as translation resources, under a couple of historical | ||
| //key/casing variants (and sometimes duplicated with an "{Area}. " prefix) - normalize every | ||
| //variant we can find to the plain name | ||
| var candidateKeys = new[] { | ||
| $"Permission.{standard.SystemName}", | ||
| $"permission.{standard.SystemName}", | ||
| $"permission.{standard.SystemName}".ToLowerInvariant() | ||
| }; | ||
|
|
||
| var translations = translationRepository.Table | ||
| .Where(x => candidateKeys.Contains(x.Name)) | ||
| .ToList(); | ||
|
|
||
| foreach (var translation in translations) | ||
| { | ||
| if (translation.Value == standard.Name) continue; | ||
|
|
||
| translation.Value = standard.Name; | ||
| translationRepository.Update(translation); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logService.LogError(ex, "UpgradeProcess - MigrationUpdateStandardPermissionNames ({SystemName})", | ||
| standard.SystemName); | ||
| } | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| return true; | ||
| } | ||
| } | ||
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.