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 Authorize(IPermission permission, AuthorizationArgs args, Dictionary 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 PreAuthorize(AuthorizationArgs args, Dictionary state) { return Task.FromResult(AuthorizationProviderResult.NotHandled); } public async Task> QueryDescriptors() { var users = await UserService.Query(); var r = new List(); 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(); } }