Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

omitting some properties when updating user

Blue Mouse9e2121f9eebd
diff --git a/src/user/dto/admin-update-user.dto.ts b/src/user/dto/admin-update-user.dto.ts
new file mode 100644
index 000000000..acca90ad1
--- /dev/null
+++ b/src/user/dto/admin-update-user.dto.ts
@@ -0,0 +1,35 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsEmail } from 'class-validator'
+
+export class AdminUpdateUserProfileDTO {
+ @ApiProperty({ example: '[email protected]', required: true })
+ @IsEmail()
+ email: string
+
+ @ApiProperty({ example: 'your_password123' })
+ password: string
+
+ @ApiProperty({ example: true })
+ isActive: boolean
+
+ @ApiProperty({ example: 'free' })
+ planCode: string
+
+ @ApiProperty({ example: 'customer' })
+ roles: Array<string>
+
+ @ApiProperty({ example: 'date' })
+ created: string
+
+ @ApiProperty({ example: 'Number' })
+ emailRequests: number
+
+ @ApiProperty({ example: 'reportFrequency' })
+ reportFrequency: string
+
+ @ApiProperty({ example: 'updated' })
+ updated: string
+
+ @ApiProperty({ example: 'Europe/Kiev' })
+ timezone: string
+}
diff --git a/src/user/dto/update-user.dto.ts b/src/user/dto/update-user.dto.ts
index c938cfa7a..2130937f8 100644
--- a/src/user/dto/update-user.dto.ts
+++ b/src/user/dto/update-user.dto.ts
@@ -9,24 +9,9 @@ export class UpdateUserProfileDTO {
@ApiProperty({ example: 'your_password123' })
password: string
- @ApiProperty({ example: true })
- isActive: boolean
-
- @ApiProperty({ example: 'free' })
- planCode: string
-
- @ApiProperty({ example: 'customer' })
- roles: Array<string>
-
- @ApiProperty({ example: 'date' })
- created: string
-
- @ApiProperty({ example: 'Number' })
- emailRequests: number
-
- @ApiProperty({ example: 'reportFrequency' })
+ @ApiProperty({ example: 'week' })
reportFrequency: string
- @ApiProperty({ example: 'updated' })
- updated: string
+ @ApiProperty({ example: 'Europe/Kiev' })
+ timezone: string
}
diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts
index 251be328a..c7f49ad53 100644
--- a/src/user/user.controller.ts
+++ b/src/user/user.controller.ts
@@ -9,6 +9,8 @@ import * as _map from 'lodash/map'
import * as _join from 'lodash/join'
import * as _isNull from 'lodash/isNull'
import * as _isEmpty from 'lodash/isEmpty'
+import * as _isString from 'lodash/isString'
+import * as _omit from 'lodash/omit'
import { UserService } from './user.service'
import { ProjectService } from '../project/project.service'
@@ -23,6 +25,7 @@ import {
import { RolesGuard } from 'src/common/guards/roles.guard'
import { SelfhostedGuard } from '../common/guards/selfhosted.guard'
import { UpdateUserProfileDTO } from './dto/update-user.dto'
+import { AdminUpdateUserProfileDTO } from './dto/admin-update-user.dto'
import { CurrentUserId } from 'src/common/decorators/current-user-id.decorator'
import { ActionTokensService } from '../action-tokens/action-tokens.service'
import { MailerService } from '../mailer/mailer.service'
@@ -185,8 +188,8 @@ export class UserController {
@UseGuards(RolesGuard)
@UseGuards(SelfhostedGuard)
@Roles(UserType.ADMIN)
- async update(@Body() userDTO: UpdateUserProfileDTO, @Param('id') id: string): Promise<User> {
- this.logger.log({ userDTO, id }, 'DELETE /user/:id')
+ async update(@Body() userDTO: AdminUpdateUserProfileDTO, @Param('id') id: string): Promise<User> {
+ this.logger.log({ userDTO, id }, 'PUT /user/:id')
if (userDTO.password) {
this.userService.validatePassword(userDTO.password)
@@ -197,9 +200,9 @@ export class UserController {
try {
if (!user) {
- await this.userService.create({...userDTO})
+ await this.userService.create({ ...userDTO })
}
- await this.userService.update(id, {...user, ...userDTO})
+ await this.userService.update(id, { ...user, ...userDTO })
return this.userService.findOneWhere({ id })
} catch (e) {
if (e.code === 'ER_DUP_ENTRY'){
@@ -219,7 +222,7 @@ export class UserController {
this.logger.log({ userDTO, id }, 'PUT /user')
const user = await this.userService.findOneWhere({ id })
- if (userDTO.password?.length > 0) {
+ if (!_isEmpty(userDTO.password) && _isString(userDTO.password)) {
this.userService.validatePassword(userDTO.password)
userDTO.password = await this.authService.hashPassword(userDTO.password)
await this.mailerService.sendEmail(userDTO.email, LetterTemplate.PasswordChanged)
@@ -237,8 +240,10 @@ export class UserController {
const url = `${request.headers.origin}/change-email/${token.id}`
await this.mailerService.sendEmail(user.email, LetterTemplate.MailAddressChangeConfirmation, { url })
}
- await this.userService.update(id, { ...userDTO })
-
+ // delete internal properties from userDTO before updating it
+ const userToUpdate = _omit(userDTO, ['id', 'isActive', 'evWarningSentOn', 'exportedAt', 'subID', 'subUpdateURL', 'subCancelURL', 'projects', 'actionTokens', 'roles', 'created', 'updated', 'planCode'])
+ console.log(userToUpdate)
+ await this.userService.update(id, userToUpdate)
return this.userService.findOneWhere({ id })
} catch (e) {
throw new BadRequestException(e.message)