From 60f52bb209f886a3a660c903497023a58086aae6 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 17 Dec 2022 16:18:00 +0100 Subject: [PATCH] Handle user signup for OAuth and Internet Identity (#1515) Co-Authored-By: gobdevel <99349192+gobdevel@users.noreply.github.com> --- apps/api/src/app/auth/auth.module.ts | 2 ++ apps/api/src/app/auth/auth.service.ts | 16 ++++++++++++++++ apps/api/src/app/info/info.service.ts | 4 +--- apps/api/src/app/user/user.controller.ts | 4 +--- .../src/services/property/property.service.ts | 12 +++++++++++- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/apps/api/src/app/auth/auth.module.ts b/apps/api/src/app/auth/auth.module.ts index b25e4c18b..dfdec5655 100644 --- a/apps/api/src/app/auth/auth.module.ts +++ b/apps/api/src/app/auth/auth.module.ts @@ -4,6 +4,7 @@ import { SubscriptionModule } from '@ghostfolio/api/app/subscription/subscriptio import { UserModule } from '@ghostfolio/api/app/user/user.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration.module'; import { PrismaModule } from '@ghostfolio/api/services/prisma.module'; +import { PropertyModule } from '@ghostfolio/api/services/property/property.module'; import { Module } from '@nestjs/common'; import { JwtModule } from '@nestjs/jwt'; @@ -21,6 +22,7 @@ import { JwtStrategy } from './jwt.strategy'; signOptions: { expiresIn: '180 days' } }), PrismaModule, + PropertyModule, SubscriptionModule, UserModule ], diff --git a/apps/api/src/app/auth/auth.service.ts b/apps/api/src/app/auth/auth.service.ts index 3178ce9ac..0be9f2877 100644 --- a/apps/api/src/app/auth/auth.service.ts +++ b/apps/api/src/app/auth/auth.service.ts @@ -1,5 +1,6 @@ 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'; @@ -11,6 +12,7 @@ export class AuthService { public constructor( private readonly configurationService: ConfigurationService, private readonly jwtService: JwtService, + private readonly propertyService: PropertyService, private readonly userService: UserService ) {} @@ -50,6 +52,13 @@ export class AuthService { }); if (!user) { + const isUserSignupEnabled = + await this.propertyService.isUserSignupEnabled(); + + if (!isUserSignupEnabled) { + throw new Error('Sign up forbidden'); + } + // Create new user if not found user = await this.userService.createUser({ provider, @@ -78,6 +87,13 @@ export class AuthService { }); if (!user) { + const isUserSignupEnabled = + await this.propertyService.isUserSignupEnabled(); + + if (!isUserSignupEnabled) { + throw new Error('Sign up forbidden'); + } + // 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 e3702fbbd..f9eab7018 100644 --- a/apps/api/src/app/info/info.service.ts +++ b/apps/api/src/app/info/info.service.ts @@ -105,9 +105,7 @@ export class InfoService { } const isUserSignupEnabled = - ((await this.propertyService.getByKey( - PROPERTY_IS_USER_SIGNUP_ENABLED - )) as boolean) ?? true; + await this.propertyService.isUserSignupEnabled(); if (isUserSignupEnabled) { globalPermissions.push(permissions.createUserAccount); diff --git a/apps/api/src/app/user/user.controller.ts b/apps/api/src/app/user/user.controller.ts index 4c8b0d748..de79eaee9 100644 --- a/apps/api/src/app/user/user.controller.ts +++ b/apps/api/src/app/user/user.controller.ts @@ -70,9 +70,7 @@ export class UserController { @Post() public async signupUser(): Promise { const isUserSignupEnabled = - ((await this.propertyService.getByKey( - PROPERTY_IS_USER_SIGNUP_ENABLED - )) as boolean) ?? true; + await this.propertyService.isUserSignupEnabled(); if (!isUserSignupEnabled) { throw new HttpException( diff --git a/apps/api/src/services/property/property.service.ts b/apps/api/src/services/property/property.service.ts index 4760c3a94..69e7ff46b 100644 --- a/apps/api/src/services/property/property.service.ts +++ b/apps/api/src/services/property/property.service.ts @@ -1,5 +1,8 @@ import { PrismaService } from '@ghostfolio/api/services/prisma.service'; -import { PROPERTY_CURRENCIES } from '@ghostfolio/common/config'; +import { + PROPERTY_CURRENCIES, + PROPERTY_IS_USER_SIGNUP_ENABLED +} from '@ghostfolio/common/config'; import { Injectable } from '@nestjs/common'; @Injectable() @@ -39,6 +42,13 @@ export class PropertyService { return properties?.[aKey]; } + public async isUserSignupEnabled() { + return ( + ((await this.getByKey(PROPERTY_IS_USER_SIGNUP_ENABLED)) as boolean) ?? + true + ); + } + public async put({ key, value }: { key: string; value: string }) { return this.prismaService.property.upsert({ create: { key, value },