From 797938881ebf87d848b8e03718ce02c1ebe1f44f Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Tue, 26 Jul 2022 19:34:45 +0200 Subject: [PATCH] Add CoreModule (#3149) --- apps/web/.eslintrc.json | 8 ++++ apps/web/src/app/accounts/lock.component.ts | 2 +- apps/web/src/app/accounts/login.component.ts | 3 +- .../src/app/accounts/register.component.ts | 2 +- .../src/app/accounts/two-factor.component.ts | 2 +- apps/web/src/app/app.component.ts | 3 +- apps/web/src/app/app.module.ts | 4 +- .../src/app/common/base.events.component.ts | 2 +- .../broadcaster-messaging.service.ts} | 0 .../core.module.ts} | 37 +++++++++---------- .../app/{services => core}/event.service.ts | 0 .../html-storage.service.ts} | 0 .../app/{services => core}/i18n.service.ts | 0 apps/web/src/app/core/index.ts | 5 +++ .../app/{services => core}/init.service.ts | 0 .../app/{services => core}/modal.service.ts | 0 .../password-reprompt.service.ts} | 0 .../{services => core}/policy-list.service.ts | 0 .../app/{services => core}/router.service.ts | 0 .../state-migration.service.ts} | 4 +- .../src/app/{models => core/state}/account.ts | 0 .../state/global-state.ts} | 0 apps/web/src/app/core/state/index.ts | 3 ++ .../{services => core/state}/state.service.ts | 6 +-- .../web-file-download.service.ts} | 0 .../web-platform-utils.service.ts} | 0 apps/web/src/app/guards/home.guard.ts | 4 +- .../organizations/guards/permissions.guard.ts | 4 +- .../manage/entity-events.component.ts | 2 +- .../organizations/manage/events.component.ts | 2 +- .../manage/policies.component.ts | 2 +- .../src/app/settings/settings.component.ts | 2 +- .../bit-web/src/app/app.module.ts | 4 +- .../app/providers/manage/events.component.ts | 2 +- 34 files changed, 59 insertions(+), 44 deletions(-) rename apps/web/src/app/{services/broadcasterMessaging.service.ts => core/broadcaster-messaging.service.ts} (100%) rename apps/web/src/app/{services/services.module.ts => core/core.module.ts} (76%) rename apps/web/src/app/{services => core}/event.service.ts (100%) rename apps/web/src/app/{services/htmlStorage.service.ts => core/html-storage.service.ts} (100%) rename apps/web/src/app/{services => core}/i18n.service.ts (100%) create mode 100644 apps/web/src/app/core/index.ts rename apps/web/src/app/{services => core}/init.service.ts (100%) rename apps/web/src/app/{services => core}/modal.service.ts (100%) rename apps/web/src/app/{services/passwordReprompt.service.ts => core/password-reprompt.service.ts} (100%) rename apps/web/src/app/{services => core}/policy-list.service.ts (100%) rename apps/web/src/app/{services => core}/router.service.ts (100%) rename apps/web/src/app/{services/stateMigration.service.ts => core/state-migration.service.ts} (85%) rename apps/web/src/app/{models => core/state}/account.ts (100%) rename apps/web/src/app/{models/globalState.ts => core/state/global-state.ts} (100%) create mode 100644 apps/web/src/app/core/state/index.ts rename apps/web/src/app/{services => core/state}/state.service.ts (97%) rename apps/web/src/app/{services/webFileDownload.service.ts => core/web-file-download.service.ts} (100%) rename apps/web/src/app/{services/webPlatformUtils.service.ts => core/web-platform-utils.service.ts} (100%) diff --git a/apps/web/.eslintrc.json b/apps/web/.eslintrc.json index e5a34aec6a..d2fa3fe89a 100644 --- a/apps/web/.eslintrc.json +++ b/apps/web/.eslintrc.json @@ -1,5 +1,13 @@ { "env": { "browser": true + }, + "rules": { + "no-restricted-imports": [ + "error", + { + "patterns": ["**/core/*"] + } + ] } } diff --git a/apps/web/src/app/accounts/lock.component.ts b/apps/web/src/app/accounts/lock.component.ts index 90d2b1d387..08d69d3316 100644 --- a/apps/web/src/app/accounts/lock.component.ts +++ b/apps/web/src/app/accounts/lock.component.ts @@ -13,7 +13,7 @@ import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUti import { StateService } from "@bitwarden/common/abstractions/state.service"; import { VaultTimeoutService } from "@bitwarden/common/abstractions/vaultTimeout.service"; -import { RouterService } from "../services/router.service"; +import { RouterService } from "../core"; @Component({ selector: "app-lock", diff --git a/apps/web/src/app/accounts/login.component.ts b/apps/web/src/app/accounts/login.component.ts index 4410311e4f..e8af9bc2f7 100644 --- a/apps/web/src/app/accounts/login.component.ts +++ b/apps/web/src/app/accounts/login.component.ts @@ -19,8 +19,7 @@ import { Policy } from "@bitwarden/common/models/domain/policy"; import { ListResponse } from "@bitwarden/common/models/response/listResponse"; import { PolicyResponse } from "@bitwarden/common/models/response/policyResponse"; -import { RouterService } from "../services/router.service"; -import { StateService } from "../services/state.service"; +import { RouterService, StateService } from "../core"; @Component({ selector: "app-login", diff --git a/apps/web/src/app/accounts/register.component.ts b/apps/web/src/app/accounts/register.component.ts index 8a87476c4f..05db3184a4 100644 --- a/apps/web/src/app/accounts/register.component.ts +++ b/apps/web/src/app/accounts/register.component.ts @@ -20,7 +20,7 @@ import { MasterPasswordPolicyOptions } from "@bitwarden/common/models/domain/mas import { Policy } from "@bitwarden/common/models/domain/policy"; import { ReferenceEventRequest } from "@bitwarden/common/models/request/referenceEventRequest"; -import { RouterService } from "../services/router.service"; +import { RouterService } from "../core"; @Component({ selector: "app-register", diff --git a/apps/web/src/app/accounts/two-factor.component.ts b/apps/web/src/app/accounts/two-factor.component.ts index 57ec40a2a4..1d9b19af21 100644 --- a/apps/web/src/app/accounts/two-factor.component.ts +++ b/apps/web/src/app/accounts/two-factor.component.ts @@ -14,7 +14,7 @@ import { StateService } from "@bitwarden/common/abstractions/state.service"; import { TwoFactorService } from "@bitwarden/common/abstractions/twoFactor.service"; import { TwoFactorProviderType } from "@bitwarden/common/enums/twoFactorProviderType"; -import { RouterService } from "../services/router.service"; +import { RouterService } from "../core"; import { TwoFactorOptionsComponent } from "./two-factor-options.component"; diff --git a/apps/web/src/app/app.component.ts b/apps/web/src/app/app.component.ts index 7c02076191..97c6dccb4f 100644 --- a/apps/web/src/app/app.component.ts +++ b/apps/web/src/app/app.component.ts @@ -26,6 +26,7 @@ import { StateService } from "@bitwarden/common/abstractions/state.service"; import { SyncService } from "@bitwarden/common/abstractions/sync.service"; import { VaultTimeoutService } from "@bitwarden/common/abstractions/vaultTimeout.service"; +import { PolicyListService, RouterService } from "./core"; import { DisableSendPolicy } from "./organizations/policies/disable-send.component"; import { MasterPasswordPolicy } from "./organizations/policies/master-password.component"; import { PasswordGeneratorPolicy } from "./organizations/policies/password-generator.component"; @@ -35,8 +36,6 @@ import { ResetPasswordPolicy } from "./organizations/policies/reset-password.com import { SendOptionsPolicy } from "./organizations/policies/send-options.component"; import { SingleOrgPolicy } from "./organizations/policies/single-org.component"; import { TwoFactorAuthenticationPolicy } from "./organizations/policies/two-factor-authentication.component"; -import { PolicyListService } from "./services/policy-list.service"; -import { RouterService } from "./services/router.service"; const BroadcasterSubscriptionId = "AppComponent"; const IdleTimeout = 60000 * 10; // 10 minutes diff --git a/apps/web/src/app/app.module.ts b/apps/web/src/app/app.module.ts index b23c4d182e..5d1afd2122 100644 --- a/apps/web/src/app/app.module.ts +++ b/apps/web/src/app/app.module.ts @@ -6,9 +6,9 @@ import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; import { InfiniteScrollModule } from "ngx-infinite-scroll"; import { AppComponent } from "./app.component"; +import { CoreModule } from "./core"; import { OssRoutingModule } from "./oss-routing.module"; import { OssModule } from "./oss.module"; -import { ServicesModule } from "./services/services.module"; import { WildcardRoutingModule } from "./wildcard-routing.module"; @NgModule({ @@ -16,7 +16,7 @@ import { WildcardRoutingModule } from "./wildcard-routing.module"; OssModule, BrowserAnimationsModule, FormsModule, - ServicesModule, + CoreModule, InfiniteScrollModule, DragDropModule, LayoutModule, diff --git a/apps/web/src/app/common/base.events.component.ts b/apps/web/src/app/common/base.events.component.ts index 7bc1a94c17..d7368a3f46 100644 --- a/apps/web/src/app/common/base.events.component.ts +++ b/apps/web/src/app/common/base.events.component.ts @@ -9,7 +9,7 @@ import { EventResponse } from "@bitwarden/common/models/response/eventResponse"; import { ListResponse } from "@bitwarden/common/models/response/listResponse"; import { EventView } from "@bitwarden/common/models/view/eventView"; -import { EventService } from "src/app/services/event.service"; +import { EventService } from "src/app/core"; @Directive() export abstract class BaseEventsComponent { diff --git a/apps/web/src/app/services/broadcasterMessaging.service.ts b/apps/web/src/app/core/broadcaster-messaging.service.ts similarity index 100% rename from apps/web/src/app/services/broadcasterMessaging.service.ts rename to apps/web/src/app/core/broadcaster-messaging.service.ts diff --git a/apps/web/src/app/services/services.module.ts b/apps/web/src/app/core/core.module.ts similarity index 76% rename from apps/web/src/app/services/services.module.ts rename to apps/web/src/app/core/core.module.ts index efb5d991c0..95e5a76606 100644 --- a/apps/web/src/app/services/services.module.ts +++ b/apps/web/src/app/core/core.module.ts @@ -1,5 +1,5 @@ -import { APP_INITIALIZER, NgModule } from "@angular/core"; -import { ToastrModule } from "ngx-toastr"; +import { CommonModule } from "@angular/common"; +import { APP_INITIALIZER, NgModule, Optional, SkipSelf } from "@angular/core"; import { JslibServicesModule, @@ -22,32 +22,24 @@ import { AbstractStorageService } from "@bitwarden/common/abstractions/storage.s import { StateFactory } from "@bitwarden/common/factories/stateFactory"; import { MemoryStorageService } from "@bitwarden/common/services/memoryStorage.service"; -import { HomeGuard } from "../guards/home.guard"; -import { Account } from "../models/account"; -import { GlobalState } from "../models/globalState"; -import { PermissionsGuard as OrgPermissionsGuard } from "../organizations/guards/permissions.guard"; -import { NavigationPermissionsService as OrgPermissionsService } from "../organizations/services/navigation-permissions.service"; - -import { BroadcasterMessagingService } from "./broadcasterMessaging.service"; +import { BroadcasterMessagingService } from "./broadcaster-messaging.service"; import { EventService } from "./event.service"; -import { HtmlStorageService } from "./htmlStorage.service"; +import { HtmlStorageService } from "./html-storage.service"; import { I18nService } from "./i18n.service"; import { InitService } from "./init.service"; import { ModalService } from "./modal.service"; -import { PasswordRepromptService } from "./passwordReprompt.service"; +import { PasswordRepromptService } from "./password-reprompt.service"; import { PolicyListService } from "./policy-list.service"; import { RouterService } from "./router.service"; -import { StateService } from "./state.service"; -import { StateMigrationService } from "./stateMigration.service"; -import { WebFileDownloadService } from "./webFileDownload.service"; -import { WebPlatformUtilsService } from "./webPlatformUtils.service"; +import { Account, GlobalState, StateService } from "./state"; +import { StateMigrationService } from "./state-migration.service"; +import { WebFileDownloadService } from "./web-file-download.service"; +import { WebPlatformUtilsService } from "./web-platform-utils.service"; @NgModule({ - imports: [ToastrModule, JslibServicesModule], declarations: [], + imports: [CommonModule, JslibServicesModule], providers: [ - OrgPermissionsService, - OrgPermissionsGuard, InitService, RouterService, EventService, @@ -106,7 +98,12 @@ import { WebPlatformUtilsService } from "./webPlatformUtils.service"; provide: FileDownloadService, useClass: WebFileDownloadService, }, - HomeGuard, ], }) -export class ServicesModule {} +export class CoreModule { + constructor(@Optional() @SkipSelf() parentModule?: CoreModule) { + if (parentModule) { + throw new Error("CoreModule is already loaded. Import it in the AppModule only"); + } + } +} diff --git a/apps/web/src/app/services/event.service.ts b/apps/web/src/app/core/event.service.ts similarity index 100% rename from apps/web/src/app/services/event.service.ts rename to apps/web/src/app/core/event.service.ts diff --git a/apps/web/src/app/services/htmlStorage.service.ts b/apps/web/src/app/core/html-storage.service.ts similarity index 100% rename from apps/web/src/app/services/htmlStorage.service.ts rename to apps/web/src/app/core/html-storage.service.ts diff --git a/apps/web/src/app/services/i18n.service.ts b/apps/web/src/app/core/i18n.service.ts similarity index 100% rename from apps/web/src/app/services/i18n.service.ts rename to apps/web/src/app/core/i18n.service.ts diff --git a/apps/web/src/app/core/index.ts b/apps/web/src/app/core/index.ts new file mode 100644 index 0000000000..80c1a44d50 --- /dev/null +++ b/apps/web/src/app/core/index.ts @@ -0,0 +1,5 @@ +export * from "./core.module"; +export * from "./event.service"; +export * from "./policy-list.service"; +export * from "./router.service"; +export * from "./state/state.service"; diff --git a/apps/web/src/app/services/init.service.ts b/apps/web/src/app/core/init.service.ts similarity index 100% rename from apps/web/src/app/services/init.service.ts rename to apps/web/src/app/core/init.service.ts diff --git a/apps/web/src/app/services/modal.service.ts b/apps/web/src/app/core/modal.service.ts similarity index 100% rename from apps/web/src/app/services/modal.service.ts rename to apps/web/src/app/core/modal.service.ts diff --git a/apps/web/src/app/services/passwordReprompt.service.ts b/apps/web/src/app/core/password-reprompt.service.ts similarity index 100% rename from apps/web/src/app/services/passwordReprompt.service.ts rename to apps/web/src/app/core/password-reprompt.service.ts diff --git a/apps/web/src/app/services/policy-list.service.ts b/apps/web/src/app/core/policy-list.service.ts similarity index 100% rename from apps/web/src/app/services/policy-list.service.ts rename to apps/web/src/app/core/policy-list.service.ts diff --git a/apps/web/src/app/services/router.service.ts b/apps/web/src/app/core/router.service.ts similarity index 100% rename from apps/web/src/app/services/router.service.ts rename to apps/web/src/app/core/router.service.ts diff --git a/apps/web/src/app/services/stateMigration.service.ts b/apps/web/src/app/core/state-migration.service.ts similarity index 85% rename from apps/web/src/app/services/stateMigration.service.ts rename to apps/web/src/app/core/state-migration.service.ts index 3b9a322a88..0c0c0ad682 100644 --- a/apps/web/src/app/services/stateMigration.service.ts +++ b/apps/web/src/app/core/state-migration.service.ts @@ -1,7 +1,7 @@ import { StateMigrationService as BaseStateMigrationService } from "@bitwarden/common/services/stateMigration.service"; -import { Account } from "../models/account"; -import { GlobalState } from "../models/globalState"; +import { Account } from "./state/account"; +import { GlobalState } from "./state/global-state"; export class StateMigrationService extends BaseStateMigrationService { protected async migrationStateFrom1To2(): Promise { diff --git a/apps/web/src/app/models/account.ts b/apps/web/src/app/core/state/account.ts similarity index 100% rename from apps/web/src/app/models/account.ts rename to apps/web/src/app/core/state/account.ts diff --git a/apps/web/src/app/models/globalState.ts b/apps/web/src/app/core/state/global-state.ts similarity index 100% rename from apps/web/src/app/models/globalState.ts rename to apps/web/src/app/core/state/global-state.ts diff --git a/apps/web/src/app/core/state/index.ts b/apps/web/src/app/core/state/index.ts new file mode 100644 index 0000000000..dd968df3a2 --- /dev/null +++ b/apps/web/src/app/core/state/index.ts @@ -0,0 +1,3 @@ +export * from "./account"; +export * from "./global-state"; +export * from "./state.service"; diff --git a/apps/web/src/app/services/state.service.ts b/apps/web/src/app/core/state/state.service.ts similarity index 97% rename from apps/web/src/app/services/state.service.ts rename to apps/web/src/app/core/state/state.service.ts index 12b180991e..074ffa4453 100644 --- a/apps/web/src/app/services/state.service.ts +++ b/apps/web/src/app/core/state/state.service.ts @@ -17,8 +17,8 @@ import { SendData } from "@bitwarden/common/models/data/sendData"; import { StorageOptions } from "@bitwarden/common/models/domain/storageOptions"; import { StateService as BaseStateService } from "@bitwarden/common/services/state.service"; -import { Account } from "../models/account"; -import { GlobalState } from "../models/globalState"; +import { Account } from "./account"; +import { GlobalState } from "./global-state"; @Injectable() export class StateService extends BaseStateService { @@ -43,7 +43,7 @@ export class StateService extends BaseStateService { } async addAccount(account: Account) { - // Apply web overides to default account values + // Apply web overrides to default account values account = new Account(account); await super.addAccount(account); } diff --git a/apps/web/src/app/services/webFileDownload.service.ts b/apps/web/src/app/core/web-file-download.service.ts similarity index 100% rename from apps/web/src/app/services/webFileDownload.service.ts rename to apps/web/src/app/core/web-file-download.service.ts diff --git a/apps/web/src/app/services/webPlatformUtils.service.ts b/apps/web/src/app/core/web-platform-utils.service.ts similarity index 100% rename from apps/web/src/app/services/webPlatformUtils.service.ts rename to apps/web/src/app/core/web-platform-utils.service.ts diff --git a/apps/web/src/app/guards/home.guard.ts b/apps/web/src/app/guards/home.guard.ts index 0a66ff33e9..6469042742 100644 --- a/apps/web/src/app/guards/home.guard.ts +++ b/apps/web/src/app/guards/home.guard.ts @@ -4,7 +4,9 @@ import { ActivatedRouteSnapshot, CanActivate, Router } from "@angular/router"; import { AuthService } from "@bitwarden/common/abstractions/auth.service"; import { AuthenticationStatus } from "@bitwarden/common/enums/authenticationStatus"; -@Injectable() +@Injectable({ + providedIn: "root", +}) export class HomeGuard implements CanActivate { constructor(private router: Router, private authService: AuthService) {} diff --git a/apps/web/src/app/organizations/guards/permissions.guard.ts b/apps/web/src/app/organizations/guards/permissions.guard.ts index b017aeac45..2ced429c76 100644 --- a/apps/web/src/app/organizations/guards/permissions.guard.ts +++ b/apps/web/src/app/organizations/guards/permissions.guard.ts @@ -7,7 +7,9 @@ import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUti import { SyncService } from "@bitwarden/common/abstractions/sync.service"; import { Permissions } from "@bitwarden/common/enums/permissions"; -@Injectable() +@Injectable({ + providedIn: "root", +}) export class PermissionsGuard implements CanActivate { constructor( private router: Router, diff --git a/apps/web/src/app/organizations/manage/entity-events.component.ts b/apps/web/src/app/organizations/manage/entity-events.component.ts index d3cb75e213..6801c90593 100644 --- a/apps/web/src/app/organizations/manage/entity-events.component.ts +++ b/apps/web/src/app/organizations/manage/entity-events.component.ts @@ -8,7 +8,7 @@ import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUti import { EventResponse } from "@bitwarden/common/models/response/eventResponse"; import { ListResponse } from "@bitwarden/common/models/response/listResponse"; -import { EventService } from "../../services/event.service"; +import { EventService } from "../../core"; @Component({ selector: "app-entity-events", diff --git a/apps/web/src/app/organizations/manage/events.component.ts b/apps/web/src/app/organizations/manage/events.component.ts index 888ce33144..e16d7c4b6d 100644 --- a/apps/web/src/app/organizations/manage/events.component.ts +++ b/apps/web/src/app/organizations/manage/events.component.ts @@ -14,7 +14,7 @@ import { Organization } from "@bitwarden/common/models/domain/organization"; import { EventResponse } from "@bitwarden/common/models/response/eventResponse"; import { BaseEventsComponent } from "../../common/base.events.component"; -import { EventService } from "../../services/event.service"; +import { EventService } from "../../core"; @Component({ selector: "app-org-events", diff --git a/apps/web/src/app/organizations/manage/policies.component.ts b/apps/web/src/app/organizations/manage/policies.component.ts index ac479ae548..9dae83ee70 100644 --- a/apps/web/src/app/organizations/manage/policies.component.ts +++ b/apps/web/src/app/organizations/manage/policies.component.ts @@ -9,7 +9,7 @@ import { PolicyType } from "@bitwarden/common/enums/policyType"; import { Organization } from "@bitwarden/common/models/domain/organization"; import { PolicyResponse } from "@bitwarden/common/models/response/policyResponse"; -import { PolicyListService } from "../../services/policy-list.service"; +import { PolicyListService } from "../../core"; import { BasePolicy } from "../policies/base-policy.component"; import { PolicyEditComponent } from "./policy-edit.component"; diff --git a/apps/web/src/app/settings/settings.component.ts b/apps/web/src/app/settings/settings.component.ts index c8840a1298..6e9fa21b1f 100644 --- a/apps/web/src/app/settings/settings.component.ts +++ b/apps/web/src/app/settings/settings.component.ts @@ -6,7 +6,7 @@ import { OrganizationService } from "@bitwarden/common/abstractions/organization import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service"; import { TokenService } from "@bitwarden/common/abstractions/token.service"; -import { StateService } from "../services/state.service"; +import { StateService } from "../core"; const BroadcasterSubscriptionId = "SettingsComponent"; diff --git a/bitwarden_license/bit-web/src/app/app.module.ts b/bitwarden_license/bit-web/src/app/app.module.ts index 1d15a71cef..e681744c51 100644 --- a/bitwarden_license/bit-web/src/app/app.module.ts +++ b/bitwarden_license/bit-web/src/app/app.module.ts @@ -8,9 +8,9 @@ import { InfiniteScrollModule } from "ngx-infinite-scroll"; import { JslibModule } from "@bitwarden/angular/jslib.module"; +import { CoreModule } from "src/app/core"; import { OssRoutingModule } from "src/app/oss-routing.module"; import { OssModule } from "src/app/oss.module"; -import { ServicesModule } from "src/app/services/services.module"; import { WildcardRoutingModule } from "src/app/wildcard-routing.module"; import { AppRoutingModule } from "./app-routing.module"; @@ -27,7 +27,7 @@ import { MaximumVaultTimeoutPolicyComponent } from "./policies/maximum-vault-tim BrowserAnimationsModule, FormsModule, ReactiveFormsModule, - ServicesModule, + CoreModule, InfiniteScrollModule, DragDropModule, AppRoutingModule, diff --git a/bitwarden_license/bit-web/src/app/providers/manage/events.component.ts b/bitwarden_license/bit-web/src/app/providers/manage/events.component.ts index 422b7fae7b..b6273805e3 100644 --- a/bitwarden_license/bit-web/src/app/providers/manage/events.component.ts +++ b/bitwarden_license/bit-web/src/app/providers/manage/events.component.ts @@ -12,7 +12,7 @@ import { ProviderService } from "@bitwarden/common/abstractions/provider.service import { EventResponse } from "@bitwarden/common/models/response/eventResponse"; import { BaseEventsComponent } from "src/app/common/base.events.component"; -import { EventService } from "src/app/services/event.service"; +import { EventService } from "src/app/core"; @Component({ selector: "provider-events",