unleash-server
Version:
Unleash is an enterprise ready feature toggles service. It provides different strategies for handling feature toggles.
309 lines • 12.9 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const controller_1 = __importDefault(require("../controller"));
const permissions_1 = require("../../types/permissions");
const simple_auth_settings_1 = require("../../types/settings/simple-auth-settings");
const anonymise_1 = require("../../util/anonymise");
const create_request_schema_1 = require("../../openapi/util/create-request-schema");
const create_response_schema_1 = require("../../openapi/util/create-response-schema");
const user_schema_1 = require("../../openapi/spec/user-schema");
const serialize_dates_1 = require("../../types/serialize-dates");
const users_schema_1 = require("../../openapi/spec/users-schema");
const users_search_schema_1 = require("../../openapi/spec/users-search-schema");
const reset_password_schema_1 = require("../../openapi/spec/reset-password-schema");
const standard_responses_1 = require("../../openapi/util/standard-responses");
const users_groups_base_schema_1 = require("../../openapi/spec/users-groups-base-schema");
class UserAdminController extends controller_1.default {
constructor(config, { userService, accessService, emailService, resetTokenService, settingService, openApiService, groupService, }) {
super(config);
this.userService = userService;
this.accessService = accessService;
this.emailService = emailService;
this.resetTokenService = resetTokenService;
this.settingService = settingService;
this.openApiService = openApiService;
this.groupService = groupService;
this.logger = config.getLogger('routes/user-controller.ts');
this.unleashUrl = config.server.unleashUrl;
this.flagResolver = config.flagResolver;
this.route({
method: 'post',
path: '/validate-password',
handler: this.validateUserPassword,
permission: permissions_1.NONE,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'validateUserPassword',
requestBody: (0, create_request_schema_1.createRequestSchema)('passwordSchema'),
responses: { 200: standard_responses_1.emptyResponse },
}),
],
});
this.route({
method: 'post',
path: '/:id/change-password',
handler: this.changeUserPassword,
permission: permissions_1.ADMIN,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'changeUserPassword',
requestBody: (0, create_request_schema_1.createRequestSchema)('passwordSchema'),
responses: { 200: standard_responses_1.emptyResponse },
}),
],
});
this.route({
method: 'post',
path: '/reset-password',
handler: this.resetUserPassword,
permission: permissions_1.ADMIN,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'resetUserPassword',
requestBody: (0, create_request_schema_1.createRequestSchema)('idSchema'),
responses: {
200: (0, create_response_schema_1.createResponseSchema)('resetPasswordSchema'),
},
}),
],
});
this.route({
method: 'get',
path: '',
handler: this.getUsers,
permission: permissions_1.ADMIN,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'getUsers',
responses: { 200: (0, create_response_schema_1.createResponseSchema)('usersSchema') },
}),
],
});
this.route({
method: 'get',
path: '/search',
handler: this.searchUsers,
permission: permissions_1.NONE,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'searchUsers',
responses: { 200: (0, create_response_schema_1.createResponseSchema)('usersSchema') },
}),
],
});
this.route({
method: 'get',
path: '/access',
handler: this.getBaseUsersAndGroups,
permission: permissions_1.NONE,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'getBaseUsersAndGroups',
responses: {
200: (0, create_response_schema_1.createResponseSchema)('usersGroupsBaseSchema'),
},
}),
],
});
this.route({
method: 'post',
path: '',
handler: this.createUser,
permission: permissions_1.ADMIN,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'createUser',
requestBody: (0, create_request_schema_1.createRequestSchema)('createUserSchema'),
responses: { 200: (0, create_response_schema_1.createResponseSchema)('userSchema') },
}),
],
});
this.route({
method: 'get',
path: '/:id',
handler: this.getUser,
permission: permissions_1.ADMIN,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'getUser',
responses: { 200: (0, create_response_schema_1.createResponseSchema)('userSchema') },
}),
],
});
this.route({
method: 'put',
path: '/:id',
handler: this.updateUser,
permission: permissions_1.ADMIN,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'updateUser',
requestBody: (0, create_request_schema_1.createRequestSchema)('updateUserSchema'),
responses: { 200: (0, create_response_schema_1.createResponseSchema)('userSchema') },
}),
],
});
this.route({
method: 'delete',
path: '/:id',
acceptAnyContentType: true,
handler: this.deleteUser,
permission: permissions_1.ADMIN,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'deleteUser',
responses: { 200: standard_responses_1.emptyResponse },
}),
],
});
}
async resetUserPassword(req, res) {
const { user } = req;
const receiver = req.body.id;
const resetPasswordUrl = await this.userService.createResetPasswordEmail(receiver, user);
this.openApiService.respondWithValidation(200, res, reset_password_schema_1.resetPasswordSchema.$id, { resetPasswordUrl: resetPasswordUrl.toString() });
}
async getUsers(req, res) {
const users = await this.userService.getAll();
const rootRoles = await this.accessService.getRootRoles();
const inviteLinks = await this.resetTokenService.getActiveInvitations();
const usersWithInviteLinks = users.map((user) => {
const inviteLink = inviteLinks[user.id] || '';
return { ...user, inviteLink };
});
this.openApiService.respondWithValidation(200, res, users_schema_1.usersSchema.$id, {
users: (0, serialize_dates_1.serializeDates)(usersWithInviteLinks),
rootRoles,
});
}
anonymiseUsers(users) {
return users.map((u) => ({
...u,
email: (0, anonymise_1.anonymise)(u.email || 'random'),
imageUrl: 'https://gravatar.com/avatar/21232f297a57a5a743894a0e4a801fc3?size=42&default=retro',
}));
}
async searchUsers(req, res) {
const { q } = req.query;
let users = typeof q === 'string' && q.length > 1
? await this.userService.search(q)
: [];
if (this.flagResolver.isEnabled('anonymiseEventLog')) {
users = this.anonymiseUsers(users);
}
this.openApiService.respondWithValidation(200, res, users_search_schema_1.usersSearchSchema.$id, (0, serialize_dates_1.serializeDates)(users));
}
async getBaseUsersAndGroups(req, res) {
let allUsers = await this.userService.getAll();
let users = allUsers.map((u) => {
return {
id: u.id,
name: u.name,
username: u.username,
email: u.email,
};
});
let allGroups = await this.groupService.getAll();
let groups = allGroups.map((g) => {
return {
id: g.id,
name: g.name,
userCount: g.users.length,
};
});
this.openApiService.respondWithValidation(200, res, users_groups_base_schema_1.usersGroupsBaseSchema.$id, {
users: (0, serialize_dates_1.serializeDates)(users),
groups: (0, serialize_dates_1.serializeDates)(groups),
});
}
async getUser(req, res) {
const { id } = req.params;
const user = await this.userService.getUser(Number(id));
this.openApiService.respondWithValidation(200, res, user_schema_1.userSchema.$id, (0, serialize_dates_1.serializeDates)(user));
}
async createUser(req, res) {
const { username, email, name, rootRole, sendEmail } = req.body;
const { user } = req;
const createdUser = await this.userService.createUser({
username,
email,
name,
rootRole,
}, user);
const passwordAuthSettings = await this.settingService.get(simple_auth_settings_1.simpleAuthSettingsKey);
let inviteLink;
if (!passwordAuthSettings?.disabled) {
const inviteUrl = await this.resetTokenService.createNewUserUrl(createdUser.id, user.email);
inviteLink = inviteUrl.toString();
}
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);
emailSent = true;
}
catch (e) {
this.logger.warn('email was configured, but sending failed due to: ', e);
}
}
else {
this.logger.warn('email was not sent to the user because email configuration is lacking');
}
const responseData = {
...(0, serialize_dates_1.serializeDates)(createdUser),
inviteLink: inviteLink || this.unleashUrl,
emailSent,
rootRole,
};
this.openApiService.respondWithValidation(201, res, user_schema_1.userSchema.$id, responseData);
}
async updateUser(req, res) {
const { user, params, body } = req;
const { id } = params;
const { name, email, rootRole } = body;
const updateUser = await this.userService.updateUser({
id: Number(id),
name,
email,
rootRole,
}, user);
this.openApiService.respondWithValidation(200, res, user_schema_1.userSchema.$id, {
...(0, serialize_dates_1.serializeDates)(updateUser),
rootRole,
});
}
async deleteUser(req, res) {
const { user, params } = req;
const { id } = params;
await this.userService.deleteUser(+id, user);
res.status(200).send();
}
async validateUserPassword(req, res) {
const { password } = req.body;
this.userService.validatePassword(password);
res.status(200).send();
}
async changeUserPassword(req, res) {
const { id } = req.params;
const { password } = req.body;
await this.userService.changePassword(+id, password);
res.status(200).send();
}
}
exports.default = UserAdminController;
//# sourceMappingURL=user-admin.js.map