unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
180 lines • 7.48 kB
JavaScript
import Controller from '../../controller.js';
import { IAuthType } from '../../../types/option.js';
import { ADMIN, NONE } from '../../../types/permissions.js';
import { createRequestSchema } from '../../../openapi/util/create-request-schema.js';
import { createResponseSchema } from '../../../openapi/util/create-response-schema.js';
import { meSchema } from '../../../openapi/spec/me-schema.js';
import { serializeDates } from '../../../types/serialize-dates.js';
import { emptyResponse, getStandardResponses, } from '../../../openapi/util/standard-responses.js';
import { profileSchema, } from '../../../openapi/spec/profile-schema.js';
import { rolesSchema, } from '../../../openapi/spec/roles-schema.js';
class UserController extends Controller {
constructor(config, { accessService, userService, userFeedbackService, userSplashService, openApiService, projectService, transactionalUserSubscriptionsService, }) {
super(config);
this.accessService = accessService;
this.userService = userService;
this.userFeedbackService = userFeedbackService;
this.userSplashService = userSplashService;
this.openApiService = openApiService;
this.projectService = projectService;
this.userSubscriptionsService = transactionalUserSubscriptionsService;
this.flagResolver = config.flagResolver;
this.route({
method: 'get',
path: '',
handler: this.getMe,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'getMe',
summary: 'Get your own user details',
description: 'Detailed information about the current user, user permissions and user feedback',
responses: {
200: createResponseSchema('meSchema'),
...getStandardResponses(401),
},
}),
],
});
this.route({
method: 'get',
path: '/profile',
handler: this.getProfile,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'getProfile',
summary: 'Get your own user profile',
description: 'Detailed information about the current user root role and project membership',
responses: {
200: createResponseSchema('profileSchema'),
...getStandardResponses(401),
},
}),
],
});
this.route({
method: 'post',
path: '/change-password',
handler: this.changeMyPassword,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Users'],
operationId: 'changeMyPassword',
summary: 'Change your own password',
description: 'Requires specifying old password and confirming new password',
requestBody: createRequestSchema('passwordSchema'),
responses: {
200: emptyResponse,
400: {
description: 'Old and new password do not match',
},
401: {
description: 'Old password is incorrect or user is not authenticated',
},
},
}),
],
});
this.route({
method: 'get',
path: '/roles',
handler: this.getRoles,
permission: NONE,
middleware: [
this.openApiService.validPath({
tags: ['Users'],
operationId: 'getUserRoles',
summary: 'Get roles for currently logged in user',
parameters: [
{
name: 'projectId',
description: 'The id of the project you want to check permissions for',
schema: {
type: 'string',
},
in: 'query',
},
],
description: 'Gets roles assigned to currently logged in user. Both explicitly and transitively through group memberships',
responses: {
200: createResponseSchema('rolesSchema'),
...getStandardResponses(401, 403),
},
}),
],
});
}
async getRoles(req, res) {
const { projectId } = req.query;
if (projectId) {
let roles;
if (this.flagResolver.isEnabled('projectRoleAssignment')) {
roles = await this.accessService.getProjectRoles();
}
else {
roles = await this.accessService.getAllProjectRolesForUser(req.user.id, projectId);
}
this.openApiService.respondWithValidation(200, res, rolesSchema.$id, {
version: 1,
roles,
});
}
else {
res.status(400).end();
}
}
async getMe(req, res) {
res.setHeader('cache-control', 'no-store');
const { user } = req;
let permissions;
if (this.config.authentication.type === IAuthType.NONE) {
permissions = [{ permission: ADMIN }];
}
else {
permissions = await this.accessService.getPermissionsForUser(user);
}
const feedback = await this.userFeedbackService.getAllUserFeedback(user);
const splash = await this.userSplashService.getAllUserSplashes(user);
const { isAPI, ...responseUser } = user;
const responseData = {
user: serializeDates(responseUser),
permissions,
feedback: serializeDates(feedback),
splash,
};
this.openApiService.respondWithValidation(200, res, meSchema.$id, responseData);
}
async getProfile(req, res) {
const { user } = req;
const [projects, rootRole, subscriptions] = await Promise.all([
this.projectService.getProjectsByUser(user.id),
this.accessService.getRootRoleForUser(user.id),
this.userSubscriptionsService.getUserSubscriptions(user.id),
]);
const responseData = {
projects,
rootRole,
subscriptions,
features: [],
};
this.openApiService.respondWithValidation(200, res, profileSchema.$id, responseData);
}
async changeMyPassword(req, res) {
const { user } = req;
const { password, confirmPassword, oldPassword } = req.body;
if (password === confirmPassword && oldPassword != null) {
this.userService.validatePassword(password);
await this.userService.changePasswordWithVerification(user.id, password, oldPassword);
res.status(200).end();
}
else {
res.status(400).end();
}
}
}
export default UserController;
//# sourceMappingURL=user.js.map