From a16f18433475de1cd896fe6286347ab6a7ac9449 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sat, 17 Dec 2022 11:01:49 +0100 Subject: [PATCH] Refactoring --- apps/api/src/app/auth/auth.module.ts | 4 +-- apps/api/src/app/auth/auth.service.ts | 29 ------------------- apps/api/src/app/info/info.service.ts | 17 +++++------ apps/api/src/app/user/user.controller.ts | 21 +++++++------- apps/api/src/app/user/user.service.ts | 7 ----- .../api/src/services/configuration.service.ts | 1 - .../interfaces/environment.interface.ts | 1 - .../admin-overview.component.ts | 19 +++++------- .../admin-overview/admin-overview.html | 17 +++++------ .../components/header/header.component.html | 5 ++-- .../app/components/header/header.component.ts | 8 +++++ .../pages/landing/landing-page.component.ts | 12 ++++---- .../src/app/pages/landing/landing-page.html | 23 ++++++++------- .../pages/register/register-page.component.ts | 7 ++++- .../src/app/pages/register/register-page.html | 4 +-- libs/common/src/lib/config.ts | 2 +- libs/common/src/lib/permissions.ts | 3 +- 17 files changed, 73 insertions(+), 107 deletions(-) diff --git a/apps/api/src/app/auth/auth.module.ts b/apps/api/src/app/auth/auth.module.ts index cb5441838..b25e4c18b 100644 --- a/apps/api/src/app/auth/auth.module.ts +++ b/apps/api/src/app/auth/auth.module.ts @@ -3,7 +3,6 @@ import { WebAuthService } from '@ghostfolio/api/app/auth/web-auth.service'; import { SubscriptionModule } from '@ghostfolio/api/app/subscription/subscription.module'; import { UserModule } from '@ghostfolio/api/app/user/user.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration.module'; -import { PropertyModule } from '@ghostfolio/api/services/property/property.module'; import { PrismaModule } from '@ghostfolio/api/services/prisma.module'; import { Module } from '@nestjs/common'; import { JwtModule } from '@nestjs/jwt'; @@ -23,8 +22,7 @@ import { JwtStrategy } from './jwt.strategy'; }), PrismaModule, SubscriptionModule, - UserModule, - PropertyModule + UserModule ], providers: [ AuthDeviceService, diff --git a/apps/api/src/app/auth/auth.service.ts b/apps/api/src/app/auth/auth.service.ts index 79481ebfd..3178ce9ac 100644 --- a/apps/api/src/app/auth/auth.service.ts +++ b/apps/api/src/app/auth/auth.service.ts @@ -1,18 +1,15 @@ import { UserService } from '@ghostfolio/api/app/user/user.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; -import { PropertyService } from '@ghostfolio/api/services/property/property.service'; import { Injectable, InternalServerErrorException } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { Provider } from '@prisma/client'; import { ValidateOAuthLoginParams } from './interfaces/interfaces'; -import { PROPERTY_DISABLE_USER_SIGNUP } from '@ghostfolio/common/config'; @Injectable() export class AuthService { public constructor( private readonly configurationService: ConfigurationService, - private readonly propertyService: PropertyService, private readonly jwtService: JwtService, private readonly userService: UserService ) {} @@ -53,19 +50,6 @@ export class AuthService { }); if (!user) { - // Guard new user registration based on config property - if (this.configurationService.get('ENABLE_FEATURE_USER_SIGNUP_CONTROL')) { - const isUserSignupDisabled = (await this.propertyService.getByKey( - PROPERTY_DISABLE_USER_SIGNUP - )) as boolean; - - if(isUserSignupDisabled){ - throw new InternalServerErrorException( - 'validateInternetIdentityLogin', - 'Not Allowed' - ); - } - } // Create new user if not found user = await this.userService.createUser({ provider, @@ -94,19 +78,6 @@ export class AuthService { }); if (!user) { - // Guard new user registration based on config property - if (this.configurationService.get('ENABLE_FEATURE_USER_SIGNUP_CONTROL')) { - const isUserSignupDisabled = (await this.propertyService.getByKey( - PROPERTY_DISABLE_USER_SIGNUP - )) as boolean; - - if(isUserSignupDisabled){ - throw new InternalServerErrorException( - 'validateInternetIdentityLogin', - 'Not Allowed' - ); - } - } // Create new user if not found user = await this.userService.createUser({ provider, diff --git a/apps/api/src/app/info/info.service.ts b/apps/api/src/app/info/info.service.ts index d8d8420af..e3702fbbd 100644 --- a/apps/api/src/app/info/info.service.ts +++ b/apps/api/src/app/info/info.service.ts @@ -8,10 +8,10 @@ import { TagService } from '@ghostfolio/api/services/tag/tag.service'; import { DEMO_USER_ID, PROPERTY_IS_READ_ONLY_MODE, + PROPERTY_IS_USER_SIGNUP_ENABLED, PROPERTY_SLACK_COMMUNITY_USERS, PROPERTY_STRIPE_CONFIG, PROPERTY_SYSTEM_MESSAGE, - PROPERTY_DISABLE_USER_SIGNUP, ghostfolioFearAndGreedIndexDataSource } from '@ghostfolio/common/config'; import { @@ -104,16 +104,13 @@ export class InfoService { )) as string; } - if (this.configurationService.get('ENABLE_FEATURE_USER_SIGNUP_CONTROL')) { - const isUserSignupDisabled = (await this.propertyService.getByKey( - PROPERTY_DISABLE_USER_SIGNUP - )) as boolean; + const isUserSignupEnabled = + ((await this.propertyService.getByKey( + PROPERTY_IS_USER_SIGNUP_ENABLED + )) as boolean) ?? true; - if (!isUserSignupDisabled) { - globalPermissions.push(permissions.createUserAccount); - } - } else { // By default enabled - globalPermissions.push(permissions.createUserAccount); + if (isUserSignupEnabled) { + globalPermissions.push(permissions.createUserAccount); } return { diff --git a/apps/api/src/app/user/user.controller.ts b/apps/api/src/app/user/user.controller.ts index f2f52b114..ce0714d61 100644 --- a/apps/api/src/app/user/user.controller.ts +++ b/apps/api/src/app/user/user.controller.ts @@ -2,7 +2,7 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration.ser import { PropertyService } from '@ghostfolio/api/services/property/property.service'; import { PROPERTY_IS_READ_ONLY_MODE, - PROPERTY_DISABLE_USER_SIGNUP + PROPERTY_IS_USER_SIGNUP_ENABLED } from '@ghostfolio/common/config'; import { User, UserSettings } from '@ghostfolio/common/interfaces'; import { hasPermission, permissions } from '@ghostfolio/common/permissions'; @@ -85,17 +85,16 @@ export class UserController { } } - if (this.configurationService.get('ENABLE_FEATURE_USER_SIGNUP_CONTROL')) { - const isUserSignupDisabled = (await this.propertyService.getByKey( - PROPERTY_DISABLE_USER_SIGNUP - )) as boolean; + const isUserSignupEnabled = + ((await this.propertyService.getByKey( + PROPERTY_IS_USER_SIGNUP_ENABLED + )) as boolean) ?? true; - if (isUserSignupDisabled) { - throw new HttpException( - getReasonPhrase(StatusCodes.FORBIDDEN), - StatusCodes.FORBIDDEN - ); - } + if (!isUserSignupEnabled) { + throw new HttpException( + getReasonPhrase(StatusCodes.FORBIDDEN), + StatusCodes.FORBIDDEN + ); } const hasAdmin = await this.userService.hasAdmin(); diff --git a/apps/api/src/app/user/user.service.ts b/apps/api/src/app/user/user.service.ts index 7ca22630a..13f82aa47 100644 --- a/apps/api/src/app/user/user.service.ts +++ b/apps/api/src/app/user/user.service.ts @@ -165,12 +165,6 @@ export class UserService { currentPermissions.push(permissions.reportDataGlitch); } - if (this.configurationService.get('ENABLE_FEATURE_USER_SIGNUP_CONTROL')) { - if (hasRole(user, Role.ADMIN)) { - currentPermissions.push(permissions.toggleUserSignupMode); - } - } - if (this.configurationService.get('ENABLE_FEATURE_READ_ONLY_MODE')) { if (hasRole(user, Role.ADMIN)) { currentPermissions.push(permissions.toggleReadOnlyMode); @@ -224,7 +218,6 @@ export class UserService { } public async createUser(data: Prisma.UserCreateInput): Promise { - if (!data?.provider) { data.provider = 'ANONYMOUS'; } diff --git a/apps/api/src/services/configuration.service.ts b/apps/api/src/services/configuration.service.ts index c077b8b69..75441c8c3 100644 --- a/apps/api/src/services/configuration.service.ts +++ b/apps/api/src/services/configuration.service.ts @@ -30,7 +30,6 @@ export class ConfigurationService { ENABLE_FEATURE_STATISTICS: bool({ default: false }), ENABLE_FEATURE_SUBSCRIPTION: bool({ default: false }), ENABLE_FEATURE_SYSTEM_MESSAGE: bool({ default: false }), - ENABLE_FEATURE_USER_SIGNUP_CONTROL: bool({ default: false }), EOD_HISTORICAL_DATA_API_KEY: str({ default: '' }), GOOGLE_CLIENT_ID: str({ default: 'dummyClientId' }), GOOGLE_SECRET: str({ default: 'dummySecret' }), diff --git a/apps/api/src/services/interfaces/environment.interface.ts b/apps/api/src/services/interfaces/environment.interface.ts index 7011c1a8f..5ac20b55e 100644 --- a/apps/api/src/services/interfaces/environment.interface.ts +++ b/apps/api/src/services/interfaces/environment.interface.ts @@ -16,7 +16,6 @@ export interface Environment extends CleanedEnvAccessors { ENABLE_FEATURE_STATISTICS: boolean; ENABLE_FEATURE_SUBSCRIPTION: boolean; ENABLE_FEATURE_SYSTEM_MESSAGE: boolean; - ENABLE_FEATURE_USER_SIGNUP_CONTROL: boolean; EOD_HISTORICAL_DATA_API_KEY: string; GOOGLE_CLIENT_ID: string; GOOGLE_SECRET: string; diff --git a/apps/client/src/app/components/admin-overview/admin-overview.component.ts b/apps/client/src/app/components/admin-overview/admin-overview.component.ts index ab355391e..2e051db12 100644 --- a/apps/client/src/app/components/admin-overview/admin-overview.component.ts +++ b/apps/client/src/app/components/admin-overview/admin-overview.component.ts @@ -7,8 +7,8 @@ import { PROPERTY_COUPONS, PROPERTY_CURRENCIES, PROPERTY_IS_READ_ONLY_MODE, - PROPERTY_SYSTEM_MESSAGE, - PROPERTY_DISABLE_USER_SIGNUP + PROPERTY_IS_USER_SIGNUP_ENABLED, + PROPERTY_SYSTEM_MESSAGE } from '@ghostfolio/common/config'; import { Coupon, InfoItem, User } from '@ghostfolio/common/interfaces'; import { hasPermission, permissions } from '@ghostfolio/common/permissions'; @@ -35,8 +35,8 @@ export class AdminOverviewComponent implements OnDestroy, OnInit { public hasPermissionForSubscription: boolean; public hasPermissionForSystemMessage: boolean; public hasPermissionToToggleReadOnlyMode: boolean; - public hasPermissionToToggleUserSignupMode: boolean; public info: InfoItem; + public permissions = permissions; public transactionCount: number; public userCount: number; public user: User; @@ -71,11 +71,6 @@ export class AdminOverviewComponent implements OnDestroy, OnInit { this.user.permissions, permissions.toggleReadOnlyMode ); - - this.hasPermissionToToggleUserSignupMode = hasPermission( - this.user.permissions, - permissions.toggleUserSignupMode - ); } }); } @@ -175,9 +170,11 @@ export class AdminOverviewComponent implements OnDestroy, OnInit { } public onEnableUserSignupModeChange(aEvent: MatSlideToggleChange) { + console.log(aEvent); + this.putAdminSetting({ - key: PROPERTY_DISABLE_USER_SIGNUP, - value: aEvent.checked ? true : undefined + key: PROPERTY_IS_USER_SIGNUP_ENABLED, + value: aEvent.checked ? undefined : false }); } @@ -228,7 +225,7 @@ export class AdminOverviewComponent implements OnDestroy, OnInit { private putAdminSetting({ key, value }: { key: string; value: any }) { this.dataService .putAdminSetting(key, { - value: value ? JSON.stringify(value) : undefined + value: value || value === false ? JSON.stringify(value) : undefined }) .pipe(takeUntil(this.unsubscribeSubject)) .subscribe(() => { diff --git a/apps/client/src/app/components/admin-overview/admin-overview.html b/apps/client/src/app/components/admin-overview/admin-overview.html index 38408c04f..bcaa58253 100644 --- a/apps/client/src/app/components/admin-overview/admin-overview.html +++ b/apps/client/src/app/components/admin-overview/admin-overview.html @@ -109,24 +109,23 @@ -
-
Read-only Mode
+
+
Enable User Signup
-
-
Disable User Signup
+
+
Read-only Mode
diff --git a/apps/client/src/app/components/header/header.component.html b/apps/client/src/app/components/header/header.component.html index 3398b99d9..ce3b98dff 100644 --- a/apps/client/src/app/components/header/header.component.html +++ b/apps/client/src/app/components/header/header.component.html @@ -1,9 +1,9 @@ @@ -289,8 +289,7 @@ Sign in
-
+
- + + Get Started + +
or
- Get Started - -
or
+ Live Demo @@ -306,7 +309,7 @@
-
+

Are you ready?

diff --git a/apps/client/src/app/pages/register/register-page.component.ts b/apps/client/src/app/pages/register/register-page.component.ts index ee18a024c..6a0d231bd 100644 --- a/apps/client/src/app/pages/register/register-page.component.ts +++ b/apps/client/src/app/pages/register/register-page.component.ts @@ -25,6 +25,7 @@ export class RegisterPageComponent implements OnDestroy, OnInit { public demoAuthToken: string; public deviceType: string; public hasPermissionForSocialLogin: boolean; + public hasPermissionToCreateUser: boolean; public historicalDataItems: LineChartItem[]; public info: InfoItem; @@ -44,7 +45,8 @@ export class RegisterPageComponent implements OnDestroy, OnInit { } public ngOnInit() { - const { demoAuthToken, globalPermissions } = this.dataService.fetchInfo(); + const { demoAuthToken, globalPermissions, isReadOnlyMode } = + this.dataService.fetchInfo(); this.demoAuthToken = demoAuthToken; this.deviceType = this.deviceService.getDeviceInfo().deviceType; @@ -52,6 +54,9 @@ export class RegisterPageComponent implements OnDestroy, OnInit { globalPermissions, permissions.enableSocialLogin ); + this.hasPermissionToCreateUser = + !isReadOnlyMode && + hasPermission(globalPermissions, permissions.createUserAccount); } public async createAccount() { diff --git a/apps/client/src/app/pages/register/register-page.html b/apps/client/src/app/pages/register/register-page.html index 0542af79c..130346ef5 100644 --- a/apps/client/src/app/pages/register/register-page.html +++ b/apps/client/src/app/pages/register/register-page.html @@ -14,14 +14,14 @@

-
+