Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement auto Deny All seeding and make read-only in app #376

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 97 additions & 37 deletions BLAZAMDatabase/Context/AppDatabaseFactory.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using BLAZAM.Common.Data;
using BLAZAM.Common.Data.Database;
using BLAZAM.Database.Exceptions;
using BLAZAM.Database.Models.Permissions;
using BLAZAM.Logger;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.Net.WebSockets;

namespace BLAZAM.Database.Context
{
Expand Down Expand Up @@ -40,15 +42,71 @@ public AppDatabaseFactory(IConfiguration configuration)
OnFatalError?.Invoke(ex);

}
SeedData();
StartDatabaseCache();

}
private void StartDatabaseCache()
/// <summary>
/// Seeds any data that can't be covered in a migration
/// </summary>
/// <remarks>
/// Each seed should have a check requirement to ensure it is needed
/// </remarks>
/// <exception cref="NotImplementedException"></exception>
private void SeedData()
{
var seedContext = this.CreateDbContext();


SetupDenyAll(seedContext);

}

private void SetupDenyAll(IDatabaseContext seedContext)
{
bool saveRequired = false;
var denyAll = seedContext.AccessLevels.First(x => x.Id == 1);
if (denyAll != null)
{
foreach (var adObjectType in Enum.GetValues(typeof(ActiveDirectoryObjectType)))
{
if ((ActiveDirectoryObjectType)adObjectType != ActiveDirectoryObjectType.All)
{
if (denyAll.ObjectMap.Any(x => x.ObjectType == (ActiveDirectoryObjectType)adObjectType))
{
var eexisingObjectMap = denyAll.ObjectMap.First(x => x.ObjectType == (ActiveDirectoryObjectType)adObjectType);
if (eexisingObjectMap.ObjectAccessLevelId != ObjectAccessLevels.Deny.Id)
{
denyAll.ObjectMap.Remove(eexisingObjectMap);
saveRequired = true;
}
}
if (!denyAll.ObjectMap.Any(x => x.ObjectType == (ActiveDirectoryObjectType)adObjectType && x.ObjectAccessLevel.Id == ObjectAccessLevels.Deny.Id))
{
denyAll.ObjectMap.Add(new()
{
ObjectType = (ActiveDirectoryObjectType)adObjectType,
ObjectAccessLevelId = ObjectAccessLevels.Deny.Id,
});
saveRequired = true;

}
}
}

}
if (saveRequired)
{
seedContext.SaveChanges();
}
}

private void StartDatabaseCache()
{
//Start the database cache
DatabaseCache.Start(this);

DatabaseCache.Start(this);

}

/// <summary>
Expand Down Expand Up @@ -81,9 +139,10 @@ private bool CheckInstallation()
{
Loggers.DatabaseLogger.Error("There was an error checking the installation flag in the database. {@Error}", ex);
}

}
}catch (Exception ex)
}
catch (Exception ex)
{
throw new DatabaseException("The database could not be checked for installation.", ex);
}
Expand Down Expand Up @@ -142,48 +201,49 @@ public IDatabaseContext CreateDbContext()

