Skip to content
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
5 changes: 3 additions & 2 deletions website/MyWebApp/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public class AccountController : Controller

private bool HasRole(string role)
{
var roles = HttpContext.Session.GetString("Roles")?.Split(',') ?? Array.Empty<string>();
return roles.Contains(role);
var roles = HttpContext.Session.GetString("Roles");
var roleNames = string.IsNullOrWhiteSpace(roles) ? new[] { "Anonym" } : roles.Split(',');
return roleNames.Contains(role);
}

public AccountController(ApplicationDbContext db, CaptchaService captchaService, IEmailSender emailSender, ILogger<AccountController> logger)
Expand Down
5 changes: 3 additions & 2 deletions website/MyWebApp/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ protected bool CheckDatabase()

protected bool HasRole(string role)
{
var roles = HttpContext.Session.GetString("Roles")?.Split(',') ?? Array.Empty<string>();
return roles.Contains(role);
var roles = HttpContext.Session.GetString("Roles");
var roleNames = string.IsNullOrWhiteSpace(roles) ? new[] { "Anonym" } : roles.Split(',');
return roleNames.Contains(role);
}

protected bool IsAdmin()
Expand Down
5 changes: 3 additions & 2 deletions website/MyWebApp/Controllers/PagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public async Task<IActionResult> Show(string? slug)
{
return NotFound();
}
var roles = HttpContext.Session.GetString("Roles")?.Split(',') ?? Array.Empty<string>();
var roles = HttpContext.Session.GetString("Roles");
var roleNames = string.IsNullOrWhiteSpace(roles) ? new[] { "Anonym" } : roles.Split(',');
if (page.RoleId != null)
{
var allowed = await Db.Roles.AsNoTracking().Where(r => roles.Contains(r.Name)).Select(r => r.Id).ToListAsync();
var allowed = await Db.Roles.AsNoTracking().Where(r => roleNames.Contains(r.Name)).Select(r => r.Id).ToListAsync();
if (!allowed.Contains(page.RoleId.Value))
{
return Unauthorized();
Expand Down
3 changes: 2 additions & 1 deletion website/MyWebApp/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Role>().HasData(
new Role { Id = 1, Name = "Admin" },
new Role { Id = 2, Name = "User" },
new Role { Id = 3, Name = "Moderator" });
new Role { Id = 3, Name = "Moderator" },
new Role { Id = 4, Name = "Anonym" });

// provider specific optimizations
var provider = Database.ProviderName ?? string.Empty;
Expand Down
5 changes: 3 additions & 2 deletions website/MyWebApp/Filters/RoleAuthorizeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public RoleAuthorizeAttribute(params string[] roles)
public void OnAuthorization(AuthorizationFilterContext context)
{
var session = context.HttpContext.Session;
var roles = session.GetString("Roles")?.Split(',') ?? Array.Empty<string>();
if (!_roles.Any(r => roles.Contains(r)))
var roles = session.GetString("Roles");
var roleNames = string.IsNullOrWhiteSpace(roles) ? new[] { "Anonym" } : roles.Split(',');
if (!_roles.Any(r => roleNames.Contains(r)))
{
var returnUrl = context.HttpContext.Request.Path + context.HttpContext.Request.QueryString;
context.Result = new RedirectToActionResult("Login", "Account", new { returnUrl });
Expand Down
6 changes: 5 additions & 1 deletion website/MyWebApp/Services/LayoutService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
private string[] GetRoles()
{
var roles = _accessor.HttpContext?.Session.GetString("Roles");
return string.IsNullOrWhiteSpace(roles) ? Array.Empty<string>() : roles.Split(',');
if (string.IsNullOrWhiteSpace(roles))
{
return new[] { "Anonym" };
}
return roles.Split(',');
}

private async Task<List<int>> GetAllowedPermissionsAsync(ApplicationDbContext db, string[] roles)
Expand Down Expand Up @@ -71,7 +75,7 @@
{
e.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5);
var parts = await db.PageSections.AsNoTracking()
.Where(s => s.Page.Slug == "layout" && s.Zone == "header" && s.PermissionId == null && s.RoleId == null)

Check warning on line 78 in website/MyWebApp/Services/LayoutService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 78 in website/MyWebApp/Services/LayoutService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
.OrderBy(s => s.SortOrder)
.Select(s => s.Html)
.ToListAsync();
Expand All @@ -82,7 +86,7 @@

var allowed = await GetAllowedPermissionsAsync(db, roles);
var query = db.PageSections.AsNoTracking()
.Where(s => s.Page.Slug == "layout" && s.Zone == "header");

Check warning on line 89 in website/MyWebApp/Services/LayoutService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 89 in website/MyWebApp/Services/LayoutService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
query = query.Where(s =>
(s.PermissionId == null || allowed.Contains(s.PermissionId.Value)) &&
(s.RoleId == null || roleIds.Contains(s.RoleId.Value)));
Expand All @@ -101,7 +105,7 @@
{
e.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5);
var parts = await db.PageSections.AsNoTracking()
.Where(s => s.Page.Slug == "layout" && s.Zone == "footer" && s.PermissionId == null && s.RoleId == null)

Check warning on line 108 in website/MyWebApp/Services/LayoutService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 108 in website/MyWebApp/Services/LayoutService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
.OrderBy(s => s.SortOrder)
.Select(s => s.Html)
.ToListAsync();
Expand All @@ -112,7 +116,7 @@

var allowed = await GetAllowedPermissionsAsync(db, roles);
var query = db.PageSections.AsNoTracking()
.Where(s => s.Page.Slug == "layout" && s.Zone == "footer");

Check warning on line 119 in website/MyWebApp/Services/LayoutService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 119 in website/MyWebApp/Services/LayoutService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
query = query.Where(s =>
(s.PermissionId == null || allowed.Contains(s.PermissionId.Value)) &&
(s.RoleId == null || roleIds.Contains(s.RoleId.Value)));
Expand Down
6 changes: 5 additions & 1 deletion website/MyWebApp/Services/TokenRenderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public TokenRenderService(IHttpContextAccessor accessor)
private string[] GetRoles()
{
var roles = _accessor.HttpContext?.Session.GetString("Roles");
return string.IsNullOrWhiteSpace(roles) ? Array.Empty<string>() : roles.Split(',');
if (string.IsNullOrWhiteSpace(roles))
{
return new[] { "Anonym" };
}
return roles.Split(',');
}

private async Task<List<int>> GetAllowedPermissionsAsync(ApplicationDbContext db, string[] roles)
Expand Down
2 changes: 1 addition & 1 deletion website/MyWebApp/Views/Account/Register.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@using Microsoft.AspNetCore.Http
@{
ViewData["Title"] = "Register";
var roles = Context.Session.GetString("Roles") ?? string.Empty;
var roles = Context.Session.GetString("Roles") ?? "Anonym";
bool canSelectType = roles.Contains("Admin") || roles.Contains("Moderator");
}
<h2>Register</h2>
Expand Down
2 changes: 1 addition & 1 deletion website/MyWebApp/Views/Shared/_SectionEditor.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<option value="">(none)</option>
@foreach (var r in roles)
{
<option value="@r.Id" @(Model.RoleId == r.Id ? "selected" : "")>@r.Name</option>
<option value="@r.Id" selected="@(Model.RoleId == r.Id)">@r.Name</option>
}
</select>
</div>
Expand Down
Loading