Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

1.3 KB
Raw Blame
using FluentValidation;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Domain.Permissions;
using Grand.Infrastructure.Validators;
using Grand.Web.AdminShared.Models.Customers;

namespace Grand.Web.AdminShared.Validators.Customers;

public class CustomerGroupAclUpdateValidator : BaseGrandValidator<CustomerGroupAclUpdateModel>
{
    public CustomerGroupAclUpdateValidator(IEnumerable<IValidatorConsumer<CustomerGroupAclUpdateModel>> validators,
        IGroupService groupService, IPermissionService permissionService)
        : base(validators)
    {
        RuleFor(x => x).CustomAsync(async (x, context, _) =>
        {
            if (!await permissionService.Authorize(StandardPermission.ManageAcl))
                context.AddFailure("You don't have permission to the update");

            var customerGroup = await groupService.GetCustomerGroupById(x.CustomerGroupId);
            if (customerGroup == null)
                context.AddFailure("No customer group found with the specified id");

            var permissionRecord = await permissionService.GetPermissionById(x.Id);
            if (permissionRecord == null)
                context.AddFailure("No permission found with the specified id");
        });
    }
}