public async Task<bool> ApplyDatabaseMigrationsAsync(bool force = false)
{
return await Task.Run(() => {
return await Task.Run(() =>
{
return ApplyDatabaseMigrations(force);
});

}
public bool ApplyDatabaseMigrations(bool force = false)
{


try

try
{
using (var context = CreateDbContext())
{
using (var context = CreateDbContext())
{
if (context != null && context.Status == ServiceConnectionState.Up)
if (context.IsSeeded() || force)
if (!context.SeedMismatch)
{
if (context.Database.GetPendingMigrations().Count() > 0)
Migrate(context);
}
else
{
throw new DatabaseException("Database incompatible with current application version.");
}
//context.Database.Migrate();
if (context != null && context.Status == ServiceConnectionState.Up)
if (context.IsSeeded() || force)
if (!context.SeedMismatch)
{
if (context.Database.GetPendingMigrations().Count() > 0)
Migrate(context);
}
else
{
throw new DatabaseException("Database incompatible with current application version.");
}
//context.Database.Migrate();


return true;
}
return true;
}
catch (DatabaseException ex)
{
OnFatalError?.Invoke(ex);
FatalError = ex;
throw ex;
}
catch (Exception ex)
{
Loggers.DatabaseLogger.Error("Database Auto-Update Failed!!!! {@Error}", ex);
throw ex;
}

}
catch (DatabaseException ex)
{
OnFatalError?.Invoke(ex);
FatalError = ex;
throw ex;
}
catch (Exception ex)
{
Loggers.DatabaseLogger.Error("Database Auto-Update Failed!!!! {@Error}", ex);
throw ex;
}



}
Expand Down
3 changes: 3 additions & 0 deletions BLAZAMDatabase/Models/Permissions/AccessLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public class AccessLevel : RecoverableAppDbSetBase, IComparable<AccessLevel>
{
[Required]
public string Name { get; set; }
/// <summary>
/// All the applied object access mappings for Deny,Read
/// </summary>
public List<ObjectAccessMapping> ObjectMap { get; set; } = new();
public List<ActionAccessMapping> ActionMap { get; set; } = new();
public List<FieldAccessMapping> FieldMap { get; set; } = new();
Expand Down
14 changes: 7 additions & 7 deletions BLAZAMGui/UI/Settings/Permissions/EditAccessLevel.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


<MudStack Row=true>
<MudTextField Label="@AppLocalization["Name"]" @bind-Value=WorkingModel.Name />
<MudTextField Disabled=@(WorkingModel.Id==1) Label="@AppLocalization["Name"]" @bind-Value=WorkingModel.Name />


<MudItem>
Expand Down Expand Up @@ -49,15 +49,15 @@
<MudCardHeader>
<MudText Typo="Typo.h4">@name</MudText>
<MudSpacer />
<MudSelect T="int" ValueChanged="(int value)=>SetObjectMapAccess(objectMap,value)" Value="objectMap.ObjectAccessLevelId">
<MudSelect Disabled=@(WorkingModel.Id==1) T="int" ValueChanged="(int value)=>SetObjectMapAccess(objectMap,value)" Value="objectMap.ObjectAccessLevelId">

@foreach (ObjectAccessLevel levelValue in ObjectAccessLevels.Levels)
{
<MudSelectItem Value="levelValue.Id">@AppLocalization[levelValue.Name]</MudSelectItem>
<MudSelectItem Disabled=@(WorkingModel.Id==1) Value="levelValue.Id">@AppLocalization[levelValue.Name]</MudSelectItem>

}
</MudSelect>
<AppCloseButton OnClick="()=>RemoveObjectTypeAccess(objectMap)" />
<AppCloseButton Disabled=@(WorkingModel.Id==1) OnClick="()=>RemoveObjectTypeAccess(objectMap)" />

</MudCardHeader>
<MudCardContent>
Expand Down Expand Up @@ -205,7 +205,7 @@
else
{

<MudText Style="width:471px;">Change @name access to Allow to set permissions</MudText>
<MudText Style="width:471px;">Change @name access to Read to set permissions</MudText>
}
</MudCardContent>
</MudCard>
Expand All @@ -219,10 +219,10 @@
if (Model.Id > 0)
buttonLabel = AppLocalization["Update Access Level"];
}
<MudButton Color=Color.Success Margin=Margin.IsAuto.OnX Disabled=SaveDisabled OnClick="SaveAccessLevel">@buttonLabel</MudButton>
<MudButton Disabled=@(SaveDisabled || WorkingModel.Id==1) Color=Color.Success Margin=Margin.IsAuto.OnX OnClick="SaveAccessLevel">@buttonLabel</MudButton>
@if (WorkingModel.Id != 0)
{
<MudButton Color=Color.Error Margin=Margin.IsAuto.OnX OnClick="DeleteAccessLevel">@AppLocalization["Delete Access Level"]</MudButton>
<MudButton Disabled=@(WorkingModel.Id==1) Color=Color.Error Margin=Margin.IsAuto.OnX OnClick="DeleteAccessLevel">@AppLocalization["Delete Access Level"]</MudButton>
}
@code {

Expand Down
Loading