Skip to content
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

[EC-377] Transition Policy service into providing observables #3259

Merged
Merged
Show file tree
Hide file tree
Changes from 67 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
fbaf57c
Added abstractions for PolicyApiService and PolicyService
r-tome Jul 28, 2022
8155a61
Added implementations for PolicyApiService and PolicyService
r-tome Jul 28, 2022
375fd3a
Updated all references to new PolicyApiService and PolicyService
r-tome Jul 28, 2022
454e212
Deleted old PolicyService abstraction and implementation
r-tome Jul 28, 2022
c7f554b
Fixed CLI import path for policy.service
r-tome Jul 29, 2022
386ce3a
Fixed main.background.ts policyApiService dependency for policyService
r-tome Jul 29, 2022
62d843f
Ran prettier
r-tome Jul 29, 2022
007954b
Updated policy-api.service with the correct imports
r-tome Jul 29, 2022
6076ccb
[EC-377] Removed methods from StateService that read policies
r-tome Aug 2, 2022
2be260b
[EC-377] Updated policy service getAll method to use observable colle…
r-tome Aug 2, 2022
c4ba9ab
[EC-377] Added first unit tests for policy service
r-tome Aug 2, 2022
5d4eac6
[EC-377] Added more unit tests for Policy Service
r-tome Aug 3, 2022
6486a3a
[EC-376] Sorted methods order in PolicyApiService
r-tome Aug 4, 2022
7367203
[EC-376] Removed unused clearCache method from PolicyService
r-tome Aug 4, 2022
aaec1cb
[EC-376] Added upsert method to PolicyService
r-tome Aug 4, 2022
fe753f5
[EC-376] PolicyApiService putPolicy method now upserts data to Policy…
r-tome Aug 4, 2022
0547a83
[EC-377] Merge branch 'EC-376-split-out-api-methods-into-api-service'…
r-tome Aug 4, 2022
a431247
[EC-377] Removed tests for deleted clearCache method
r-tome Aug 4, 2022
270863c
[EC-377] Added unit test for PolicyService.upsert
r-tome Aug 4, 2022
9a7026d
Merge remote-tracking branch 'origin/master' into EC-377-transition-p…
r-tome Aug 8, 2022
3d37a30
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Aug 8, 2022
25e2a31
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Aug 10, 2022
0d8eed9
[EC-377] Updated references to state service observables
r-tome Aug 10, 2022
775ff34
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Aug 11, 2022
1372ebf
[EC-377] Removed getAll method from PolicyService and refactored comp…
r-tome Aug 11, 2022
2399f10
[EC-377] Updated components to use concatMap instead of async subscribe
r-tome Aug 11, 2022
a9a804e
[EC-377] Removed getPolicyForOrganization from policyApiService
r-tome Aug 17, 2022
91bc630
[EC-377] Updated policyAppliesToUser to return observable collection
r-tome Aug 17, 2022
82a4e1d
[EC-377] Changed policyService.policyAppliesToUser to return observable
r-tome Aug 18, 2022
2f65298
[EC-377] Fixed browser settings.component.ts getting vault timeout
r-tome Aug 18, 2022
6b7d40c
Updated people.component.ts to get ResetPassword policy through a sub…
r-tome Aug 18, 2022
4295830
[EC-377] Changed passwordGenerationService.getOptions to return obser…
r-tome Aug 19, 2022
8bd62e2
[EC-377] Fixed CLI generate.command.ts getting enforcePasswordGenerat…
r-tome Aug 19, 2022
00fccdf
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Aug 22, 2022
429d3af
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Aug 25, 2022
af45e8c
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Aug 29, 2022
1ed7b52
[EC-377] Fixed eslint errors on rxjs
r-tome Aug 29, 2022
68d9d4b
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Aug 30, 2022
e979062
[EC-377] Reverted changes on passwordGeneration.service and vaultTime…
r-tome Aug 30, 2022
b05cbb9
[EC-377] Removed eslint disable on web/vault/add-edit-component
r-tome Aug 30, 2022
8aadf8f
[EC-377] Changed AccountData.policies to TemporaryDataEncryption
r-tome Aug 30, 2022
6db74e9
[EC-377] Updated import.component to be reactive to policyAppliesToUser$
r-tome Aug 30, 2022
197e349
[EC-377] Updated importBlockedByPolicy$
r-tome Aug 30, 2022
aae9446
[EC-377] Fixed missing rename
r-tome Aug 30, 2022
f1d805e
[EC-377] Updated policyService.masterPasswordPolicyOptions to return …
r-tome Aug 30, 2022
53ebeee
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Aug 30, 2022
fdbd44e
[EC-377] Fixed vaultTimeout imports from merge
r-tome Aug 30, 2022
0c7013a
[EC-377] Reverted call to passwordGenerationService.getOptions
r-tome Aug 30, 2022
2202e61
[EC-377] Reverted call to enforcePasswordGeneratorPoliciesOnOptions
r-tome Aug 30, 2022
7ddc7e4
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Aug 31, 2022
6e91825
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Sep 16, 2022
0ce5213
[EC-377] Removed unneeded ngOnDestroy
r-tome Sep 16, 2022
5502112
Apply suggestions from code review
r-tome Sep 16, 2022
1559eb1
Merge branch 'EC-377-transition-policy-service-into-providing-observa…
r-tome Sep 16, 2022
52a1c42
[EC-377] Fixed login.component.ts and register.component.ts
r-tome Sep 16, 2022
909da7e
[EC-377] Updated PolicyService to update vaultTimeout
r-tome Sep 16, 2022
c617fba
[EC-377] Updated PolicyService dependencies
r-tome Sep 16, 2022
e5b625f
[EC-377] Renamed policyAppliesToUser to policyAppliesToActiveUser
r-tome Sep 19, 2022
22f73df
[EC-377] VaultTimeoutSettings service now gets the vault timeout dire…
r-tome Sep 19, 2022
d585998
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Sep 19, 2022
3aa20b9
[EC-377] Fixed unit tests by removing unneeded vaultTimeoutSettingsSe…
r-tome Sep 19, 2022
e442163
[EC-377] Set getDecryptedPolicies and setDecryptedPolicies as deprecated
r-tome Sep 20, 2022
1c8f121
[EC-377] Set PolicyService.getAll as deprecated and updated to use pr…
r-tome Sep 20, 2022
3274da9
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
mimartin12 Sep 21, 2022
3e32142
[EC-565] Reverted unintended change to vaultTimeoutSettings that was …
r-tome Sep 22, 2022
b654fd3
[EC-377] Removed unneeded destroy$ from preferences.component.ts
r-tome Sep 22, 2022
3bf3523
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Sep 27, 2022
ca6975c
Merge branch 'master' into EC-377-transition-policy-service-into-prov…
r-tome Oct 10, 2022
12129b7
[EC-377] Fixed policy.service.ts import of OrganizationService
r-tome Oct 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/browser/src/background/commands.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default class CommandsBackground {
}

