Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Feature/simplify admin user sign up (#675)

Thomas Kaul65bfe52db48c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 679bb42feb..a5e160794c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Improved the export functionality for activities (respect filtering)
+- Removed the _Admin_ user from the database seeding
+- Assigned the role `ADMIN` on sign up (only if there is no admin yet)
+
+### Todo
+
+- Apply data migration (`yarn database:migrate`)
## 1.111.0 - 03.02.2022
diff --git a/README.md b/README.md
index 260aca9be4..64c8cfd3a8 100644
--- a/README.md
+++ b/README.md
@@ -124,16 +124,10 @@ docker-compose -f docker/docker-compose.build.yml exec ghostfolio yarn database:
Open http://localhost:3333 in your browser and accomplish these steps:
-1. Login as _Admin_ with the following _Security Token_: `ae76872ae8f3419c6d6f64bf51888ecbcc703927a342d815fafe486acdb938da07d0cf44fca211a0be74a423238f535362d390a41e81e633a9ce668a6e31cdf9`
+1. Create a new user via _Get Started_ (this first user will get the role `ADMIN`)
1. Go to the _Admin Control Panel_ and click _Gather All Data_ to fetch historical data
1. Click _Sign out_ and check out the _Live Demo_
-### Finalization
-
-1. Create a new user via _Get Started_
-1. Assign the role `ADMIN` to this user (directly in the database)
-1. Delete the original _Admin_ (directly in the database)
-
### Migrate Database
With the following command you can keep your database schema in sync after a Ghostfolio version update:
@@ -155,8 +149,8 @@ docker-compose -f docker/docker-compose-build-local.yml exec ghostfolio yarn dat
1. Run `yarn install`
1. Run `docker-compose -f docker/docker-compose.dev.yml up -d` to start [PostgreSQL](https://www.postgresql.org) and [Redis](https://redis.io)
1. Run `yarn database:setup` to initialize the database schema and populate your database with (example) data
-1. Start server and client (see [_Development_](#Development))
-1. Login as _Admin_ with the following _Security Token_: `ae76872ae8f3419c6d6f64bf51888ecbcc703927a342d815fafe486acdb938da07d0cf44fca211a0be74a423238f535362d390a41e81e633a9ce668a6e31cdf9`
+1. Start the server and the client (see [_Development_](#Development))
+1. Create a new user via _Get Started_ (this first user will get the role `ADMIN`)
1. Go to the _Admin Control Panel_ and click _Gather All Data_ to fetch historical data
1. Click _Sign out_ and check out the _Live Demo_
diff --git a/apps/api/src/app/user/user.controller.ts b/apps/api/src/app/user/user.controller.ts
index adb059412f..a2003f9d25 100644
--- a/apps/api/src/app/user/user.controller.ts
+++ b/apps/api/src/app/user/user.controller.ts
@@ -23,7 +23,7 @@ import {
import { REQUEST } from '@nestjs/core';
import { JwtService } from '@nestjs/jwt';
import { AuthGuard } from '@nestjs/passport';
-import { Provider } from '@prisma/client';
+import { Provider, Role } from '@prisma/client';
import { User as UserModel } from '@prisma/client';
import { StatusCodes, getReasonPhrase } from 'http-status-codes';
@@ -83,8 +83,10 @@ export class UserController {
}
}
+ const hasAdmin = await this.userService.hasAdmin();
+
const { accessToken, id } = await this.userService.createUser({
- provider: Provider.ANONYMOUS
+ role: hasAdmin ? 'USER' : 'ADMIN'
});
return {
diff --git a/apps/api/src/app/user/user.service.ts b/apps/api/src/app/user/user.service.ts
index 13a5b7d1d0..527710474d 100644
--- a/apps/api/src/app/user/user.service.ts
+++ b/apps/api/src/app/user/user.service.ts
@@ -70,6 +70,18 @@ export class UserService {
};
}
+ public async hasAdmin() {
+ const usersWithAdminRole = await this.users({
+ where: {
+ role: {
+ equals: 'ADMIN'
+ }
+ }
+ });
+
+ return usersWithAdminRole.length > 0;
+ }
+
public isRestrictedView(aUser: UserWithSettings) {
return (aUser.Settings.settings as UserSettings)?.isRestrictedView ?? false;
}
diff --git a/prisma/migrations/20220205195653_added_default_value_for_provider_in_user/migration.sql b/prisma/migrations/20220205195653_added_default_value_for_provider_in_user/migration.sql
new file mode 100644
index 0000000000..eae68b5718
--- /dev/null
+++ b/prisma/migrations/20220205195653_added_default_value_for_provider_in_user/migration.sql
@@ -0,0 +1,6 @@
+-- Set default value
+UPDATE "User" SET "provider" = 'ANONYMOUS' WHERE "provider" IS NULL;
+
+-- AlterTable
+ALTER TABLE "User" ALTER COLUMN "provider" SET NOT NULL,
+ALTER COLUMN "provider" SET DEFAULT E'ANONYMOUS';
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 2625c0fcd6..53467d7b77 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -156,7 +156,7 @@ model User {
createdAt DateTime @default(now())
id String @id @default(uuid())
Order Order[]
- provider Provider?
+ provider Provider @default(ANONYMOUS)
role Role @default(USER)
Settings Settings?
Subscription Subscription[]
diff --git a/prisma/seed.js b/prisma/seed.js
index b355a22f05..3e4996a1e4 100644
--- a/prisma/seed.js
+++ b/prisma/seed.js
@@ -78,30 +78,6 @@ async function main() {
where: { id: '1377d9df-0d25-42c2-9d9b-e4c63156291f' }
});
- const userAdmin = await prisma.user.upsert({
- create: {
- accessToken:
- 'c689bcc894e4a420cb609ee34271f3e07f200594f7d199c50d75add7102889eb60061a04cd2792ebc853c54e37308271271e7bf588657c9e0c37faacbc28c3c6',
- Account: {
- create: [
- {
- accountType: AccountType.SECURITIES,
- balance: 0,
- currency: 'USD',
- id: 'f4425b66-9ba9-4ac4-93d7-fdf9a145e8cb',
- isDefault: true,
- name: 'Default Account'
- }
- ]
- },
- alias: 'Admin',
- id: '4e1af723-95f6-44f8-92a7-464df17f6ec3',
- role: Role.ADMIN
- },
- update: {},
- where: { id: '4e1af723-95f6-44f8-92a7-464df17f6ec3' }
- });
-
const userDemo = await prisma.user.upsert({
create: {
accessToken:
@@ -345,7 +321,6 @@ async function main() {
platformInteractiveBrokers,
platformPostFinance,
platformSwissquote,
- userAdmin,
userDemo
});
}