|
|
@ -1,7 +1,10 @@ |
|
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; |
|
|
|
import { PropertyService } from '@ghostfolio/api/services/property/property.service'; |
|
|
|
import { PROPERTY_IS_READ_ONLY_MODE } from '@ghostfolio/common/config'; |
|
|
|
import { User } from '@ghostfolio/common/interfaces'; |
|
|
|
import { |
|
|
|
getPermissions, |
|
|
|
hasPermission, |
|
|
|
hasRole, |
|
|
|
permissions |
|
|
|
} from '@ghostfolio/common/permissions'; |
|
|
|
import type { RequestWithUser } from '@ghostfolio/common/types'; |
|
|
@ -20,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'; |
|
|
|
|
|
|
@ -34,7 +37,9 @@ import { UserService } from './user.service'; |
|
|
|
@Controller('user') |
|
|
|
export class UserController { |
|
|
|
public constructor( |
|
|
|
private readonly configurationService: ConfigurationService, |
|
|
|
private jwtService: JwtService, |
|
|
|
private readonly propertyService: PropertyService, |
|
|
|
@Inject(REQUEST) private readonly request: RequestWithUser, |
|
|
|
private readonly userService: UserService |
|
|
|
) {} |
|
|
@ -43,10 +48,7 @@ export class UserController { |
|
|
|
@UseGuards(AuthGuard('jwt')) |
|
|
|
public async deleteUser(@Param('id') id: string): Promise<UserModel> { |
|
|
|
if ( |
|
|
|
!hasPermission( |
|
|
|
getPermissions(this.request.user.role), |
|
|
|
permissions.deleteUser |
|
|
|
) || |
|
|
|
!hasPermission(this.request.user.permissions, permissions.deleteUser) || |
|
|
|
id === this.request.user.id |
|
|
|
) { |
|
|
|
throw new HttpException( |
|
|
@ -68,6 +70,19 @@ export class UserController { |
|
|
|
|
|
|
|
@Post() |
|
|
|
public async signupUser(): Promise<UserItem> { |
|
|
|
if (this.configurationService.get('ENABLE_FEATURE_READ_ONLY_MODE')) { |
|
|
|
const isReadOnlyMode = (await this.propertyService.getByKey( |
|
|
|
PROPERTY_IS_READ_ONLY_MODE |
|
|
|
)) as boolean; |
|
|
|
|
|
|
|
if (isReadOnlyMode) { |
|
|
|
throw new HttpException( |
|
|
|
getReasonPhrase(StatusCodes.FORBIDDEN), |
|
|
|
StatusCodes.FORBIDDEN |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const { accessToken, id } = await this.userService.createUser({ |
|
|
|
provider: Provider.ANONYMOUS |
|
|
|
}); |
|
|
@ -85,7 +100,7 @@ export class UserController { |
|
|
|
public async updateUserSetting(@Body() data: UpdateUserSettingDto) { |
|
|
|
if ( |
|
|
|
!hasPermission( |
|
|
|
getPermissions(this.request.user.role), |
|
|
|
this.request.user.permissions, |
|
|
|
permissions.updateUserSettings |
|
|
|
) |
|
|
|
) { |
|
|
@ -111,7 +126,7 @@ export class UserController { |
|
|
|
public async updateUserSettings(@Body() data: UpdateUserSettingsDto) { |
|
|
|
if ( |
|
|
|
!hasPermission( |
|
|
|
getPermissions(this.request.user.role), |
|
|
|
this.request.user.permissions, |
|
|
|
permissions.updateUserSettings |
|
|
|
) |
|
|
|
) { |
|
|
@ -127,10 +142,7 @@ export class UserController { |
|
|
|
}; |
|
|
|
|
|
|
|
if ( |
|
|
|
hasPermission( |
|
|
|
getPermissions(this.request.user.role), |
|
|
|
permissions.updateViewMode |
|
|
|
) |
|
|
|
hasPermission(this.request.user.permissions, permissions.updateViewMode) |
|
|
|
) { |
|
|
|
userSettings.viewMode = data.viewMode; |
|
|
|
} |
|
|
|