private async generatePasswordToClipboard() {
const options = (await this.passwordGenerationService.getOptions())[0];
const options = (await this.passwordGenerationService.getOptions())?.[0] ?? {};
const password = await this.passwordGenerationService.generatePassword(options);
this.platformUtilsService.copyToClipboard(password, { window: window });
this.passwordGenerationService.addHistory(password);
Expand Down
2 changes: 1 addition & 1 deletion apps/browser/src/background/contextMenus.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class ContextMenusBackground {
}

private async generatePasswordToClipboard() {
const options = (await this.passwordGenerationService.getOptions())[0];
const options = (await this.passwordGenerationService.getOptions())?.[0] ?? {};
const password = await this.passwordGenerationService.generatePassword(options);
this.platformUtilsService.copyToClipboard(password, { window: window });
this.passwordGenerationService.addHistory(password);
Expand Down
4 changes: 3 additions & 1 deletion apps/browser/src/background/notification.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ export default class NotificationBackground {
}

private async allowPersonalOwnership(): Promise<boolean> {
return !(await this.policyService.policyAppliesToUser(PolicyType.PersonalOwnership));
return !(await firstValueFrom(
this.policyService.policyAppliesToActiveUser$(PolicyType.PersonalOwnership)
));
}
}
5 changes: 4 additions & 1 deletion apps/cli/src/commands/export.command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as program from "commander";
import * as inquirer from "inquirer";
import { firstValueFrom } from "rxjs";

import { ExportFormat, ExportService } from "@bitwarden/common/abstractions/export.service";
import { PolicyService } from "@bitwarden/common/abstractions/policy/policy.service.abstraction";
Expand All @@ -15,7 +16,9 @@ export class ExportCommand {
async run(options: program.OptionValues): Promise<Response> {
if (
options.organizationid == null &&
(await this.policyService.policyAppliesToUser(PolicyType.DisablePersonalVaultExport))
(await firstValueFrom(
this.policyService.policyAppliesToActiveUser$(PolicyType.DisablePersonalVaultExport)
))
) {
return Response.badRequest(
"One or more organization policies prevents you from exporting your personal vault."
Expand Down
21 changes: 16 additions & 5 deletions apps/web/src/app/accounts/login/login.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, NgZone } from "@angular/core";
import { Component, NgZone, OnDestroy, OnInit } from "@angular/core";
import { FormBuilder } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router";
import { Subject, takeUntil } from "rxjs";
import { first } from "rxjs/operators";

import { LoginComponent as BaseLoginComponent } from "@bitwarden/angular/components/login.component";
Expand Down Expand Up @@ -30,13 +31,14 @@ import { RouterService, StateService } from "../../core";
selector: "app-login",
templateUrl: "login.component.html",
})
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
export class LoginComponent extends BaseLoginComponent {
export class LoginComponent extends BaseLoginComponent implements OnInit, OnDestroy {
showResetPasswordAutoEnrollWarning = false;
enforcedPasswordPolicyOptions: MasterPasswordPolicyOptions;
policies: ListResponse<PolicyResponse>;
showPasswordless = false;

private destroy$ = new Subject<void>();

constructor(
authService: AuthService,
router: Router,
Expand Down Expand Up @@ -129,12 +131,21 @@ export class LoginComponent extends BaseLoginComponent {
this.showResetPasswordAutoEnrollWarning =
resetPasswordPolicy[1] && resetPasswordPolicy[0].autoEnrollEnabled;

this.enforcedPasswordPolicyOptions =
await this.policyService.getMasterPasswordPolicyOptions(policyList);
this.policyService
.masterPasswordPolicyOptions$(policyList)
.pipe(takeUntil(this.destroy$))
.subscribe((enforcedPasswordPolicyOptions) => {
this.enforcedPasswordPolicyOptions = enforcedPasswordPolicyOptions;
});
}
}
}

ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}

async goAfterLogIn() {
const masterPassword = this.formGroup.get("masterPassword")?.value;

Expand Down
21 changes: 15 additions & 6 deletions apps/web/src/app/accounts/register.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component } from "@angular/core";
import { Component, OnDestroy, OnInit } from "@angular/core";
import { UntypedFormBuilder } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router";
import { Subject, takeUntil } from "rxjs";
import { first } from "rxjs/operators";

import { RegisterComponent as BaseRegisterComponent } from "@bitwarden/angular/components/register.component";
Expand All @@ -27,14 +28,14 @@ import { RouterService } from "../core";
selector: "app-register",
templateUrl: "register.component.html",
})
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
export class RegisterComponent extends BaseRegisterComponent {
export class RegisterComponent extends BaseRegisterComponent implements OnInit, OnDestroy {
email = "";
showCreateOrgMessage = false;
layout = "";
enforcedPolicyOptions: MasterPasswordPolicyOptions;

private policies: Policy[];
private destroy$ = new Subject<void>();

constructor(
formValidationErrorService: FormValidationErrorsService,
Expand Down Expand Up @@ -130,11 +131,19 @@ export class RegisterComponent extends BaseRegisterComponent {
}

if (this.policies != null) {
this.enforcedPolicyOptions = await this.policyService.getMasterPasswordPolicyOptions(
this.policies
);
this.policyService
.masterPasswordPolicyOptions$(this.policies)
.pipe(takeUntil(this.destroy$))
.subscribe((enforcedPasswordPolicyOptions) => {
this.enforcedPolicyOptions = enforcedPasswordPolicyOptions;
});
}

await super.ngOnInit();
}

ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FormBuilder, UntypedFormBuilder } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router";
import { RouterTestingModule } from "@angular/router/testing";
import { Substitute } from "@fluffy-spoon/substitute";
import { BehaviorSubject } from "rxjs";
import { BehaviorSubject, of } from "rxjs";

import { I18nPipe } from "@bitwarden/angular/pipes/i18n.pipe";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
Expand Down Expand Up @@ -46,7 +46,7 @@ describe("TrialInitiationComponent", () => {
};

policyServiceMock = {
getMasterPasswordPolicyOptions: jest.fn(),
masterPasswordPolicyOptions$: jest.fn(),
};

TestBed.configureTestingModule({
Expand Down Expand Up @@ -144,14 +144,16 @@ describe("TrialInitiationComponent", () => {
},
],
});
policyServiceMock.getMasterPasswordPolicyOptions.mockReturnValueOnce({
minComplexity: 4,
minLength: 10,
requireLower: null,
requireNumbers: null,
requireSpecial: null,
requireUpper: null,
} as MasterPasswordPolicyOptions);
policyServiceMock.masterPasswordPolicyOptions$.mockReturnValue(
of({
minComplexity: 4,
minLength: 10,
requireLower: null,
requireNumbers: null,
requireSpecial: null,
requireUpper: null,
} as MasterPasswordPolicyOptions)
);

// Need to recreate component with new service mocks
fixture = TestBed.createComponent(TrialInitiationComponent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { StepperSelectionEvent } from "@angular/cdk/stepper";
import { TitleCasePipe } from "@angular/common";
import { Component, OnInit, ViewChild } from "@angular/core";
import { Component, OnDestroy, OnInit, ViewChild } from "@angular/core";
import { UntypedFormBuilder, Validators } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router";
import { first } from "rxjs";
import { first, Subject, takeUntil } from "rxjs";

import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/abstractions/log.service";
Expand All @@ -24,8 +24,7 @@ import { VerticalStepperComponent } from "./vertical-stepper/vertical-stepper.co
selector: "app-trial",
templateUrl: "trial-initiation.component.html",
})
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
export class TrialInitiationComponent implements OnInit {
export class TrialInitiationComponent implements OnInit, OnDestroy {
email = "";
org = "";
orgInfoSubLabel = "";
Expand Down Expand Up @@ -63,6 +62,8 @@ export class TrialInitiationComponent implements OnInit {
}
}

private destroy$ = new Subject<void>();

constructor(
private route: ActivatedRoute,
protected router: Router,
Expand Down Expand Up @@ -140,12 +141,20 @@ export class TrialInitiationComponent implements OnInit {
}

if (this.policies != null) {
this.enforcedPolicyOptions = await this.policyService.getMasterPasswordPolicyOptions(
this.policies
);
this.policyService
.masterPasswordPolicyOptions$(this.policies)
.pipe(takeUntil(this.destroy$))
.subscribe((enforcedPasswordPolicyOptions) => {
this.enforcedPolicyOptions = enforcedPasswordPolicyOptions;
});
}
}

ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}

stepSelectionChange(event: StepperSelectionEvent) {
// Set org info sub label
if (event.selectedIndex === 1 && this.orgInfoFormGroup.controls.name.value === "") {
Expand Down
23 changes: 19 additions & 4 deletions apps/web/src/app/core/event.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import { Injectable } from "@angular/core";
import { Injectable, OnDestroy, OnInit } from "@angular/core";
import { Subject, takeUntil } from "rxjs";

import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { PolicyService } from "@bitwarden/common/abstractions/policy/policy.service.abstraction";
import { DeviceType } from "@bitwarden/common/enums/deviceType";
import { EventType } from "@bitwarden/common/enums/eventType";
import { PolicyType } from "@bitwarden/common/enums/policyType";
import { Policy } from "@bitwarden/common/models/domain/policy";
import { EventResponse } from "@bitwarden/common/models/response/eventResponse";

@Injectable()
export class EventService {
export class EventService implements OnInit, OnDestroy {
private destroy$ = new Subject<void>();
private policies: Policy[];

constructor(private i18nService: I18nService, private policyService: PolicyService) {}

ngOnInit(): void {
this.policyService.policies$.pipe(takeUntil(this.destroy$)).subscribe((policies) => {
this.policies = policies;
});
}

ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}

getDefaultDateFilters() {
const d = new Date();
const end = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59);
Expand Down Expand Up @@ -326,8 +342,7 @@ export class EventService {
case EventType.Policy_Updated: {
msg = this.i18nService.t("modifiedPolicyId", this.formatPolicyId(ev));

const policies = await this.policyService.getAll();
const policy = policies.filter((p) => p.id === ev.policyId)[0];
const policy = this.policies.filter((p) => p.id === ev.policyId)[0];
let p1 = this.getShortId(ev.policyId);
if (policy != null) {
p1 = PolicyType[policy.type];
Expand Down
Loading