Add Authentication stuff

This commit is contained in:
Stone_Red
2023-06-13 22:28:00 +02:00
parent 996cf987c6
commit 9ac40ba0cb
9 changed files with 437 additions and 429 deletions
@@ -1 +0,0 @@

@@ -1,4 +1,5 @@
<Bar Breakpoint="Breakpoint.Desktop" NavigationBreakpoint="Breakpoint.Tablet" ThemeContrast="ThemeContrast.Dark" @using Elektrifikatsiya.Models;
<Bar Breakpoint="Breakpoint.Desktop" NavigationBreakpoint="Breakpoint.Tablet" ThemeContrast="ThemeContrast.Dark"
Mode="BarMode.VerticalInline" CollapseMode="BarCollapseMode.Small"> Mode="BarMode.VerticalInline" CollapseMode="BarCollapseMode.Small">
<BarToggler TextColor="TextColor.Primary" /> <BarToggler TextColor="TextColor.Primary" />
<BarBrand> <BarBrand>
@@ -17,12 +18,16 @@
Dashboard Dashboard
</BarLink> </BarLink>
</BarItem> </BarItem>
<Protected RequiredRole="Role.Admin">
<Authorized>
<BarItem> <BarItem>
<BarLink To="/admin" TextColor="TextColor.White"> <BarLink To="/admin" TextColor="TextColor.White">
<BarIcon Style="color:white" IconName="IconName.ShieldAlt" /> <BarIcon Style="color:white" IconName="IconName.ShieldAlt" />
Admin Admin
</BarLink> </BarLink>
</BarItem> </BarItem>
</Authorized>
</Protected>
<BarItem> <BarItem>
<BarLink To="/settings" TextColor="TextColor.White"> <BarLink To="/settings" TextColor="TextColor.White">
<BarIcon Style="color:white" IconName="IconName.Wrench" /> <BarIcon Style="color:white" IconName="IconName.Wrench" />
@@ -1,5 +1,6 @@
@page "/Admin" @page "/Admin"
@using Blazorise.Components; @using Blazorise.Components;
@using Elektrifikatsiya.Components
@using Elektrifikatsiya.Models; @using Elektrifikatsiya.Models;
@using System.Net @using System.Net
@using Elektrifikatsiya.Services; @using Elektrifikatsiya.Services;
@@ -10,9 +11,11 @@
@inject IMessageService MessageService @inject IMessageService MessageService
@inject IAuthenticationService AuthenthicationService; @inject IAuthenticationService AuthenthicationService;
@inject NavigationManager NavigationManager; @inject NavigationManager NavigationManager;
<Div>
<Row Style="width: 100%; height: 100%" Margin="Margin.Is3.FromTop.OnMobile" Padding="Padding.Is4.FromStart.OnMobile">
<Protected RequiredRole="Role.Admin">
<Authorized>
<Div>
<Row Style="width: 100%; height: 100%" Margin="Margin.Is3.FromTop.OnMobile" Padding="Padding.Is4.FromStart.OnMobile">
<Div Display="Display.Flex.Row.OnDesktop" Margin=" Margin.Is3.FromBottom" Width="Width.Is100"> <Div Display="Display.Flex.Row.OnDesktop" Margin=" Margin.Is3.FromBottom" Width="Width.Is100">
<Column ColumnSize="ColumnSize.Is5.OnDesktop.Is12.OnMobile"> <Column ColumnSize="ColumnSize.Is5.OnDesktop.Is12.OnMobile">
<Bar Breakpoint="Breakpoint.Desktop" <Bar Breakpoint="Breakpoint.Desktop"
@@ -30,10 +33,6 @@
</Button> </Button>
</Column> </Column>
</BarBrand> </BarBrand>
</Bar> </Bar>
<ListView TItem="User" <ListView TItem="User"
Data="@users" Data="@users"
@@ -193,7 +192,12 @@
</Column> </Column>
</Div> </Div>
</Row> </Row>
</Div> </Div>
</Authorized>
<NotAuthorized>
Bro no! this page is only for admins! 😠
</NotAuthorized>
</Protected>
@code { @code {
List<User> users = new List<User>(); List<User> users = new List<User>();
@@ -12,6 +12,7 @@
@inject IDeviceStatusService DeviceStatusService; @inject IDeviceStatusService DeviceStatusService;
@inject IDeviceManagmentService DeviceManagmentService; @inject IDeviceManagmentService DeviceManagmentService;
@inject IEventService EventService; @inject IEventService EventService;
@inject IAuthenticationService AuthenticationService;
@inject IEnergyPriceService EnergyPriceService; @inject IEnergyPriceService EnergyPriceService;
@inject MainDatabaseContext MainDatabaseContext; @inject MainDatabaseContext MainDatabaseContext;
@@ -27,9 +27,9 @@ public partial class Dashboard
} }
} }
protected override void OnInitialized() protected override async Task OnInitializedAsync()
{ {
plugs = DeviceStatusService.GetDevices().ValueOrDefault ?? new List<Device>(); plugs = DeviceManagmentService.GetDevices((await AuthenticationService.GetUserAsync()).ValueOrDefault).ValueOrDefault ?? new List<Device>();
events = EventService.Events; events = EventService.Events;
EventService.OnEventCalled += (__, e) => EventService.OnEventCalled += (__, e) =>
@@ -10,6 +10,7 @@
@inject IDeviceManagmentService DeviceManagmentService @inject IDeviceManagmentService DeviceManagmentService
@inject IMessageService MessageService @inject IMessageService MessageService
@inject IAuthenticationService AuthenticationService
@inject MainDatabaseContext MainDatabaseContext @inject MainDatabaseContext MainDatabaseContext
<Div> <Div>
@@ -157,9 +158,9 @@
Device? SelectedDevice = null; Device? SelectedDevice = null;
Device? DeviceCopy = null; Device? DeviceCopy = null;
protected override void OnInitialized() protected override async Task OnInitializedAsync()
{ {
Result<List<Device>> getDevicesResult = DeviceManagmentService.GetDevices(); Result<List<Device>> getDevicesResult = DeviceManagmentService.GetDevices((await AuthenticationService.GetUserAsync()).ValueOrDefault);
if (getDevicesResult.IsSuccess) if (getDevicesResult.IsSuccess)
{ {
@@ -14,7 +14,7 @@ public interface IDeviceManagmentService
public Result<Device> GetDevice(string macAdress); public Result<Device> GetDevice(string macAdress);
public Result<List<Device>> GetDevices(); public Result<List<Device>> GetDevices(User user);
public Result<List<Device>> GetDevicesInRoom(string room); public Result<List<Device>> GetDevicesInRoom(string room);
@@ -49,7 +49,7 @@ public class DeviceManagmentService : IDeviceManagmentService
return device; return device;
} }
public Result<List<Device>> GetDevices() public Result<List<Device>> GetDevices(User user)
{ {
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices(); Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
@@ -58,7 +58,7 @@ public class DeviceManagmentService : IDeviceManagmentService
return getDevicesResult.ToResult(); return getDevicesResult.ToResult();
} }
return getDevicesResult.Value.ToList(); return getDevicesResult.Value.Where(d => d.User.Id == user.Id || user.Role == Role.Admin).ToList();
} }
public Result<List<Device>> GetDevicesInRoom(string room) public Result<List<Device>> GetDevicesInRoom(string room)
@@ -1,19 +1,18 @@
using Elektrifikatsiya.Models; using Elektrifikatsiya.Models;
using FluentResults;
using System.Reflection; using System.Reflection;
namespace Elektrifikatsiya.Services.Implementations namespace Elektrifikatsiya.Services.Implementations;
public class EventService : IEventService
{ {
public class EventService : IEventService public event EventHandler<EventServiceEventArgs>? OnEventCalled;
{
private readonly ILogger<EventService> logger;
public event EventHandler<EventServiceEventArgs> OnEventCalled;
public List<Event> Events { get; } = new(); public List<Event> Events { get; } = new();
public EventService(ILogger<EventService> logger, IDeviceStatusService deviceStatusService, IDeviceManagmentService deviceManagmentService) public EventService(IDeviceStatusService deviceStatusService, IDeviceManagmentService deviceManagmentService)
{ {
this.logger = logger; List<Device> deviceList = deviceStatusService.GetDevices().ValueOrDefault.Select(x => x.CopyDevice()).ToList();
List<Device> deviceList = deviceManagmentService.GetDevices().ValueOrDefault.Select(x=>x.CopyDevice()).ToList();
deviceStatusService.OnDeviceStatusChanged += (_, e) => deviceStatusService.OnDeviceStatusChanged += (_, e) =>
{ {
@@ -26,20 +25,20 @@ namespace Elektrifikatsiya.Services.Implementations
{ {
PropertyInfo[] properties = typeof(Device).GetProperties(); PropertyInfo[] properties = typeof(Device).GetProperties();
foreach(PropertyInfo property in properties) { foreach (PropertyInfo property in properties)
var currentvalue = property.GetValue(deviceList[i], null); {
var newvalue = property.GetValue(currentDevice, null); object? currentvalue = property.GetValue(deviceList[i], null);
if(newvalue is not null && !newvalue.ToString().Equals(currentvalue.ToString())) object? newvalue = property.GetValue(currentDevice, null);
if (newvalue is not null && !newvalue?.ToString()?.Equals(currentvalue?.ToString()) == true)
{ {
string eventName = $"Plug {deviceList[i].Name} changed: {property.Name}"; string eventName = $"Plug {deviceList[i].Name} changed: {property.Name}";
string description = $"Changed {property.Name} of plug [{currentvalue}] to [{newvalue}]"; string description = $"Changed {property.Name} of plug [{currentvalue}] to [{newvalue}]";
DateTime dateTime = DateTime.Now; DateTime dateTime = DateTime.Now;
Event newEvent = new Event(eventName, description, dateTime, currentDevice); Event newEvent = new Event(eventName, description, dateTime, currentDevice);
Events.Insert(0,newEvent); Events.Insert(0, newEvent);
OnEventCalled?.Invoke(this, new EventServiceEventArgs(newEvent)); OnEventCalled?.Invoke(this, new EventServiceEventArgs(newEvent));
} }
} }
deviceList[i].OverwiteDevice(currentDevice); deviceList[i].OverwiteDevice(currentDevice);
newDevice = false; break; newDevice = false; break;
@@ -51,5 +50,4 @@ namespace Elektrifikatsiya.Services.Implementations
} }
}; };
} }
}
} }