Open Source Web Application Framework for ASP.NET Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

298 lines
9.9 KiB

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; }
}
}