mirror of https://github.com/abpframework/abp.git
3 changed files with 474 additions and 1 deletions
@ -0,0 +1,172 @@ |
|||
@using Blazorise.Components |
|||
@using Volo.Abp.BlazoriseUI.Components |
|||
@using Volo.Abp.PermissionManagement.Localization |
|||
@inherits Volo.Abp.AspNetCore.Components.AbpComponentBase |
|||
@inject AbpBlazorMessageLocalizerHelper<AbpPermissionManagementResource> LH |
|||
<Modal @ref="Modal" Closing="@ClosingModal"> |
|||
<ModalContent Size="ModalSize.ExtraLarge" Centered="true"> |
|||
<ModalHeader> |
|||
<ModalTitle>@L["ResourcePermissions"] - @ResourceDisplayName</ModalTitle> |
|||
<CloseButton Clicked="CloseModal" /> |
|||
</ModalHeader> |
|||
<ModalBody Overflow="Overflow.Hidden"> |
|||
<div class="d-grid gap-2 mb-2 d-md-flex justify-content-md-end"> |
|||
<Button Color="Color.Primary" size="Size.Small" Clicked="OpenCreateModalAsync">@L["AddResourcePermission"]</Button> |
|||
</div> |
|||
<DataGrid TItem="ResourcePermissionGrantInfoDto" |
|||
Data="ResourcePermissionList.Permissions" |
|||
TotalItems="ResourcePermissionList.Permissions.Count" |
|||
ShowPager="true" |
|||
PageSize="PageSize"> |
|||
<DataGridColumns> |
|||
<DataGridColumn |
|||
Width="150px" |
|||
Sortable="false" |
|||
TItem="ResourcePermissionGrantInfoDto" |
|||
Field="@nameof(ResourcePermissionGrantInfoDto.ProviderName)" |
|||
Caption="@L["Actions"]"> |
|||
<DisplayTemplate> |
|||
<Dropdown> |
|||
<DropdownToggle Color="Color.Primary"> |
|||
@L["Actions"] |
|||
</DropdownToggle> |
|||
<DropdownMenu> |
|||
<DropdownItem Clicked="() => OpenEditModalAsync(context)"> |
|||
@L["Edit"] |
|||
</DropdownItem> |
|||
<DropdownItem Clicked="() => DeleteResourcePermissionAsync(context)"> |
|||
@L["Delete"] |
|||
</DropdownItem> |
|||
</DropdownMenu> |
|||
</Dropdown> |
|||
</DisplayTemplate> |
|||
</DataGridColumn> |
|||
<DataGridColumn TItem="ResourcePermissionGrantInfoDto" Field="@nameof(ResourcePermissionGrantInfoDto.ProviderName)" Caption="@L["ResourcePermissionTarget"]" Sortable="false"> |
|||
<DisplayTemplate> |
|||
@{ |
|||
<span class="d-inline-block bg-light rounded-pill px-2 me-1 ms-1 mb-1">@context.ProviderName</span> |
|||
@context.ProviderDisplayName |
|||
} |
|||
</DisplayTemplate> |
|||
</DataGridColumn> |
|||
<DataGridColumn TItem="ResourcePermissionGrantInfoDto" Field="@nameof(ResourcePermissionGrantInfoDto.Permissions)" Caption="@L["ResourcePermissionPermissions"]" Sortable="false"> |
|||
<DisplayTemplate> |
|||
@{ |
|||
foreach (var permission in context.Permissions) |
|||
{ |
|||
<span class="d-inline-block bg-light rounded-pill px-2 me-1 mb-1">@permission.DisplayName</span> |
|||
} |
|||
} |
|||
</DisplayTemplate> |
|||
</DataGridColumn> |
|||
</DataGridColumns> |
|||
<EmptyTemplate> |
|||
@L["NoDataAvailableInDatatable"] |
|||
</EmptyTemplate> |
|||
</DataGrid> |
|||
</ModalBody> |
|||
<ModalFooter> |
|||
<Button Color="Color.Primary" Outline Clicked="CloseModal">@L["Close"]</Button> |
|||
</ModalFooter> |
|||
</ModalContent> |
|||
</Modal> |
|||
|
|||
<Modal @ref="CreateModal" Closing="@ClosingCreateModal"> |
|||
<ModalContent Centered="true"> |
|||
<Form> |
|||
<ModalHeader> |
|||
<ModalTitle>@L["AddResourcePermissions"]</ModalTitle> |
|||
<CloseButton Clicked="CloseCreateModalAsync" /> |
|||
</ModalHeader> |
|||
<ModalBody> |
|||
<Validations @ref="@CreateValidationsRef" Model="@CreateEntity" ValidateOnLoad="false"> |
|||
<div class="mb-3"> |
|||
<label class="form-label">@L["Resource"]:</label> |
|||
<p>@ResourceDisplayName</p> |
|||
</div> |
|||
<div class="mb-3"> |
|||
<FieldLabel>@L["ProviderName"]:</FieldLabel> |
|||
<RadioGroup TValue="string" |
|||
CheckedValue="@CurrentLookupService" |
|||
CheckedValueChanged="@OnLookupServiceCheckedValueChanged"> |
|||
@foreach(var keyLookupService in ResourceProviderKeyLookupServices) |
|||
{ |
|||
<Radio Value="@(keyLookupService.Name)">@keyLookupService.DisplayName</Radio> |
|||
} |
|||
</RadioGroup> |
|||
</div> |
|||
<div class="mb-3"> |
|||
<FieldLabel>@L["ProviderKey"]: *</FieldLabel> |
|||
<Autocomplete @ref="ProviderKeyAutocompleteRef" |
|||
TItem="SearchProviderKeyInfo" |
|||
TValue="string" |
|||
Data="@ProviderKeys" |
|||
ReadData="@SearchProviderKeyAsync" |
|||
TotalItems="ProviderKeys.Count" |
|||
TextField="@((item) => item.ProviderDisplayName)" |
|||
ValueField="@((item) => item.ProviderKey)" |
|||
SelectedValue="@ProviderKey" |
|||
SelectedText="@ProviderDisplayName" |
|||
SelectedValueChanged="SelectedProviderKeyAsync" |
|||
class="mt-1"> |
|||
</Autocomplete> |
|||
<Validation @ref="ProviderKeyValidationRef" Validator="ValidateProviderKey"> |
|||
<TextEdit Style="display: none;"> |
|||
<Feedback> |
|||
<ValidationError></ValidationError> |
|||
</Feedback> |
|||
</TextEdit> |
|||
</Validation> |
|||
</div> |
|||
<div class="mb-3"> |
|||
<div class="mb-1 d-flex align-items-center justify-content-between"> |
|||
<label class="form-labe">@L["ResourcePermissionPermissions"]:</label> |
|||
<Check TValue="bool" Checked="CreateEntity.Permissions.All(x => x.IsGranted)" CheckedChanged="GrantAllAsync">@L["GrantAllResourcePermissions"]</Check> |
|||
</div> |
|||
@foreach (var permission in CreateEntity.Permissions) |
|||
{ |
|||
<Check TValue="bool" Checked="@permission.IsGranted" CheckedChanged="@((c) => OnPermissionCheckedChanged(permission, c))">@permission.DisplayName</Check> |
|||
} |
|||
</div> |
|||
</Validations> |
|||
</ModalBody> |
|||
<ModalFooter> |
|||
<Button Color="Color.Primary" Outline Clicked="CloseCreateModalAsync">@L["Cancel"]</Button> |
|||
<SubmitButton Clicked="@CreateResourcePermissionAsync" /> |
|||
</ModalFooter> |
|||
</Form> |
|||
</ModalContent> |
|||
</Modal> |
|||
|
|||
<Modal @ref="EditModal" Closing="@ClosingEditModal"> |
|||
<ModalContent Centered="true"> |
|||
<Form> |
|||
<ModalHeader> |
|||
<ModalTitle>@L["UpdateResourcePermissions"]</ModalTitle> |
|||
<CloseButton Clicked="CloseEditModalAsync" /> |
|||
</ModalHeader> |
|||
<ModalBody> |
|||
<Validations @ref="@EditValidationsRef" Model="@EditEntity" ValidateOnLoad="false"> |
|||
<div class="mb-3"> |
|||
<label class="form-label">@L["Resource"]:</label> |
|||
<p>@ResourceDisplayName</p> |
|||
</div> |
|||
<div class="mb-3"> |
|||
<div class="mb-1 d-flex align-items-center justify-content-between"> |
|||
<label class="form-labe">@L["ResourcePermissionPermissions"]:</label> |
|||
<Check TValue="bool" Checked="EditEntity.Permissions.All(x => x.IsGranted)" CheckedChanged="GrantAllAsync">@L["GrantAllResourcePermissions"]</Check> |
|||
</div> |
|||
@foreach (var permission in EditEntity.Permissions) |
|||
{ |
|||
<Check TValue="bool" Checked="@permission.IsGranted" CheckedChanged="@((c) => OnPermissionCheckedChanged(permission, c))">@permission.DisplayName</Check> |
|||
} |
|||
</div> |
|||
</Validations> |
|||
</ModalBody> |
|||
<ModalFooter> |
|||
<Button Color="Color.Primary" Outline Clicked="CloseEditModalAsync">@L["Cancel"]</Button> |
|||
<SubmitButton Clicked="@UpdateResourcePermissionAsync" /> |
|||
</ModalFooter> |
|||
</Form> |
|||
</ModalContent> |
|||
</Modal> |
|||
@ -0,0 +1,298 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Blazorise; |
|||
using Blazorise.Components; |
|||
using Microsoft.AspNetCore.Components; |
|||
using Volo.Abp.AspNetCore.Components.Messages; |
|||
using Volo.Abp.PermissionManagement.Localization; |
|||
|
|||
namespace Volo.Abp.PermissionManagement.Blazor.Components; |
|||
|
|||
public partial class ResourcePermissionManagementModal |
|||
{ |
|||
[Inject] protected IPermissionAppService PermissionAppService { get; set; } |
|||
|
|||
[Inject] protected IUiMessageService UiMessageService { get; set; } |
|||
|
|||
protected Modal Modal { get; set; } |
|||
|
|||
protected string ResourceName { get; set; } |
|||
protected string ResourceKey { get; set; } |
|||
protected string ResourceDisplayName{ get; set; } |
|||
protected int PageSize { get; set; } = 10; |
|||
|
|||
protected Modal CreateModal { get; set; } |
|||
protected Validations CreateValidationsRef { get; set; } |
|||
protected CreateModel CreateEntity { get; set; } = new CreateModel |
|||
{ |
|||
Permissions = [] |
|||
}; |
|||
protected Autocomplete<SearchProviderKeyInfo, string> ProviderKeyAutocompleteRef { get; set; } |
|||
protected Blazorise.Validation ProviderKeyValidationRef { get; set; } |
|||
public GetResourcePermissionDefinitionListResultDto ResourcePermissionDefinitions { get; set; } = new() |
|||
{ |
|||
Permissions = [] |
|||
}; |
|||
protected string CurrentLookupService { get; set; } |
|||
protected string ProviderKey { get; set; } |
|||
protected string ProviderDisplayName { get; set; } |
|||
protected List<ResourceProviderDto> ResourceProviderKeyLookupServices { get; set; } = new(); |
|||
protected List<SearchProviderKeyInfo> ProviderKeys { get; set; } = new(); |
|||
protected GetResourcePermissionListResultDto ResourcePermissionList = new() |
|||
{ |
|||
Permissions = [] |
|||
}; |
|||
|
|||
protected Validations EditValidationsRef { get; set; } |
|||
protected Modal EditModal { get; set; } |
|||
protected EditModel EditEntity { get; set; } = new EditModel |
|||
{ |
|||
Permissions = [] |
|||
}; |
|||
|
|||
public ResourcePermissionManagementModal() |
|||
{ |
|||
LocalizationResource = typeof(AbpPermissionManagementResource); |
|||
} |
|||
|
|||
public virtual async Task OpenAsync(string resourceName, string resourceKey, string resourceDisplayName = null) |
|||
{ |
|||
try |
|||
{ |
|||
ResourceName = resourceName; |
|||
ResourceKey = resourceKey; |
|||
ResourceDisplayName = resourceDisplayName; |
|||
|
|||
ResourcePermissionDefinitions = await PermissionAppService.GetResourceDefinitionsAsync(ResourceName); |
|||
ResourceProviderKeyLookupServices = (await PermissionAppService.GetResourceProviderKeyLookupServicesAsync(ResourceName)).Providers; |
|||
|
|||
ResourcePermissionList = await PermissionAppService.GetResourceAsync(ResourceName, ResourceKey); |
|||
|
|||
await Modal.Show(); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
await HandleErrorAsync(ex); |
|||
} |
|||
} |
|||
|
|||
protected Task CloseModal() |
|||
{ |
|||
return InvokeAsync(Modal.Hide); |
|||
} |
|||
|
|||
protected virtual Task ClosingModal(ModalClosingEventArgs eventArgs) |
|||
{ |
|||
eventArgs.Cancel = eventArgs.CloseReason == CloseReason.FocusLostClosing; |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
protected virtual async Task OpenCreateModalAsync() |
|||
{ |
|||
CurrentLookupService = ResourceProviderKeyLookupServices.FirstOrDefault()?.Name; |
|||
|
|||
ProviderKey = null; |
|||
ProviderDisplayName = null; |
|||
ProviderKeys = new List<SearchProviderKeyInfo>(); |
|||
await ProviderKeyAutocompleteRef.Clear(); |
|||
await CreateValidationsRef.ClearAll(); |
|||
|
|||
CreateEntity = new CreateModel |
|||
{ |
|||
Permissions = ResourcePermissionDefinitions.Permissions.Select(x => new ResourcePermissionModel |
|||
{ |
|||
Name = x.Name, |
|||
DisplayName = x.DisplayName, |
|||
IsGranted = false |
|||
}).ToList() |
|||
}; |
|||
|
|||
await CreateModal.Show(); |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task SelectedProviderKeyAsync(string value) |
|||
{ |
|||
ProviderKey = value; |
|||
ProviderDisplayName = ProviderKeys.FirstOrDefault(p => p.ProviderKey == value)?.ProviderDisplayName; |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
private async Task SearchProviderKeyAsync(AutocompleteReadDataEventArgs autocompleteReadDataEventArgs) |
|||
{ |
|||
if ( !autocompleteReadDataEventArgs.CancellationToken.IsCancellationRequested ) |
|||
{ |
|||
if (autocompleteReadDataEventArgs.SearchValue.IsNullOrWhiteSpace()) |
|||
{ |
|||
ProviderKeys = new List<SearchProviderKeyInfo>(); |
|||
return; |
|||
} |
|||
|
|||
ProviderKeys = (await PermissionAppService.SearchResourceProviderKeyAsync(ResourceName, CurrentLookupService, autocompleteReadDataEventArgs.SearchValue, 1)).Keys; |
|||
|
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
} |
|||
|
|||
protected virtual async Task OnPermissionCheckedChanged(ResourcePermissionModel permission, bool value) |
|||
{ |
|||
permission.IsGranted = value; |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task GrantAllAsync(bool value) |
|||
{ |
|||
foreach (var permission in CreateEntity.Permissions) |
|||
{ |
|||
permission.IsGranted = value; |
|||
} |
|||
|
|||
foreach (var permission in EditEntity.Permissions) |
|||
{ |
|||
permission.IsGranted = value; |
|||
} |
|||
|
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual async Task OpenEditModalAsync(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() |
|||
}; |
|||
|
|||
await EditModal.Show(); |
|||
} |
|||
|
|||
protected virtual Task ClosingCreateModal(ModalClosingEventArgs eventArgs) |
|||
{ |
|||
eventArgs.Cancel = eventArgs.CloseReason == CloseReason.FocusLostClosing; |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
protected virtual Task ClosingEditModal(ModalClosingEventArgs eventArgs) |
|||
{ |
|||
eventArgs.Cancel = eventArgs.CloseReason == CloseReason.FocusLostClosing; |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
protected virtual async Task CloseCreateModalAsync() |
|||
{ |
|||
await CreateModal.Hide(); |
|||
} |
|||
|
|||
protected virtual async Task CloseEditModalAsync() |
|||
{ |
|||
await EditModal.Hide(); |
|||
} |
|||
|
|||
protected virtual async Task OnLookupServiceCheckedValueChanged(string value) |
|||
{ |
|||
CurrentLookupService = value; |
|||
ProviderKey = null; |
|||
ProviderDisplayName = null; |
|||
await ProviderKeyAutocompleteRef.Clear(); |
|||
await CreateValidationsRef.ClearAll(); |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
|
|||
protected virtual void ValidateProviderKey(ValidatorEventArgs validatorEventArgs) |
|||
{ |
|||
validatorEventArgs.Status = ProviderKey.IsNullOrWhiteSpace() |
|||
? ValidationStatus.Error |
|||
: ValidationStatus.Success; |
|||
validatorEventArgs.ErrorText = L["ThisFieldIsRequired."]; |
|||
} |
|||
|
|||
protected virtual async Task CreateResourcePermissionAsync() |
|||
{ |
|||
if (await CreateValidationsRef.ValidateAll()) |
|||
{ |
|||
await PermissionAppService.UpdateResourceAsync( |
|||
ResourceName, |
|||
ResourceKey, |
|||
new UpdateResourcePermissionsDto |
|||
{ |
|||
ProviderName = CurrentLookupService, |
|||
ProviderKey = ProviderKey, |
|||
Permissions = CreateEntity.Permissions.Where(p => p.IsGranted).Select(p => p.Name).ToList() |
|||
} |
|||
); |
|||
|
|||
await CloseCreateModalAsync(); |
|||
ResourcePermissionList = await PermissionAppService.GetResourceAsync(ResourceName, ResourceKey); |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
} |
|||
|
|||
protected virtual async Task UpdateResourcePermissionAsync() |
|||
{ |
|||
if (await EditValidationsRef.ValidateAll()) |
|||
{ |
|||
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 CloseEditModalAsync(); |
|||
ResourcePermissionList = await PermissionAppService.GetResourceAsync(ResourceName, ResourceKey); |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
} |
|||
|
|||
protected virtual async Task DeleteResourcePermissionAsync(ResourcePermissionGrantInfoDto permission) |
|||
{ |
|||
if(await UiMessageService.Confirm(L["ResourcePermissionDeletionConfirmationMessage"])) |
|||
{ |
|||
await PermissionAppService.DeleteResourceAsync( |
|||
ResourceName, |
|||
ResourceKey, |
|||
permission.ProviderName, |
|||
permission.ProviderKey |
|||
); |
|||
|
|||
ResourcePermissionList = await PermissionAppService.GetResourceAsync(ResourceName, ResourceKey); |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
} |
|||
|
|||
public class CreateModel |
|||
{ |
|||
public List<ResourcePermissionModel> Permissions { get; set; } |
|||
} |
|||
|
|||
public class EditModel |
|||
{ |
|||
public string ProviderName { get; set; } |
|||
|
|||
public string ProviderKey { get; set; } |
|||
|
|||
public List<ResourcePermissionModel> Permissions { get; set; } |
|||
} |
|||
|
|||
public class ResourcePermissionModel |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public string DisplayName { get; set; } |
|||
|
|||
public bool IsGranted { get; set; } |
|||
} |
|||
} |
|||
Loading…
Reference in new issue