Skip to content

Commit

Permalink
feat(users): update change user password validation for admin (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDO authored Feb 5, 2024
1 parent e24ef6a commit 0e399ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
18 changes: 18 additions & 0 deletions http-requests/users/requests.http
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,21 @@ Authorization: Bearer admintokenooooooooooooooooooooon
}
}
}

### Change user password as an application admin
POST http://127.0.0.1:3000
Accept: application/json
Content-Type: application/json
## Admin token
Authorization: Bearer admintokenooooooooooooooooooooon

{
"id": "1",
"method": "users.user.change-password",
"params": {
"userId": "68827b31-33e9-45b5-bf9f-8823b993d0ef",
"newPassword": "123456789!A",
"allowedByAdmin": true
}
}

15 changes: 13 additions & 2 deletions microservices/users/src/methods/user/change-password.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Endpoint, IsType, IsUndefinable } from '@lomray/microservice-helpers';
import { IsBoolean, IsEnum, IsNotEmpty, IsString, ValidateIf } from 'class-validator';
import { JSONSchema } from 'class-validator-jsonschema';
import { getCustomRepository, getRepository } from 'typeorm';
import ConfirmCode from '@entities/confirm-code';
import User from '@entities/user';
Expand All @@ -23,18 +24,28 @@ class ChangePasswordInput {
@IsNotEmpty()
newPassword: string;

@JSONSchema({
description: 'Skip if change password has allowed by admin',
})
@IsString()
@IsNotEmpty()
@ValidateIf(({ confirmCode, oldPassword }) => !confirmCode || oldPassword)
@ValidateIf(
({ confirmCode, oldPassword, allowByAdmin }) => !allowByAdmin && (!confirmCode || oldPassword),
)
oldPassword?: string;

@IsEnum(ConfirmBy)
@ValidateIf(({ confirmCode }) => confirmCode)
confirmBy?: ConfirmBy;

@JSONSchema({
description: 'Skip if change password has allowed by admin',
})
@IsType(['string', 'number'])
@IsNotEmpty()
@ValidateIf(({ confirmCode, oldPassword }) => !oldPassword || confirmCode)
@ValidateIf(
({ confirmCode, oldPassword, allowByAdmin }) => !allowByAdmin && (!oldPassword || confirmCode),
)
confirmCode?: string | number;

@IsBoolean()
Expand Down

0 comments on commit 0e399ad

Please sign in to comment.