Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unassign existing groups (optionally leave them) #5

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions services/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,15 @@ export default class UsersService extends moleculer.Service {
convert: true,
},
groups: 'array',
unassign: {
type: 'boolean',
default: true,
},
},
})
async assignGroups(ctx: Context<{ id: number; groups: Array<string> }, AppAuthMeta>) {
async assignGroups(
ctx: Context<{ id: number; groups: Array<string>; unassign: boolean }, AppAuthMeta>,
) {
const userId = ctx.params.id;
if (!userId) return;

Expand All @@ -380,7 +386,7 @@ export default class UsersService extends moleculer.Service {

if (everyGroupAssigned) return false;

this.assignNewGroupsToUser(newGroups, assignedGroups, ctx.meta);
this.assignNewGroupsToUser(newGroups, ctx.params.unassign ? assignedGroups : [], ctx.meta);
return true;
}

Expand Down
16 changes: 14 additions & 2 deletions services/usersLocal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ export default class UsersLocalService extends moleculer.Service {
},
},
},
unassignExistingGroups: {
type: 'boolean',
default: true,
},
},
})
async invite(
Expand All @@ -231,6 +235,7 @@ export default class UsersLocalService extends moleculer.Service {
password: string;
apps: number[];
doNotSendEmail: boolean;
unassignExistingGroups: boolean;
},
AppAuthMeta & UserAuthMeta
>,
Expand All @@ -243,7 +248,7 @@ export default class UsersLocalService extends moleculer.Service {
ctx.params.apps = [appId];
}

const { email, groups, password, apps } = ctx.params;
const { email, groups, password, apps, unassignExistingGroups } = ctx.params;

const userLocal: UserLocal = await ctx.call('usersLocal.findOrCreate', {
email,
Expand Down Expand Up @@ -271,6 +276,7 @@ export default class UsersLocalService extends moleculer.Service {
const hasGroupChanges = await ctx.call('users.assignGroups', {
id: userLocal.user,
groups,
unassign: !!unassignExistingGroups,
});

if (!hasGroupChanges) {
Expand Down Expand Up @@ -345,6 +351,10 @@ export default class UsersLocalService extends moleculer.Service {
},
},
},
unassignExistingGroups: {
type: 'boolean',
default: true,
},
},
})
async updateUser(
Expand All @@ -363,11 +373,12 @@ export default class UsersLocalService extends moleculer.Service {
role: UserGroupRole;
}>;
apps: string[];
unassignExistingGroups: boolean;
},
AppAuthMeta & UserAuthMeta
>,
) {
const { id, groups, password, oldPassword, email } = ctx.params;
const { id, groups, password, oldPassword, email, unassignExistingGroups } = ctx.params;
let user: User = await ctx.call('users.resolve', { id });

const { meta } = ctx;
Expand Down Expand Up @@ -417,6 +428,7 @@ export default class UsersLocalService extends moleculer.Service {
{
id,
groups,
unassign: !!unassignExistingGroups,
},
{ meta },
);
Expand Down
96 changes: 96 additions & 0 deletions test/integration/api/users/users.create.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const removeUser = (id: string) => {
return broker.call('users.removeUser', { id });
};

const getUser = (id: any): Promise<any> => {
return broker.call('users.findOne', { query: { id }, populate: 'groups' });
};

const getDataForCreate = (data = {}) => {
return {
firstName: 'Firstname',
Expand All @@ -32,6 +36,16 @@ const getDataForCreate = (data = {}) => {
};
};

const checkGroups = async (id: any, groups: Array<any>) => {
const user = await getUser(id);
const userGroups = user.groups;
groups.forEach((g) => {
const foundGroup = userGroups.find((i: any) => i.id == g.id);
expect(foundGroup.id).toEqual(g.id);
expect(foundGroup.role).toEqual(g.role);
});
};

describe("Test POST '/api/users'", () => {
beforeAll(() => initialize(broker));
afterAll(() => broker.stop());
Expand Down Expand Up @@ -107,6 +121,88 @@ describe("Test POST '/api/users'", () => {

return removeUser(res.body.id);
});

it('Create admin with groups (without unassigning) in admin app (success)', async () => {
const createData = getDataForCreate({
type: UserType.ADMIN,
});

const group1 = { id: apiHelper.groupAdminInner.id, role: UserGroupRole.USER };
const group2 = { id: apiHelper.groupAdminInner2.id, role: UserGroupRole.USER };
const res1 = await request(apiService.server)
.post(endpoint)
.set(apiHelper.getHeaders(apiHelper.superAdminToken))
.send({
...createData,
groups: [group1],
})
.expect(200)
.expect((res: any) => {
expect(res.body.id).not.toBeUndefined();
testUpdatedData(res, createData);
});

checkGroups(res1.body.id, [group1]);

const res2 = await request(apiService.server)
.post(endpoint)
.set(apiHelper.getHeaders(apiHelper.superAdminToken))
.send({
...createData,
groups: [group2],
unassignExistingGroups: false,
})
.expect(200)
.expect((res: any) => {
expect(res.body.id).not.toBeUndefined();
testUpdatedData(res, createData);
});
expect(res1.body.id).toEqual(res2.body.id);
checkGroups(res2.body.id, [group1, group2]);

return removeUser(res1.body.id);
});

it('Create admin with groups (with unassigning) in admin app (success)', async () => {
const createData = getDataForCreate({
type: UserType.ADMIN,
});

const group1 = { id: apiHelper.groupAdminInner.id, role: UserGroupRole.USER };
const group2 = { id: apiHelper.groupAdminInner2.id, role: UserGroupRole.USER };
const res1 = await request(apiService.server)
.post(endpoint)
.set(apiHelper.getHeaders(apiHelper.superAdminToken))
.send({
...createData,
groups: [group1],
})
.expect(200)
.expect((res: any) => {
expect(res.body.id).not.toBeUndefined();
testUpdatedData(res, createData);
});

checkGroups(res1.body.id, [group1]);

const res2 = await request(apiService.server)
.post(endpoint)
.set(apiHelper.getHeaders(apiHelper.superAdminToken))
.send({
...createData,
groups: [group2],
})
.expect(200)
.expect((res: any) => {
expect(res.body.id).not.toBeUndefined();
testUpdatedData(res, createData);
});

expect(res1.body.id).toEqual(res2.body.id);
checkGroups(res2.body.id, [group2]);

return removeUser(res1.body.id);
});
});

describe('Acting as admin', () => {
Expand Down
37 changes: 32 additions & 5 deletions test/integration/api/users/users.groups.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ describe('Test assigning groups to user', () => {
beforeAll(() => initialize(broker));
afterAll(() => broker.stop());

const groupToAssign1 = () => ({ id: apiHelper.groupAdminInner.id, role: UserGroupRole.ADMIN });
const groupToAssign2 = () => ({ id: apiHelper.groupAdminInner2.id, role: UserGroupRole.USER });

afterEach(async () => {
await broker.call('usersLocal.updateUser', {
id: apiHelper.adminInner.id,
groups: [{ id: apiHelper.groupAdminInner.id, role: UserGroupRole.ADMIN }],
groups: [groupToAssign1()],
apps: [],
});

Expand All @@ -52,10 +55,7 @@ describe('Test assigning groups to user', () => {

const endpoint = '/api/users';
const groupsToAssign = () => {
return [
{ id: apiHelper.groupAdminInner.id, role: UserGroupRole.ADMIN },
{ id: apiHelper.groupAdminInner2.id, role: UserGroupRole.USER },
];
return [groupToAssign1(), groupToAssign2()];
};

it('Assign groups for inner admin acting as admin (success)', async () => {
Expand All @@ -71,6 +71,33 @@ describe('Test assigning groups to user', () => {
});
});

it('Assign groups (with unassign) for inner admin acting as admin (success)', async () => {
return request(apiService.server)
.patch(`${endpoint}/${apiHelper.adminInner.id}`)
.set(apiHelper.getHeaders(apiHelper.adminToken))
.send({
groups: [groupToAssign2()],
})
.expect(200)
.expect(async (res: any) => {
await checkGroups(apiHelper.adminInner.id, [groupToAssign2()]);
});
});

it('Assign groups (without unassign) for inner admin acting as admin (success)', async () => {
return request(apiService.server)
.patch(`${endpoint}/${apiHelper.adminInner.id}`)
.set(apiHelper.getHeaders(apiHelper.adminToken))
.send({
groups: [groupToAssign2()],
unassignExistingGroups: false,
})
.expect(200)
.expect(async (res: any) => {
await checkGroups(apiHelper.adminInner.id, [groupToAssign1(), groupToAssign2()]);
});
});

it('Assign groups for inner admin acting as inner admin (unauthorized)', async () => {
return request(apiService.server)
.patch(`${endpoint}/${apiHelper.adminInner.id}`)
Expand Down
Loading