initial permissions based webfront access implementation

This commit is contained in:
RaidMax
2022-04-04 22:16:40 -05:00
parent dc97956bc3
commit 19bd47d0f4
8 changed files with 125 additions and 41 deletions

View File

@ -30,6 +30,7 @@ namespace SharedLibraryCore
private static string SocialTitle;
protected readonly DatabaseContext Context;
protected List<Page> Pages;
protected List<string> PermissionsSet;
public BaseController(IManager manager)
{
@ -43,7 +44,6 @@ namespace SharedLibraryCore
SocialTitle = AppConfig.SocialLinkTitle;
}
Pages = Manager.GetPageList().Pages
.Select(page => new Page
{
@ -135,6 +135,11 @@ namespace SharedLibraryCore
var claimsIdentity = new ClaimsIdentity(claims, "login");
SignInAsync(new ClaimsPrincipal(claimsIdentity)).Wait();
}
if (AppConfig.PermissionSets.ContainsKey(Client.Level.ToString()))
{
PermissionsSet = AppConfig.PermissionSets[Client.Level.ToString()];
}
var communityName = AppConfig.CommunityInformation?.Name;
var shouldUseCommunityName = !string.IsNullOrWhiteSpace(communityName)
@ -160,4 +165,4 @@ namespace SharedLibraryCore
base.OnActionExecuting(context);
}
}
}
}

View File

@ -115,7 +115,7 @@ namespace SharedLibraryCore.Configuration
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_ENABLE_COLOR_CODES")]
public bool EnableColorCodes { get; set; }
[ConfigurationIgnore] public string IngameAccentColorKey { get; set; } = "Cyan";
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_AUTOMESSAGE_PERIOD")]
@ -144,6 +144,16 @@ namespace SharedLibraryCore.Configuration
TimeSpan.FromDays(30)
};
public Dictionary<string, List<string>> PermissionSets { get; set; } = new()
{
{ Permission.Trusted.ToString(), new List<string> { "*" } },
{ Permission.Moderator.ToString(), new List<string> { "*" } },
{ Permission.Administrator.ToString(), new List<string> { "*" } },
{ Permission.SeniorAdmin.ToString(), new List<string> { "*" } },
{ Permission.Owner.ToString(), new List<string> { "*" } },
{ Permission.Console.ToString(), new List<string> { "*" } }
};
[ConfigurationIgnore]
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_PRESET_BAN_REASONS")]
public Dictionary<string, string> PresetPenaltyReasons { get; set; } = new Dictionary<string, string>

View File

@ -525,6 +525,45 @@ namespace SharedLibraryCore
return new TimeSpan(1, 0, 0);
}
public static bool HasPermission<TEntity, TPermission>(this IEnumerable<string> permissionsSet, TEntity entity,
TPermission permission) where TEntity : Enum where TPermission : Enum
{
return permissionsSet?.Any(raw =>
{
if (raw == "*")
{
return true;
}
var split = raw.Split(".");
if (split.Length != 2)
{
return false;
}
if (!Enum.TryParse(typeof(TEntity), split[0], out var e))
{
return false;
}
if (!Enum.TryParse(typeof(TPermission), split[1], out var p))
{
return false;
}
return (e?.Equals(entity) ?? false) && (p?.Equals(permission) ?? false);
}) ?? false;
}
public static bool HasPermission<TEntity, TPermission>(this ApplicationConfiguration appConfig,
Permission permissionLevel, TEntity entity,
TPermission permission) where TEntity : Enum where TPermission : Enum
{
return appConfig.PermissionSets.ContainsKey(permissionLevel.ToString()) &&
HasPermission(appConfig.PermissionSets[permissionLevel.ToString()], entity, permission);
}
/// <summary>
/// returns a list of penalty types that should be shown across all profiles
/// </summary>