2022-04-04 18:57:18 -07:00
|
|
|
/* eslint-disable consistent-return */
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
import express = require('express');
|
|
|
|
import validator from 'validator';
|
|
|
|
import config = require('../../config');
|
|
|
|
import type { UserRequest } from '../requests';
|
2022-03-30 20:11:26 -07:00
|
|
|
|
2022-04-04 18:57:18 -07:00
|
|
|
type Role = 'owner' | 'member';
|
|
|
|
|
|
|
|
const instanceOwnerSetup = (
|
|
|
|
req: express.Request,
|
|
|
|
res: express.Response,
|
|
|
|
next: express.NextFunction,
|
|
|
|
): any => {
|
|
|
|
if (config.get('userManagement.isInstanceOwnerSetUp')) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return res.status(400).json({ message: 'asasas' });
|
|
|
|
};
|
|
|
|
|
|
|
|
const emailSetup = (
|
|
|
|
req: express.Request,
|
|
|
|
res: express.Response,
|
|
|
|
next: express.NextFunction,
|
|
|
|
): any => {
|
|
|
|
if (config.get('userManagement.emails.mode')) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return res.status(400).json({ message: 'asasas' });
|
|
|
|
};
|
|
|
|
|
|
|
|
const authorize =
|
|
|
|
(role: [Role]) =>
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction): any => {
|
|
|
|
const {
|
|
|
|
globalRole: { name: userRole },
|
|
|
|
} = req.user as { globalRole: { name: Role } };
|
|
|
|
if (role.includes(userRole)) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return res.status(400).json({
|
|
|
|
message: 'asasas',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const validEmail = (
|
|
|
|
req: UserRequest.Invite,
|
|
|
|
res: express.Response,
|
|
|
|
next: express.NextFunction,
|
|
|
|
): any => {
|
|
|
|
req.body.forEach((invite) => {
|
|
|
|
if (!validator.isEmail(invite.email)) {
|
|
|
|
return res.status(400).json({
|
|
|
|
message: `Request to send email invite(s) to user(s) failed because of an invalid email address: ${invite.email}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-03-30 20:11:26 -07:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const middlewares = {
|
2022-04-04 18:57:18 -07:00
|
|
|
createUsers: [instanceOwnerSetup, emailSetup, validEmail, authorize(['owner'])],
|
|
|
|
getUsers: [instanceOwnerSetup, authorize(['owner'])],
|
|
|
|
getUser: [instanceOwnerSetup, authorize(['owner'])],
|
2022-03-30 20:11:26 -07:00
|
|
|
};
|