Finished permission management
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using HopFrame.Security.Claims;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
@@ -19,6 +20,13 @@ public class AuthorizedFilter : IAuthorizationFilter {
|
||||
return;
|
||||
}
|
||||
|
||||
//TODO: Check Permissions
|
||||
if (_permissions.Length == 0) return;
|
||||
|
||||
var permissions = context.HttpContext.User.GetPermissions();
|
||||
|
||||
if (!_permissions.Any(permission => PermissionValidator.IncludesPermission(permission, permissions))) {
|
||||
context.Result = new UnauthorizedResult();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
30
HopFrame.Security/Authorization/PermissionValidator.cs
Normal file
30
HopFrame.Security/Authorization/PermissionValidator.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace HopFrame.Security.Authorization;
|
||||
|
||||
internal static class PermissionValidator {
|
||||
|
||||
/// <summary>
|
||||
/// Checks for the user to have the specified permission<br/>
|
||||
/// Permission system:<br/>
|
||||
/// - "*" -> all rights<br/>
|
||||
/// - "group.[name]" -> group member<br/>
|
||||
/// - "[namespace].[name]" -> single permission<br/>
|
||||
/// - "[namespace].*" -> all permissions in the namespace
|
||||
/// </summary>
|
||||
/// <param name="permission">The permission the user needs</param>
|
||||
/// <param name="permissions">All the permissions the user has (includes group permissions)</param>
|
||||
/// <returns></returns>
|
||||
public static bool IncludesPermission(string permission, string[] permissions) {
|
||||
if (permission == "*") return true;
|
||||
if (permissions.Contains(permission)) return true;
|
||||
|
||||
foreach (var perm in permissions) {
|
||||
if (!perm.EndsWith(".*")) continue;
|
||||
|
||||
var permissionGroup = perm.Replace(".*", "");
|
||||
if (permission.StartsWith(permissionGroup)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user