2021-04-20 09:58:23 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-04-22 16:05:59 +02:00
|
|
|
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';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../../types/option';
|
2021-04-22 16:05:59 +02:00
|
|
|
import { IUnleashServices } from '../../types/services';
|
|
|
|
import UserService from '../../services/user-service';
|
2021-04-22 23:40:52 +02:00
|
|
|
import User from '../../types/user';
|
2021-04-22 16:05:59 +02:00
|
|
|
import { Logger } from '../../logger';
|
|
|
|
import { handleErrors } from './util';
|
2021-04-20 09:58:23 +02:00
|
|
|
|
2021-04-22 16:05:59 +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;
|
|
|
|
|
2021-04-22 16:05:59 +02:00
|
|
|
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;
|
2021-04-22 16:05:59 +02:00
|
|
|
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);
|
2021-04-22 16:05:59 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-04-22 16:05:59 +02:00
|
|
|
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;
|