Skip to content

Commit

Permalink
Merge pull request #294 from Blazam-App/0.9.1-Gen
Browse files Browse the repository at this point in the history
Fix for possible null exception when computing news items
  • Loading branch information
jacobsen9026 authored Apr 12, 2024
2 parents 0b41841 + 24dcda7 commit 4ad9be4
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 62 deletions.
2 changes: 1 addition & 1 deletion BLAZAM/BLAZAM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<ServerGarbageCollection>false</ServerGarbageCollection>
<AssemblyVersion>0.9.0</AssemblyVersion>
<Version>2024.04.03.1454</Version>
<Version>2024.04.12.2206</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<RootNamespace>BLAZAM</RootNamespace>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
Expand Down
6 changes: 6 additions & 0 deletions BLAZAM/Pages/SignIn.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public async Task<IActionResult> OnPost([FromFormAttribute] LoginRequest req)
try
{
req.IPAddress = HttpContext.Connection.RemoteIpAddress?.ToString();
if (User != null)
{
req.ImpersonatorClaims = User;
}
}
catch (Exception ex)
{
Expand All @@ -67,6 +71,8 @@ public async Task<IActionResult> OnPost([FromFormAttribute] LoginRequest req)
await HttpContext.SignInAsync(result.AuthenticationState.User);
if (result.AuthenticationState.User.Identity?.IsAuthenticated == true)
await AuditLogger.Logon.Login(result.AuthenticationState.User, req.IPAddress);
req.AuthenticationState = null;
req.ImpersonatorClaims = null;
}
return new JsonResult(req);

Expand Down
12 changes: 1 addition & 11 deletions BLAZAMDatabase/Models/Permissions/PermissionDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@ public override int GetHashCode()
{
return Id.ToString().GetHashCode();
}
public override bool Equals(object? obj)
{
if (obj is PermissionDelegate l)
{
if (l.Id == Id || l.DelegateSid == DelegateSid)
{
return true;
}
}
return false;
}


public override string? ToString()
{
Expand Down
2 changes: 1 addition & 1 deletion BLAZAMGui/BLAZAMGui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.28" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.17" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="8.0.3" />
<PackageReference Include="MudBlazor" Version="6.19.1" />
<PackageReference Include="MudBlazor" Version="6.17.0" />
<PackageReference Include="MudBlazor.Markdown" Version="0.1.3" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.3" />
Expand Down
10 changes: 8 additions & 2 deletions BLAZAMGui/UI/Dashboard/Widgets/FavoritesWidget.razor
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@
</MudTooltip>
</CellTemplate>
</TemplateColumn>
<PropertyColumn Property="x=>x.LastChanged" Title="Changed" />

<TemplateColumn Title=@AppLocalization["Changed"]>
<CellTemplate>
@if (context.Item?.LastChanged != null)
{
<MudText>@context.Item.LastChanged?.ToLocalTime()</MudText>
}
</CellTemplate>
</TemplateColumn>
</Columns>
<PagerContent>
<MudDataGridPager T="IDirectoryEntryAdapter" />
Expand Down
3 changes: 2 additions & 1 deletion BLAZAMGui/UI/DirectoryEntryViewHeader.razor
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@
}
<MudHidden Breakpoint="Breakpoint.SmAndDown">
<MudToolBar Class="align-middle" Style="height:32px" DisableGutters Dense>
@if (accountEntry != null && ((accountEntry.Enabled && accountEntry.CanDisable) || (accountEntry.Disabled && accountEntry.CanEnable)))
@if (accountEntry != null)

