Skip to content

Commit fe9e627

Browse files
committed
Granular permissions: permission name localization (in progress)
1 parent 539a902 commit fe9e627

6 files changed

Lines changed: 71 additions & 5 deletions

File tree

src/Libraries/SmartStore.Core/Domain/Security/PermissionNode.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace SmartStore.Core.Domain.Security
44
{
5-
public interface IPermissionNode/* : ILocalizedEntity*/
5+
public interface IPermissionNode
66
{
77
int PermissionRecordId { get; }
88
string SystemName { get; }
9+
string DisplayName { get; }
910
bool? Allow { get; }
1011
}
1112

@@ -14,6 +15,7 @@ public class PermissionNode : IPermissionNode
1415
{
1516
public int PermissionRecordId { get; set; }
1617
public string SystemName { get; set; }
18+
public string DisplayName { get; set; }
1719
public bool? Allow { get; set; }
1820
}
1921
}

src/Libraries/SmartStore.Data/Migrations/201907221803421_GranularPermissions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ public void Seed(SmartObjectContext context)
5050

5151
public void MigrateLocaleResources(LocaleResourcesBuilder builder)
5252
{
53-
builder.AddOrUpdate("Admin.Customers.Customers.CustomerRole",
54-
"Customer role",
55-
"Kundengruppe");
53+
builder.AddOrUpdate("Admin.Customers.Customers.CustomerRole", "Customer role", "Kundengruppe");
54+
55+
builder.AddOrUpdate("Common.Read", "Read", "Lesen");
56+
builder.AddOrUpdate("Common.Create", "Create", "Erstellen");
57+
58+
builder.AddOrUpdate("Permissions.DisplayName.DisplayPrice", "Display prices", "Preise anzeigen");
5659
}
5760
}
5861
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using SmartStore.Collections;
5+
using SmartStore.Core.Domain.Security;
6+
using SmartStore.Services.Localization;
7+
8+
namespace SmartStore.Services.Security
9+
{
10+
public static class PermissionExtensions
11+
{
12+
public static void GetDisplayNames(this TreeNode<IPermissionNode> tree, ILocalizationService localizationService)
13+
{
14+
Guard.NotNull(localizationService, nameof(localizationService));
15+
16+
if (tree == null)
17+
{
18+
return;
19+
}
20+
21+
var tokenSeparator = new char[] { '.' };
22+
var names = new Dictionary<string, string>();
23+
24+
Add("read", "Common.Read");
25+
Add("update", "Common.Edit");
26+
Add("create", "Common.Create");
27+
Add("delete", "Common.Delete");
28+
Add("catalog", "Admin.Catalog");
29+
30+
foreach (PermissionNode node in tree.Flatten())
31+
{
32+
var tokens = node.SystemName.EmptyNull().ToLower().Split(tokenSeparator, StringSplitOptions.RemoveEmptyEntries);
33+
var token = tokens.LastOrDefault();
34+
if (token != null)
35+
{
36+
// Known token of default permissions.
37+
if (!names.TryGetValue(token, out var name))
38+
{
39+
// Unknown token (e.g. permission added by plugin). Try resource by name convention.
40+
var key = "Permissions.DisplayName." + token.Replace("-", "");
41+
name = localizationService.GetResource(key, defaultValue: token);
42+
}
43+
44+
node.DisplayName = string.IsNullOrWhiteSpace(name) ? token : name;
45+
}
46+
else
47+
{
48+
node.DisplayName = node.SystemName;
49+
}
50+
}
51+
52+
void Add(string token, string resourceKey)
53+
{
54+
names.Add(token, localizationService.GetResource(resourceKey, defaultValue: token));
55+
}
56+
}
57+
}
58+
}

src/Libraries/SmartStore.Services/SmartStore.Services.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@
587587
<Compile Include="Security\IAclService.cs" />
588588
<Compile Include="Security\IEncryptionService.cs" />
589589
<Compile Include="Security\IPermissionService2.cs" />
590+
<Compile Include="Security\PermissionExtensions.cs" />
590591
<Compile Include="Security\PermissionService.cs" />
591592
<Compile Include="Security\IPermissionService.cs" />
592593
<Compile Include="Security\IPermissionProvider.cs" />

src/Presentation/SmartStore.Web/Administration/Controllers/CustomerRoleController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ public ActionResult Edit(int id)
165165
model.TaxDisplayTypes = GetTaxDisplayTypesList(model);
166166
model.PermissionTree = _permissionService2.GetPermissionTree(customerRole);
167167

168+
model.PermissionTree.GetDisplayNames(Services.Localization);
169+
168170
return View(model);
169171
}
170172

src/Presentation/SmartStore.Web/Administration/Views/CustomerRole/_CreateOrUpdate.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
@foreach (var child in node.Children)
127127
{
128128
var value = child.Value.Allow.HasValue ? (child.Value.Allow.Value ? 2 : 1) : 0;
129-
<li data-name="permission-@child.Value.PermissionRecordId" data-value="@value" data-label="@child.Value.SystemName">
129+
<li data-name="permission-@child.Value.PermissionRecordId" data-value="@value" data-label="@child.Value.DisplayName">
130130
@if (child.HasChildren)
131131
{
132132
@PermissionNode(child)

0 commit comments

Comments
 (0)