1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 05:00:19 -05:00

Remove base class

This commit is contained in:
Thomas Rittson 2025-03-21 14:12:29 +10:00
parent a0ac86888d
commit 645f5fa366
No known key found for this signature in database
GPG Key ID: CDDDA03861C35E27
2 changed files with 23 additions and 24 deletions

View File

@ -3,6 +3,7 @@
using Bit.Core.AdminConsole.OrganizationFeatures.Shared.Authorization; using Bit.Core.AdminConsole.OrganizationFeatures.Shared.Authorization;
using Bit.Core.Context; using Bit.Core.Context;
using Bit.Core.Enums; using Bit.Core.Enums;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
namespace Bit.Core.AdminConsole.OrganizationFeatures; namespace Bit.Core.AdminConsole.OrganizationFeatures;
@ -10,23 +11,34 @@ namespace Bit.Core.AdminConsole.OrganizationFeatures;
public class ManageUsersRequirement : IOrganizationRequirement; public class ManageUsersRequirement : IOrganizationRequirement;
public class AdminConsoleRequirementsHandler(ICurrentContext currentContext, IHttpContextAccessor httpContextAccessor) public class AdminConsoleRequirementsHandler(ICurrentContext currentContext, IHttpContextAccessor httpContextAccessor)
: OrganizationRequirementHandler(currentContext, httpContextAccessor) : AuthorizationHandler<IOrganizationRequirement>
{ {
protected override async Task<bool> HandleOrganizationRequirementAsync(IOrganizationRequirement requirement, protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context,
Guid organizationId, CurrentContextOrganization? organization) IOrganizationRequirement requirement)
{ {
var organizationId = httpContextAccessor.GetOrganizationId();
if (organizationId is null)
{
return;
}
var organization = currentContext.GetOrganization(organizationId.Value);
var authorized = requirement switch var authorized = requirement switch
{ {
ManageUsersRequirement => await ManageUsersAsync(organizationId, organization), ManageUsersRequirement => await ManageUsersAsync(organizationId.Value, organization),
_ => false _ => false
}; };
return authorized; if (authorized)
{
context.Succeed(requirement);
}
} }
private async Task<bool> ManageUsersAsync(Guid organizationId, CurrentContextOrganization? organization) private async Task<bool> ManageUsersAsync(Guid organizationId, CurrentContextOrganization? organization)
=> organization is => organization is
{ Type: OrganizationUserType.Owner or OrganizationUserType.Admin } or { Type: OrganizationUserType.Owner or OrganizationUserType.Admin } or
{ Permissions.ManageUsers: true } { Permissions.ManageUsers: true }
|| await IsProviderForOrganizationAsync(organizationId); || await currentContext.ProviderUserForOrgAsync(organizationId);
} }

View File

@ -1,6 +1,5 @@
#nullable enable #nullable enable
using Bit.Core.Context;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
@ -9,33 +8,21 @@ namespace Bit.Core.AdminConsole.OrganizationFeatures.Shared.Authorization;
public interface IOrganizationRequirement : IAuthorizationRequirement; public interface IOrganizationRequirement : IAuthorizationRequirement;
public abstract class OrganizationRequirementHandler(ICurrentContext currentContext, IHttpContextAccessor httpContextAccessor) : AuthorizationHandler<IOrganizationRequirement> public static class OrganizationRequirementHelpers
{ {
protected abstract Task<bool> HandleOrganizationRequirementAsync(IOrganizationRequirement requirement, Guid organizationId, CurrentContextOrganization? organization); public static Guid? GetOrganizationId(this IHttpContextAccessor httpContextAccessor)
protected async Task<bool> IsProviderForOrganizationAsync(Guid organizationId) =>
await currentContext.ProviderUserForOrgAsync(organizationId);
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, IOrganizationRequirement requirement)
{ {
if (httpContextAccessor.HttpContext is null) if (httpContextAccessor.HttpContext is null)
{ {
return; return null;
} }
httpContextAccessor.HttpContext.GetRouteData().Values.TryGetValue("orgId", out var orgIdParam); httpContextAccessor.HttpContext.GetRouteData().Values.TryGetValue("orgId", out var orgIdParam);
if (!Guid.TryParse(orgIdParam?.ToString(), out var orgId)) if (!Guid.TryParse(orgIdParam?.ToString(), out var orgId))
{ {
// No orgId supplied, unable to authorize return null;
return;
} }
var organization = currentContext.GetOrganization(orgId); return orgId;
var authorized = await HandleOrganizationRequirementAsync(requirement, orgId, organization);
if (authorized)
{
context.Succeed(requirement);
}
} }
} }