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

Fix all groups being removed, even when no external groups were defined (#2197)

* Syncing groups

* Add tests

* Fix all groups being removed

Co-authored-by: sjaanus <sellinjaanus@gmail.com>
This commit is contained in:
sellinjaanus 2022-10-17 11:44:36 +03:00 committed by GitHub
parent c4d68110fc
commit 4068e4749f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 14 deletions

View File

@ -254,9 +254,11 @@ export default class GroupStore implements IGroupStore {
.select('id') .select('id')
.whereRaw('mappings_sso \\?| :groups', { .whereRaw('mappings_sso \\?| :groups', {
groups: externalGroups, groups: externalGroups,
}), })
.orWhereRaw('jsonb_array_length(mappings_sso) = 0'),
) )
.where('gu.user_id', userId); .where('gu.user_id', userId);
return rows.map(rowToGroupUser); return rows.map(rowToGroupUser);
} }

View File

@ -220,19 +220,21 @@ export class GroupService {
userId: number, userId: number,
externalGroups: string[], externalGroups: string[],
): Promise<void> { ): Promise<void> {
let newGroups = await this.groupStore.getNewGroupsForExternalUser( if (Array.isArray(externalGroups)) {
userId, let newGroups = await this.groupStore.getNewGroupsForExternalUser(
externalGroups, userId,
); externalGroups,
await this.groupStore.addUserToGroups( );
userId, await this.groupStore.addUserToGroups(
newGroups.map((g) => g.id), userId,
); newGroups.map((g) => g.id),
let oldGroups = await this.groupStore.getOldGroupsForExternalUser( );
userId, let oldGroups = await this.groupStore.getOldGroupsForExternalUser(
externalGroups, userId,
); externalGroups,
await this.groupStore.deleteUsersFromGroup(oldGroups); );
await this.groupStore.deleteUsersFromGroup(oldGroups);
}
} }
async getGroupsForUser(userId: number): Promise<IGroup[]> { async getGroupsForUser(userId: number): Promise<IGroup[]> {

View File

@ -2,11 +2,13 @@ import dbInit, { ITestDb } from '../helpers/database-init';
import getLogger from '../../fixtures/no-logger'; import getLogger from '../../fixtures/no-logger';
import { createTestConfig } from '../../config/test-config'; import { createTestConfig } from '../../config/test-config';
import { GroupService } from '../../../lib/services/group-service'; import { GroupService } from '../../../lib/services/group-service';
import GroupStore from '../../../lib/db/group-store';
let stores; let stores;
let db: ITestDb; let db: ITestDb;
let groupService: GroupService; let groupService: GroupService;
let groupStore: GroupStore;
let user; let user;
beforeAll(async () => { beforeAll(async () => {
@ -20,6 +22,7 @@ beforeAll(async () => {
getLogger, getLogger,
}); });
groupService = new GroupService(stores, config); groupService = new GroupService(stores, config);
groupStore = stores.groupStore;
await stores.groupStore.create({ await stores.groupStore.create({
name: 'dev_group', name: 'dev_group',
@ -69,3 +72,28 @@ test('should add person to completely new group with new name', async () => {
expect(groups.length).toBe(1); expect(groups.length).toBe(1);
expect(groups[0].name).toEqual('dev_group'); expect(groups[0].name).toEqual('dev_group');
}); });
test('should not update groups when not string array ', async () => {
await groupService.syncExternalGroups(user.id, 'Everyone' as any);
const groups = await groupService.getGroupsForUser(user.id);
expect(groups.length).toBe(1);
expect(groups[0].name).toEqual('dev_group');
});
test('should clear groups when empty array ', async () => {
await groupService.syncExternalGroups(user.id, []);
const groups = await groupService.getGroupsForUser(user.id);
expect(groups.length).toBe(0);
});
test('should not remove user from no SSO definition group', async () => {
const group = await groupStore.create({
name: 'no_mapping_group',
description: 'no_mapping_group',
});
await groupStore.addUserToGroups(user.id, [group.id]);
await groupService.syncExternalGroups(user.id, []);
const groups = await groupService.getGroupsForUser(user.id);
expect(groups.length).toBe(1);
expect(groups[0].name).toEqual('no_mapping_group');
});