feat(server): add administrator feature (#6995)

This commit is contained in:
forehalo
2024-05-27 11:17:21 +00:00
parent 5ba9e2e9b1
commit aff166a0ef
23 changed files with 305 additions and 293 deletions

View File

@@ -1,16 +1,18 @@
import { ModuleRef } from '@nestjs/core';
import { PrismaClient } from '@prisma/client';
import { FeatureManagementService } from '../../core/features';
import { UserService } from '../../core/user';
import { Config, CryptoHelper } from '../../fundamentals';
export class SelfHostAdmin99999999 {
export class SelfHostAdmin1 {
// do the migration
static async up(_db: PrismaClient, ref: ModuleRef) {
static async up(db: PrismaClient, ref: ModuleRef) {
const config = ref.get(Config, { strict: false });
const crypto = ref.get(CryptoHelper, { strict: false });
const user = ref.get(UserService, { strict: false });
if (config.isSelfhosted) {
const crypto = ref.get(CryptoHelper, { strict: false });
const user = ref.get(UserService, { strict: false });
const feature = ref.get(FeatureManagementService, { strict: false });
if (
!process.env.AFFINE_ADMIN_EMAIL ||
!process.env.AFFINE_ADMIN_PASSWORD
@@ -19,6 +21,7 @@ export class SelfHostAdmin99999999 {
'You have to set AFFINE_ADMIN_EMAIL and AFFINE_ADMIN_PASSWORD environment variables to generate the initial user for self-hosted AFFiNE Server.'
);
}
await user.findOrCreateUser(process.env.AFFINE_ADMIN_EMAIL, {
name: 'AFFINE First User',
emailVerifiedAt: new Date(),
@@ -26,6 +29,15 @@ export class SelfHostAdmin99999999 {
process.env.AFFINE_ADMIN_PASSWORD
),
});
const firstUser = await db.user.findFirst({
orderBy: {
createdAt: 'asc',
},
});
if (firstUser) {
await feature.addAdmin(firstUser.id);
}
}
}

View File

@@ -0,0 +1,14 @@
import { PrismaClient } from '@prisma/client';
import { FeatureType } from '../../core/features';
import { upsertLatestFeatureVersion } from './utils/user-features';
export class AdministratorFeature1716195522794 {
// do the migration
static async up(db: PrismaClient) {
await upsertLatestFeatureVersion(db, FeatureType.Admin);
}
// revert the migration
static async down(_db: PrismaClient) {}
}