From b5c88fa1ee478eb71e59d393ef83066861f5e2dc Mon Sep 17 00:00:00 2001 From: Dimitri DO BAIRRO Date: Thu, 2 May 2019 11:37:33 +0200 Subject: [PATCH 1/4] feat(interfaces/IStore.ts) del keys can accept number type --- src/interfaces/IStore.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interfaces/IStore.ts b/src/interfaces/IStore.ts index 0a5d421..40085a2 100644 --- a/src/interfaces/IStore.ts +++ b/src/interfaces/IStore.ts @@ -56,7 +56,7 @@ export interface IStore { * @param keys * @return Promise */ - del(bucket: string, keys: string | string[]): Promise; + del(bucket: string, keys: number | string | string[]): Promise; /** * @description Removes values from a given key inside a bucket. From 10295571df92734bd84e4eb0b7ccb22a245e186e Mon Sep 17 00:00:00 2001 From: Dimitri DO BAIRRO Date: Thu, 2 May 2019 11:38:01 +0200 Subject: [PATCH 2/4] feat(classes/acl.ts) removeUser() implemented --- src/classes/acl.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/classes/acl.ts b/src/classes/acl.ts index d1186dd..139ee3e 100644 --- a/src/classes/acl.ts +++ b/src/classes/acl.ts @@ -395,6 +395,19 @@ export class Acl extends Common { return this.store.end(); } + /** + * @description Removes user with his associated roles. + * @param userId + * @return Promise + */ + public async removeUser(userId: string | number): Promise { + const userRoles = await this.userRoles(userId); + await this.removeUserRoles(userId, userRoles); + this.store.begin(); + await this.store.del(this.options.buckets.users, userId); + await this.store.end(); + } + /** * @description Remove roles from a given user. * @param userId From 0a8b0d55e3c62d14502abaa56e8e4bb162dc7d88 Mon Sep 17 00:00:00 2001 From: Dimitri DO BAIRRO Date: Thu, 2 May 2019 11:38:33 +0200 Subject: [PATCH 3/4] chroe(__tests__/tests.ts) removeUser() tests created --- __tests__/tests.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/__tests__/tests.ts b/__tests__/tests.ts index 8ff9ba5..e32c354 100644 --- a/__tests__/tests.ts +++ b/__tests__/tests.ts @@ -818,5 +818,21 @@ import { Acl, HttpError, MemoryStore, MongoDBStore, RedisStore } from '../src'; expect(res2.res1.sort()).toEqual(['perm1', 'perm2', 'perm3'].sort()); // tslint:disable-line no-unsafe-any }); }); + + describe('Removing a user', () => { + it('Remove a user', async () => { + const guest = await acl.allow('dimitri', 'blogs', 'view'); + expect(guest).toBeUndefined(); + + const isAllowed = await acl.isAllowed('dimitri', 'blogs', 'view'); + expect(isAllowed).toBeTruthy(); + + const removeUser = await acl.removeUser('dimitri'); + expect(removeUser).toBeUndefined(); + + const isAllowedAfterRemoveUser = await acl.isAllowed('dimitri', 'blogs', 'view'); + expect(isAllowedAfterRemoveUser).toBeFalsy(); + }); + }); }); }); From 037ab2f8680acf03d32db66d744f19c65de716cb Mon Sep 17 00:00:00 2001 From: Dimitri DO BAIRRO Date: Thu, 2 May 2019 11:50:01 +0200 Subject: [PATCH 4/4] chore(__tests__/tests.ts) remove user test improved --- __tests__/tests.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/__tests__/tests.ts b/__tests__/tests.ts index e32c354..241da56 100644 --- a/__tests__/tests.ts +++ b/__tests__/tests.ts @@ -821,16 +821,19 @@ import { Acl, HttpError, MemoryStore, MongoDBStore, RedisStore } from '../src'; describe('Removing a user', () => { it('Remove a user', async () => { - const guest = await acl.allow('dimitri', 'blogs', 'view'); + const guest = await acl.allow('member', 'panel', 'edit'); expect(guest).toBeUndefined(); - const isAllowed = await acl.isAllowed('dimitri', 'blogs', 'view'); + const addUserRoles = await acl.addUserRoles('dimitri', 'member'); + expect(addUserRoles).toBeUndefined(); + + const isAllowed = await acl.isAllowed('dimitri', 'panel', 'edit'); expect(isAllowed).toBeTruthy(); const removeUser = await acl.removeUser('dimitri'); expect(removeUser).toBeUndefined(); - const isAllowedAfterRemoveUser = await acl.isAllowed('dimitri', 'blogs', 'view'); + const isAllowedAfterRemoveUser = await acl.isAllowed('dimitri', 'panel', 'edit'); expect(isAllowedAfterRemoveUser).toBeFalsy(); }); });