2021-04-27 09:16:44 +02:00
|
|
|
import { Request, Response } from 'express';
|
2021-04-09 13:46:53 +02:00
|
|
|
import Controller from '../controller';
|
2021-12-03 12:46:50 +01:00
|
|
|
import { ADMIN, NONE } from '../../types/permissions';
|
2021-04-09 13:46:53 +02:00
|
|
|
import UserService from '../../services/user-service';
|
2023-01-18 17:08:07 +01:00
|
|
|
import { AccountService } from '../../services/account-service';
|
2021-04-16 15:29:23 +02:00
|
|
|
import { AccessService } from '../../services/access-service';
|
2021-04-09 13:46:53 +02:00
|
|
|
import { Logger } from '../../logger';
|
2022-07-26 13:39:55 +02:00
|
|
|
import { IUnleashConfig, IUnleashServices } from '../../types';
|
2021-04-27 20:47:11 +02:00
|
|
|
import { EmailService } from '../../services/email-service';
|
2021-04-23 10:58:47 +02:00
|
|
|
import ResetTokenService from '../../services/reset-token-service';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { IAuthRequest } from '../unleash-types';
|
2022-01-28 12:50:35 +01:00
|
|
|
import SettingService from '../../services/setting-service';
|
2022-05-31 08:43:53 +02:00
|
|
|
import { IUser, SimpleAuthSettings } from '../../server-impl';
|
2022-08-26 09:09:48 +02:00
|
|
|
import { simpleAuthSettingsKey } from '../../types/settings/simple-auth-settings';
|
2022-05-31 08:43:53 +02:00
|
|
|
import { anonymise } from '../../util/anonymise';
|
2022-06-22 14:55:43 +02:00
|
|
|
import { OpenApiService } from '../../services/openapi-service';
|
2022-07-01 08:06:33 +02:00
|
|
|
import { createRequestSchema } from '../../openapi/util/create-request-schema';
|
|
|
|
import { createResponseSchema } from '../../openapi/util/create-response-schema';
|
2022-06-22 14:55:43 +02:00
|
|
|
import { userSchema, UserSchema } from '../../openapi/spec/user-schema';
|
|
|
|
import { serializeDates } from '../../types/serialize-dates';
|
|
|
|
import { usersSchema, UsersSchema } from '../../openapi/spec/users-schema';
|
|
|
|
import {
|
|
|
|
usersSearchSchema,
|
|
|
|
UsersSearchSchema,
|
|
|
|
} from '../../openapi/spec/users-search-schema';
|
|
|
|
import { CreateUserSchema } from '../../openapi/spec/create-user-schema';
|
|
|
|
import { UpdateUserSchema } from '../../openapi/spec/update-user-schema';
|
|
|
|
import { PasswordSchema } from '../../openapi/spec/password-schema';
|
|
|
|
import { IdSchema } from '../../openapi/spec/id-schema';
|
|
|
|
import {
|
|
|
|
resetPasswordSchema,
|
|
|
|
ResetPasswordSchema,
|
|
|
|
} from '../../openapi/spec/reset-password-schema';
|
2022-06-30 14:48:39 +02:00
|
|
|
import { emptyResponse } from '../../openapi/util/standard-responses';
|
2022-07-26 13:39:55 +02:00
|
|
|
import { GroupService } from '../../services/group-service';
|
|
|
|
import {
|
|
|
|
UsersGroupsBaseSchema,
|
|
|
|
usersGroupsBaseSchema,
|
|
|
|
} from '../../openapi/spec/users-groups-base-schema';
|
|
|
|
import { IGroup } from '../../types/group';
|
2022-08-26 08:22:42 +02:00
|
|
|
import { IFlagResolver } from '../../types/experimental';
|
2021-08-13 10:50:48 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
export default class UserAdminController extends Controller {
|
2022-08-26 08:22:42 +02:00
|
|
|
private flagResolver: IFlagResolver;
|
2022-05-31 08:43:53 +02:00
|
|
|
|
2021-04-09 13:46:53 +02:00
|
|
|
private userService: UserService;
|
|
|
|
|
2023-01-18 17:08:07 +01:00
|
|
|
private accountService: AccountService;
|
|
|
|
|
2021-04-09 13:46:53 +02:00
|
|
|
private accessService: AccessService;
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
private readonly logger: Logger;
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2021-04-23 10:58:47 +02:00
|
|
|
private emailService: EmailService;
|
|
|
|
|
|
|
|
private resetTokenService: ResetTokenService;
|
|
|
|
|
2022-01-28 12:50:35 +01:00
|
|
|
private settingService: SettingService;
|
|
|
|
|
2022-06-22 14:55:43 +02:00
|
|
|
private openApiService: OpenApiService;
|
|
|
|
|
2022-07-26 13:39:55 +02:00
|
|
|
private groupService: GroupService;
|
|
|
|
|
2022-01-28 12:50:35 +01:00
|
|
|
readonly unleashUrl: string;
|
|
|
|
|
2021-04-23 10:58:47 +02:00
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
2021-04-27 09:05:46 +02:00
|
|
|
{
|
|
|
|
userService,
|
2023-01-18 17:08:07 +01:00
|
|
|
accountService,
|
2021-04-27 09:05:46 +02:00
|
|
|
accessService,
|
|
|
|
emailService,
|
|
|
|
resetTokenService,
|
2022-01-28 12:50:35 +01:00
|
|
|
settingService,
|
2022-06-22 14:55:43 +02:00
|
|
|
openApiService,
|
2022-07-26 13:39:55 +02:00
|
|
|
groupService,
|
2021-04-27 09:05:46 +02:00
|
|
|
}: Pick<
|
2021-05-11 12:15:20 +02:00
|
|
|
IUnleashServices,
|
|
|
|
| 'userService'
|
2023-01-18 17:08:07 +01:00
|
|
|
| 'accountService'
|
2021-05-11 12:15:20 +02:00
|
|
|
| 'accessService'
|
|
|
|
| 'emailService'
|
|
|
|
| 'resetTokenService'
|
2022-01-28 12:50:35 +01:00
|
|
|
| 'settingService'
|
2022-06-22 14:55:43 +02:00
|
|
|
| 'openApiService'
|
2022-07-26 13:39:55 +02:00
|
|
|
| 'groupService'
|
2021-04-27 09:05:46 +02:00
|
|
|
>,
|
2021-04-23 10:58:47 +02:00
|
|
|
) {
|
2021-04-09 13:46:53 +02:00
|
|
|
super(config);
|
|
|
|
this.userService = userService;
|
2023-01-18 17:08:07 +01:00
|
|
|
this.accountService = accountService;
|
2021-04-09 13:46:53 +02:00
|
|
|
this.accessService = accessService;
|
2021-04-23 10:58:47 +02:00
|
|
|
this.emailService = emailService;
|
|
|
|
this.resetTokenService = resetTokenService;
|
2022-01-28 12:50:35 +01:00
|
|
|
this.settingService = settingService;
|
2022-06-22 14:55:43 +02:00
|
|
|
this.openApiService = openApiService;
|
2022-07-26 13:39:55 +02:00
|
|
|
this.groupService = groupService;
|
2022-01-28 12:50:35 +01:00
|
|
|
this.logger = config.getLogger('routes/user-controller.ts');
|
|
|
|
this.unleashUrl = config.server.unleashUrl;
|
2022-08-26 08:22:42 +02:00
|
|
|
this.flagResolver = config.flagResolver;
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2022-06-22 14:55:43 +02:00
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/validate-password',
|
2022-06-30 10:51:26 +02:00
|
|
|
handler: this.validateUserPassword,
|
2022-06-22 14:55:43 +02:00
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Users'],
|
2022-06-30 10:51:26 +02:00
|
|
|
operationId: 'validateUserPassword',
|
2022-06-22 14:55:43 +02:00
|
|
|
requestBody: createRequestSchema('passwordSchema'),
|
|
|
|
responses: { 200: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/:id/change-password',
|
2022-06-30 10:51:26 +02:00
|
|
|
handler: this.changeUserPassword,
|
2022-06-22 14:55:43 +02:00
|
|
|
permission: ADMIN,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Users'],
|
2022-06-30 10:51:26 +02:00
|
|
|
operationId: 'changeUserPassword',
|
2022-06-22 14:55:43 +02:00
|
|
|
requestBody: createRequestSchema('passwordSchema'),
|
|
|
|
responses: { 200: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/reset-password',
|
2022-06-30 10:51:26 +02:00
|
|
|
handler: this.resetUserPassword,
|
2022-06-22 14:55:43 +02:00
|
|
|
permission: ADMIN,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Users'],
|
2022-06-30 10:51:26 +02:00
|
|
|
operationId: 'resetUserPassword',
|
2022-06-22 14:55:43 +02:00
|
|
|
requestBody: createRequestSchema('idSchema'),
|
|
|
|
responses: {
|
|
|
|
200: createResponseSchema('resetPasswordSchema'),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'get',
|
|
|
|
path: '',
|
|
|
|
handler: this.getUsers,
|
|
|
|
permission: ADMIN,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Users'],
|
2022-06-22 14:55:43 +02:00
|
|
|
operationId: 'getUsers',
|
|
|
|
responses: { 200: createResponseSchema('usersSchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'get',
|
|
|
|
path: '/search',
|
|
|
|
handler: this.searchUsers,
|
2022-07-26 13:39:55 +02:00
|
|
|
permission: NONE,
|
2022-06-22 14:55:43 +02:00
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Users'],
|
2022-06-22 14:55:43 +02:00
|
|
|
operationId: 'searchUsers',
|
|
|
|
responses: { 200: createResponseSchema('usersSchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2022-07-26 13:39:55 +02:00
|
|
|
this.route({
|
|
|
|
method: 'get',
|
|
|
|
path: '/access',
|
|
|
|
handler: this.getBaseUsersAndGroups,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Users'],
|
2022-07-26 13:39:55 +02:00
|
|
|
operationId: 'getBaseUsersAndGroups',
|
|
|
|
responses: {
|
|
|
|
200: createResponseSchema('usersGroupsBaseSchema'),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2022-06-22 14:55:43 +02:00
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '',
|
|
|
|
handler: this.createUser,
|
|
|
|
permission: ADMIN,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Users'],
|
2022-06-22 14:55:43 +02:00
|
|
|
operationId: 'createUser',
|
|
|
|
requestBody: createRequestSchema('createUserSchema'),
|
|
|
|
responses: { 200: createResponseSchema('userSchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'get',
|
|
|
|
path: '/:id',
|
|
|
|
handler: this.getUser,
|
|
|
|
permission: ADMIN,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Users'],
|
2022-06-22 14:55:43 +02:00
|
|
|
operationId: 'getUser',
|
|
|
|
responses: { 200: createResponseSchema('userSchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'put',
|
|
|
|
path: '/:id',
|
|
|
|
handler: this.updateUser,
|
|
|
|
permission: ADMIN,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Users'],
|
2022-06-22 14:55:43 +02:00
|
|
|
operationId: 'updateUser',
|
|
|
|
requestBody: createRequestSchema('updateUserSchema'),
|
|
|
|
responses: { 200: createResponseSchema('userSchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'delete',
|
|
|
|
path: '/:id',
|
|
|
|
acceptAnyContentType: true,
|
|
|
|
handler: this.deleteUser,
|
|
|
|
permission: ADMIN,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Users'],
|
2022-06-22 14:55:43 +02:00
|
|
|
operationId: 'deleteUser',
|
|
|
|
responses: { 200: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2021-04-16 15:29:23 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 10:51:26 +02:00
|
|
|
async resetUserPassword(
|
2022-06-22 14:55:43 +02:00
|
|
|
req: IAuthRequest<unknown, ResetPasswordSchema, IdSchema>,
|
|
|
|
res: Response<ResetPasswordSchema>,
|
|
|
|
): Promise<void> {
|
2021-04-27 20:47:11 +02:00
|
|
|
const { user } = req;
|
2021-09-13 10:23:57 +02:00
|
|
|
const receiver = req.body.id;
|
|
|
|
const resetPasswordUrl =
|
|
|
|
await this.userService.createResetPasswordEmail(receiver, user);
|
2022-06-22 14:55:43 +02:00
|
|
|
|
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
resetPasswordSchema.$id,
|
|
|
|
{ resetPasswordUrl: resetPasswordUrl.toString() },
|
|
|
|
);
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
2022-06-22 14:55:43 +02:00
|
|
|
async getUsers(req: Request, res: Response<UsersSchema>): Promise<void> {
|
2023-01-18 17:08:07 +01:00
|
|
|
const users = await this.userService.getAll();
|
2022-01-28 12:50:35 +01:00
|
|
|
const rootRoles = await this.accessService.getRootRoles();
|
|
|
|
const inviteLinks = await this.resetTokenService.getActiveInvitations();
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2022-01-28 12:50:35 +01:00
|
|
|
const usersWithInviteLinks = users.map((user) => {
|
|
|
|
const inviteLink = inviteLinks[user.id] || '';
|
|
|
|
return { ...user, inviteLink };
|
|
|
|
});
|
|
|
|
|
2022-06-22 14:55:43 +02:00
|
|
|
this.openApiService.respondWithValidation(200, res, usersSchema.$id, {
|
|
|
|
users: serializeDates(usersWithInviteLinks),
|
|
|
|
rootRoles,
|
|
|
|
});
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 08:43:53 +02:00
|
|
|
anonymiseUsers(users: IUser[]): IUser[] {
|
|
|
|
return users.map((u) => ({
|
|
|
|
...u,
|
|
|
|
email: anonymise(u.email || 'random'),
|
|
|
|
imageUrl:
|
|
|
|
'https://gravatar.com/avatar/21232f297a57a5a743894a0e4a801fc3?size=42&default=retro',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-06-22 14:55:43 +02:00
|
|
|
async searchUsers(
|
|
|
|
req: Request,
|
|
|
|
res: Response<UsersSearchSchema>,
|
|
|
|
): Promise<void> {
|
|
|
|
const { q } = req.query;
|
|
|
|
let users =
|
|
|
|
typeof q === 'string' && q.length > 1
|
|
|
|
? await this.userService.search(q)
|
|
|
|
: [];
|
2022-08-26 08:22:42 +02:00
|
|
|
if (this.flagResolver.isEnabled('anonymiseEventLog')) {
|
2022-06-22 14:55:43 +02:00
|
|
|
users = this.anonymiseUsers(users);
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
2022-06-22 14:55:43 +02:00
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
usersSearchSchema.$id,
|
|
|
|
serializeDates(users),
|
|
|
|
);
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 13:39:55 +02:00
|
|
|
async getBaseUsersAndGroups(
|
|
|
|
req: Request,
|
|
|
|
res: Response<UsersGroupsBaseSchema>,
|
|
|
|
): Promise<void> {
|
2023-01-18 17:08:07 +01:00
|
|
|
let allUsers = await this.accountService.getAll();
|
2022-07-26 13:39:55 +02:00
|
|
|
let users = allUsers.map((u) => {
|
|
|
|
return {
|
|
|
|
id: u.id,
|
|
|
|
name: u.name,
|
|
|
|
username: u.username,
|
|
|
|
email: u.email,
|
2023-01-18 13:12:44 +01:00
|
|
|
accountType: u.accountType,
|
2022-07-26 13:39:55 +02:00
|
|
|
} as IUser;
|
|
|
|
});
|
|
|
|
|
|
|
|
let allGroups = await this.groupService.getAll();
|
|
|
|
let groups = allGroups.map((g) => {
|
|
|
|
return {
|
|
|
|
id: g.id,
|
|
|
|
name: g.name,
|
|
|
|
userCount: g.users.length,
|
|
|
|
} as IGroup;
|
|
|
|
});
|
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
usersGroupsBaseSchema.$id,
|
|
|
|
{
|
|
|
|
users: serializeDates(users),
|
|
|
|
groups: serializeDates(groups),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-22 14:55:43 +02:00
|
|
|
async getUser(req: Request, res: Response<UserSchema>): Promise<void> {
|
2021-10-28 14:24:09 +02:00
|
|
|
const { id } = req.params;
|
|
|
|
const user = await this.userService.getUser(Number(id));
|
2022-06-22 14:55:43 +02:00
|
|
|
|
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
userSchema.$id,
|
|
|
|
serializeDates(user),
|
|
|
|
);
|
2021-10-28 14:24:09 +02:00
|
|
|
}
|
|
|
|
|
2021-08-13 10:50:48 +02:00
|
|
|
async createUser(
|
2022-06-22 14:55:43 +02:00
|
|
|
req: IAuthRequest<unknown, unknown, CreateUserSchema>,
|
|
|
|
res: Response<UserSchema>,
|
2021-08-13 10:50:48 +02:00
|
|
|
): Promise<void> {
|
2023-03-08 11:47:42 +01:00
|
|
|
const { username, email, name, rootRole, sendEmail, password } =
|
|
|
|
req.body;
|
2021-04-23 10:58:47 +02:00
|
|
|
const { user } = req;
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2022-06-22 14:55:43 +02:00
|
|
|
const createdUser = await this.userService.createUser(
|
|
|
|
{
|
|
|
|
username,
|
|
|
|
email,
|
|
|
|
name,
|
2023-03-08 11:47:42 +01:00
|
|
|
password,
|
2022-06-22 14:55:43 +02:00
|
|
|
rootRole,
|
|
|
|
},
|
|
|
|
user,
|
|
|
|
);
|
|
|
|
|
|
|
|
const passwordAuthSettings =
|
2022-08-26 09:09:48 +02:00
|
|
|
await this.settingService.get<SimpleAuthSettings>(
|
|
|
|
simpleAuthSettingsKey,
|
|
|
|
);
|
2022-06-22 14:55:43 +02:00
|
|
|
|
|
|
|
let inviteLink: string;
|
|
|
|
if (!passwordAuthSettings?.disabled) {
|
|
|
|
const inviteUrl = await this.resetTokenService.createNewUserUrl(
|
|
|
|
createdUser.id,
|
|
|
|
user.email,
|
2021-04-27 20:47:11 +02:00
|
|
|
);
|
2022-06-22 14:55:43 +02:00
|
|
|
inviteLink = inviteUrl.toString();
|
|
|
|
}
|
2022-01-28 12:50:35 +01:00
|
|
|
|
2022-06-22 14:55:43 +02:00
|
|
|
let emailSent = false;
|
|
|
|
const emailConfigured = this.emailService.configured();
|
|
|
|
const reallySendEmail =
|
|
|
|
emailConfigured && (sendEmail !== undefined ? sendEmail : true);
|
|
|
|
|
|
|
|
if (reallySendEmail) {
|
|
|
|
try {
|
|
|
|
await this.emailService.sendGettingStartedMail(
|
|
|
|
createdUser.name,
|
|
|
|
createdUser.email,
|
|
|
|
this.unleashUrl,
|
|
|
|
inviteLink,
|
2022-01-28 12:50:35 +01:00
|
|
|
);
|
2022-06-22 14:55:43 +02:00
|
|
|
emailSent = true;
|
|
|
|
} catch (e) {
|
2021-04-23 10:58:47 +02:00
|
|
|
this.logger.warn(
|
2022-06-22 14:55:43 +02:00
|
|
|
'email was configured, but sending failed due to: ',
|
|
|
|
e,
|
2021-04-23 10:58:47 +02:00
|
|
|
);
|
|
|
|
}
|
2022-06-22 14:55:43 +02:00
|
|
|
} else {
|
|
|
|
this.logger.warn(
|
|
|
|
'email was not sent to the user because email configuration is lacking',
|
|
|
|
);
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
2022-06-22 14:55:43 +02:00
|
|
|
|
|
|
|
const responseData: UserSchema = {
|
|
|
|
...serializeDates(createdUser),
|
|
|
|
inviteLink: inviteLink || this.unleashUrl,
|
|
|
|
emailSent,
|
|
|
|
rootRole,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
201,
|
|
|
|
res,
|
|
|
|
userSchema.$id,
|
|
|
|
responseData,
|
|
|
|
);
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
2022-06-22 14:55:43 +02:00
|
|
|
async updateUser(
|
|
|
|
req: IAuthRequest<{ id: string }, UserSchema, UpdateUserSchema>,
|
|
|
|
res: Response<UserSchema>,
|
|
|
|
): Promise<void> {
|
2021-04-27 20:47:11 +02:00
|
|
|
const { user, params, body } = req;
|
|
|
|
const { id } = params;
|
|
|
|
const { name, email, rootRole } = body;
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2022-06-22 14:55:43 +02:00
|
|
|
const updateUser = await this.userService.updateUser(
|
|
|
|
{
|
|
|
|
id: Number(id),
|
|
|
|
name,
|
|
|
|
email,
|
|
|
|
rootRole,
|
|
|
|
},
|
|
|
|
user,
|
|
|
|
);
|
|
|
|
|
|
|
|
this.openApiService.respondWithValidation(200, res, userSchema.$id, {
|
|
|
|
...serializeDates(updateUser),
|
|
|
|
rootRole,
|
|
|
|
});
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async deleteUser(req: IAuthRequest, res: Response): Promise<void> {
|
2021-04-27 20:47:11 +02:00
|
|
|
const { user, params } = req;
|
|
|
|
const { id } = params;
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2021-09-13 10:23:57 +02:00
|
|
|
await this.userService.deleteUser(+id, user);
|
|
|
|
res.status(200).send();
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 10:51:26 +02:00
|
|
|
async validateUserPassword(
|
2022-06-22 14:55:43 +02:00
|
|
|
req: IAuthRequest<unknown, unknown, PasswordSchema>,
|
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
2021-04-09 13:46:53 +02:00
|
|
|
const { password } = req.body;
|
|
|
|
|
2021-09-13 10:23:57 +02:00
|
|
|
this.userService.validatePassword(password);
|
|
|
|
res.status(200).send();
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 10:51:26 +02:00
|
|
|
async changeUserPassword(
|
2022-06-22 14:55:43 +02:00
|
|
|
req: IAuthRequest<{ id: string }, unknown, PasswordSchema>,
|
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
2021-04-09 13:46:53 +02:00
|
|
|
const { id } = req.params;
|
|
|
|
const { password } = req.body;
|
|
|
|
|
2021-09-13 10:23:57 +02:00
|
|
|
await this.userService.changePassword(+id, password);
|
|
|
|
res.status(200).send();
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
}
|