diff --git a/src/VirtoCommerce.WhiteLabeling.ExperienceApi/Authorization/OrganizationLogoAuthorizationHandler.cs b/src/VirtoCommerce.WhiteLabeling.ExperienceApi/Authorization/OrganizationLogoAuthorizationHandler.cs index 10fcdcb..18eb6fc 100644 --- a/src/VirtoCommerce.WhiteLabeling.ExperienceApi/Authorization/OrganizationLogoAuthorizationHandler.cs +++ b/src/VirtoCommerce.WhiteLabeling.ExperienceApi/Authorization/OrganizationLogoAuthorizationHandler.cs @@ -2,12 +2,12 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using VirtoCommerce.CustomerModule.Core.Extensions; -using VirtoCommerce.CustomerModule.Core.Model; using VirtoCommerce.FileExperienceApi.Core.Models; using VirtoCommerce.Platform.Core; using VirtoCommerce.Platform.Core.Security; using VirtoCommerce.Platform.Security.Authorization; using static VirtoCommerce.FileExperienceApi.Core.ModuleConstants.Security.Permissions; +using static VirtoCommerce.WhiteLabeling.Core.ModuleConstants; namespace VirtoCommerce.WhiteLabeling.ExperienceApi.Authorization; @@ -26,26 +26,7 @@ protected override Task HandleRequirementAsync(AuthorizationHandlerContext conte if (!authorized) { - var organizationId = ""; - - switch (context.Resource) - { - case File file when file.OwnerEntityType == nameof(Organization): - organizationId = file.OwnerEntityId; - break; - case string id: - organizationId = id; - break; - } - - if (context.User.GetCurrentOrganizationId() == organizationId) - { - authorized = requirement.Permission switch - { - Create or Update or Delete => IsOrganizationMaintainer(context.User), - _ => false, - }; - } + authorized = CheckRequirement(context, requirement); } if (authorized) @@ -60,6 +41,35 @@ protected override Task HandleRequirementAsync(AuthorizationHandlerContext conte return Task.CompletedTask; } + private static bool CheckRequirement(AuthorizationHandlerContext context, OrganizationLogoAuthorizationRequirement requirement) + { + if (context.Resource is not File file || file.Scope != OrganizationLogoUploadScope) + { + return false; + } + + var authorized = false; + + if (context.User.GetCurrentOrganizationId() == file.OwnerEntityId) + { + authorized = requirement.Permission switch + { + Create or Update or Delete => IsOrganizationMaintainer(context.User), + _ => false, + }; + } + else if (string.IsNullOrEmpty(file.OwnerEntityId)) + { + authorized = requirement.Permission switch + { + Delete => IsOrganizationMaintainer(context.User), + _ => false, + }; + } + + return authorized; + } + private static bool IsOrganizationMaintainer(ClaimsPrincipal principal) { return principal.HasGlobalPermission("xapi:my_organization:edit"); diff --git a/src/VirtoCommerce.WhiteLabeling.ExperienceApi/Commands/ChangeOrganizationLogoCommandBuilder.cs b/src/VirtoCommerce.WhiteLabeling.ExperienceApi/Commands/ChangeOrganizationLogoCommandBuilder.cs index 381a5c2..39cf868 100644 --- a/src/VirtoCommerce.WhiteLabeling.ExperienceApi/Commands/ChangeOrganizationLogoCommandBuilder.cs +++ b/src/VirtoCommerce.WhiteLabeling.ExperienceApi/Commands/ChangeOrganizationLogoCommandBuilder.cs @@ -2,11 +2,14 @@ using GraphQL; using MediatR; using Microsoft.AspNetCore.Authorization; +using VirtoCommerce.CustomerModule.Core.Model; +using VirtoCommerce.FileExperienceApi.Core.Models; using VirtoCommerce.WhiteLabeling.ExperienceApi.Authorization; using VirtoCommerce.WhiteLabeling.ExperienceApi.Models; using VirtoCommerce.WhiteLabeling.ExperienceApi.Schemas; using VirtoCommerce.Xapi.Core.BaseQueries; using static VirtoCommerce.FileExperienceApi.Core.ModuleConstants.Security.Permissions; +using static VirtoCommerce.WhiteLabeling.Core.ModuleConstants; namespace VirtoCommerce.WhiteLabeling.ExperienceApi.Commands; @@ -24,6 +27,14 @@ public ChangeOrganizationLogoCommandBuilder( protected override async Task BeforeMediatorSend(IResolveFieldContext context, ChangeOrganizationLogoCommand request) { await base.BeforeMediatorSend(context, request); - await Authorize(context, request.OrganizationId, new OrganizationLogoAuthorizationRequirement(Update)); + + var organizationFileResource = new File + { + OwnerEntityId = request.OrganizationId, + OwnerEntityType = nameof(Organization), + Scope = OrganizationLogoUploadScope, + }; + + await Authorize(context, organizationFileResource, new OrganizationLogoAuthorizationRequirement(Update)); } }