-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[PM-31885] Consolidate all Send policies to a single policy #7113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
harr1424
wants to merge
14
commits into
main
Choose a base branch
from
tools/PM-31885-SendControls-Policy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2bb7c17
initial send controls
harr1424 6f51c42
update vNext methods and add test coverage for policy validators
harr1424 94b7025
Merge branch 'main' into tools/PM-31885-SendControls-Policy
harr1424 7c4c042
Merge remote-tracking branch 'origin/main' into tools/PM-31885-SendCo…
harr1424 bcc2960
add comments to tests
harr1424 1f7caf8
Merge branch 'main' into tools/PM-31885-SendControls-Policy
harr1424 d52424c
Apply suggestion from @mkincaid-bw
harr1424 235d832
renamne migrations for correct sorting
harr1424 e6bf8e6
Merge branch 'tools/PM-31885-SendControls-Policy' of github.com:bitwa…
harr1424 b0af868
Merge branch 'main' into tools/PM-31885-SendControls-Policy
harr1424 4a0728c
respond to csharp related review comments
harr1424 16cffeb
fix failing lints
harr1424 ea8527e
fix tests
harr1424 783b426
revise policy sync logic
harr1424 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Core/AdminConsole/Models/Data/Organizations/Policies/SendControlsPolicyData.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using System.ComponentModel.DataAnnotations; | ||
|
|
||
| namespace Bit.Core.AdminConsole.Models.Data.Organizations.Policies; | ||
|
|
||
| public class SendControlsPolicyData : IPolicyDataModel | ||
| { | ||
| [Display(Name = "DisableSend")] | ||
| public bool DisableSend { get; set; } | ||
| [Display(Name = "DisableHideEmail")] | ||
| public bool DisableHideEmail { get; set; } | ||
| } |
40 changes: 40 additions & 0 deletions
40
...Console/OrganizationFeatures/Policies/PolicyRequirements/SendControlsPolicyRequirement.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using Bit.Core.AdminConsole.Enums; | ||
| using Bit.Core.AdminConsole.Models.Data.Organizations.Policies; | ||
|
|
||
| namespace Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyRequirements; | ||
|
|
||
| /// <summary> | ||
| /// Policy requirements for the Send Controls policy. | ||
| /// Supersedes DisableSend and SendOptions when the pm-31885-send-controls feature flag is active. | ||
| /// </summary> | ||
| public class SendControlsPolicyRequirement : IPolicyRequirement | ||
| { | ||
| /// <summary> | ||
| /// Indicates whether Send is disabled for the user. If true, the user should not be able to create or edit Sends. | ||
| /// They may still delete existing Sends. | ||
| /// </summary> | ||
| public bool DisableSend { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Indicates whether the user is prohibited from hiding their email from the recipient of a Send. | ||
| /// </summary> | ||
| public bool DisableHideEmail { get; init; } | ||
| } | ||
|
|
||
| public class SendControlsPolicyRequirementFactory : BasePolicyRequirementFactory<SendControlsPolicyRequirement> | ||
| { | ||
| public override PolicyType PolicyType => PolicyType.SendControls; | ||
|
|
||
| public override SendControlsPolicyRequirement Create(IEnumerable<PolicyDetails> policyDetails) | ||
| { | ||
| return policyDetails | ||
| .Select(p => p.GetDataModel<SendControlsPolicyData>()) | ||
| .Aggregate( | ||
| new SendControlsPolicyRequirement(), | ||
| (result, data) => new SendControlsPolicyRequirement | ||
| { | ||
| DisableSend = result.DisableSend || data.DisableSend, | ||
| DisableHideEmail = result.DisableHideEmail || data.DisableHideEmail, | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...AdminConsole/OrganizationFeatures/Policies/PolicyValidators/DisableSendSyncPolicyEvent.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| using Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.AdminConsole.Enums; | ||
| using Bit.Core.AdminConsole.Models.Data.Organizations.Policies; | ||
| using Bit.Core.AdminConsole.OrganizationFeatures.Policies.Models; | ||
| using Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyUpdateEvents.Interfaces; | ||
| using Bit.Core.AdminConsole.Repositories; | ||
| using Bit.Core.Utilities; | ||
|
|
||
| namespace Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyValidators; | ||
|
|
||
| /// <summary> | ||
| /// Syncs changes to the DisableSend policy into the SendControls policy row. | ||
| /// Runs regardless of the pm-31885-send-controls feature flag to ensure SendControls | ||
| /// always stays current for when the flag is eventually enabled. | ||
| /// </summary> | ||
| public class DisableSendSyncPolicyEvent(IPolicyRepository policyRepository) : IOnPolicyPostUpdateEvent | ||
| { | ||
| public PolicyType Type => PolicyType.DisableSend; | ||
|
|
||
| public async Task ExecutePostUpsertSideEffectAsync( | ||
| SavePolicyModel policyRequest, | ||
| Policy postUpsertedPolicyState, | ||
| Policy? previousPolicyState) | ||
| { | ||
| var policyUpdate = policyRequest.PolicyUpdate; | ||
|
|
||
| var sendControlsPolicy = await policyRepository.GetByOrganizationIdTypeAsync( | ||
| policyUpdate.OrganizationId, PolicyType.SendControls) ?? new Policy | ||
| { | ||
| Id = CoreHelpers.GenerateComb(), | ||
| OrganizationId = policyUpdate.OrganizationId, | ||
| Type = PolicyType.SendControls, | ||
| }; | ||
|
|
||
| var sendOptionsPolicy = await policyRepository.GetByOrganizationIdTypeAsync( | ||
| policyUpdate.OrganizationId, PolicyType.SendOptions) ?? new Policy | ||
| { | ||
| Id = CoreHelpers.GenerateComb(), | ||
| OrganizationId = policyUpdate.OrganizationId, | ||
| Type = PolicyType.SendOptions, | ||
| }; | ||
|
|
||
| var sendControlsPolicyData = | ||
| sendControlsPolicy.GetDataModel<SendControlsPolicyData>(); | ||
|
|
||
| var sendOptionsPolicyData = sendOptionsPolicy.GetDataModel<SendOptionsPolicyData>(); | ||
|
|
||
| sendControlsPolicyData.DisableSend = postUpsertedPolicyState.Enabled; | ||
| sendControlsPolicyData.DisableHideEmail = sendOptionsPolicy.Enabled && sendOptionsPolicyData.DisableHideEmail; | ||
| sendControlsPolicy.Enabled = sendControlsPolicyData.DisableSend || sendControlsPolicyData.DisableHideEmail; | ||
| sendControlsPolicy.SetDataModel(sendControlsPolicyData); | ||
|
|
||
| await policyRepository.UpsertAsync(sendControlsPolicy); | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
...dminConsole/OrganizationFeatures/Policies/PolicyValidators/SendControlsSyncPolicyEvent.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| using Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.AdminConsole.Enums; | ||
| using Bit.Core.AdminConsole.Models.Data.Organizations.Policies; | ||
| using Bit.Core.AdminConsole.OrganizationFeatures.Policies.Models; | ||
| using Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyUpdateEvents.Interfaces; | ||
| using Bit.Core.AdminConsole.Repositories; | ||
| using Bit.Core.Utilities; | ||
|
|
||
| namespace Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyValidators; | ||
|
|
||
| /// <summary> | ||
| /// When the pm-31885-send-controls flag is active, syncs changes to the SendControls policy | ||
| /// back into the legacy DisableSend and SendOptions policy rows, enabling safe rollback. | ||
| /// </summary> | ||
| public class SendControlsSyncPolicyEvent( | ||
| IPolicyRepository policyRepository, | ||
| TimeProvider timeProvider) : IOnPolicyPostUpdateEvent | ||
| { | ||
| public PolicyType Type => PolicyType.SendControls; | ||
|
|
||
| public async Task ExecutePostUpsertSideEffectAsync( | ||
| SavePolicyModel policyRequest, | ||
| Policy postUpsertedPolicyState, | ||
| Policy? previousPolicyState) | ||
| { | ||
| var policyUpdate = policyRequest.PolicyUpdate; | ||
|
|
||
| var sendControlsPolicy = await policyRepository.GetByOrganizationIdTypeAsync( | ||
| policyUpdate.OrganizationId, PolicyType.SendControls) ?? new Policy | ||
| { | ||
| Id = CoreHelpers.GenerateComb(), | ||
| OrganizationId = policyUpdate.OrganizationId, | ||
| Type = PolicyType.SendControls, | ||
| }; | ||
|
|
||
| var sendControlsPolicyData = | ||
| sendControlsPolicy.GetDataModel<SendControlsPolicyData>(); | ||
|
|
||
| await UpsertLegacyPolicyAsync( | ||
| policyRequest.PolicyUpdate.OrganizationId, | ||
| PolicyType.DisableSend, | ||
| enabled: postUpsertedPolicyState.Enabled && sendControlsPolicyData.DisableSend, | ||
| policyData: null); | ||
|
|
||
| var sendOptionsData = new SendOptionsPolicyData { DisableHideEmail = sendControlsPolicyData.DisableHideEmail }; | ||
| await UpsertLegacyPolicyAsync( | ||
| policyRequest.PolicyUpdate.OrganizationId, | ||
| PolicyType.SendOptions, | ||
| enabled: postUpsertedPolicyState.Enabled && sendControlsPolicyData.DisableHideEmail, | ||
| policyData: CoreHelpers.ClassToJsonData(sendOptionsData)); | ||
| } | ||
|
|
||
| private async Task UpsertLegacyPolicyAsync( | ||
| Guid organizationId, | ||
| PolicyType type, | ||
| bool enabled, | ||
| string? policyData) | ||
| { | ||
| var existing = await policyRepository.GetByOrganizationIdTypeAsync(organizationId, type); | ||
|
|
||
| var policy = existing ?? new Policy { OrganizationId = organizationId, Type = type, }; | ||
|
|
||
| if (existing == null) | ||
| { | ||
| policy.SetNewId(); | ||
| } | ||
|
|
||
| policy.Enabled = enabled; | ||
| policy.Data = policyData; | ||
| policy.RevisionDate = timeProvider.GetUtcNow().UtcDateTime; | ||
|
|
||
| await policyRepository.UpsertAsync(policy); | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
...AdminConsole/OrganizationFeatures/Policies/PolicyValidators/SendOptionsSyncPolicyEvent.cs
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feedback on the DisableSend version of this class is also relevant here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.AdminConsole.Enums; | ||
| using Bit.Core.AdminConsole.Models.Data.Organizations.Policies; | ||
| using Bit.Core.AdminConsole.OrganizationFeatures.Policies.Models; | ||
| using Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyUpdateEvents.Interfaces; | ||
| using Bit.Core.AdminConsole.Repositories; | ||
| using Bit.Core.Utilities; | ||
|
|
||
| namespace Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyValidators; | ||
|
|
||
| /// <summary> | ||
| /// Syncs changes to the SendOptions policy into the SendControls policy row. | ||
| /// Runs regardless of the pm-31885-send-controls feature flag to ensure SendControls | ||
| /// always stays current for when the flag is eventually enabled. | ||
| /// </summary> | ||
| public class SendOptionsSyncPolicyEvent(IPolicyRepository policyRepository) : IOnPolicyPostUpdateEvent | ||
| { | ||
| public PolicyType Type => PolicyType.SendOptions; | ||
|
|
||
| public async Task ExecutePostUpsertSideEffectAsync( | ||
| SavePolicyModel policyRequest, | ||
| Policy postUpsertedPolicyState, | ||
| Policy? previousPolicyState) | ||
| { | ||
| var policyUpdate = policyRequest.PolicyUpdate; | ||
|
|
||
| var sendControlsPolicy = await policyRepository.GetByOrganizationIdTypeAsync( | ||
| policyUpdate.OrganizationId, PolicyType.SendControls) ?? new Policy | ||
| { | ||
| Id = CoreHelpers.GenerateComb(), | ||
| OrganizationId = policyUpdate.OrganizationId, | ||
| Type = PolicyType.SendControls, | ||
| }; | ||
|
|
||
| var sendControlsPolicyData = | ||
| sendControlsPolicy.GetDataModel<SendControlsPolicyData>(); | ||
|
|
||
| var disableSendPolicyState = await policyRepository.GetByOrganizationIdTypeAsync( | ||
| policyUpdate.OrganizationId, PolicyType.DisableSend); | ||
|
|
||
| sendControlsPolicyData.DisableSend = disableSendPolicyState?.Enabled ?? false; | ||
| sendControlsPolicyData.DisableHideEmail = postUpsertedPolicyState.Enabled && postUpsertedPolicyState.GetDataModel<SendOptionsPolicyData>().DisableHideEmail; | ||
| sendControlsPolicy.Enabled = sendControlsPolicyData.DisableSend || sendControlsPolicyData.DisableHideEmail; | ||
| sendControlsPolicy.SetDataModel(sendControlsPolicyData); | ||
|
|
||
| await policyRepository.UpsertAsync(sendControlsPolicy); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...pi.Test/Tools/SendControlsPolicyIntegrationTests/CreateSendControlsPolicyMigrationTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // TODO remove after testing | ||
| // This file will be included to demonstrate testing performed to code reviewers, but should not be merged | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also need to bump the
RevisionDate(making sure to useTimeProvider).