Skip to content

Commit

Permalink
Merge pull request #44 from Aclify/implement-remove-user
Browse files Browse the repository at this point in the history
removeUser() implementation
  • Loading branch information
rimiti authored May 2, 2019
2 parents 9e40fa8 + 037ab2f commit 39d5973
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
19 changes: 19 additions & 0 deletions __tests__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,5 +818,24 @@ 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('member', 'panel', 'edit');
expect(guest).toBeUndefined();

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', 'panel', 'edit');
expect(isAllowedAfterRemoveUser).toBeFalsy();
});
});
});
});
13 changes: 13 additions & 0 deletions src/classes/acl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,19 @@ export class Acl extends Common {
return this.store.end();
}

/**
* @description Removes user with his associated roles.
* @param userId
* @return Promise<void>
*/
public async removeUser(userId: string | number): Promise<void> {
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
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface IStore {
* @param keys
* @return Promise<void>
*/
del(bucket: string, keys: string | string[]): Promise<void>;
del(bucket: string, keys: number | string | string[]): Promise<void>;

/**
* @description Removes values from a given key inside a bucket.
Expand Down

0 comments on commit 39d5973

Please sign in to comment.