Adminpage

This commit is contained in:
Friend2868
2023-06-13 23:40:21 +02:00
parent 996cf987c6
commit 3cf17138b9
2 changed files with 140 additions and 41 deletions
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Net.Mail;
namespace Elektrifikatsiya.Models; namespace Elektrifikatsiya.Models;
@@ -10,7 +11,7 @@ public class User
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key] [Key]
public int Id { get; private set; } public int Id { get; set; }
public DateTime LastLoginDate { get; set; } public DateTime LastLoginDate { get; set; }
@@ -18,7 +19,7 @@ public class User
public string Name { get; set; } public string Name { get; set; }
[Required] [Required]
public string PasswordHash { get; private set; } public string PasswordHash { get; set; }
[Required] [Required]
public string Email { get; set; } public string Email { get; set; }
@@ -36,4 +37,8 @@ public class User
Email = email; Email = email;
Role = role; Role = role;
} }
public User CopyUser()
{
return new User(Name, PasswordHash, Email, Role) { Id = Id };
}
} }
@@ -1,15 +1,18 @@
@page "/Admin" @page "/Admin"
@using Blazorise.Components; @using Blazorise.Components;
@using Elektrifikatsiya.Models; @using Elektrifikatsiya.Models;
@using Elektrifikatsiya.Database;
@using System.Net @using System.Net
@using Elektrifikatsiya.Services; @using Elektrifikatsiya.Services;
@using Elektrifikatsiya.Services.Implementations; @using Elektrifikatsiya.Services.Implementations;
@using FluentResults; @using FluentResults;
@using BC = BCrypt.Net.BCrypt;
@inject IDeviceManagmentService DeviceManagmentService @inject IDeviceManagmentService DeviceManagmentService
@inject IMessageService MessageService @inject IMessageService MessageService
@inject IAuthenticationService AuthenthicationService; @inject IAuthenticationService AuthenthicationService;
@inject NavigationManager NavigationManager; @inject NavigationManager NavigationManager;
@inject MainDatabaseContext MainDatabaseContext;
<Div> <Div>
<Row Style="width: 100%; height: 100%" Margin="Margin.Is3.FromTop.OnMobile" Padding="Padding.Is4.FromStart.OnMobile"> <Row Style="width: 100%; height: 100%" Margin="Margin.Is3.FromTop.OnMobile" Padding="Padding.Is4.FromStart.OnMobile">
@@ -95,7 +98,7 @@
</ModalBody> </ModalBody>
<ModalFooter> <ModalFooter>
<Button Color="Color.Secondary" style="border-bottom-left-radius: 3px; border-bottom-right-radius: 3px" Clicked="@HideModalAddUser">Close</Button> <Button Color="Color.Secondary" style="border-bottom-left-radius: 3px; border-bottom-right-radius: 3px" Clicked="@HideModalAddUser">Close</Button>
<Button Color="Color.Primary" style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0" Clicked="@AddButtonPressed">Add</Button> <Button Color="Color.Primary" style="border-bottom-left-radius: 3px; border-bottom-right-radius: 3px" Clicked="@AddButtonPressed">Add</Button>
</ModalFooter> </ModalFooter>
</ModalContent> </ModalContent>
</Modal> </Modal>
@@ -126,6 +129,7 @@
</h2> </h2>
</FieldLabel> </FieldLabel>
</Field> </Field>
<Validation @ref="updateValidation" Validator="ValidationRule.IsNotEmpty">
<Field> <Field>
<FieldLabel> <FieldLabel>
<h5> <h5>
@@ -135,10 +139,12 @@
Here you can change the name of the User Here you can change the name of the User
</Paragraph> </Paragraph>
</FieldLabel> </FieldLabel>
<TextEdit @bind-Text="@SelectedUser.Name" Placeholder="Enter User Name" /> <TextEdit @ref="updateName" @bind-Text="@SelectedUser.Name" Placeholder="Enter User Name" />
</Field> </Field>
</Validation>
<br /> <br />
<br /> <br />
<Validation @ref="updateValidation" Validator="ValidationRule.IsEmail">
<Field> <Field>
<FieldLabel> <FieldLabel>
<h5> <h5>
@@ -148,9 +154,12 @@
Here you can change the E-Mail of a User Here you can change the E-Mail of a User
</Paragraph> </Paragraph>
</FieldLabel> </FieldLabel>
<TextEdit @bind-Text="@SelectedUser.Email" Placeholder="Enter new email" /> <TextEdit @ref="updateEmail" @bind-Text="@SelectedUser.Email" Placeholder="Enter new email" />
</Field> </Field>
</Validation>
<br /> <br />
<br />
<Validation @ref="updateValidation" Validator="ValidatePW">
<Field> <Field>
<FieldLabel> <FieldLabel>
<h5> <h5>
@@ -160,10 +169,9 @@
Here you can change the password of a User Here you can change the password of a User
</Paragraph> </Paragraph>
</FieldLabel> </FieldLabel>
<TextEdit Placeholder="Enter old password" /> <TextEdit @ref="updatePW" Placeholder="Enter new password" />
<br />
<TextEdit Placeholder="Enter new password" />
</Field> </Field>
</Validation>
<br /> <br />
<br /> <br />
<Field> <Field>
@@ -179,14 +187,14 @@
<Dropdown Display="Display.InlineBlock"> <Dropdown Display="Display.InlineBlock">
<DropdownToggle Color="Color.Primary" Width="Width.Is100">Roles</DropdownToggle> <DropdownToggle Color="Color.Primary" Width="Width.Is100">Roles</DropdownToggle>
<DropdownMenu> <DropdownMenu>
<DropdownItem>Admin</DropdownItem> <DropdownItem @onclick="() => SetRoleAdmin()">Admin</DropdownItem>
<DropdownDivider /> <DropdownDivider />
<DropdownItem>User</DropdownItem> <DropdownItem @onclick="() => SetRoleUser()">User</DropdownItem>
</DropdownMenu> </DropdownMenu>
</Dropdown> </Dropdown>
</Field> </Field>
<Column ColumnSize="ColumnSize.IsFull" TextAlignment="TextAlignment.End"> <Column ColumnSize="ColumnSize.IsFull" TextAlignment="TextAlignment.End">
<Button Color="Color.Primary">Save</Button> <Button @onclick="OnSave" Color="Color.Primary">Save</Button>
</Column> </Column>
</Div> </Div>
} }
@@ -199,6 +207,7 @@
List<User> users = new List<User>(); List<User> users = new List<User>();
User? SelectedUser = null; User? SelectedUser = null;
User? UserCopy = null;
bool hideButtonSettings = true; bool hideButtonSettings = true;
bool newpage = true; bool newpage = true;
@@ -207,6 +216,12 @@
private TextEdit? modalAddUserName; private TextEdit? modalAddUserName;
private TextEdit? modalAddUserEmail; private TextEdit? modalAddUserEmail;
private TextEdit? modalAddUserPW; private TextEdit? modalAddUserPW;
private TextEdit? updateName;
private TextEdit? updateEmail;
private TextEdit? updatePW;
bool admin = false;
Validation? updateValidation;
protected override async void OnInitialized() protected override async void OnInitialized()
{ {
@@ -232,6 +247,7 @@
} }
} }
SelectedUser = user; SelectedUser = user;
UserCopy = user.CopyUser();
newpage = false; newpage = false;
} }
@@ -271,6 +287,31 @@
StateHasChanged(); StateHasChanged();
} }
private async Task OnSave()
{
if (UserCopy is not null && SelectedUser is not null)
{
MainDatabaseContext.ChangeTracker.Clear();
UserCopy.Name = updateName.Text.ToString();
UserCopy.Email = updateEmail.Text.ToString();
UserCopy.PasswordHash = BC.HashPassword(updatePW.Text.ToString());
if (admin)
{
UserCopy.Role = Role.Admin;
}
else
{
UserCopy.Role = Role.User;
}
MainDatabaseContext.Update(UserCopy);
await Result.Try(Task () => MainDatabaseContext.SaveChangesAsync());
await MessageService.Success("User updated!");
}
}
private async Task Delete(User contextItem) private async Task Delete(User contextItem)
{ {
bool succ = await MessageService.Confirm($"Do you really want to delete the User \"{contextItem.Name}\"?"); bool succ = await MessageService.Confirm($"Do you really want to delete the User \"{contextItem.Name}\"?");
@@ -284,4 +325,57 @@
NavigationManager.NavigateTo(NavigationManager.Uri, true); NavigationManager.NavigateTo(NavigationManager.Uri, true);
} }
private void SetRoleAdmin()
{
admin = true;
}
private void SetRoleUser()
{
admin = false;
}
public void ValidateName(ValidatorEventArgs e)
{
string? input = Convert.ToString(e.Value);
if (string.IsNullOrWhiteSpace(input))
{
e.Status = ValidationStatus.None;
return;
}
else
{
e.Status = ValidationStatus.Success;
return;
}
}
public void ValidateEmail(ValidatorEventArgs e)
{
string? input = Convert.ToString(e.Value);
if (string.IsNullOrWhiteSpace(input))
{
e.Status = ValidationStatus.None;
return;
}
else
{
e.Status = ValidationStatus.Success;
return;
}
}
public void ValidatePW(ValidatorEventArgs e)
{
string? input = Convert.ToString(e.Value);
if (string.IsNullOrWhiteSpace(input))
{
e.Status = ValidationStatus.None;
return;
}
else
{
e.Status = ValidationStatus.Success;
return;
}
}
} }