start working on security group view
This commit is contained in:
36
UserService/Pages/MembersBase.cs
Normal file
36
UserService/Pages/MembersBase.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Blazorise;
|
||||
using Blazorise.DataGrid;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using UserService.Infrastructure;
|
||||
using UserService.Infrastructure.DataModels;
|
||||
|
||||
namespace UserService.Pages
|
||||
{
|
||||
public abstract class MembersBase<T> : ComponentBase where T : Member
|
||||
{
|
||||
protected IReadOnlyList<OrganizationUnit>? OrganizationUnits { get; set; }
|
||||
|
||||
protected IReadOnlyList<T>? Members { get; set; }
|
||||
|
||||
protected string? CustomFilterValue { get; set; }
|
||||
|
||||
protected abstract Task RowInsertingCallback(CancellableRowChange<T, Dictionary<string, object>> arg);
|
||||
|
||||
protected abstract Task RowInsertedCallback(SavedRowItem<T, Dictionary<string, object>> arg);
|
||||
protected abstract Task RowDeletingCallback(CancellableRowChange<T> arg);
|
||||
protected abstract Task RowUpdatingCallback(CancellableRowChange<T, Dictionary<string, object>> arg);
|
||||
protected abstract bool OnCustomFilter(T model);
|
||||
|
||||
protected void ValidateCommonName(ValidatorEventArgs e)
|
||||
{
|
||||
if (e == null) throw new ArgumentNullException(nameof(e));
|
||||
var commonName = e.Value?.ToString();
|
||||
var validationResult = Validators.ValidateCommonName(commonName, Members ?? Enumerable.Empty<Member>());
|
||||
e.Status = validationResult == false ? ValidationStatus.Error : ValidationStatus.Success;
|
||||
}
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
|
||||
<h1>Table of all security groups</h1>
|
||||
|
||||
@if (Groups is null)
|
||||
@if (Members is null)
|
||||
{
|
||||
<p>
|
||||
<em>Loading...</em>
|
||||
@ -12,10 +12,76 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
<DataGrid TItem="SecurityGroup" Data="@Groups">
|
||||
<DataGridCommandColumn TItem="User" />
|
||||
<DataGridColumn TItem="SecurityGroup" Field="@nameof(SecurityGroup.Id)" Caption="#" Sortable="false" />
|
||||
<DataGridColumn TItem="SecurityGroup" Field="@nameof(SecurityGroup.CommonName)" Caption="CN" Editable="true" />
|
||||
<DataGridColumn TItem="SecurityGroup" Field="@nameof(SecurityGroup.EMail)" Caption="EMail" Editable="true" />
|
||||
</DataGrid>
|
||||
<TextEdit Placeholder="Search" Size="Size.Large" @bind-Text="@CustomFilterValue" />
|
||||
|
||||
<DataGrid
|
||||
TItem="SecurityGroup"
|
||||
RowSelectable="@(u => false)"
|
||||
CustomFilter="@OnCustomFilter"
|
||||
Sortable="true"
|
||||
Editable="true"
|
||||
EditMode="DataGridEditMode.Inline"
|
||||
RowRemoving="RowDeletingCallback"
|
||||
Data="@Members"
|
||||
RowInserted="RowInsertedCallback"
|
||||
RowInserting="RowInsertingCallback"
|
||||
RowUpdating="RowUpdatingCallback">
|
||||
<DataGridCommandColumn TItem="SecurityGroup">
|
||||
<NewCommandTemplate>
|
||||
<Button Color="Color.Success" Clicked="@context.Clicked" title="Create security group">
|
||||
<i class="fas fa-users"></i>
|
||||
</Button>
|
||||
</NewCommandTemplate>
|
||||
<EditCommandTemplate>
|
||||
<Button Color="Color.Primary" Clicked="@context.Clicked" title="Edit security group">
|
||||
<i class="fa fa-user-edit"></i>
|
||||
</Button>
|
||||
</EditCommandTemplate>
|
||||
<DeleteCommandTemplate>
|
||||
<Button Color="Color.Danger" Clicked="@context.Clicked" title="Delete security group">
|
||||
<i class="fa fa-user-minus"></i>
|
||||
</Button>
|
||||
</DeleteCommandTemplate>
|
||||
<SaveCommandTemplate>
|
||||
<Button Color="Color.Success" Clicked="@context.Clicked" title="Save security group">
|
||||
<i class="fas fa-save"></i>
|
||||
</Button>
|
||||
</SaveCommandTemplate>
|
||||
<CancelCommandTemplate>
|
||||
<Button Color="Color.Danger" Clicked="@context.Clicked" title="Cancel editing">
|
||||
<i class="far fa-times-circle"></i>
|
||||
</Button>
|
||||
</CancelCommandTemplate>
|
||||
</DataGridCommandColumn>
|
||||
<DataGridColumn TItem="SecurityGroup" Field="@nameof(SecurityGroup.Id)" Caption="#" Sortable="false" />
|
||||
|
||||
<DataGridColumn TItem="SecurityGroup" Field="@nameof(SecurityGroup.CommonName)" Caption="CN" CellsEditableOnEditCommand="false" Editable="true">
|
||||
<EditTemplate>
|
||||
<Validation Validator="@ValidateCommonName">
|
||||
<TextEdit Placeholder="Enter common name" Text="@((string)(((CellEditContext)context).CellValue))" TextChanged="@(v=>((CellEditContext)context).CellValue=v)">
|
||||
<Feedback>
|
||||
<ValidationSuccess></ValidationSuccess>
|
||||
<ValidationError>Please enter a valid common name!</ValidationError>
|
||||
</Feedback>
|
||||
</TextEdit>
|
||||
</Validation>
|
||||
</EditTemplate>
|
||||
</DataGridColumn>
|
||||
<DataGridSelectColumn TItem="SecurityGroup" Field="@nameof(SecurityGroup.ParentId)" Caption="Parent" Editable="true">
|
||||
<DisplayTemplate>
|
||||
@{
|
||||
var name = ((SecurityGroup) context ).Parent?.CommonName ?? "-";
|
||||
@name
|
||||
}
|
||||
</DisplayTemplate>
|
||||
<EditTemplate>
|
||||
<Select TValue="int?" SelectedValue="@((int?)(context.CellValue))" SelectedValueChanged="@(v => context.CellValue = v)" >
|
||||
@foreach (var item in OrganizationUnits ?? Enumerable.Empty<OrganizationUnit>())
|
||||
{
|
||||
<SelectItem TValue="int" Value="@(item.Id)">@item.CommonName</SelectItem>
|
||||
}
|
||||
</Select>
|
||||
</EditTemplate>
|
||||
</DataGridSelectColumn>
|
||||
</DataGrid>
|
||||
}
|
@ -1,47 +1,49 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Blazorise.DataGrid;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using UserService.DatabaseLayer.Repository;
|
||||
using UserService.Infrastructure.DataModels;
|
||||
|
||||
namespace UserService.Pages
|
||||
{
|
||||
public class SecurityGroupsBase : ComponentBase
|
||||
public class SecurityGroupsBase : MembersBase<SecurityGroup>
|
||||
{
|
||||
[Inject] private ISecurityGroupsRepository SecurityGroupsRepository { get; set; } = null!;
|
||||
[Inject] private IOrganizationUnitsRepository OrganizationUnitsRepository { get; set; } = null!;
|
||||
|
||||
protected bool DialogIsOpen { get; set; }
|
||||
protected SecurityGroup? SecurityGroupToEdit { get; set; }
|
||||
|
||||
protected IReadOnlyList<SecurityGroup>? Groups { get; set; }
|
||||
protected IReadOnlyList<OrganizationUnit>? OrganizationUnits { get; set; }
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
Groups = await SecurityGroupsRepository.GetAllAsync().ConfigureAwait(false);
|
||||
Members = await SecurityGroupsRepository.GetAllAsync().ConfigureAwait(false);
|
||||
OrganizationUnits = await OrganizationUnitsRepository.GetAllAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected void EditSecurityGroup(SecurityGroup securityGroup)
|
||||
/// <inheritdoc />
|
||||
protected override async Task RowInsertingCallback(CancellableRowChange<SecurityGroup, Dictionary<string, object>> arg)
|
||||
{
|
||||
SecurityGroupToEdit = securityGroup;
|
||||
DialogIsOpen = true;
|
||||
}
|
||||
|
||||
protected async Task OkClick()
|
||||
/// <inheritdoc />
|
||||
protected override async Task RowInsertedCallback(SavedRowItem<SecurityGroup, Dictionary<string, object>> arg)
|
||||
{
|
||||
if (!(SecurityGroupToEdit is null))
|
||||
{
|
||||
await SecurityGroupsRepository.UpdateAsync(SecurityGroupToEdit).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
DialogIsOpen = false;
|
||||
}
|
||||
|
||||
protected async Task DeleteSecurityGroup(SecurityGroup securityGroup)
|
||||
/// <inheritdoc />
|
||||
protected override async Task RowDeletingCallback(CancellableRowChange<SecurityGroup> arg)
|
||||
{
|
||||
await SecurityGroupsRepository.DeleteAsync(securityGroup).ConfigureAwait(false);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async Task RowUpdatingCallback(CancellableRowChange<SecurityGroup, Dictionary<string, object>> arg)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override bool OnCustomFilter(SecurityGroup model)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
|
||||
<h1>List of all users</h1>
|
||||
|
||||
@if (Users == null)
|
||||
@if (Members == null)
|
||||
{
|
||||
<p>
|
||||
<em>Loading...</em>
|
||||
@ -14,7 +14,18 @@ else
|
||||
{
|
||||
<TextEdit Placeholder="Search" Size="Size.Large" @bind-Text="@CustomFilterValue" />
|
||||
|
||||
<DataGrid TItem="User" RowSelectable="@(u => false)" CustomFilter="@OnCustomFilter" Sortable="true" Editable="true" EditMode="DataGridEditMode.Popup" RowRemoving="RowDeletingCallback" Data="@Users" RowInserted="RowInsertedCallback" RowInserting="RowInsertingCallback" RowUpdating="RowUpdatingCallback">
|
||||
<DataGrid
|
||||
TItem="User"
|
||||
RowSelectable="@(u => false)"
|
||||
CustomFilter="@OnCustomFilter"
|
||||
Sortable="true"
|
||||
Editable="true"
|
||||
EditMode="DataGridEditMode.Inline"
|
||||
RowRemoving="RowDeletingCallback"
|
||||
Data="@Members"
|
||||
RowInserted="RowInsertedCallback"
|
||||
RowInserting="RowInsertingCallback"
|
||||
RowUpdating="RowUpdatingCallback">
|
||||
<DataGridCommandColumn TItem="User">
|
||||
<NewCommandTemplate>
|
||||
<Button Color="Color.Success" Clicked="@context.Clicked" title="Create user">
|
||||
@ -77,11 +88,10 @@ else
|
||||
var name = ((User) context ).Parent?.CommonName ?? "-";
|
||||
@name
|
||||
}
|
||||
|
||||
</DisplayTemplate>
|
||||
<EditTemplate>
|
||||
<Select TValue="int?" SelectedValue="@((int?)(context.CellValue))" SelectedValueChanged="@(v => context.CellValue = v)" >
|
||||
@foreach (var item in OrganizationUnits)
|
||||
@foreach (var item in OrganizationUnits ?? Enumerable.Empty<OrganizationUnit>())
|
||||
{
|
||||
<SelectItem TValue="int" Value="@(item.Id)">@item.CommonName</SelectItem>
|
||||
}
|
||||
|
@ -12,42 +12,40 @@ using UserService.Infrastructure.DataModels;
|
||||
|
||||
namespace UserService.Pages
|
||||
{
|
||||
public class UsersBase : ComponentBase
|
||||
public class UsersBase : MembersBase<User>
|
||||
{
|
||||
[Inject] private IUsersRepository UsersRepository { get; set; } = null!;
|
||||
[Inject] private IOrganizationUnitsRepository OrganizationUnitsRepository { get; set; } = null!;
|
||||
|
||||
[Inject] private IJSRuntime JsRuntime { get; set; } = null!;
|
||||
|
||||
protected IReadOnlyList<User>? Users { get; private set; }
|
||||
protected IReadOnlyList<OrganizationUnit>? OrganizationUnits { get; private set; }
|
||||
protected string? CustomFilterValue { get; set; }
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
Users = await UsersRepository.GetAllAsync().ConfigureAwait(false);
|
||||
Members = await UsersRepository.GetAllAsync().ConfigureAwait(false);
|
||||
OrganizationUnits = await OrganizationUnitsRepository.GetAllAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected async Task RowInsertingCallback(CancellableRowChange<User, Dictionary<string, object>> arg)
|
||||
protected override async Task RowInsertingCallback(CancellableRowChange<User, Dictionary<string, object>> arg)
|
||||
{
|
||||
if (arg is null) throw new ArgumentNullException(nameof(arg));
|
||||
var mailValidation = Validators.ValidateEmail(arg.Values[nameof(User.EMail)]?.ToString());
|
||||
var commonNameValidation = Validators.ValidateCommonName(arg.Values[nameof(User.CommonName)]?.ToString(), Users);
|
||||
var commonNameValidation = Validators.ValidateCommonName(arg.Values[nameof(User.CommonName)]?.ToString(),
|
||||
Members ?? Enumerable.Empty<User>());
|
||||
if (mailValidation == true && commonNameValidation == true) return;
|
||||
|
||||
await JsRuntime.InvokeVoidAsync("alert", "User could not be added").ConfigureAwait(false);
|
||||
arg.Cancel = true;
|
||||
}
|
||||
|
||||
protected async Task RowInsertedCallback(SavedRowItem<User, Dictionary<string, object>> arg)
|
||||
protected override async Task RowInsertedCallback(SavedRowItem<User, Dictionary<string, object>> arg)
|
||||
{
|
||||
if (arg is null) throw new ArgumentNullException(nameof(arg));
|
||||
var user = arg.Item;
|
||||
user.Parent = OrganizationUnits?.FirstOrDefault(x => x.Id == user.ParentId);
|
||||
await UsersRepository.AddAsync(user).ConfigureAwait(false);
|
||||
user.Parent = OrganizationUnits?.FirstOrDefault(x => x.Id == user.ParentId);
|
||||
}
|
||||
protected async Task RowDeletingCallback(CancellableRowChange<User> arg)
|
||||
|
||||
protected override async Task RowDeletingCallback(CancellableRowChange<User> arg)
|
||||
{
|
||||
if (arg == null) throw new ArgumentNullException(nameof(arg));
|
||||
var confirmed = await JsRuntime.InvokeAsync<bool>("confirm",
|
||||
@ -61,14 +59,6 @@ namespace UserService.Pages
|
||||
arg.Cancel = true;
|
||||
}
|
||||
|
||||
protected void ValidateCommonName(ValidatorEventArgs e)
|
||||
{
|
||||
if (e == null) throw new ArgumentNullException(nameof(e));
|
||||
var commonName = e.Value?.ToString();
|
||||
var validationResult = Validators.ValidateCommonName(commonName, Users);
|
||||
e.Status = validationResult == false ? ValidationStatus.Error : ValidationStatus.Success;
|
||||
}
|
||||
|
||||
protected static void ValidateEmail(ValidatorEventArgs e)
|
||||
{
|
||||
if (e == null) throw new ArgumentNullException(nameof(e));
|
||||
@ -82,25 +72,28 @@ namespace UserService.Pages
|
||||
_ => ValidationStatus.Success
|
||||
};
|
||||
}
|
||||
protected async Task RowUpdatingCallback(CancellableRowChange<User, Dictionary<string, object>> arg)
|
||||
|
||||
protected override async Task RowUpdatingCallback(CancellableRowChange<User, Dictionary<string, object>> arg)
|
||||
{
|
||||
if (arg == null) throw new ArgumentNullException(nameof(arg));
|
||||
var user = arg.Item;
|
||||
user.Parent = OrganizationUnits.FirstOrDefault(x => x.Id == (int?)arg.Values[nameof(Node.ParentId)]);
|
||||
user.MapFields(arg.Values);
|
||||
user.Parent = OrganizationUnits?.FirstOrDefault(x => x.Id == (int?) arg.Values[nameof(Node.ParentId)]);
|
||||
var result = await UsersRepository.UpdateAsync(user).ConfigureAwait(false);
|
||||
arg.Cancel = !result;
|
||||
}
|
||||
|
||||
protected bool OnCustomFilter(User model)
|
||||
protected override bool OnCustomFilter(User model)
|
||||
{
|
||||
if (model == null) throw new ArgumentNullException(nameof(model));
|
||||
// We want to accept empty value as valid or otherwise
|
||||
// datagrid will not show anything.
|
||||
if (string.IsNullOrEmpty(CustomFilterValue) || CustomFilterValue.Length < 3) return true;
|
||||
|
||||
return
|
||||
model?.FirstName?.Contains(CustomFilterValue, StringComparison.OrdinalIgnoreCase) == true
|
||||
|| model?.LastName?.Contains(CustomFilterValue, StringComparison.OrdinalIgnoreCase) == true
|
||||
|| model?.CommonName?.Contains(CustomFilterValue, StringComparison.OrdinalIgnoreCase) == true;
|
||||
model.FirstName?.Contains(CustomFilterValue, StringComparison.OrdinalIgnoreCase) == true
|
||||
|| model.LastName?.Contains(CustomFilterValue, StringComparison.OrdinalIgnoreCase) == true
|
||||
|| model.CommonName.Contains(CustomFilterValue, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user