Skip to content

Commit c277637

Browse files
authored
[PM-29152] Rename VNextSavePolicyCommand to SavePolicyCommand and remove deprecated policy interfaces (#7364)
* Remove deprecated ISavePolicyCommand interface and SavePolicyCommand implementation along with associated tests * Refactor policy validation: remove IPolicyValidator interface and related implementations. Update PolicyServiceCollectionExtensions to eliminate deprecated methods. Adjust policy validator classes to remove IPolicyValidator dependency and streamline validation methods. * Remove IPostSavePolicySideEffect interface and its implementation from the policy validation framework. * Rename VNextSavePolicyCommand to SavePolicyCommand * Continue renaming VNextSavePolicyCommand * Refactor policy validation tests to use SavePolicyModel in ValidateAsync and side effect methods * Refactor policy validators to directly use PolicyUpdate from SavePolicyModel in validation and side effect methods, improving code clarity and reducing method complexity. * Rename test methods in PoliciesControllerTests and VerifyOrganizationDomainCommandTests to better reflect their functionality, enhancing clarity and consistency across the test suite. * Refactor OrganizationUserNotificationPolicyValidator by removing unused methods and simplifying the implementation. Update corresponding tests to reflect these changes. * Remove unnecessary nullable enable directives from policy validator files and update using statements for consistency. * Rename policy validators to handlers * dotnet format
1 parent 8d00c2b commit c277637

56 files changed

Lines changed: 680 additions & 1625 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Api/AdminConsole/Controllers/PoliciesController.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Bit.Core.AdminConsole.Enums;
1111
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationDomains.Interfaces;
1212
using Bit.Core.AdminConsole.OrganizationFeatures.Policies;
13-
using Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyUpdateEvents.Interfaces;
1413
using Bit.Core.AdminConsole.Repositories;
1514
using Bit.Core.Auth.Models.Business.Tokenables;
1615
using Bit.Core.Context;
@@ -32,7 +31,7 @@ public class PoliciesController : Controller
3231
private readonly IOrganizationUserRepository _organizationUserRepository;
3332
private readonly IDataProtectorTokenFactory<OrgUserInviteTokenable> _orgUserInviteTokenDataFactory;
3433
private readonly IPolicyRepository _policyRepository;
35-
private readonly IVNextSavePolicyCommand _vNextSavePolicyCommand;
34+
private readonly ISavePolicyCommand _savePolicyCommand;
3635
private readonly IPolicyQuery _policyQuery;
3736

3837
public PoliciesController(IPolicyRepository policyRepository,
@@ -41,7 +40,7 @@ public PoliciesController(IPolicyRepository policyRepository,
4140
IDataProtectorTokenFactory<OrgUserInviteTokenable> orgUserInviteTokenDataFactory,
4241
IOrganizationHasVerifiedDomainsQuery organizationHasVerifiedDomainsQuery,
4342
IOrganizationRepository organizationRepository,
44-
IVNextSavePolicyCommand vNextSavePolicyCommand,
43+
ISavePolicyCommand savePolicyCommand,
4544
IPolicyQuery policyQuery)
4645
{
4746
_policyRepository = policyRepository;
@@ -50,7 +49,7 @@ public PoliciesController(IPolicyRepository policyRepository,
5049
_organizationRepository = organizationRepository;
5150
_orgUserInviteTokenDataFactory = orgUserInviteTokenDataFactory;
5251
_organizationHasVerifiedDomainsQuery = organizationHasVerifiedDomainsQuery;
53-
_vNextSavePolicyCommand = vNextSavePolicyCommand;
52+
_savePolicyCommand = savePolicyCommand;
5453
_policyQuery = policyQuery;
5554
}
5655

@@ -146,7 +145,7 @@ public async Task<PolicyResponseModel> PutVNext(Guid orgId, PolicyType type, [Fr
146145
{
147146
var savePolicyRequest = await model.ToSavePolicyModelAsync(orgId, type, _currentContext);
148147

149-
var policy = await _vNextSavePolicyCommand.SaveAsync(savePolicyRequest);
148+
var policy = await _savePolicyCommand.SaveAsync(savePolicyRequest);
150149

151150
return new PolicyResponseModel(policy);
152151
}

src/Api/AdminConsole/Public/Controllers/PoliciesController.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Bit.Api.AdminConsole.Public.Models.Response;
77
using Bit.Api.Models.Public.Response;
88
using Bit.Core.AdminConsole.Enums;
9-
using Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyUpdateEvents.Interfaces;
9+
using Bit.Core.AdminConsole.OrganizationFeatures.Policies;
1010
using Bit.Core.AdminConsole.Repositories;
1111
using Bit.Core.Context;
1212
using Microsoft.AspNetCore.Authorization;
@@ -20,16 +20,16 @@ public class PoliciesController : Controller
2020
{
2121
private readonly IPolicyRepository _policyRepository;
2222
private readonly ICurrentContext _currentContext;
23-
private readonly IVNextSavePolicyCommand _vNextSavePolicyCommand;
23+
private readonly ISavePolicyCommand _savePolicyCommand;
2424

2525
public PoliciesController(
2626
IPolicyRepository policyRepository,
2727
ICurrentContext currentContext,
28-
IVNextSavePolicyCommand vNextSavePolicyCommand)
28+
ISavePolicyCommand savePolicyCommand)
2929
{
3030
_policyRepository = policyRepository;
3131
_currentContext = currentContext;
32-
_vNextSavePolicyCommand = vNextSavePolicyCommand;
32+
_savePolicyCommand = savePolicyCommand;
3333
}
3434

3535
/// <summary>
@@ -84,7 +84,7 @@ public async Task<IActionResult> List()
8484
public async Task<IActionResult> Put(PolicyType type, [FromBody] PolicyUpdateRequestModel model)
8585
{
8686
var savePolicyModel = model.ToSavePolicyModel(_currentContext.OrganizationId!.Value, type);
87-
var policy = await _vNextSavePolicyCommand.SaveAsync(savePolicyModel);
87+
var policy = await _savePolicyCommand.SaveAsync(savePolicyModel);
8888

8989
var response = new PolicyResponseModel(policy);
9090
return new JsonResult(response);

src/Core/AdminConsole/OrganizationFeatures/OrganizationDomains/VerifyOrganizationDomainCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using Bit.Core.AdminConsole.Enums;
55
using Bit.Core.AdminConsole.Models.Data;
66
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationDomains.Interfaces;
7+
using Bit.Core.AdminConsole.OrganizationFeatures.Policies;
78
using Bit.Core.AdminConsole.OrganizationFeatures.Policies.Models;
8-
using Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyUpdateEvents.Interfaces;
99
using Bit.Core.Context;
1010
using Bit.Core.Entities;
1111
using Bit.Core.Enums;
@@ -24,7 +24,7 @@ public class VerifyOrganizationDomainCommand(
2424
IEventService eventService,
2525
IGlobalSettings globalSettings,
2626
ICurrentContext currentContext,
27-
IVNextSavePolicyCommand vNextSavePolicyCommand,
27+
ISavePolicyCommand savePolicyCommand,
2828
IMailService mailService,
2929
IOrganizationUserRepository organizationUserRepository,
3030
IOrganizationRepository organizationRepository,
@@ -142,7 +142,7 @@ private async Task EnableSingleOrganizationPolicyAsync(Guid organizationId, IAct
142142
};
143143

144144
var savePolicyModel = new SavePolicyModel(policyUpdate, actingUser);
145-
await vNextSavePolicyCommand.SaveAsync(savePolicyModel);
145+
await savePolicyCommand.SaveAsync(savePolicyModel);
146146
}
147147

148148
private async Task SendVerifiedDomainUserEmailAsync(OrganizationDomain domain)

src/Core/AdminConsole/OrganizationFeatures/Policies/IPolicyValidator.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Core/AdminConsole/OrganizationFeatures/Policies/IPostSavePolicySideEffect.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
using Bit.Core.AdminConsole.Entities;
22
using Bit.Core.AdminConsole.OrganizationFeatures.Policies.Models;
3+
using Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyUpdateEvents.Interfaces;
4+
using Bit.Core.Exceptions;
35

46
namespace Bit.Core.AdminConsole.OrganizationFeatures.Policies;
57

8+
/// <summary>
9+
/// Handles creating or updating organization policies with validation and side effect execution.
10+
/// </summary>
11+
/// <remarks>
12+
/// Workflow:
13+
/// 1. Validates organization can use policies
14+
/// 2. Validates required and dependent policies
15+
/// 3. Runs policy-specific validation (<see cref="IPolicyValidationEvent"/>)
16+
/// 4. Executes pre-save logic (<see cref="IOnPolicyPreUpdateEvent"/>)
17+
/// 5. Saves the policy
18+
/// 6. Logs the event
19+
/// 7. Executes post-save logic (<see cref="IOnPolicyPostUpdateEvent"/>)
20+
/// </remarks>
621
public interface ISavePolicyCommand
722
{
8-
Task<Policy> SaveAsync(PolicyUpdate policy);
9-
1023
/// <summary>
11-
/// FIXME: this is a first pass at implementing side effects after the policy has been saved, which was not supported by the validator pattern.
12-
/// However, this needs to be implemented in a policy-agnostic way rather than building out switch statements in the command itself.
24+
/// Performs the necessary validations, saves the policy and any side effects
1325
/// </summary>
14-
Task<Policy> VNextSaveAsync(SavePolicyModel policyRequest);
26+
/// <param name="policyRequest">Policy data, acting user, and metadata.</param>
27+
/// <returns>The saved policy with updated revision and applied changes.</returns>
28+
/// <exception cref="BadRequestException">
29+
/// Thrown if:
30+
/// - The organization can’t use policies
31+
/// - Dependent policies are missing or block changes
32+
/// - Custom validation fails
33+
/// </exception>
34+
Task<Policy> SaveAsync(SavePolicyModel policyRequest);
1535
}

0 commit comments

Comments
 (0)