You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.7 KiB
64 lines
1.7 KiB
using Connected.Middleware;
|
|
using Connected.Security.Identity;
|
|
using Connected.Security.Permissions;
|
|
using System.Collections.Immutable;
|
|
|
|
namespace Connected.Security.Authorization.Middleware;
|
|
|
|
internal class UserAuthorizationMiddleware : MiddlewareComponent, IAuthorizationMiddleware
|
|
{
|
|
public UserAuthorizationMiddleware(IUserService userService)
|
|
{
|
|
UserService = userService;
|
|
}
|
|
|
|
public string Id => "Users";
|
|
|
|
public IUserService UserService { get; }
|
|
|
|
public Task<AuthorizationProviderResult> Authorize(IPermission permission, AuthorizationArgs args, Dictionary<string, object> state)
|
|
{
|
|
if (!string.Equals(args.User.ToString(), permission.Evidence, StringComparison.OrdinalIgnoreCase))
|
|
return Task.FromResult(AuthorizationProviderResult.NotHandled);
|
|
|
|
switch (permission.Value)
|
|
{
|
|
case PermissionValue.NotSet:
|
|
return Task.FromResult(AuthorizationProviderResult.NotHandled);
|
|
case PermissionValue.Allow:
|
|
return Task.FromResult(AuthorizationProviderResult.Success);
|
|
case PermissionValue.Deny:
|
|
return Task.FromResult(AuthorizationProviderResult.Fail);
|
|
default:
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
|
|
public Task<AuthorizationProviderResult> PreAuthorize(AuthorizationArgs args, Dictionary<string, object> state)
|
|
{
|
|
return Task.FromResult(AuthorizationProviderResult.NotHandled);
|
|
}
|
|
|
|
public async Task<ImmutableList<IPermissionSchemaDescriptor>> QueryDescriptors()
|
|
{
|
|
var users = await UserService.Query();
|
|
var r = new List<IPermissionSchemaDescriptor>();
|
|
|
|
foreach (var i in users)
|
|
{
|
|
r.Add(new PermissionSchemaDescriptor
|
|
{
|
|
Id = i.Id.ToString(),
|
|
Title = i.DisplayName(),
|
|
Description = i.Email
|
|
/*
|
|
* TODO: handle avatar
|
|
*/
|
|
|
|
});
|
|
}
|
|
|
|
return r.ToImmutableList();
|
|
}
|
|
}
|