1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

fix: do not allow segment deletion when used in private projects (#5236)

This commit is contained in:
Jaanus Sellin 2023-11-01 11:01:23 +02:00 committed by GitHub
parent 74bbc7799e
commit 598d022a5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 9 deletions

View File

@ -348,7 +348,10 @@ export class SegmentsController extends Controller {
): Promise<void> {
const { id } = req.params;
const { user } = req;
const strategies = await this.segmentService.getStrategies(id, user.id);
const strategies = await this.segmentService.getVisibleStrategies(
id,
user.id,
);
// Remove unnecessary IFeatureStrategy fields from the response.
const segmentStrategies = strategies.map((strategy) => ({
@ -369,8 +372,7 @@ export class SegmentsController extends Controller {
res: Response,
): Promise<void> {
const id = Number(req.params.id);
const { user } = req;
const strategies = await this.segmentService.getStrategies(id, user.id);
const strategies = await this.segmentService.getAllStrategies(id);
if (strategies.length > 0) {
res.status(409).send();

View File

@ -13,7 +13,17 @@ export interface ISegmentService {
get(id: number): Promise<ISegment>;
getStrategies(id: number, userId: number): Promise<IFeatureStrategy[]>;
/**
* Gets all strategies for a segment
* This is NOT considering the private projects
* For most use cases, use `getVisibleStrategies`
*/
getAllStrategies(id: number): Promise<IFeatureStrategy[]>;
getVisibleStrategies(
id: number,
userId: number,
): Promise<IFeatureStrategy[]>;
validateName(name: string): Promise<void>;

View File

@ -77,18 +77,15 @@ export class SegmentService implements ISegmentService {
return this.segmentStore.getActiveForClient();
}
// Used by unleash-enterprise.
async getByStrategy(strategyId: string): Promise<ISegment[]> {
return this.segmentStore.getByStrategy(strategyId);
}
// Used by unleash-enterprise.
async getStrategies(
async getVisibleStrategies(
id: number,
userId: number,
): Promise<IFeatureStrategy[]> {
const strategies =
await this.featureStrategiesStore.getStrategiesBySegment(id);
const strategies = await this.getAllStrategies(id);
if (this.flagResolver.isEnabled('privateProjects')) {
const accessibleProjects =
await this.privateProjectChecker.getUserAccessibleProjects(
@ -105,6 +102,12 @@ export class SegmentService implements ISegmentService {
return strategies;
}
async getAllStrategies(id: number): Promise<IFeatureStrategy[]> {
const strategies =
await this.featureStrategiesStore.getStrategiesBySegment(id);
return strategies;
}
async create(
data: unknown,
user: Partial<Pick<User, 'username' | 'email'>>,