Added permissions to users controller and the client.

This commit is contained in:
Masu-Baumgartner
2024-10-07 16:37:18 +02:00
parent bb29177e41
commit 19afc5d055
11 changed files with 101 additions and 19 deletions

View File

@@ -25,6 +25,8 @@ public class IdentityService
ApiClient = apiClient;
}
#region Login / Logout
public async Task Check()
{
try
@@ -56,4 +58,42 @@ public class IdentityService
await CookieService.SetValue("token", "", 30);
await Check();
}
#endregion
public bool HasPermission(string requiredPermission)
{
// Check for wildcard permission
if (Permissions.Contains("*"))
return true;
var requiredSegments = requiredPermission.Split('.');
// Check if the user has the exact permission or a wildcard match
foreach (var permission in Permissions)
{
var permissionSegments = permission.Split('.');
// Iterate over the segments of the required permission
for (var i = 0; i < requiredSegments.Length; i++)
{
// If the current segment matches or is a wildcard, continue to the next segment
if (i < permissionSegments.Length && requiredSegments[i] == permissionSegments[i] ||
permissionSegments[i] == "*")
{
// If we've reached the end of the permissionSegments array, it means we've found a match
if (i == permissionSegments.Length - 1)
return true; // Found an exact match or a wildcard match
}
else
{
// If we reach here, it means the segments don't match and we break out of the loop
break;
}
}
}
// No matching permission found
return false;
}
}