mirror of
https://github.com/Stefan-5422/-T5-Elektrifikatsiya.git
synced 2026-09-04 00:45:58 +02:00
Add Authentication stuff
This commit is contained in:
@@ -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">
|
||||
<BarToggler TextColor="TextColor.Primary" />
|
||||
<BarBrand>
|
||||
@@ -17,12 +18,16 @@
|
||||
Dashboard
|
||||
</BarLink>
|
||||
</BarItem>
|
||||
<Protected RequiredRole="Role.Admin">
|
||||
<Authorized>
|
||||
<BarItem>
|
||||
<BarLink To="/admin" TextColor="TextColor.White">
|
||||
<BarIcon Style="color:white" IconName="IconName.ShieldAlt" />
|
||||
Admin
|
||||
</BarLink>
|
||||
</BarItem>
|
||||
</Authorized>
|
||||
</Protected>
|
||||
<BarItem>
|
||||
<BarLink To="/settings" TextColor="TextColor.White">
|
||||
<BarIcon Style="color:white" IconName="IconName.Wrench" />
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
@page "/Admin"
|
||||
@using Blazorise.Components;
|
||||
@using Elektrifikatsiya.Components
|
||||
@using Elektrifikatsiya.Models;
|
||||
@using System.Net
|
||||
@using Elektrifikatsiya.Services;
|
||||
@@ -10,9 +11,11 @@
|
||||
@inject IMessageService MessageService
|
||||
@inject IAuthenticationService AuthenthicationService;
|
||||
@inject NavigationManager NavigationManager;
|
||||
|
||||
<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">
|
||||
<Column ColumnSize="ColumnSize.Is5.OnDesktop.Is12.OnMobile">
|
||||
<Bar Breakpoint="Breakpoint.Desktop"
|
||||
@@ -30,10 +33,6 @@
|
||||
</Button>
|
||||
</Column>
|
||||
</BarBrand>
|
||||
|
||||
|
||||
|
||||
|
||||
</Bar>
|
||||
<ListView TItem="User"
|
||||
Data="@users"
|
||||
@@ -194,6 +193,11 @@
|
||||
</Div>
|
||||
</Row>
|
||||
</Div>
|
||||
</Authorized>
|
||||
<NotAuthorized>
|
||||
Bro no! this page is only for admins! 😠
|
||||
</NotAuthorized>
|
||||
</Protected>
|
||||
|
||||
@code {
|
||||
List<User> users = new List<User>();
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
@inject IDeviceStatusService DeviceStatusService;
|
||||
@inject IDeviceManagmentService DeviceManagmentService;
|
||||
@inject IEventService EventService;
|
||||
@inject IAuthenticationService AuthenticationService;
|
||||
@inject IEnergyPriceService EnergyPriceService;
|
||||
@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;
|
||||
|
||||
EventService.OnEventCalled += (__, e) =>
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
@inject IDeviceManagmentService DeviceManagmentService
|
||||
@inject IMessageService MessageService
|
||||
@inject IAuthenticationService AuthenticationService
|
||||
@inject MainDatabaseContext MainDatabaseContext
|
||||
|
||||
<Div>
|
||||
@@ -157,9 +158,9 @@
|
||||
Device? SelectedDevice = 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)
|
||||
{
|
||||
|
||||
@@ -14,7 +14,7 @@ public interface IDeviceManagmentService
|
||||
|
||||
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);
|
||||
|
||||
|
||||
+2
-2
@@ -49,7 +49,7 @@ public class DeviceManagmentService : IDeviceManagmentService
|
||||
return device;
|
||||
}
|
||||
|
||||
public Result<List<Device>> GetDevices()
|
||||
public Result<List<Device>> GetDevices(User user)
|
||||
{
|
||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||
|
||||
@@ -58,7 +58,7 @@ public class DeviceManagmentService : IDeviceManagmentService
|
||||
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)
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
using Elektrifikatsiya.Models;
|
||||
using FluentResults;
|
||||
|
||||
using System.Reflection;
|
||||
|
||||
namespace Elektrifikatsiya.Services.Implementations
|
||||
{
|
||||
namespace Elektrifikatsiya.Services.Implementations;
|
||||
|
||||
public class EventService : IEventService
|
||||
{
|
||||
private readonly ILogger<EventService> logger;
|
||||
public event EventHandler<EventServiceEventArgs> OnEventCalled;
|
||||
public event EventHandler<EventServiceEventArgs>? OnEventCalled;
|
||||
|
||||
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 = deviceManagmentService.GetDevices().ValueOrDefault.Select(x=>x.CopyDevice()).ToList();
|
||||
List<Device> deviceList = deviceStatusService.GetDevices().ValueOrDefault.Select(x => x.CopyDevice()).ToList();
|
||||
|
||||
deviceStatusService.OnDeviceStatusChanged += (_, e) =>
|
||||
{
|
||||
@@ -26,10 +25,11 @@ namespace Elektrifikatsiya.Services.Implementations
|
||||
{
|
||||
PropertyInfo[] properties = typeof(Device).GetProperties();
|
||||
|
||||
foreach(PropertyInfo property in properties) {
|
||||
var currentvalue = property.GetValue(deviceList[i], null);
|
||||
var newvalue = property.GetValue(currentDevice, null);
|
||||
if(newvalue is not null && !newvalue.ToString().Equals(currentvalue.ToString()))
|
||||
foreach (PropertyInfo property in properties)
|
||||
{
|
||||
object? currentvalue = property.GetValue(deviceList[i], null);
|
||||
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 description = $"Changed {property.Name} of plug [{currentvalue}] to [{newvalue}]";
|
||||
@@ -39,7 +39,6 @@ namespace Elektrifikatsiya.Services.Implementations
|
||||
Events.Insert(0, newEvent);
|
||||
OnEventCalled?.Invoke(this, new EventServiceEventArgs(newEvent));
|
||||
}
|
||||
|
||||
}
|
||||
deviceList[i].OverwiteDevice(currentDevice);
|
||||
newDevice = false; break;
|
||||
@@ -52,4 +51,3 @@ namespace Elektrifikatsiya.Services.Implementations
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user