mirror of https://github.com/abpframework/abp.git
4 changed files with 928 additions and 0 deletions
@ -0,0 +1,70 @@ |
|||
@inherits Volo.Abp.AspNetCore.Components.AbpComponentBase |
|||
@using MudBlazor |
|||
@using Volo.Abp.PermissionManagement.Localization |
|||
|
|||
<MudDialog @bind-IsVisible="@_isVisible" Options="@(new DialogOptions { MaxWidth = MaxWidth.Large, FullWidth = true })" CloseOnEscapeKey="true"> |
|||
<TitleContent> |
|||
<MudText Typo="Typo.h6">@L["Permissions"] - @_entityDisplayName</MudText> |
|||
</TitleContent> |
|||
<DialogContent> |
|||
<MudGrid Class="mb-2"> |
|||
<MudItem xs="12" sm="8"> |
|||
<MudTextField Value="@_permissionGroupSearchText" |
|||
ValueChanged="@OnPermissionGroupSearchTextChangedAsync" |
|||
Label="@L["Search"]" |
|||
Variant="Variant.Outlined" |
|||
Adornment="Adornment.Start" |
|||
AdornmentIcon="@Icons.Material.Filled.Search" |
|||
Immediate="true" /> |
|||
</MudItem> |
|||
<MudItem xs="12" sm="4" Class="d-flex align-items-center"> |
|||
<MudCheckBox Disabled="@_selectAllDisabled" |
|||
Checked="@GrantAll" |
|||
Indeterminate="@GrantAny" |
|||
CheckedChanged="@GrantAllAsync" |
|||
Label="@L["SelectAllInAllTabs"]" /> |
|||
</MudItem> |
|||
</MudGrid> |
|||
|
|||
<MudPaper Elevation="1" Class="pa-4"> |
|||
<MudText Typo="Typo.h6" Class="mb-3">@L["PermissionGroup"]</MudText> |
|||
|
|||
@if (_groups != null && _groups.Any()) |
|||
{ |
|||
<MudTabs @bind-ActivePanelIndex="@_activeTabIndex" |
|||
Variant="Variant.Pills" |
|||
Position="Position.Left" |
|||
PanelClass="pa-4" |
|||
Style="max-height: 600px; overflow-y: auto;"> |
|||
@foreach (var group in _groups) |
|||
{ |
|||
<MudTabPanel Text="@($"{group.DisplayName} ({group.Permissions.Count(x => x.IsGranted)})")"> |
|||
<div style="height: 500px; overflow-y: auto; padding: 8px;"> |
|||
<MudCheckBox Disabled="@(IsPermissionGroupDisabled(group))" |
|||
Checked="@(group.Permissions.All(x => x.IsGranted))" |
|||
CheckedChanged="@(b => GroupGrantAllChanged(b, group))" |
|||
Label="@L["SelectAllInThisTab"]" /> |
|||
<MudDivider Class="my-2" /> |
|||
|
|||
@foreach (var permission in group.Permissions) |
|||
{ |
|||
var depth = GetPermissionDepthOrDefault(permission.Name); |
|||
<div style="margin-left: @(depth * 20)px;"> |
|||
<MudCheckBox Disabled="@(IsDisabledPermission(permission))" |
|||
Checked="@permission.IsGranted" |
|||
CheckedChanged="@(async (b) => await PermissionChanged(b, group, permission))" |
|||
Label="@GetShownName(permission)" /> |
|||
</div> |
|||
} |
|||
</div> |
|||
</MudTabPanel> |
|||
} |
|||
</MudTabs> |
|||
} |
|||
</MudPaper> |
|||
</DialogContent> |
|||
<DialogActions> |
|||
<MudButton OnClick="@CloseModal">@L["Cancel"]</MudButton> |
|||
<MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="@SaveAsync">@L["Save"]</MudButton> |
|||
</DialogActions> |
|||
</MudDialog> |
|||
@ -0,0 +1,378 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Components; |
|||
using Microsoft.Extensions.Options; |
|||
using MudBlazor; |
|||
using Volo.Abp.AspNetCore.Components.Messages; |
|||
using Volo.Abp.AspNetCore.Components.Web.Configuration; |
|||
using Volo.Abp.Authorization.Permissions; |
|||
using Volo.Abp.Localization; |
|||
using Volo.Abp.PermissionManagement.Localization; |
|||
|
|||
namespace Volo.Abp.PermissionManagement.Blazor.MudBlazor.Components; |
|||
|
|||
public partial class PermissionManagementModal |
|||
{ |
|||
[Inject] protected IPermissionAppService PermissionAppService { get; set; } = default!; |
|||
[Inject] protected ICurrentApplicationConfigurationCacheResetService CurrentApplicationConfigurationCacheResetService { get; set; } = default!; |
|||
[Inject] protected IOptions<AbpLocalizationOptions> LocalizationOptions { get; set; } = default!; |
|||
[Inject] protected IDialogService DialogService { get; set; } = default!; |
|||
[Inject] protected ISnackbar Snackbar { get; set; } = default!; |
|||
|
|||
protected bool _isVisible; |
|||
|
|||
protected string? _providerName; |
|||
protected string? _providerKey; |
|||
|
|||
protected string? _entityDisplayName; |
|||
protected List<PermissionGroupDto>? _allGroups; |
|||
protected List<PermissionGroupDto>? _groups; |
|||
|
|||
protected List<PermissionGrantInfoDto> _disabledPermissions = new List<PermissionGrantInfoDto>(); |
|||
|
|||
protected int _activeTabIndex = 0; |
|||
|
|||
protected bool _selectAllDisabled; |
|||
|
|||
protected string? _permissionGroupSearchText; |
|||
|
|||
protected bool GrantAll { get; set; } |
|||
protected bool GrantAny { get; set; } |
|||
|
|||
protected Dictionary<string, int> _permissionDepths = new Dictionary<string, int>(); |
|||
|
|||
public PermissionManagementModal() |
|||
{ |
|||
LocalizationResource = typeof(AbpPermissionManagementResource); |
|||
} |
|||
|
|||
public virtual async Task OpenAsync(string providerName, string providerKey, string? entityDisplayName = null) |
|||
{ |
|||
try |
|||
{ |
|||
_providerName = providerName; |
|||
_providerKey = providerKey; |
|||
_permissionGroupSearchText = null; |
|||
|
|||
var result = await PermissionAppService.GetAsync(_providerName, _providerKey); |
|||
|
|||
_entityDisplayName = entityDisplayName ?? result.EntityDisplayName; |
|||
_allGroups = result.Groups.OrderBy(x => x.DisplayName).ToList(); |
|||
_groups = _allGroups.ToList(); |
|||
|
|||
NormalizePermissionGroup(); |
|||
|
|||
GrantAll = _groups.SelectMany(x => x.Permissions).All(p => p.IsGranted); |
|||
GrantAny = !GrantAll && _groups.SelectMany(x => x.Permissions).Any(p => p.IsGranted); |
|||
|
|||
_isVisible = true; |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
await HandleErrorAsync(ex); |
|||
} |
|||
} |
|||
|
|||
protected virtual async Task GrantAllAsync(bool grantAll) |
|||
{ |
|||
GrantAll = grantAll; |
|||
GrantAny = false; |
|||
|
|||
if (_allGroups == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
await ResetSearchTextAsync(); |
|||
|
|||
foreach (var permission in _allGroups.SelectMany(x => x.Permissions)) |
|||
{ |
|||
if (IsDisabledPermission(permission)) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
permission.IsGranted = grantAll; |
|||
} |
|||
|
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual void NormalizePermissionGroup(bool checkDisabledPermissions = true) |
|||
{ |
|||
if (_groups == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
_selectAllDisabled = _groups.All(IsPermissionGroupDisabled); |
|||
|
|||
if (checkDisabledPermissions) |
|||
{ |
|||
_disabledPermissions.Clear(); |
|||
} |
|||
|
|||
foreach (var permission in _groups.SelectMany(x => x.Permissions)) |
|||
{ |
|||
if (checkDisabledPermissions && permission.IsGranted && permission.GrantedProviders.All(x => x.ProviderName != _providerName)) |
|||
{ |
|||
_disabledPermissions.Add(permission); |
|||
} |
|||
} |
|||
|
|||
foreach (var group in _groups) |
|||
{ |
|||
SetPermissionDepths(group.Permissions, null, 0); |
|||
} |
|||
|
|||
if (_groups.Count != 0) |
|||
{ |
|||
_activeTabIndex = 0; |
|||
} |
|||
} |
|||
|
|||
protected Task CloseModal() |
|||
{ |
|||
_isVisible = false; |
|||
return InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task SaveAsync() |
|||
{ |
|||
try |
|||
{ |
|||
if (_allGroups == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var updateDto = new UpdatePermissionsDto |
|||
{ |
|||
Permissions = _allGroups |
|||
.SelectMany(g => g.Permissions) |
|||
.Select(p => new UpdatePermissionDto { IsGranted = p.IsGranted, Name = p.Name }) |
|||
.ToArray() |
|||
}; |
|||
|
|||
if (!updateDto.Permissions.Any(x => x.IsGranted)) |
|||
{ |
|||
var confirmed = await DialogService.ShowMessageBox( |
|||
L["Warning"], |
|||
L["SaveWithoutAnyPermissionsWarningMessage"], |
|||
yesText: L["Yes"], |
|||
cancelText: L["Cancel"]); |
|||
|
|||
if (confirmed != true) |
|||
{ |
|||
return; |
|||
} |
|||
} |
|||
|
|||
await PermissionAppService.UpdateAsync(_providerName!, _providerKey!, updateDto); |
|||
|
|||
Guid? userId = null; |
|||
if (_providerName == UserPermissionValueProvider.ProviderName && Guid.TryParse(_providerKey, out var parsedUserId)) |
|||
{ |
|||
userId = parsedUserId; |
|||
} |
|||
|
|||
await CurrentApplicationConfigurationCacheResetService.ResetAsync(userId); |
|||
|
|||
_isVisible = false; |
|||
await InvokeAsync(StateHasChanged); |
|||
Snackbar.Add(L["SavedSuccessfully"], Severity.Success); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
await HandleErrorAsync(ex); |
|||
} |
|||
} |
|||
|
|||
protected virtual string GetNormalizedGroupName(string name) |
|||
{ |
|||
return "PermissionGroup_" + name.Replace(".", "_"); |
|||
} |
|||
|
|||
protected virtual void SetPermissionDepths(List<PermissionGrantInfoDto> permissions, string? currentParent, int currentDepth) |
|||
{ |
|||
foreach (var item in permissions) |
|||
{ |
|||
if (item.ParentName == currentParent) |
|||
{ |
|||
_permissionDepths[item.Name] = currentDepth; |
|||
SetPermissionDepths(permissions, item.Name, currentDepth + 1); |
|||
} |
|||
} |
|||
} |
|||
|
|||
protected virtual int GetPermissionDepthOrDefault(string name) |
|||
{ |
|||
return _permissionDepths.GetValueOrDefault(name, 0); |
|||
} |
|||
|
|||
protected virtual async Task GroupGrantAllChanged(bool value, PermissionGroupDto permissionGroup) |
|||
{ |
|||
foreach (var permission in permissionGroup.Permissions) |
|||
{ |
|||
if (!IsDisabledPermission(permission)) |
|||
{ |
|||
SetPermissionGrant(permission, value); |
|||
} |
|||
} |
|||
|
|||
if (_groups != null) |
|||
{ |
|||
GrantAll = _groups.SelectMany(x => x.Permissions).All(p => p.IsGranted); |
|||
GrantAny = !GrantAll && _groups.SelectMany(x => x.Permissions).Any(p => p.IsGranted); |
|||
} |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task PermissionChanged(bool value, PermissionGroupDto permissionGroup, PermissionGrantInfoDto permission) |
|||
{ |
|||
SetPermissionGrant(permission, value); |
|||
|
|||
if (value) |
|||
{ |
|||
SetParentPermissionGrant(permissionGroup, permission); |
|||
} |
|||
else |
|||
{ |
|||
var childPermissions = GetChildPermissions(permissionGroup, permission); |
|||
|
|||
foreach (var childPermission in childPermissions) |
|||
{ |
|||
SetPermissionGrant(childPermission, false); |
|||
} |
|||
} |
|||
|
|||
if (_groups != null) |
|||
{ |
|||
GrantAll = _groups.SelectMany(x => x.Permissions).All(p => p.IsGranted); |
|||
GrantAny = !GrantAll && _groups.SelectMany(x => x.Permissions).Any(p => p.IsGranted); |
|||
} |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
private void SetParentPermissionGrant(PermissionGroupDto permissionGroup, PermissionGrantInfoDto permission) |
|||
{ |
|||
if (permission.ParentName == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var parentPermission = GetParentPermission(permissionGroup, permission); |
|||
SetPermissionGrant(parentPermission, true); |
|||
|
|||
SetParentPermissionGrant(permissionGroup, parentPermission); |
|||
} |
|||
|
|||
private void SetPermissionGrant(PermissionGrantInfoDto permission, bool value) |
|||
{ |
|||
if (permission.IsGranted == value) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
permission.IsGranted = value; |
|||
} |
|||
|
|||
protected virtual PermissionGrantInfoDto GetParentPermission(PermissionGroupDto permissionGroup, PermissionGrantInfoDto permission) |
|||
{ |
|||
return permissionGroup.Permissions.First(x => x.Name == permission.ParentName); |
|||
} |
|||
|
|||
protected virtual List<PermissionGrantInfoDto> GetChildPermissions(PermissionGroupDto permissionGroup, PermissionGrantInfoDto permission) |
|||
{ |
|||
var childPermissions = new List<PermissionGrantInfoDto>(); |
|||
GetChildPermissions(childPermissions, permissionGroup.Permissions, permission); |
|||
return childPermissions; |
|||
} |
|||
|
|||
protected virtual void GetChildPermissions(List<PermissionGrantInfoDto> allChildPermissions, List<PermissionGrantInfoDto> permissions, PermissionGrantInfoDto permission) |
|||
{ |
|||
var childPermissions = permissions.Where(x => x.ParentName == permission.Name).ToList(); |
|||
if (childPermissions.Count == 0) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
allChildPermissions.AddRange(childPermissions); |
|||
|
|||
foreach (var childPermission in childPermissions) |
|||
{ |
|||
GetChildPermissions(allChildPermissions, permissions, childPermission); |
|||
} |
|||
} |
|||
|
|||
protected virtual bool IsDisabledPermission(PermissionGrantInfoDto permissionGrantInfo) |
|||
{ |
|||
return _disabledPermissions.Any(x => x == permissionGrantInfo); |
|||
} |
|||
|
|||
protected virtual string GetShownName(PermissionGrantInfoDto permissionGrantInfo) |
|||
{ |
|||
if (!IsDisabledPermission(permissionGrantInfo)) |
|||
{ |
|||
return permissionGrantInfo.DisplayName; |
|||
} |
|||
|
|||
return string.Format( |
|||
"{0} ({1})", |
|||
permissionGrantInfo.DisplayName, |
|||
permissionGrantInfo.GrantedProviders |
|||
.Where(p => p.ProviderName != _providerName) |
|||
.Select(p => p.ProviderName) |
|||
.JoinAsString(", ") |
|||
); |
|||
} |
|||
|
|||
protected virtual bool IsPermissionGroupDisabled(PermissionGroupDto group) |
|||
{ |
|||
var permissions = group.Permissions; |
|||
var grantedProviders = permissions.SelectMany(x => x.GrantedProviders); |
|||
|
|||
return permissions.All(x => x.IsGranted) && grantedProviders.Any(p => p.ProviderName != _providerName); |
|||
} |
|||
|
|||
protected virtual async Task ResetSearchTextAsync() |
|||
{ |
|||
_permissionGroupSearchText = string.Empty; |
|||
if (_allGroups != null) |
|||
{ |
|||
_groups = _permissionGroupSearchText.IsNullOrWhiteSpace() |
|||
? _allGroups.ToList() |
|||
: _allGroups.Where(x => x.DisplayName.Contains(_permissionGroupSearchText!, StringComparison.OrdinalIgnoreCase) || x.Permissions.Any(permission => permission.DisplayName.Contains(_permissionGroupSearchText!, StringComparison.OrdinalIgnoreCase))).ToList(); |
|||
|
|||
NormalizePermissionGroup(false); |
|||
} |
|||
|
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task OnPermissionGroupSearchTextChangedAsync(string? value) |
|||
{ |
|||
if (value == _permissionGroupSearchText) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
_permissionGroupSearchText = value; |
|||
if (_allGroups != null) |
|||
{ |
|||
_groups = _permissionGroupSearchText.IsNullOrWhiteSpace() |
|||
? _allGroups.ToList() |
|||
: _allGroups.Where(x => x.DisplayName.Contains(_permissionGroupSearchText!, StringComparison.OrdinalIgnoreCase) || x.Permissions.Any(permission => permission.DisplayName.Contains(_permissionGroupSearchText!, StringComparison.OrdinalIgnoreCase))).ToList(); |
|||
|
|||
GrantAll = _groups.SelectMany(x => x.Permissions).All(p => p.IsGranted); |
|||
GrantAny = !GrantAll && _groups.SelectMany(x => x.Permissions).Any(p => p.IsGranted); |
|||
|
|||
NormalizePermissionGroup(false); |
|||
} |
|||
|
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
} |
|||
@ -0,0 +1,156 @@ |
|||
@using MudBlazor |
|||
@using Volo.Abp.PermissionManagement.Localization |
|||
@inherits Volo.Abp.AspNetCore.Components.AbpComponentBase |
|||
|
|||
<MudDialog @bind-IsVisible="@_isVisible" Options="@(new DialogOptions { MaxWidth = MaxWidth.ExtraLarge, FullWidth = true })" CloseOnEscapeKey="true"> |
|||
<TitleContent> |
|||
<MudText Typo="Typo.h6">@L["ResourcePermissions"] - @ResourceDisplayName</MudText> |
|||
</TitleContent> |
|||
<DialogContent> |
|||
@if (HasAnyResourcePermission && HasAnyResourceProviderKeyLookupService) |
|||
{ |
|||
<div class="d-flex justify-content-end mb-2"> |
|||
<MudButton Color="Color.Primary" |
|||
Size="Size.Small" |
|||
Variant="Variant.Filled" |
|||
OnClick="@OpenCreateDialogAsync"> |
|||
@L["AddResourcePermission"] |
|||
</MudButton> |
|||
</div> |
|||
<MudDataGrid T="ResourcePermissionGrantInfoDto" |
|||
Items="@ResourcePermissionList.Permissions" |
|||
Dense="true" |
|||
Hover="true" |
|||
Striped="true"> |
|||
<Columns> |
|||
<TemplateColumn Title="@L["Actions"]" Sortable="false" Style="width: 150px;"> |
|||
<CellTemplate> |
|||
<MudMenu Icon="@Icons.Material.Filled.MoreVert"> |
|||
<MudMenuItem OnClick="@(() => OpenEditDialogAsync(context))"> |
|||
@L["Edit"] |
|||
</MudMenuItem> |
|||
<MudMenuItem OnClick="@(() => DeleteResourcePermissionAsync(context))"> |
|||
@L["Delete"] |
|||
</MudMenuItem> |
|||
</MudMenu> |
|||
</CellTemplate> |
|||
</TemplateColumn> |
|||
<PropertyColumn Property="x => x.ProviderName" Title="@L["ResourcePermissionTarget"]" Sortable="false"> |
|||
<CellTemplate> |
|||
<MudTooltip Text="@context.ProviderNameDisplayName"> |
|||
<MudChip Size="Size.Small" Color="Color.Default" Class="me-1 mb-1"> |
|||
@context.ProviderName |
|||
</MudChip> |
|||
</MudTooltip> |
|||
@context.ProviderDisplayName |
|||
</CellTemplate> |
|||
</PropertyColumn> |
|||
<TemplateColumn Title="@L["ResourcePermissionPermissions"]" Sortable="false"> |
|||
<CellTemplate> |
|||
@foreach (var permission in context.Permissions) |
|||
{ |
|||
<MudChip Size="Size.Small" Color="Color.Default" Class="me-1 mb-1"> |
|||
@permission.DisplayName |
|||
</MudChip> |
|||
} |
|||
</CellTemplate> |
|||
</TemplateColumn> |
|||
</Columns> |
|||
<NoRecordsContent> |
|||
<MudText>@L["NoDataAvailableInDatatable"]</MudText> |
|||
</NoRecordsContent> |
|||
</MudDataGrid> |
|||
} |
|||
else |
|||
{ |
|||
<MudAlert Severity="Severity.Warning"> |
|||
@if (!HasAnyResourcePermission) |
|||
{ |
|||
@L["NoResourcePermissionFound"] |
|||
} |
|||
else if (!HasAnyResourceProviderKeyLookupService) |
|||
{ |
|||
@L["NoResourceProviderKeyLookupServiceFound"] |
|||
} |
|||
</MudAlert> |
|||
} |
|||
</DialogContent> |
|||
<DialogActions> |
|||
<MudButton OnClick="@CloseModal">@L["Close"]</MudButton> |
|||
</DialogActions> |
|||
</MudDialog> |
|||
|
|||
@* Create Resource Permission Dialog *@ |
|||
@if (_createDialogVisible) |
|||
{ |
|||
<MudDialog @bind-IsVisible="@_createDialogVisible" Options="@(new DialogOptions { MaxWidth = MaxWidth.Medium, FullWidth = true })" CloseOnEscapeKey="true"> |
|||
<TitleContent> |
|||
<MudText Typo="Typo.h6">@L["AddResourcePermission"]</MudText> |
|||
</TitleContent> |
|||
<DialogContent> |
|||
<MudForm @ref="@_createFormRef" Model="@CreateEntity"> |
|||
<MudText Typo="Typo.subtitle1" Class="mb-2">@L["ResourcePermissionTarget"]</MudText> |
|||
<MudRadioGroup SelectedOption="@CurrentLookupService" SelectedOptionChanged="@OnLookupServiceCheckedValueChanged"> |
|||
@foreach (var keyLookupService in ResourceProviderKeyLookupServices) |
|||
{ |
|||
<MudRadio Option="@keyLookupService.Name">@keyLookupService.DisplayName</MudRadio> |
|||
} |
|||
</MudRadioGroup> |
|||
<MudAutocomplete T="SearchProviderKeyInfo" |
|||
Value="@_selectedProviderKey" |
|||
ValueChanged="@SelectedProviderKeyChanged" |
|||
SearchFunc="@SearchProviderKeyAsync" |
|||
ToStringFunc="@(item => item?.ProviderDisplayName ?? string.Empty)" |
|||
Label="@L["ProviderKey"]" |
|||
Variant="Variant.Outlined" |
|||
Required="true" |
|||
RequiredError="@L["ThisFieldIsRequired"]" |
|||
Class="mt-2" /> |
|||
<MudText Typo="Typo.subtitle1" Class="mb-2 mt-4">@L["ResourcePermissionPermissions"]</MudText> |
|||
<MudSwitch @bind-Checked="@_grantAllCreate" |
|||
CheckedChanged="@(async (b) => await GrantAllCreateAsync(b))" |
|||
Label="@L["GrantAllResourcePermissions"]" /> |
|||
<div class="mt-2"> |
|||
@foreach (var permission in CreateEntity.Permissions) |
|||
{ |
|||
<MudCheckBox @bind-Checked="@permission.IsGranted" |
|||
Label="@permission.DisplayName" /> |
|||
} |
|||
</div> |
|||
</MudForm> |
|||
</DialogContent> |
|||
<DialogActions> |
|||
<MudButton OnClick="@CloseCreateDialogAsync">@L["Cancel"]</MudButton> |
|||
<MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="@CreateResourcePermissionAsync">@L["Save"]</MudButton> |
|||
</DialogActions> |
|||
</MudDialog> |
|||
} |
|||
|
|||
@* Edit Resource Permission Dialog *@ |
|||
@if (_editDialogVisible) |
|||
{ |
|||
<MudDialog @bind-IsVisible="@_editDialogVisible" Options="@(new DialogOptions { MaxWidth = MaxWidth.Medium, FullWidth = true })" CloseOnEscapeKey="true"> |
|||
<TitleContent> |
|||
<MudText Typo="Typo.h6">@L["UpdateResourcePermission"]</MudText> |
|||
</TitleContent> |
|||
<DialogContent> |
|||
<MudForm @ref="@_editFormRef" Model="@EditEntity"> |
|||
<MudText Typo="Typo.subtitle1" Class="mb-2">@L["ResourcePermissionPermissions"]</MudText> |
|||
<MudSwitch @bind-Checked="@_grantAllEdit" |
|||
CheckedChanged="@(async (b) => await GrantAllEditAsync(b))" |
|||
Label="@L["GrantAllResourcePermissions"]" /> |
|||
<div class="mt-2"> |
|||
@foreach (var permission in EditEntity.Permissions) |
|||
{ |
|||
<MudCheckBox @bind-Checked="@permission.IsGranted" |
|||
Label="@permission.DisplayName" /> |
|||
} |
|||
</div> |
|||
</MudForm> |
|||
</DialogContent> |
|||
<DialogActions> |
|||
<MudButton OnClick="@CloseEditDialogAsync">@L["Cancel"]</MudButton> |
|||
<MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="@UpdateResourcePermissionAsync">@L["Save"]</MudButton> |
|||
</DialogActions> |
|||
</MudDialog> |
|||
} |
|||
@ -0,0 +1,324 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Components; |
|||
using MudBlazor; |
|||
using Volo.Abp.AspNetCore.Components.Messages; |
|||
using Volo.Abp.PermissionManagement.Localization; |
|||
|
|||
namespace Volo.Abp.PermissionManagement.Blazor.MudBlazor.Components; |
|||
|
|||
public partial class ResourcePermissionManagementModal |
|||
{ |
|||
[Inject] protected IPermissionAppService PermissionAppService { get; set; } = default!; |
|||
|
|||
[Inject] protected IUiMessageService UiMessageService { get; set; } = default!; |
|||
|
|||
[Inject] protected ISnackbar Snackbar { get; set; } = default!; |
|||
|
|||
[Inject] protected IDialogService DialogService { get; set; } = default!; |
|||
|
|||
protected bool _isVisible; |
|||
protected bool _createDialogVisible; |
|||
protected bool _editDialogVisible; |
|||
protected MudForm? _createFormRef; |
|||
protected MudForm? _editFormRef; |
|||
|
|||
public bool HasAnyResourcePermission { get; set; } |
|||
public bool HasAnyResourceProviderKeyLookupService { get; set; } |
|||
protected string? ResourceName { get; set; } |
|||
protected string? ResourceKey { get; set; } |
|||
protected string? ResourceDisplayName { get; set; } |
|||
protected int PageSize { get; set; } = 10; |
|||
|
|||
protected CreateModel CreateEntity { get; set; } = new CreateModel |
|||
{ |
|||
Permissions = new List<ResourcePermissionModel>() |
|||
}; |
|||
|
|||
protected SearchProviderKeyInfo? _selectedProviderKey; |
|||
protected string? ProviderKey => _selectedProviderKey?.ProviderKey; |
|||
protected string? ProviderDisplayName => _selectedProviderKey?.ProviderDisplayName; |
|||
|
|||
public GetResourcePermissionDefinitionListResultDto ResourcePermissionDefinitions { get; set; } = new() |
|||
{ |
|||
Permissions = new List<ResourcePermissionDefinitionDto>() |
|||
}; |
|||
protected string? CurrentLookupService { get; set; } |
|||
protected List<ResourceProviderDto> ResourceProviderKeyLookupServices { get; set; } = new(); |
|||
protected List<SearchProviderKeyInfo> ProviderKeys { get; set; } = new(); |
|||
protected GetResourcePermissionListResultDto ResourcePermissionList = new() |
|||
{ |
|||
Permissions = new List<ResourcePermissionGrantInfoDto>() |
|||
}; |
|||
|
|||
protected EditModel EditEntity { get; set; } = new EditModel |
|||
{ |
|||
Permissions = new List<ResourcePermissionModel>() |
|||
}; |
|||
|
|||
protected bool _grantAllCreate; |
|||
protected bool _grantAllEdit; |
|||
|
|||
public ResourcePermissionManagementModal() |
|||
{ |
|||
LocalizationResource = typeof(AbpPermissionManagementResource); |
|||
} |
|||
|
|||
public virtual async Task OpenAsync(string resourceName, string resourceKey, string resourceDisplayName) |
|||
{ |
|||
try |
|||
{ |
|||
ResourceName = resourceName; |
|||
ResourceKey = resourceKey; |
|||
ResourceDisplayName = resourceDisplayName; |
|||
|
|||
ResourcePermissionDefinitions = await PermissionAppService.GetResourceDefinitionsAsync(ResourceName); |
|||
ResourceProviderKeyLookupServices = (await PermissionAppService.GetResourceProviderKeyLookupServicesAsync(ResourceName)).Providers; |
|||
|
|||
HasAnyResourcePermission = ResourcePermissionDefinitions.Permissions.Any(); |
|||
if (HasAnyResourcePermission) |
|||
{ |
|||
HasAnyResourceProviderKeyLookupService = ResourceProviderKeyLookupServices.Count > 0; |
|||
} |
|||
|
|||
await InvokeAsync(StateHasChanged); |
|||
|
|||
ResourcePermissionList = await PermissionAppService.GetResourceAsync(ResourceName, ResourceKey); |
|||
|
|||
_isVisible = true; |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
await HandleErrorAsync(ex); |
|||
} |
|||
} |
|||
|
|||
protected virtual Task CloseModal() |
|||
{ |
|||
_isVisible = false; |
|||
return InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task OpenCreateDialogAsync() |
|||
{ |
|||
CurrentLookupService = ResourceProviderKeyLookupServices.FirstOrDefault()?.Name; |
|||
|
|||
_selectedProviderKey = null; |
|||
ProviderKeys = new List<SearchProviderKeyInfo>(); |
|||
if (_createFormRef != null) |
|||
{ |
|||
await _createFormRef.ResetAsync(); |
|||
} |
|||
|
|||
CreateEntity = new CreateModel |
|||
{ |
|||
Permissions = ResourcePermissionDefinitions.Permissions.Select(x => new ResourcePermissionModel |
|||
{ |
|||
Name = x.Name, |
|||
DisplayName = x.DisplayName, |
|||
IsGranted = false |
|||
}).ToList() |
|||
}; |
|||
|
|||
_grantAllCreate = false; |
|||
_createDialogVisible = true; |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task SelectedProviderKeyChanged(SearchProviderKeyInfo? value) |
|||
{ |
|||
_selectedProviderKey = value; |
|||
|
|||
if (value != null && CurrentLookupService != null) |
|||
{ |
|||
var permissionGrants = await PermissionAppService.GetResourceByProviderAsync(ResourceName!, ResourceKey!, CurrentLookupService, value.ProviderKey); |
|||
foreach (var permission in CreateEntity.Permissions) |
|||
{ |
|||
permission.IsGranted = permissionGrants.Permissions.Any(p => p.Name == permission.Name && p.Providers.Contains(CurrentLookupService) && p.IsGranted); |
|||
} |
|||
|
|||
_grantAllCreate = CreateEntity.Permissions.All(x => x.IsGranted); |
|||
} |
|||
|
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
private async Task<IEnumerable<SearchProviderKeyInfo>> SearchProviderKeyAsync(string value, CancellationToken token) |
|||
{ |
|||
if (value.IsNullOrWhiteSpace() || CurrentLookupService == null) |
|||
{ |
|||
ProviderKeys = new List<SearchProviderKeyInfo>(); |
|||
return ProviderKeys; |
|||
} |
|||
|
|||
ProviderKeys = (await PermissionAppService.SearchResourceProviderKeyAsync(ResourceName!, CurrentLookupService, value, 1)).Keys; |
|||
|
|||
return ProviderKeys; |
|||
} |
|||
|
|||
protected virtual async Task GrantAllCreateAsync(bool value) |
|||
{ |
|||
_grantAllCreate = value; |
|||
foreach (var permission in CreateEntity.Permissions) |
|||
{ |
|||
permission.IsGranted = value; |
|||
} |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task GrantAllEditAsync(bool value) |
|||
{ |
|||
_grantAllEdit = value; |
|||
foreach (var permission in EditEntity.Permissions) |
|||
{ |
|||
permission.IsGranted = value; |
|||
} |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task OpenEditDialogAsync(ResourcePermissionGrantInfoDto permission) |
|||
{ |
|||
var resourcePermissions = await PermissionAppService.GetResourceByProviderAsync(ResourceName!, ResourceKey!, permission.ProviderName, permission.ProviderKey); |
|||
EditEntity = new EditModel |
|||
{ |
|||
ProviderName = permission.ProviderName, |
|||
ProviderKey = permission.ProviderKey, |
|||
Permissions = resourcePermissions.Permissions.Select(x => new ResourcePermissionModel |
|||
{ |
|||
Name = x.Name, |
|||
DisplayName = x.DisplayName, |
|||
IsGranted = x.IsGranted |
|||
}).ToList() |
|||
}; |
|||
|
|||
_grantAllEdit = EditEntity.Permissions.All(x => x.IsGranted); |
|||
_editDialogVisible = true; |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task CloseCreateDialogAsync() |
|||
{ |
|||
_createDialogVisible = false; |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task CloseEditDialogAsync() |
|||
{ |
|||
_editDialogVisible = false; |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task OnLookupServiceCheckedValueChanged(string? value) |
|||
{ |
|||
CurrentLookupService = value; |
|||
_selectedProviderKey = null; |
|||
ProviderKeys = new List<SearchProviderKeyInfo>(); |
|||
if (_createFormRef != null) |
|||
{ |
|||
await _createFormRef.ResetAsync(); |
|||
} |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task CreateResourcePermissionAsync() |
|||
{ |
|||
if (_createFormRef != null && !await _createFormRef.Validate()) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
if (ProviderKey == null || CurrentLookupService == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
await PermissionAppService.UpdateResourceAsync( |
|||
ResourceName!, |
|||
ResourceKey!, |
|||
new UpdateResourcePermissionsDto |
|||
{ |
|||
ProviderName = CurrentLookupService, |
|||
ProviderKey = ProviderKey, |
|||
Permissions = CreateEntity.Permissions.Where(p => p.IsGranted).Select(p => p.Name).ToList() |
|||
} |
|||
); |
|||
|
|||
await CloseCreateDialogAsync(); |
|||
ResourcePermissionList = await PermissionAppService.GetResourceAsync(ResourceName!, ResourceKey!); |
|||
await InvokeAsync(StateHasChanged); |
|||
Snackbar.Add(L["SavedSuccessfully"], Severity.Success); |
|||
} |
|||
|
|||
protected virtual async Task UpdateResourcePermissionAsync() |
|||
{ |
|||
if (_editFormRef != null && !await _editFormRef.Validate()) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
await PermissionAppService.UpdateResourceAsync( |
|||
ResourceName!, |
|||
ResourceKey!, |
|||
new UpdateResourcePermissionsDto |
|||
{ |
|||
ProviderName = EditEntity.ProviderName, |
|||
ProviderKey = EditEntity.ProviderKey, |
|||
Permissions = EditEntity.Permissions.Where(p => p.IsGranted).Select(p => p.Name).ToList() |
|||
} |
|||
); |
|||
|
|||
await CloseEditDialogAsync(); |
|||
ResourcePermissionList = await PermissionAppService.GetResourceAsync(ResourceName!, ResourceKey!); |
|||
await InvokeAsync(StateHasChanged); |
|||
Snackbar.Add(L["SavedSuccessfully"], Severity.Success); |
|||
} |
|||
|
|||
protected virtual async Task DeleteResourcePermissionAsync(ResourcePermissionGrantInfoDto permission) |
|||
{ |
|||
var confirmed = await DialogService.ShowMessageBox( |
|||
L["AreYouSure"], |
|||
L["ResourcePermissionDeletionConfirmationMessage"], |
|||
yesText: L["Yes"], |
|||
cancelText: L["Cancel"]); |
|||
|
|||
if (confirmed == true) |
|||
{ |
|||
await PermissionAppService.DeleteResourceAsync( |
|||
ResourceName!, |
|||
ResourceKey!, |
|||
permission.ProviderName, |
|||
permission.ProviderKey |
|||
); |
|||
|
|||
ResourcePermissionList = await PermissionAppService.GetResourceAsync(ResourceName!, ResourceKey!); |
|||
Snackbar.Add(L["DeletedSuccessfully"], Severity.Success); |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
} |
|||
|
|||
public class CreateModel |
|||
{ |
|||
public List<ResourcePermissionModel> Permissions { get; set; } = new(); |
|||
} |
|||
|
|||
public class EditModel |
|||
{ |
|||
public string ProviderName { get; set; } = string.Empty; |
|||
|
|||
public string ProviderKey { get; set; } = string.Empty; |
|||
|
|||
public List<ResourcePermissionModel> Permissions { get; set; } = new(); |
|||
} |
|||
|
|||
public class ResourcePermissionModel |
|||
{ |
|||
public string Name { get; set; } = string.Empty; |
|||
|
|||
public string DisplayName { get; set; } = string.Empty; |
|||
|
|||
public bool IsGranted { get; set; } |
|||
} |
|||
} |
|||
Loading…
Reference in new issue