add DEFAULT_ROLE and LOGIN_NOTE env variables https://github.com/ohmyform/ohmyform/issues/30
This commit is contained in:
parent
d23ec38025
commit
91812f822f
@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- `DEFAULT_ROLE` -> `admin` | `superuser` | `user` - with `user` being the default, making it possible that new users can create their own forms after creating
|
||||
- `LOGIN_NOTE` -> markdown for Login Page, to show info text on login page
|
||||
|
||||
### Changed
|
||||
### Fixed
|
||||
|
||||
|
||||
@ -1,20 +1,14 @@
|
||||
import {Injectable} from '@nestjs/common'
|
||||
import {ConfigService} from '@nestjs/config'
|
||||
import {Args, ID, Query} from '@nestjs/graphql'
|
||||
import {Roles} from '../../decorator/roles.decorator'
|
||||
import {User} from '../../decorator/user.decorator'
|
||||
import {PagerSettingModel} from '../../dto/setting/pager.setting.model'
|
||||
import {SettingModel} from '../../dto/setting/setting.model'
|
||||
import {UserModel} from '../../dto/user/user.model'
|
||||
import {UserDocument} from '../../schema/user.schema'
|
||||
import {SettingService} from '../../service/setting.service'
|
||||
|
||||
@Injectable()
|
||||
export class SettingResolver {
|
||||
private publicKeys: string[] = [
|
||||
'SIGNUP_DISABLED',
|
||||
]
|
||||
|
||||
constructor(
|
||||
private readonly settingService: SettingService,
|
||||
) {
|
||||
@ -37,7 +31,7 @@ export class SettingResolver {
|
||||
@Args('key', {type: () => ID}) key: string,
|
||||
@User() user: UserDocument,
|
||||
): Promise<SettingModel> {
|
||||
if (!this.publicKeys.includes(key) && !user.roles.includes('superuser')) {
|
||||
if (!this.settingService.isPublicKey(key) && !user.roles.includes('superuser')) {
|
||||
throw new Error(`no access to key ${key}`)
|
||||
}
|
||||
|
||||
|
||||
@ -9,9 +9,17 @@ export class SettingService {
|
||||
) {
|
||||
}
|
||||
|
||||
isPublicKey(key: string): boolean {
|
||||
return [
|
||||
'SIGNUP_DISABLED',
|
||||
'LOGIN_NOTE',
|
||||
].includes(key)
|
||||
}
|
||||
|
||||
async getByKey(key: string): Promise<SettingModel> {
|
||||
switch (key) {
|
||||
case 'SIGNUP_DISABLED':
|
||||
case 'LOGIN_NOTE':
|
||||
return new SettingModel(key, this.configService.get(key))
|
||||
}
|
||||
|
||||
|
||||
@ -2,10 +2,12 @@ import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model } from 'mongoose';
|
||||
import { PinoLogger } from 'nestjs-pino/dist';
|
||||
import {rolesType} from '../../config/roles'
|
||||
import { UserCreateInput } from '../../dto/user/user.create.input';
|
||||
import { UserDocument, UserSchemaName } from '../../schema/user.schema';
|
||||
import { PasswordService } from '../auth/password.service';
|
||||
import { MailService } from '../mail.service';
|
||||
import {SettingService} from '../setting.service'
|
||||
|
||||
@Injectable()
|
||||
export class UserCreateService {
|
||||
@ -14,13 +16,30 @@ export class UserCreateService {
|
||||
private readonly mailerService: MailService,
|
||||
private readonly logger: PinoLogger,
|
||||
private readonly passwordService: PasswordService,
|
||||
private readonly settingService: SettingService,
|
||||
) {}
|
||||
|
||||
private async getDefaultRoles(): Promise<rolesType> {
|
||||
const roleSetting = await this.settingService.getByKey('DEFAULT_ROLE')
|
||||
|
||||
switch (roleSetting.value) {
|
||||
case 'superuser':
|
||||
return ['superuser', 'admin', 'user']
|
||||
|
||||
case 'admin':
|
||||
return ['admin', 'user']
|
||||
}
|
||||
|
||||
return ['user']
|
||||
}
|
||||
|
||||
async create(user: UserCreateInput): Promise<UserDocument> {
|
||||
// TODO check for uniqueness of email & username!
|
||||
|
||||
|
||||
const entry = new this.userModel({
|
||||
...user,
|
||||
roles: await this.getDefaultRoles(),
|
||||
passwordHash: await this.passwordService.hash(user.password),
|
||||
})
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user