1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/src/lib/routes/admin-api/user.ts

88 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-04-20 09:58:23 +02:00
'use strict';
import { Request, Response } from 'express';
2021-04-20 09:58:23 +02:00
import { IAuthRequest } from '../unleash-types';
import Controller from '../controller';
import { AccessService } from '../../services/access-service';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import UserService from '../../services/user-service';
import User from '../../user';
import { Logger } from '../../logger';
import { handleErrors } from './util';
2021-04-20 09:58:23 +02:00
interface IChangeUserRequest {
password: string;
confirmPassword: string;
}
interface UserRequest<PARAM, QUERY, BODY, RESPONSE>
extends Request<PARAM, QUERY, BODY, RESPONSE> {
user: User;
2021-04-20 09:58:23 +02:00
}
class UserController extends Controller {
private accessService: AccessService;
private userService: UserService;
private logger: Logger;
constructor(
config: IUnleashConfig,
{
accessService,
userService,
}: Pick<IUnleashServices, 'accessService' | 'userService'>,
) {
2021-04-20 09:58:23 +02:00
super(config);
this.accessService = accessService;
this.userService = userService;
this.logger = config.getLogger('lib/routes/admin-api/user.ts');
2021-04-20 09:58:23 +02:00
this.get('/', this.getUser);
this.post('/change-password', this.updateUserPass);
2021-04-20 09:58:23 +02:00
}
async getUser(req: IAuthRequest, res: Response): Promise<void> {
const { user } = req;
if (user) {
const permissions = await this.accessService.getPermissionsForUser(
user,
);
delete user.permissions; // TODO: remove
return res
.status(200)
.json({ user, permissions })
.end();
}
return res.status(404).end();
}
async updateUserPass(
req: UserRequest<any, any, IChangeUserRequest, any>,
res: Response,
): Promise<void> {
const { user } = req;
if (user) {
const { password, confirmPassword } = req.body;
try {
if (password === confirmPassword) {
this.userService.validatePassword(password);
await this.userService.changePassword(user.id, password);
res.status(200).end();
} else {
res.status(400).end();
}
} catch (e) {
handleErrors(res, this.logger, e);
}
} else {
res.status(401).end();
2021-04-20 09:58:23 +02:00
}
}
}
module.exports = UserController;
export default UserController;