2021-04-09 13:46:53 +02:00
|
|
|
import Controller from '../controller';
|
|
|
|
import { ADMIN } from '../../permissions';
|
|
|
|
import UserService from '../../services/user-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';
|
2021-04-16 15:29:23 +02:00
|
|
|
import { handleErrors } from './util';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../../types/option';
|
2021-04-23 10:58:47 +02:00
|
|
|
import { EmailService, MAIL_ACCEPTED } from '../../services/email-service';
|
|
|
|
import ResetTokenService from '../../services/reset-token-service';
|
2021-04-27 09:05:46 +02:00
|
|
|
import { IUnleashServices } from '../../types/services';
|
2021-04-16 15:29:23 +02:00
|
|
|
|
|
|
|
const getCreatorUsernameOrPassword = req => req.user.username || req.user.email;
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
export default class UserAdminController extends Controller {
|
2021-04-09 13:46:53 +02:00
|
|
|
private userService: UserService;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
2021-04-27 09:05:46 +02:00
|
|
|
{
|
|
|
|
userService,
|
|
|
|
accessService,
|
|
|
|
emailService,
|
|
|
|
resetTokenService,
|
|
|
|
}: Pick<
|
|
|
|
IUnleashServices,
|
|
|
|
| 'userService'
|
|
|
|
| 'accessService'
|
|
|
|
| 'emailService'
|
|
|
|
| 'resetTokenService'
|
|
|
|
>,
|
2021-04-23 10:58:47 +02:00
|
|
|
) {
|
2021-04-09 13:46:53 +02:00
|
|
|
super(config);
|
|
|
|
this.userService = userService;
|
|
|
|
this.accessService = accessService;
|
2021-04-22 10:07:10 +02:00
|
|
|
this.logger = config.getLogger('routes/user-controller.ts');
|
2021-04-23 10:58:47 +02:00
|
|
|
this.emailService = emailService;
|
|
|
|
this.resetTokenService = resetTokenService;
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2021-04-22 12:13:41 +02:00
|
|
|
this.get('/', this.getUsers, ADMIN);
|
2021-04-09 13:46:53 +02:00
|
|
|
this.get('/search', this.search);
|
|
|
|
this.post('/', this.createUser, ADMIN);
|
|
|
|
this.post('/validate-password', this.validatePassword);
|
|
|
|
this.put('/:id', this.updateUser, ADMIN);
|
|
|
|
this.post('/:id/change-password', this.changePassword, ADMIN);
|
|
|
|
this.delete('/:id', this.deleteUser, ADMIN);
|
2021-04-16 15:29:23 +02:00
|
|
|
this.post('/reset-password', this.resetPassword);
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async resetPassword(req, res): Promise<void> {
|
2021-04-16 15:29:23 +02:00
|
|
|
try {
|
|
|
|
const requester = getCreatorUsernameOrPassword(req);
|
|
|
|
const receiver = req.body.id;
|
|
|
|
const resetPasswordUrl = await this.userService.createResetPasswordEmail(
|
|
|
|
receiver,
|
|
|
|
requester,
|
|
|
|
);
|
|
|
|
res.json({ resetPasswordUrl });
|
|
|
|
} catch (e) {
|
|
|
|
handleErrors(res, this.logger, e);
|
|
|
|
}
|
2021-04-09 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async getUsers(req, res): Promise<void> {
|
2021-04-09 13:46:53 +02:00
|
|
|
try {
|
|
|
|
const users = await this.userService.getAll();
|
|
|
|
const rootRoles = await this.accessService.getRootRoles();
|
2021-04-23 10:58:47 +02:00
|
|
|
const inviteLinks = await this.resetTokenService.getActiveInvitations();
|
|
|
|
|
|
|
|
const usersWithInviteLinks = users.map(user => {
|
|
|
|
const inviteLink = inviteLinks[user.id] || '';
|
|
|
|
return { ...user, inviteLink };
|
|
|
|
});
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2021-04-23 10:58:47 +02:00
|
|
|
res.json({ users: usersWithInviteLinks, rootRoles });
|
2021-04-09 13:46:53 +02:00
|
|
|
} catch (error) {
|
|
|
|
this.logger.error(error);
|
|
|
|
res.status(500).send({ msg: 'server errors' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async search(req, res): Promise<void> {
|
2021-04-09 13:46:53 +02:00
|
|
|
const { q } = req.query;
|
|
|
|
try {
|
|
|
|
const users =
|
|
|
|
q && q.length > 1 ? await this.userService.search(q) : [];
|
|
|
|
res.json(users);
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.error(error);
|
|
|
|
res.status(500).send({ msg: 'server errors' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async createUser(req, res): Promise<void> {
|
2021-04-09 13:46:53 +02:00
|
|
|
const { username, email, name, rootRole } = req.body;
|
2021-04-23 10:58:47 +02:00
|
|
|
const { user } = req;
|
2021-04-09 13:46:53 +02:00
|
|
|
|
|
|
|
try {
|
2021-04-23 10:58:47 +02:00
|
|
|
const createdUser = await this.userService.createUser({
|
2021-04-09 13:46:53 +02:00
|
|
|
username,
|
|
|
|
email,
|
|
|
|
name,
|
|
|
|
rootRole: Number(rootRole),
|
|
|
|
});
|
2021-04-23 10:58:47 +02:00
|
|
|
|
|
|
|
const inviteLink = await this.resetTokenService.createNewUserUrl(
|
|
|
|
createdUser.id,
|
|
|
|
user.email,
|
|
|
|
);
|
|
|
|
|
|
|
|
const emailConfigured = this.emailService.configured();
|
|
|
|
if (emailConfigured) {
|
2021-04-23 15:24:32 +02:00
|
|
|
await this.emailService.sendGettingStartedMail(
|
2021-04-23 10:58:47 +02:00
|
|
|
createdUser.name,
|
|
|
|
createdUser.email,
|
|
|
|
inviteLink.toString(),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.logger.warn(
|
|
|
|
'email was not sent to the user because email configuration is lacking',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(201).send({
|
|
|
|
...createdUser,
|
|
|
|
inviteLink,
|
2021-04-23 15:24:32 +02:00
|
|
|
emailSent: emailConfigured,
|
2021-04-23 10:58:47 +02:00
|
|
|
rootRole,
|
|
|
|
});
|
2021-04-09 13:46:53 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logger.warn(e.message);
|
|
|
|
res.status(400).send([{ msg: e.message }]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async updateUser(req, res): Promise<void> {
|
2021-04-09 13:46:53 +02:00
|
|
|
const { id } = req.params;
|
|
|
|
const { name, email, rootRole } = req.body;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const user = await this.userService.updateUser({
|
|
|
|
id: Number(id),
|
|
|
|
name,
|
|
|
|
email,
|
|
|
|
rootRole: Number(rootRole),
|
|
|
|
});
|
|
|
|
res.status(200).send({ ...user, rootRole });
|
|
|
|
} catch (e) {
|
|
|
|
this.logger.warn(e.message);
|
|
|
|
res.status(400).send([{ msg: e.message }]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async deleteUser(req, res): Promise<void> {
|
2021-04-09 13:46:53 +02:00
|
|
|
const { id } = req.params;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.userService.deleteUser(+id);
|
|
|
|
res.status(200).send();
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.warn(error);
|
|
|
|
res.status(500).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async validatePassword(req, res): Promise<void> {
|
2021-04-09 13:46:53 +02:00
|
|
|
const { password } = req.body;
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.userService.validatePassword(password);
|
|
|
|
res.status(200).send();
|
|
|
|
} catch (e) {
|
|
|
|
res.status(400).send([{ msg: e.message }]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async changePassword(req, res): Promise<void> {
|
2021-04-09 13:46:53 +02:00
|
|
|
const { id } = req.params;
|
|
|
|
const { password } = req.body;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.userService.changePassword(+id, password);
|
|
|
|
res.status(200).send();
|
|
|
|
} catch (e) {
|
|
|
|
res.status(400).send([{ msg: e.message }]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = UserAdminController;
|