Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKhymenko committed Sep 23, 2020
2 parents 8e26ff7 + e174da1 commit 1bbb1d6
Show file tree
Hide file tree
Showing 8 changed files with 942 additions and 892 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 8.1.0
Add support for loading roles with permissions at the same time
Add remove roles with permissions at the same time

## 7.0.3
Fix ivy build on different versions (https://github.com/AlexKhymenko/ngx-permissions/issues/112)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Permission and roles based access control for your angular(angular 2,4,5,6,7,8+)
[![npm version](https://badge.fury.io/js/ngx-permissions.svg)](https://badge.fury.io/js/ngx-permissions)

## Documentation and examples

Documentation here is outdated please visit [wiki-page](https://github.com/AlexKhymenko/ngx-permissions/wiki).
To see better structured documentation go to [wiki-page](https://github.com/AlexKhymenko/ngx-permissions/wiki).
In `one month` the detailed functionality description will be available only on wiki page.

Expand Down
1,760 changes: 873 additions & 887 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "application",
"version": "8.0.0",
"version": "8.1.0",
"license": "MIT",
"scripts": {
"ng": "ng",
Expand Down Expand Up @@ -51,6 +51,6 @@
"ts-node": "~8.3.0",
"tslint": "~5.18.0",
"typescript": "3.9.6",
"codecov": "~3.6.5"
"codecov": "~3.7.1"
}
}
2 changes: 1 addition & 1 deletion projects/ngx-permissions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-permissions",
"version": "8.0.0",
"version": "8.1.0",
"repository": {
"type": "git",
"url": "https://github.com/AlexKhymenko/ngx-permissions"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class RootRolesComponent {
class ParentLazyRolesLoadedComponent {
}

function getLazyRolesLoadedModule(importedModule: ModuleWithProviders) {
function getLazyRolesLoadedModule(importedModule: ModuleWithProviders<any>) {
@Component({selector: 'ngx-permissions-lazy', template: 'lazy-loaded-child'})
class ChildLazyLoadedComponent {
constructor(public permissions: NgxRolesService) {
Expand Down
44 changes: 44 additions & 0 deletions projects/ngx-permissions/src/lib/service/roles.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,50 @@ describe('Roles Service', () => {
});
}));

it('should add permissions to roles automatically', fakeAsync(() => {
localService.addRoleWithPermissions('test', ['one', 'two']);
localService.hasOnlyRoles('test').then((data) => {
expect(data).toBe(true);
});
}));

it('should remove roles and permissions add the same time', fakeAsync(() => {
localService.addRoleWithPermissions('test', ['one', 'two']);
localService.hasOnlyRoles('test').then((data) => {
expect(data).toBe(true);
});
}));

it('should remove all permissions and roles', fakeAsync(() => {
expect(Object.keys(localService.getRoles()).length).toEqual(0);
localService.addRoleWithPermissions(RoleNamesEnum.ADMIN as any, ['edit', 'remove']);
localService.addRoleWithPermissions(RoleNamesEnum.GUEST as any, ['edit1', 'remove2']);
expect(Object.keys(localService.getRoles()).length).toEqual(2);
expect(Object.keys(permissionsService.getPermissions()).length).toEqual(4);
localService.flushRolesAndPermissions();
expect(Object.keys(localService.getRoles()).length).toEqual(0);
expect(Object.keys(permissionsService.getPermissions()).length).toEqual(0);
}));


it('should add multiple roles with permissions', fakeAsync(() => {
expect(Object.keys(localService.getRoles()).length).toEqual(0);
localService.addRolesWithPermissions({
ADMIN: ['Nice'],
GUEST: ['Awesome', 'Another awesome']
});

expect(Object.keys(localService.getRoles()).length).toEqual(2);
expect(localService.getRoles()).toEqual(
{
ADMIN: {name: 'ADMIN', validationFunction: ['Nice']},
GUEST: {name: 'GUEST', validationFunction: ['Awesome', 'Another awesome']}
});

expect(Object.keys(localService.getRoles()).length).toEqual(2);
expect(Object.keys(permissionsService.getPermissions()).length).toEqual(3);
}));

xit('maybe add functionality when function returns array', fakeAsync(() => {
localService.addRole('test', () => ['nice']);
localService.hasOnlyRoles(['nice']).then((data) => {
Expand Down
16 changes: 16 additions & 0 deletions projects/ngx-permissions/src/lib/service/roles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,32 @@ export class NgxRolesService {
this.rolesSource.next(roles);
}

public addRoleWithPermissions(name: string, permissions: string[]) {
this.permissionsService.addPermission(permissions);
this.addRole(name, permissions);
}

public addRoles(rolesObj: { [name: string]: ValidationFn | string[] }) {
Object.keys(rolesObj).forEach((key, index) => {
this.addRole(key, rolesObj[key]);
});
}

public addRolesWithPermissions(rolesObj: { [name: string]: string[] }) {
Object.keys(rolesObj).forEach((key, index) => {
this.addRoleWithPermissions(key, rolesObj[key]);
});
}

public flushRoles() {
this.rolesSource.next({});
}

public flushRolesAndPermissions() {
this.flushRoles();
this.permissionsService.flushPermissions();
}

public removeRole(roleName: string) {
const roles = {
...this.rolesSource.value
Expand Down

0 comments on commit 1bbb1d6

Please sign in to comment.