Removed unnecessary files + added proper documentation to admin page generators
This commit is contained in:
@@ -1,292 +0,0 @@
|
||||
@rendermode InteractiveServer
|
||||
|
||||
@using BlazorStrap
|
||||
@using BlazorStrap.Shared.Components.Modal
|
||||
@using static Microsoft.AspNetCore.Components.Web.RenderMode
|
||||
@using BlazorStrap.V5
|
||||
@using CurrieTechnologies.Razor.SweetAlert2
|
||||
@using HopFrame.Database.Models
|
||||
@using HopFrame.Database.Repositories
|
||||
@using HopFrame.Security.Claims
|
||||
@using HopFrame.Web.Model
|
||||
|
||||
<BSModal DataId="add-group-modal" HideOnValidSubmit="true" IsStaticBackdrop="true" @ref="_modal">
|
||||
<BSForm Model="_group" OnValidSubmit="AddGroup">
|
||||
@if (_isEdit) {
|
||||
<BSModalHeader>Edit group</BSModalHeader>
|
||||
}
|
||||
else {
|
||||
<BSModalHeader>Add group</BSModalHeader>
|
||||
}
|
||||
<BSModalContent>
|
||||
<div class="mb-3">
|
||||
<BSLabel>Name</BSLabel>
|
||||
@if (!_isEdit) {
|
||||
<BSInputGroup>
|
||||
<span class="@BS.Input_Group_Text">group.</span>
|
||||
<BSInput InputType="InputType.Text" @bind-Value="_group.GroupName" required/>
|
||||
</BSInputGroup>
|
||||
}
|
||||
else {
|
||||
<input type="text" class="form-control" disabled value="@_group.Name"/>
|
||||
}
|
||||
</div>
|
||||
|
||||
@if (_isEdit) {
|
||||
<div class="mb-3">
|
||||
<BSLabel>Created at</BSLabel>
|
||||
<input type="text" class="form-control" disabled value="@_group.CreatedAt"/>
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="mb-3">
|
||||
<BSLabel>Description</BSLabel>
|
||||
<BSInput InputType="InputType.TextArea" @bind-Value="_group.Description"/>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<BSInputSwitch @bind-Value="_group.IsDefaultGroup" CheckedValue="true" UnCheckedValue="false">
|
||||
Default group
|
||||
</BSInputSwitch>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<BSLabel>Inherits from</BSLabel>
|
||||
<BSListGroup>
|
||||
<BSListGroupItem>
|
||||
<BSListGroup IsFlush="true">
|
||||
@foreach (var group in _group.Permissions.Where(g => g.PermissionName.StartsWith("group."))) {
|
||||
<BSListGroupItem>
|
||||
<BSButton Color="BSColor.Danger" Size="Size.ExtraSmall" MarginEnd="Margins.Small" OnClick="() => RemovePermission(group)">
|
||||
<HopIconDisplay Type="HopIconDisplay.HopIcon.Cross"/>
|
||||
</BSButton>
|
||||
|
||||
<span>@group.PermissionName.Replace("group.", "")</span>
|
||||
</BSListGroupItem>
|
||||
}
|
||||
</BSListGroup>
|
||||
</BSListGroupItem>
|
||||
<BSListGroupItem>
|
||||
<div style="display: flex; gap: 20px">
|
||||
<BSInput InputType="InputType.Select" @bind-Value="_groupToAdd">
|
||||
<option selected>Select group</option>
|
||||
|
||||
@foreach (var group in _allGroups) {
|
||||
@if (_group.Permissions.All(g => g.PermissionName != group.Name) && group.Name != _group.Name) {
|
||||
<option value="@group.Name">@group.Name.Replace("group.", "")</option>
|
||||
}
|
||||
}
|
||||
</BSInput>
|
||||
<BSButton Color="BSColor.Secondary" OnClick="AddInheritanceGroup">Add</BSButton>
|
||||
</div>
|
||||
</BSListGroupItem>
|
||||
</BSListGroup>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<BSLabel>Permissions</BSLabel>
|
||||
<BSListGroup>
|
||||
<BSListGroupItem>
|
||||
<BSListGroup IsFlush="true">
|
||||
@foreach (var perm in _group.Permissions.Where(perm => !perm.PermissionName.StartsWith("group."))) {
|
||||
<BSListGroupItem>
|
||||
<BSButton Color="BSColor.Danger" Size="Size.ExtraSmall" MarginEnd="Margins.Small" OnClick="() => RemovePermission(perm)">
|
||||
<HopIconDisplay Type="HopIconDisplay.HopIcon.Cross"/>
|
||||
</BSButton>
|
||||
|
||||
<span>@perm.PermissionName</span>
|
||||
</BSListGroupItem>
|
||||
}
|
||||
</BSListGroup>
|
||||
</BSListGroupItem>
|
||||
<BSListGroupItem>
|
||||
<div style="display: flex; gap: 20px">
|
||||
<BSInput InputType="InputType.Text" @bind-Value="_permissionToAdd"/>
|
||||
<BSButton Color="BSColor.Secondary" OnClick="AddPermission">Add</BSButton>
|
||||
</div>
|
||||
</BSListGroupItem>
|
||||
</BSListGroup>
|
||||
</div>
|
||||
</BSModalContent>
|
||||
<BSModalFooter>
|
||||
<BSButton Target="add-group-modal">Cancel</BSButton>
|
||||
<BSButton IsSubmit="true" Color="BSColor.Primary">Save</BSButton>
|
||||
</BSModalFooter>
|
||||
</BSForm>
|
||||
</BSModal>
|
||||
|
||||
@inject IGroupRepository Groups
|
||||
@inject IPermissionRepository Permissions
|
||||
@inject SweetAlertService Alerts
|
||||
@inject ITokenContext Context
|
||||
|
||||
@code {
|
||||
[Parameter] public Func<Task> ReloadPage { get; set; }
|
||||
|
||||
private PermissionGroupAdd _group;
|
||||
|
||||
private BSModalBase _modal;
|
||||
private string _permissionToAdd;
|
||||
private string _groupToAdd;
|
||||
|
||||
private IList<PermissionGroup> _allGroups;
|
||||
|
||||
private bool _isEdit;
|
||||
|
||||
public async Task ShowAsync(PermissionGroup group = null) {
|
||||
_allGroups = await Groups.GetPermissionGroups();
|
||||
|
||||
if (group is not null) {
|
||||
_group = new PermissionGroupAdd {
|
||||
CreatedAt = group.CreatedAt,
|
||||
Description = group.Description,
|
||||
Name = group.Name,
|
||||
IsDefaultGroup = group.IsDefaultGroup,
|
||||
Permissions = group.Permissions
|
||||
};
|
||||
_isEdit = true;
|
||||
}
|
||||
else {
|
||||
_group = new PermissionGroupAdd {
|
||||
Permissions = new List<Permission>(),
|
||||
IsDefaultGroup = false
|
||||
};
|
||||
_isEdit = false;
|
||||
}
|
||||
|
||||
await _modal.ShowAsync();
|
||||
}
|
||||
|
||||
private async Task AddPermission() {
|
||||
if (string.IsNullOrWhiteSpace(_permissionToAdd)) {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Enter a permission name!",
|
||||
Icon = SweetAlertIcon.Error,
|
||||
ShowConfirmButton = true
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (_isEdit) {
|
||||
if (!await Permissions.HasPermission(Context.User, Security.AdminPermissions.EditGroup)) {
|
||||
await NoEditPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
await Permissions.AddPermission(_group, _permissionToAdd);
|
||||
}
|
||||
|
||||
_group.Permissions.Add(new Permission {
|
||||
PermissionName = _permissionToAdd,
|
||||
GrantedAt = DateTime.Now
|
||||
});
|
||||
|
||||
_permissionToAdd = null;
|
||||
}
|
||||
|
||||
private async Task RemovePermission(Permission permission) {
|
||||
if (_isEdit) {
|
||||
await Permissions.RemovePermission(_group, permission.PermissionName);
|
||||
}
|
||||
|
||||
_group.Permissions.Remove(permission);
|
||||
}
|
||||
|
||||
private async Task AddInheritanceGroup() {
|
||||
if (string.IsNullOrWhiteSpace(_groupToAdd)) {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Select a group!",
|
||||
Icon = SweetAlertIcon.Error,
|
||||
ShowConfirmButton = true
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (_isEdit) {
|
||||
if (!await Permissions.HasPermission(Context.User, Security.AdminPermissions.EditGroup)) {
|
||||
await NoEditPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
await Permissions.AddPermission(_group, _groupToAdd);
|
||||
}
|
||||
|
||||
_group.Permissions.Add(new Permission {
|
||||
PermissionName = _groupToAdd
|
||||
});
|
||||
|
||||
_groupToAdd = null;
|
||||
}
|
||||
|
||||
private async Task AddGroup() {
|
||||
if (_isEdit) {
|
||||
if (!await Permissions.HasPermission(Context.User, Security.AdminPermissions.EditGroup)) {
|
||||
await NoEditPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
await Groups.EditPermissionGroup(_group);
|
||||
|
||||
if (ReloadPage is not null)
|
||||
await ReloadPage.Invoke();
|
||||
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Group edited!",
|
||||
Icon = SweetAlertIcon.Success,
|
||||
Timer = 1500,
|
||||
ShowConfirmButton = false
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!await Permissions.HasPermission(Context.User, Security.AdminPermissions.AddGroup)) {
|
||||
await NoAddPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_allGroups.Any(group => group.Name == _group.Name)) {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Something went wrong!",
|
||||
Text = "This group already exists!",
|
||||
Icon = SweetAlertIcon.Error,
|
||||
ShowConfirmButton = false,
|
||||
Timer = 1500
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await Groups.CreatePermissionGroup(new PermissionGroup {
|
||||
Description = _group.Description,
|
||||
IsDefaultGroup = _group.IsDefaultGroup,
|
||||
Permissions = _group.Permissions,
|
||||
Name = "group." + _group.GroupName
|
||||
});
|
||||
|
||||
if (ReloadPage is not null)
|
||||
await ReloadPage.Invoke();
|
||||
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Group added!",
|
||||
Icon = SweetAlertIcon.Success,
|
||||
Timer = 1500,
|
||||
ShowConfirmButton = false
|
||||
});
|
||||
}
|
||||
|
||||
private async Task NoEditPermissions() {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Unauthorized!",
|
||||
Text = "You don't have the required permissions to edit a group!",
|
||||
Icon = SweetAlertIcon.Error
|
||||
});
|
||||
}
|
||||
|
||||
private async Task NoAddPermissions() {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Unauthorized!",
|
||||
Text = "You don't have the required permissions to add a group!",
|
||||
Icon = SweetAlertIcon.Error
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
@rendermode InteractiveServer
|
||||
|
||||
@using BlazorStrap
|
||||
@using static Microsoft.AspNetCore.Components.Web.RenderMode
|
||||
@using BlazorStrap.Shared.Components.Modal
|
||||
@using BlazorStrap.V5
|
||||
@using CurrieTechnologies.Razor.SweetAlert2
|
||||
@using HopFrame.Database.Models
|
||||
@using HopFrame.Database.Repositories
|
||||
@using HopFrame.Security.Claims
|
||||
@using HopFrame.Web.Model
|
||||
|
||||
<BSModal DataId="add-user-modal" HideOnValidSubmit="true" IsStaticBackdrop="true" OnShow="() => _user = new()" @ref="_modal">
|
||||
<BSForm Model="_user" OnValidSubmit="AddUser">
|
||||
<BSModalHeader>Add user</BSModalHeader>
|
||||
<BSModalContent>
|
||||
<div class="mb-3">
|
||||
<BSLabel>E-Mail</BSLabel>
|
||||
<BSInput InputType="InputType.Email" @bind-Value="_user.Email" required/>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<BSLabel>Username</BSLabel>
|
||||
<BSInput InputType="InputType.Text" @bind-Value="_user.Username" required/>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<BSLabel>Password</BSLabel>
|
||||
<BSInput InputType="InputType.Password" @bind-Value="_user.Password" required/>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<BSLabel>Primary group</BSLabel>
|
||||
<BSInput InputType="InputType.Select" @bind-Value="_user.Group">
|
||||
<option value="">Select group</option>
|
||||
|
||||
@foreach (var group in _allGroups) {
|
||||
<option value="@group.Name">@group.Name.Replace("group.", "")</option>
|
||||
}
|
||||
</BSInput>
|
||||
</div>
|
||||
</BSModalContent>
|
||||
<BSModalFooter>
|
||||
<BSButton Target="add-user-modal">Cancel</BSButton>
|
||||
<BSButton IsSubmit="true" Color="BSColor.Primary">Save</BSButton>
|
||||
</BSModalFooter>
|
||||
</BSForm>
|
||||
</BSModal>
|
||||
|
||||
@inject IUserRepository Users
|
||||
@inject IPermissionRepository Permissions
|
||||
@inject IGroupRepository Groups
|
||||
@inject SweetAlertService Alerts
|
||||
@inject ITokenContext Auth
|
||||
|
||||
@code {
|
||||
[Parameter] public Func<Task> ReloadPage { get; set; }
|
||||
|
||||
private IList<PermissionGroup> _allGroups = new List<PermissionGroup>();
|
||||
private IList<User> _allUsers = new List<User>();
|
||||
private UserAdd _user;
|
||||
|
||||
private BSModalBase _modal;
|
||||
|
||||
public async Task ShowAsync() {
|
||||
_allGroups = await Groups.GetPermissionGroups();
|
||||
_allUsers = await Users.GetUsers();
|
||||
|
||||
await _modal.ShowAsync();
|
||||
}
|
||||
|
||||
private async Task AddUser() {
|
||||
if (!(await Permissions.HasPermission(Auth.User, Security.AdminPermissions.AddUser))) {
|
||||
await NoAddPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
string errorMessage = null;
|
||||
|
||||
if (_allUsers.Any(user => user.Username == _user.Username)) {
|
||||
errorMessage = "Username is already taken!";
|
||||
}
|
||||
else if (_allUsers.Any(user => user.Email == _user.Email)) {
|
||||
errorMessage = "E-Mail is already taken!";
|
||||
}
|
||||
else if (!_user.PasswordIsValid) {
|
||||
errorMessage = "The password needs to be at least 8 characters long!";
|
||||
}
|
||||
else if (!_user.EmailIsValid) {
|
||||
errorMessage = "Invalid E-Mail address!";
|
||||
}
|
||||
else if (string.IsNullOrWhiteSpace(_user.Username)) {
|
||||
errorMessage = "You need to set a username!";
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(errorMessage)) {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Something went wrong!",
|
||||
Text = errorMessage,
|
||||
Icon = SweetAlertIcon.Error,
|
||||
ShowConfirmButton = false,
|
||||
Timer = 1500
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var user = await Users.AddUser(new User {
|
||||
Username = _user.Username,
|
||||
Email = _user.Email,
|
||||
Password = _user.Password
|
||||
});
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(_user.Group)) {
|
||||
await Permissions.AddPermission(user, _user.Group);
|
||||
}
|
||||
|
||||
await ReloadPage.Invoke();
|
||||
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "New user added!",
|
||||
Icon = SweetAlertIcon.Success,
|
||||
ShowConfirmButton = false,
|
||||
Timer = 1500
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
private async Task NoAddPermissions() {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Unauthorized!",
|
||||
Text = "You don't have the required permissions to add a user!",
|
||||
Icon = SweetAlertIcon.Error
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,306 +0,0 @@
|
||||
@rendermode InteractiveServer
|
||||
|
||||
@using BlazorStrap
|
||||
@using BlazorStrap.Shared.Components.Modal
|
||||
@using static Microsoft.AspNetCore.Components.Web.RenderMode
|
||||
@using BlazorStrap.V5
|
||||
@using CurrieTechnologies.Razor.SweetAlert2
|
||||
@using HopFrame.Database.Models
|
||||
@using HopFrame.Database.Repositories
|
||||
@using HopFrame.Security.Claims
|
||||
@using HopFrame.Web.Model
|
||||
|
||||
<BSModal DataId="edit-user-modal" HideOnValidSubmit="true" IsStaticBackdrop="true" @ref="_modal">
|
||||
<BSForm Model="_user" OnValidSubmit="EditUser">
|
||||
<BSModalHeader>Edit @_user.Username</BSModalHeader>
|
||||
<BSModalContent>
|
||||
<div class="mb-3">
|
||||
<BSLabel>User id</BSLabel>
|
||||
<input type="text" class="form-control" disabled value="@_user.Id"/>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<BSLabel>Created at</BSLabel>
|
||||
<input type="text" class="form-control" disabled value="@_user.CreatedAt"/>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<BSLabel>E-Mail</BSLabel>
|
||||
<BSInput InputType="InputType.Email" @bind-Value="_user.Email" required/>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<BSLabel>Username</BSLabel>
|
||||
<BSInput InputType="InputType.Text" @bind-Value="_user.Username" required/>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<BSLabel>Password</BSLabel>
|
||||
<BSInput InputType="InputType.Password" @bind-Value="_newPassword"/>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<BSLabel>Groups</BSLabel>
|
||||
<BSListGroup>
|
||||
<BSListGroupItem>
|
||||
<BSListGroup IsFlush="true">
|
||||
@foreach (var group in _userGroups) {
|
||||
<BSListGroupItem>
|
||||
<BSButton Color="BSColor.Danger" Size="Size.ExtraSmall" MarginEnd="Margins.Small" OnClick="() => RemoveGroup(group)">
|
||||
<HopIconDisplay Type="HopIconDisplay.HopIcon.Cross"/>
|
||||
</BSButton>
|
||||
|
||||
<span>@group.Name.Replace("group.", "")</span>
|
||||
</BSListGroupItem>
|
||||
}
|
||||
</BSListGroup>
|
||||
</BSListGroupItem>
|
||||
<BSListGroupItem>
|
||||
<div style="display: flex; gap: 20px">
|
||||
<BSInput InputType="InputType.Select" @bind-Value="_selectedGroup">
|
||||
<option selected>Select group</option>
|
||||
|
||||
@foreach (var group in _allGroups) {
|
||||
@if (_userGroups?.All(g => g.Name != group.Name) == true) {
|
||||
<option value="@group.Name">@group.Name.Replace("group.", "")</option>
|
||||
}
|
||||
}
|
||||
</BSInput>
|
||||
<BSButton Color="BSColor.Secondary" OnClick="AddGroup">Add</BSButton>
|
||||
</div>
|
||||
</BSListGroupItem>
|
||||
</BSListGroup>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<BSLabel>Permissions</BSLabel>
|
||||
<BSListGroup>
|
||||
<BSListGroupItem>
|
||||
<BSListGroup IsFlush="true">
|
||||
@foreach (var perm in _user.Permissions.Where(perm => !perm.PermissionName.StartsWith("group."))) {
|
||||
<BSListGroupItem>
|
||||
<BSButton Color="BSColor.Danger" Size="Size.ExtraSmall" MarginEnd="Margins.Small" OnClick="() => RemovePermission(perm)">
|
||||
<HopIconDisplay Type="HopIconDisplay.HopIcon.Cross"/>
|
||||
</BSButton>
|
||||
|
||||
<span>@perm.PermissionName</span>
|
||||
</BSListGroupItem>
|
||||
}
|
||||
</BSListGroup>
|
||||
</BSListGroupItem>
|
||||
<BSListGroupItem>
|
||||
<div style="display: flex; gap: 20px">
|
||||
<BSInput InputType="InputType.Text" @bind-Value="_permissionToAdd"/>
|
||||
<BSButton Color="BSColor.Secondary" OnClick="AddPermission">Add</BSButton>
|
||||
</div>
|
||||
</BSListGroupItem>
|
||||
</BSListGroup>
|
||||
</div>
|
||||
</BSModalContent>
|
||||
<BSModalFooter>
|
||||
<BSButton Target="edit-user-modal">Cancel</BSButton>
|
||||
<BSButton IsSubmit="true" Color="BSColor.Primary">Save</BSButton>
|
||||
</BSModalFooter>
|
||||
</BSForm>
|
||||
</BSModal>
|
||||
|
||||
@inject IUserRepository Users
|
||||
@inject IPermissionRepository Permissions
|
||||
@inject IGroupRepository Groups
|
||||
@inject SweetAlertService Alerts
|
||||
@inject ITokenContext Auth
|
||||
|
||||
@code {
|
||||
[Parameter] public Func<Task> ReloadPage { get; set; }
|
||||
|
||||
private BSModalBase _modal;
|
||||
private User _user;
|
||||
private string _newPassword;
|
||||
|
||||
private IList<PermissionGroup> _userGroups;
|
||||
private IList<PermissionGroup> _allGroups;
|
||||
private string _selectedGroup;
|
||||
private string _permissionToAdd;
|
||||
|
||||
public async Task ShowAsync(User user) {
|
||||
if (!await Permissions.HasPermission(Auth.User, Security.AdminPermissions.EditUser)) {
|
||||
await NoEditPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
_user = user;
|
||||
_userGroups = await Groups.GetUserGroups(user);
|
||||
_allGroups = await Groups.GetPermissionGroups();
|
||||
await _modal.ShowAsync();
|
||||
}
|
||||
|
||||
private async Task AddGroup() {
|
||||
if (!await Permissions.HasPermission(Auth.User, Security.AdminPermissions.EditUser)) {
|
||||
await NoEditPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_selectedGroup)) {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Select a group!",
|
||||
Icon = SweetAlertIcon.Error,
|
||||
ShowConfirmButton = true
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
var group = _allGroups.SingleOrDefault(group => group.Name == _selectedGroup);
|
||||
|
||||
await Permissions.AddPermission(_user, group?.Name);
|
||||
_userGroups.Add(group);
|
||||
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Group added!",
|
||||
Icon = SweetAlertIcon.Success,
|
||||
Timer = 1500,
|
||||
ShowConfirmButton = false
|
||||
});
|
||||
}
|
||||
|
||||
private async Task RemoveGroup(PermissionGroup group) {
|
||||
if (!await Permissions.HasPermission(Auth.User, Security.AdminPermissions.EditUser)) {
|
||||
await NoEditPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
var result = await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Are you sure?",
|
||||
Icon = SweetAlertIcon.Warning,
|
||||
ConfirmButtonText = "Yes",
|
||||
ShowCancelButton = true,
|
||||
ShowConfirmButton = true
|
||||
});
|
||||
|
||||
if (result.IsConfirmed) {
|
||||
await Permissions.RemovePermission(_user, group.Name);
|
||||
_userGroups.Remove(group);
|
||||
StateHasChanged();
|
||||
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Group removed!",
|
||||
Icon = SweetAlertIcon.Success,
|
||||
Timer = 1500,
|
||||
ShowConfirmButton = false
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AddPermission() {
|
||||
if (!await Permissions.HasPermission(Auth.User, Security.AdminPermissions.EditUser)) {
|
||||
await NoEditPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_permissionToAdd)) {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Enter a permission name!",
|
||||
Icon = SweetAlertIcon.Error,
|
||||
ShowConfirmButton = true
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
_user.Permissions.Add(await Permissions.AddPermission(_user, _permissionToAdd));
|
||||
_permissionToAdd = "";
|
||||
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Permission added!",
|
||||
Icon = SweetAlertIcon.Success,
|
||||
Timer = 1500,
|
||||
ShowConfirmButton = false
|
||||
});
|
||||
}
|
||||
|
||||
private async Task RemovePermission(Permission perm) {
|
||||
if (!await Permissions.HasPermission(Auth.User, Security.AdminPermissions.EditUser)) {
|
||||
await NoEditPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
var result = await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Are you sure?",
|
||||
Icon = SweetAlertIcon.Warning,
|
||||
ConfirmButtonText = "Yes",
|
||||
ShowCancelButton = true,
|
||||
ShowConfirmButton = true
|
||||
});
|
||||
|
||||
if (result.IsConfirmed) {
|
||||
await Permissions.RemovePermission(perm.User, perm.PermissionName);
|
||||
_user.Permissions.Remove(perm);
|
||||
StateHasChanged();
|
||||
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Permission removed!",
|
||||
Icon = SweetAlertIcon.Success,
|
||||
Timer = 1500,
|
||||
ShowConfirmButton = false
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async void EditUser() {
|
||||
if (!await Permissions.HasPermission(Auth.User, Security.AdminPermissions.EditUser)) {
|
||||
await NoEditPermissions();
|
||||
return;
|
||||
}
|
||||
|
||||
string errorMessage = null;
|
||||
var validator = new RegisterData {
|
||||
Password = _newPassword,
|
||||
Email = _user.Email
|
||||
};
|
||||
|
||||
var allUsers = await Users.GetUsers();
|
||||
|
||||
if (allUsers.Any(user => user.Username == _user.Username && user.Id != _user.Id)) {
|
||||
errorMessage = "Username is already taken!";
|
||||
}
|
||||
else if (allUsers.Any(user => user.Email == _user.Email && user.Id != _user.Id)) {
|
||||
errorMessage = "E-Mail is already taken!";
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(_newPassword) && !validator.PasswordIsValid) {
|
||||
errorMessage = "The password needs to be at least 8 characters long!";
|
||||
}
|
||||
else if (!validator.EmailIsValid) {
|
||||
errorMessage = "Invalid E-Mail address!";
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(errorMessage)) {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Something went wrong!",
|
||||
Text = errorMessage,
|
||||
Icon = SweetAlertIcon.Error,
|
||||
ShowConfirmButton = false,
|
||||
Timer = 1500
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await Users.UpdateUser(_user);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(_newPassword)) {
|
||||
await Users.ChangePassword(_user, _newPassword);
|
||||
}
|
||||
|
||||
if (ReloadPage is not null)
|
||||
await ReloadPage.Invoke();
|
||||
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "User edited!",
|
||||
Icon = SweetAlertIcon.Success,
|
||||
Timer = 1500,
|
||||
ShowConfirmButton = false
|
||||
});
|
||||
}
|
||||
|
||||
private async Task NoEditPermissions() {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Unauthorized!",
|
||||
Text = "You don't have the required permissions to edit a user!",
|
||||
Icon = SweetAlertIcon.Error
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user