2021-04-22 23:40:52 +02:00
|
|
|
import User from '../types/user';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { AccessService } from './access-service';
|
2021-04-20 12:32:02 +02:00
|
|
|
import NameExistsError from '../error/name-exists-error';
|
|
|
|
import InvalidOperationError from '../error/invalid-operation-error';
|
|
|
|
import { nameType } from '../routes/admin-api/util';
|
|
|
|
import schema from './project-schema';
|
|
|
|
import NotFoundError from '../error/notfound-error';
|
2021-04-29 10:21:29 +02:00
|
|
|
import {
|
|
|
|
PROJECT_CREATED,
|
|
|
|
PROJECT_DELETED,
|
|
|
|
PROJECT_UPDATED,
|
|
|
|
} from '../types/events';
|
2021-04-30 12:51:46 +02:00
|
|
|
import { IUnleashStores } from '../types/stores';
|
|
|
|
import { IUnleashConfig } from '../types/option';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { IProjectOverview, IUserWithRole, RoleName } from '../types/model';
|
2021-07-14 13:20:36 +02:00
|
|
|
import { GLOBAL_ENV } from '../types/environment';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { IEnvironmentStore } from '../types/stores/environment-store';
|
|
|
|
import { IFeatureTypeStore } from '../types/stores/feature-type-store';
|
|
|
|
import { IFeatureToggleStore } from '../types/stores/feature-toggle-store';
|
|
|
|
import { IProject, IProjectStore } from '../types/stores/project-store';
|
|
|
|
import { IRole } from '../types/stores/access-store';
|
|
|
|
import { IEventStore } from '../types/stores/event-store';
|
2021-03-11 22:51:58 +01:00
|
|
|
|
|
|
|
const getCreatedBy = (user: User) => user.email || user.username;
|
|
|
|
|
|
|
|
const DEFAULT_PROJECT = 'default';
|
|
|
|
|
2021-04-20 12:32:02 +02:00
|
|
|
export interface UsersWithRoles {
|
|
|
|
users: IUserWithRole[];
|
|
|
|
roles: IRole[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class ProjectService {
|
2021-08-12 15:04:37 +02:00
|
|
|
private projectStore: IProjectStore;
|
2021-03-11 22:51:58 +01:00
|
|
|
|
|
|
|
private accessService: AccessService;
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
private eventStore: IEventStore;
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
private featureToggleStore: IFeatureToggleStore;
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
private featureTypeStore: IFeatureTypeStore;
|
2021-07-07 10:46:50 +02:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
private environmentStore: IEnvironmentStore;
|
2021-07-14 13:20:36 +02:00
|
|
|
|
2021-03-11 22:51:58 +01:00
|
|
|
private logger: any;
|
|
|
|
|
|
|
|
constructor(
|
2021-04-30 12:51:46 +02:00
|
|
|
{
|
|
|
|
projectStore,
|
|
|
|
eventStore,
|
|
|
|
featureToggleStore,
|
2021-07-07 10:46:50 +02:00
|
|
|
featureTypeStore,
|
2021-07-14 13:20:36 +02:00
|
|
|
environmentStore,
|
2021-04-30 12:51:46 +02:00
|
|
|
}: Pick<
|
2021-07-07 10:46:50 +02:00
|
|
|
IUnleashStores,
|
|
|
|
| 'projectStore'
|
|
|
|
| 'eventStore'
|
|
|
|
| 'featureToggleStore'
|
|
|
|
| 'featureTypeStore'
|
2021-07-14 13:20:36 +02:00
|
|
|
| 'environmentStore'
|
2021-04-30 12:51:46 +02:00
|
|
|
>,
|
|
|
|
config: IUnleashConfig,
|
2021-03-11 22:51:58 +01:00
|
|
|
accessService: AccessService,
|
|
|
|
) {
|
|
|
|
this.projectStore = projectStore;
|
2021-07-14 13:20:36 +02:00
|
|
|
this.environmentStore = environmentStore;
|
2021-03-11 22:51:58 +01:00
|
|
|
this.accessService = accessService;
|
|
|
|
this.eventStore = eventStore;
|
|
|
|
this.featureToggleStore = featureToggleStore;
|
2021-07-07 10:46:50 +02:00
|
|
|
this.featureTypeStore = featureTypeStore;
|
2021-03-11 22:51:58 +01:00
|
|
|
this.logger = config.getLogger('services/project-service.js');
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:32:02 +02:00
|
|
|
async getProjects(): Promise<IProject[]> {
|
2021-03-11 22:51:58 +01:00
|
|
|
return this.projectStore.getAll();
|
|
|
|
}
|
|
|
|
|
2021-04-30 12:51:46 +02:00
|
|
|
async getProject(id: string): Promise<IProject> {
|
2021-03-11 22:51:58 +01:00
|
|
|
return this.projectStore.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
async createProject(newProject: IProject, user: User): Promise<IProject> {
|
|
|
|
const data = await schema.validateAsync(newProject);
|
|
|
|
await this.validateUniqueId(data.id);
|
|
|
|
|
|
|
|
await this.projectStore.create(data);
|
|
|
|
|
2021-07-14 13:20:36 +02:00
|
|
|
await this.environmentStore.connectProject(GLOBAL_ENV, data.id);
|
|
|
|
|
2021-04-12 20:25:03 +02:00
|
|
|
await this.accessService.createDefaultProjectRoles(user, data.id);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
|
|
|
await this.eventStore.store({
|
2021-04-29 10:21:29 +02:00
|
|
|
type: PROJECT_CREATED,
|
2021-03-11 22:51:58 +01:00
|
|
|
createdBy: getCreatedBy(user),
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateProject(updatedProject: IProject, user: User): Promise<void> {
|
|
|
|
await this.projectStore.get(updatedProject.id);
|
|
|
|
const project = await schema.validateAsync(updatedProject);
|
|
|
|
|
|
|
|
await this.projectStore.update(project);
|
|
|
|
|
|
|
|
await this.eventStore.store({
|
2021-04-29 10:21:29 +02:00
|
|
|
type: PROJECT_UPDATED,
|
2021-03-11 22:51:58 +01:00
|
|
|
createdBy: getCreatedBy(user),
|
|
|
|
data: project,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteProject(id: string, user: User): Promise<void> {
|
|
|
|
if (id === DEFAULT_PROJECT) {
|
|
|
|
throw new InvalidOperationError(
|
|
|
|
'You can not delete the default project!',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const toggles = await this.featureToggleStore.getFeaturesBy({
|
|
|
|
project: id,
|
2021-07-07 10:46:50 +02:00
|
|
|
archived: false,
|
2021-03-11 22:51:58 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (toggles.length > 0) {
|
|
|
|
throw new InvalidOperationError(
|
2021-07-07 10:46:50 +02:00
|
|
|
'You can not delete a project with active feature toggles',
|
2021-03-11 22:51:58 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.projectStore.delete(id);
|
|
|
|
|
|
|
|
await this.eventStore.store({
|
2021-04-29 10:21:29 +02:00
|
|
|
type: PROJECT_DELETED,
|
2021-03-11 22:51:58 +01:00
|
|
|
createdBy: getCreatedBy(user),
|
|
|
|
data: { id },
|
|
|
|
});
|
|
|
|
|
2021-04-12 20:25:03 +02:00
|
|
|
this.accessService.removeDefaultProjectRoles(user, id);
|
2021-03-11 22:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async validateId(id: string): Promise<boolean> {
|
|
|
|
await nameType.validateAsync(id);
|
|
|
|
await this.validateUniqueId(id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async validateUniqueId(id: string): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
const exists = await this.projectStore.hasProject(id);
|
|
|
|
if (exists) {
|
|
|
|
throw new NameExistsError('A project with this id already exists.');
|
2021-03-11 22:51:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RBAC methods
|
2021-04-20 12:32:02 +02:00
|
|
|
async getUsersWithAccess(projectId: string): Promise<UsersWithRoles> {
|
2021-03-11 22:51:58 +01:00
|
|
|
const [roles, users] = await this.accessService.getProjectRoleUsers(
|
|
|
|
projectId,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
roles,
|
|
|
|
users,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async addUser(
|
|
|
|
projectId: string,
|
|
|
|
roleId: number,
|
|
|
|
userId: number,
|
|
|
|
): Promise<void> {
|
|
|
|
const [roles, users] = await this.accessService.getProjectRoleUsers(
|
|
|
|
projectId,
|
|
|
|
);
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
const role = roles.find((r) => r.id === roleId);
|
2021-03-11 22:51:58 +01:00
|
|
|
if (!role) {
|
|
|
|
throw new NotFoundError(
|
|
|
|
`Could not find roleId=${roleId} on project=${projectId}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
const alreadyHasAccess = users.some((u) => u.id === userId);
|
2021-03-11 22:51:58 +01:00
|
|
|
if (alreadyHasAccess) {
|
|
|
|
throw new Error(`User already have access to project=${projectId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.accessService.addUserToRole(userId, role.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeUser(
|
|
|
|
projectId: string,
|
|
|
|
roleId: number,
|
|
|
|
userId: number,
|
|
|
|
): Promise<void> {
|
|
|
|
const roles = await this.accessService.getRolesForProject(projectId);
|
2021-08-12 15:04:37 +02:00
|
|
|
const role = roles.find((r) => r.id === roleId);
|
2021-03-11 22:51:58 +01:00
|
|
|
if (!role) {
|
|
|
|
throw new NotFoundError(
|
|
|
|
`Couldn't find roleId=${roleId} on project=${projectId}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-25 19:28:29 +02:00
|
|
|
if (role.name === RoleName.OWNER) {
|
2021-03-11 22:51:58 +01:00
|
|
|
const users = await this.accessService.getUsersForRole(role.id);
|
|
|
|
if (users.length < 2) {
|
2021-05-25 19:28:29 +02:00
|
|
|
throw new Error('A project must have at least one owner');
|
2021-03-11 22:51:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.accessService.removeUserFromRole(userId, role.id);
|
|
|
|
}
|
2021-07-07 10:46:50 +02:00
|
|
|
|
|
|
|
async getMembers(projectId: string): Promise<number> {
|
|
|
|
return this.projectStore.getMembers(projectId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getProjectOverview(
|
|
|
|
projectId: string,
|
|
|
|
archived: boolean = false,
|
|
|
|
): Promise<IProjectOverview> {
|
|
|
|
const project = await this.projectStore.get(projectId);
|
|
|
|
const features = await this.projectStore.getProjectOverview(
|
|
|
|
projectId,
|
|
|
|
archived,
|
|
|
|
);
|
|
|
|
const members = await this.projectStore.getMembers(projectId);
|
|
|
|
return {
|
|
|
|
name: project.name,
|
|
|
|
description: project.description,
|
|
|
|
health: project.health,
|
|
|
|
features,
|
|
|
|
members,
|
|
|
|
version: 1,
|
|
|
|
};
|
|
|
|
}
|
2021-03-11 22:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ProjectService;
|