{
<MudTooltip Text="@AppLocalization[accountEntry.Enabled==true?"Disable":"Enable"]">

<MudToggleIconButton @bind-Toggled="@accountEntry.Enabled"
Icon="@Icons.Material.Filled.PersonOff"
Color="Color.Error"
Disabled=@(!(accountEntry.Enabled && accountEntry.CanDisable) && !(accountEntry.Disabled && accountEntry.CanEnable))
ToggledIcon="@Icons.Material.Filled.Person"
ToggledColor="Color.Success" />
</MudTooltip>
Expand Down
2 changes: 1 addition & 1 deletion BLAZAMGui/UI/Outputs/AppDocumentationButton.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@inject IStringLocalizer<AppLocalization> AppLocalization
@if (!Page.IsNullOrEmpty())
{
<MudTooltip Text=@AppLocalization["Click to view help documentation"] RootClass="d-inline" Class="px-3">
<MudTooltip Text=@AppLocalization["Click to view help documentation"] RootClass="d-inline" Class="pa-4">
<MudFab StartIcon="@Icon"
Href=@("https://docs.blazam.org"+@Page)
Target="_blank"
Expand Down
30 changes: 14 additions & 16 deletions BLAZAMGui/UI/Settings/Permissions/ImpersonateUser.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@inherits AppComponentBase
@using Newtonsoft.Json
@inherits AppComponentBase
@inject IJSRuntime JSRuntime

<form action="" @onsubmit=@AttemptSignIn id="login-form">
Expand All @@ -10,8 +11,6 @@
SearchObjectType="ActiveDirectoryObjectType.User"
@bind-SelectedResult=User />

<MudTextField T="string" name="Impersonation" Text="true" Class="d-none" />
<MudTextField T="string" Text="@User?.SamAccountName" Class="d-none" name="Username"/>

<MudButton Disabled=@attemptingImpersonation
Color=Color.Primary
Expand All @@ -22,40 +21,39 @@

</SettingsField>

@*Hidden Login form to send impersonation login data*@

<input type="text" class="d-none" name="Username" value="@User?.SamAccountName" />
<input type="text" class="d-none" name="Passowrd" value="impersonate" />
<input type="text" class="d-none" name="Impersonation" value="true"/>

</form>
@code {
private LoginRequest imperonationRequest = new(){ Impersonation=true};
public IDirectoryEntryAdapter? User { get; set; }
bool attemptingImpersonation;
async void AttemptSignIn()
{
string? authenticationResult=null;
imperonationRequest.Username = User?.SamAccountName;
imperonationRequest.Password = "impersonate";
LoginRequest? authenticationResult = null;
if (User != null)
{
try
{
authenticationResult = await JSRuntime.InvokeAsync<string>("attemptSignIn", null);
var response = await JSRuntime.InvokeAsync<string>("attemptSignIn", imperonationRequest);
authenticationResult = JsonConvert.DeserializeObject<LoginRequest>(response);

}
catch
catch (Exception ex)
{

Loggers.SystemLogger.Error("Error attempting to impersonate " + User.CanonicalName + "{@Error}", ex);
}
}



if (!authenticationResult.IsNullOrEmpty())
if (authenticationResult!=null)
{
int resultCode = int.Parse(authenticationResult);

attemptingImpersonation = false;

switch ((LoginResultStatus)resultCode)
switch (authenticationResult.AuthenticationResult)
{

case LoginResultStatus.NoUsername:
Expand All @@ -79,7 +77,7 @@

break;
case LoginResultStatus.DeniedLogin:
SnackBarService.Error("You are not authorized to login");
SnackBarService.Error("That account is not authorized to login");

break;
case LoginResultStatus.UnknownFailure:
Expand Down
2 changes: 1 addition & 1 deletion BLAZAMNotifications/BLAZAMNotifications.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MudBlazor" Version="6.19.1" />
<PackageReference Include="MudBlazor" Version="6.17.0" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion BLAZAMServices/AppAuthenticationStateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ public async Task<LoginRequest> Login(LoginRequest loginReq)
settings.DuoEnabled &&
settings.DuoClientSecret != null &&
settings.DuoClientId != null &&
settings.DuoApiHost != null
settings.DuoApiHost != null &&
!loginReq.Impersonation
)
{
var mfaRRedirect = await PerformDuoAuthentication(loginReq);
Expand Down
66 changes: 40 additions & 26 deletions BLAZAMSession/ApplicationNewsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,50 +62,64 @@ private async Task GetAllNewsItems()
}
public List<NewsItem> GetUnreadNewsItems(IApplicationUserState user)
{
var activeItems = activeNewsItems;
var unreadItems = new List<NewsItem>();
foreach(var item in activeItems)
try
{
if (user.ReadNewsItems != null)
var activeItems = activeNewsItems;
var unreadItems = new List<NewsItem>();
foreach (var item in activeItems)
{
if(!user.ReadNewsItems.Any(x=>x.NewsItemId == item.Id))
unreadItems.Add(item);
if(user.ReadNewsItems.Any(r => r.NewsItemId == item.Id && r.NewsItemUpdatedAt < item.UpdatedAt))
unreadItems.Add(item);
if (user?.ReadNewsItems != null)
{
if (!user.ReadNewsItems.Any(x => x.NewsItemId == item.Id))
unreadItems.Add(item);
if (user.ReadNewsItems.Any(r => r.NewsItemId == item.Id && r.NewsItemUpdatedAt < item.UpdatedAt))
unreadItems.Add(item);



}
}
}
// var unreadItems = activeItems.Where(x => user.ReadNewsItems?.Any(r=>r.NewsItemId==x.Id)==false||user.ReadNewsItems?.Any(r=>r.NewsItemId==x.Id&& r.NewsItemUpdatedAt<x.UpdatedAt)==false).ToList();
if (_pollCompleted && user.ReadNewsItems != null)
{
var staleItems = user.ReadNewsItems.Where(x => x.NewsItemId < 100000000000 && !activeItems.Any(a => a.Id == x.NewsItemId)).ToList();
if (staleItems.Count > 0)
// var unreadItems = activeItems.Where(x => user.ReadNewsItems?.Any(r=>r.NewsItemId==x.Id)==false||user.ReadNewsItems?.Any(r=>r.NewsItemId==x.Id&& r.NewsItemUpdatedAt<x.UpdatedAt)==false).ToList();
if (_pollCompleted && user.ReadNewsItems != null)
{
staleItems.ForEach(x =>
var staleItems = user.ReadNewsItems.Where(x => x.NewsItemId < 100000000000 && !activeItems.Any(a => a.Id == x.NewsItemId)).ToList();
if (staleItems.Count > 0)
{
user.ReadNewsItems.Remove(x);
});
user.SaveUserSettings();
staleItems.ForEach(x =>
{
user.ReadNewsItems.Remove(x);
});
user.SaveUserSettings();
}
}
return unreadItems;
}catch (Exception ex)
{
Loggers.SystemLogger.Error("Error while trying to get unread news items for user. {@Error}", ex);
return new();
}
return unreadItems;

}


public List<NewsItem> GetReadNewsItems(IApplicationUserState user)
{
var activeItems = activeNewsItems;
if (user.ReadNewsItems != null)
try
{
var readItems = activeItems.Where(x => user.ReadNewsItems.Any(r => r.NewsItemId == x.Id && r.NewsItemUpdatedAt >= x.UpdatedAt)).ToList();

return readItems;
}
return new();
var activeItems = activeNewsItems;
if (user.ReadNewsItems != null)
{
var readItems = activeItems.Where(x => user.ReadNewsItems.Any(r => r.NewsItemId == x.Id && r.NewsItemUpdatedAt >= x.UpdatedAt)).ToList();

return readItems;
}

return new();

}catch (Exception ex) {
Loggers.SystemLogger.Error("Error while trying to get read news items for user. {@Error}", ex);
return new();
}
}
public void Dispose()
{
Expand Down
2 changes: 1 addition & 1 deletion BLAZAMThemes/BLAZAMThemes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MudBlazor" Version="6.19.1" />
<PackageReference Include="MudBlazor" Version="6.17.0" />
<PackageReference Include="MudBlazor.ThemeManager" Version="1.0.9" />
</ItemGroup>

Expand Down

0 comments on commit 4ad9be4

Please sign in to comment.