Skip to content

Commit

Permalink
[+] Create App Zones(WIP) CactuseSecurity#2597
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidProgramming committed Nov 8, 2024
1 parent 210579c commit 901a1f2
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mutation newAppZone(
$name: String
$idString: String
$creator: String
) {
insert_modelling_nwgroup(objects: {
name: $name
id_string: $idString
creator: $creator
group_type: 21
}) {
returning {
newId: id
}
}
}
21 changes: 21 additions & 0 deletions roles/lib/files/FWO.Api.Client/Data/ModellingAppZone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using FWO.Api.Data;

namespace FWO.Api.Client.Data
{
public class ModellingAppZone : ModellingAppRole
{
public ModellingAppZone()
{

}

public ModellingAppZone(ModellingAppZone appZone) : base(appZone)
{
Comment = appZone.Comment;
Creator = appZone.Creator;
CreationDate = appZone.CreationDate;
AppServers = appZone.AppServers;
Area = appZone.Area;
}
}
}
4 changes: 4 additions & 0 deletions roles/lib/files/FWO.Api.Client/Queries/ModellingQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public class ModellingQueries : Queries
public static readonly string getHistoryForApp;
public static readonly string addHistoryEntry;

public static readonly string newAppZone;


static ModellingQueries()
{
Expand Down Expand Up @@ -183,6 +185,8 @@ static ModellingQueries()
getHistory = File.ReadAllText(QueryPath + "modelling/getHistory.graphql");
getHistoryForApp = File.ReadAllText(QueryPath + "modelling/getHistoryForApp.graphql");
addHistoryEntry = File.ReadAllText(QueryPath + "modelling/addHistoryEntry.graphql");

newAppZone = File.ReadAllText(QueryPath + "modelling/addNwAppZone.graphql");
}
catch (Exception exception)
{
Expand Down
77 changes: 77 additions & 0 deletions roles/lib/files/FWO.Services/ModellingAppZoneHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using FWO.Api.Client;
using FWO.Api.Data;
using FWO.Config.Api;
using FWO.Api.Client.Data;
using FWO.Api.Client.Queries;
using System.Text.Json;

namespace FWO.Services
{
public class ModellingAppZoneHandler : ModellingHandlerBase
{
public ModellingNamingConvention NamingConvention = new();
public List<ModellingAppZone> AppZones { get; set; } = [];

public ModellingAppZoneHandler(ApiConnection apiConnection, UserConfig userConfig) : base(apiConnection, userConfig)
{

}
public async Task CreateAppZones(int appId)
{
List<ModellingAppServer> appServers = await apiConnection.SendQueryAsync<List<ModellingAppServer>>(ModellingQueries.getAppServers, new { appId = appId });
List<FwoOwner> owners = await apiConnection.SendQueryAsync<List<FwoOwner>>(OwnerQueries.getOwners);

FwoOwner? owner = owners.FirstOrDefault(_ => _.Id == appId);

foreach (ModellingAppServer appServer in appServers)
{
ModellingAppZone appZone = new();
appZone.AppServers.Add(new ModellingAppServerWrapper() { Content = appServer });
ApplyNamingConvention(owner.ExtAppId.ToUpper(), appZone);
await AddAppZoneToDb(appZone);
}
}

private void ApplyNamingConvention(string extAppId, ModellingAppZone appZone)
{
NamingConvention = JsonSerializer.Deserialize<ModellingNamingConvention>(userConfig.ModNamingConvention) ?? new();
appZone.ManagedIdString.NamingConvention = NamingConvention;
appZone.ManagedIdString.SetAppPartFromExtId(extAppId);
appZone.Name = $"{NamingConvention.AppZone}{appZone.ManagedIdString.AppPart}";
}

private async Task AddAppZoneToDb(ModellingAppZone appZone)
{
var azVars = new
{
name = appZone.Name,
idString = appZone.IdString,
creator = "CreateAZObjects"
};

ReturnId[]? returnIds = ( await apiConnection.SendQueryAsync<NewReturning>(ModellingQueries.newAppZone, azVars) ).ReturnIds;

if (returnIds != null)
{
appZone.Id = returnIds[0].NewId;

await LogChange(ModellingTypes.ChangeType.Insert, ModellingTypes.ModObjectType.AppZone, appZone.Id,
$"New App Zone: {appZone.Display()}", appZone.AppId);

foreach (var appServer in appZone.AppServers)
{
var nwobject_nwgroupVars = new
{
nwObjectId = appServer.Content.Id,
nwGroupId = appZone.Id
};

await apiConnection.SendQueryAsync<ReturnId>(ModellingQueries.addNwObjectToNwGroup, nwobject_nwgroupVars);

await LogChange(ModellingTypes.ChangeType.Assign, ModellingTypes.ModObjectType.AppRole, appZone.Id,
$"Added App Server {appServer.Content.Display()} to App Role: {appZone.Display()}", Application.Id);
}
}
}
}
}
8 changes: 7 additions & 1 deletion roles/lib/files/FWO.Services/ModellingHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ public ModellingHandlerBase(ApiConnection apiConnection, UserConfig userConfig,
ReadOnly = readOnly;
IsOwner = isOwner;
}


public ModellingHandlerBase(ApiConnection apiConnection, UserConfig userConfig)
{
this.apiConnection = apiConnection;
this.userConfig = userConfig;
}

public MarkupString DisplayButton(string text, string icon, string iconText = "", string objIcon = "")
{
return DisplayButton(userConfig, text, icon, iconText, objIcon);
Expand Down
14 changes: 13 additions & 1 deletion roles/ui/files/FWO.UI/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage
@using RestSharp
@using FWO.Services
@*luca testing*@
@using FWO.Api.Client.Data

@inject ApiConnection apiConnection
@inject GlobalConfig globalConfig
Expand All @@ -31,11 +33,13 @@
<div class="main">
<div class="content p-4">
@* DON'T DELETE THESE BUTTONS / They might be needed for testing *@
@* <button type="button" @onclick="@(() => ShowMessage("info", "info", MessageType.Info))">info</button>
@* <button type="button" @onclick="@(() => ShowMessage("info", "info", MessageType.Info))">info</button>
<button type="button" @onclick="@(() => ShowMessage("warning", "warning", MessageType.Warning))">warning</button>
<button type="button" @onclick="@(() => ShowMessage("error", "error", MessageType.Error))">error</button>
<button type="button" @onclick="@(() => ShowMessage("success", "success", MessageType.Success))">success</button>
<button type="button" @onclick="@(() => { UIMessageQue.Clear(); UIMessages.Clear(); })">clear</button> *@
@*Luca testing *@
<button type="button" @onclick=test>Create App Zones</button>
<CascadingValue Value="@DisplayMessageInUiFunction">
@Body
</CascadingValue>
Expand Down Expand Up @@ -114,6 +118,14 @@

private Timer? UIMessageQueTimer;

//Luca testing
private async Task test()
{
ModellingAppZone appZone = new();
ModellingAppZoneHandler appZoneHandler = new(apiConnection, userConfig);
await appZoneHandler.CreateAppZones(5);
}

protected override void OnInitialized()
{
user = authenticationStateTask!.Result.User;
Expand Down

0 comments on commit 901a1f2

Please sign in to comment.