Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Roles from './roles';
- interface RolesData {
- name: string;
- permissions?: Array<string>;
- }
- class RBAC {
- private roles: Array<Roles>;
- constructor() {
- this.roles = [];
- }
- blockPermission(name: string): void {
- }
- can(roleName: string, permission: string): boolean {
- const roleResult = this.getRole(roleName);
- return !!roleResult?.permissions.find((role) => role === permission);
- }
- createRole(name: string, permission: Array<string> = []): Roles {
- const role = new Roles(name, permission);
- this.roles.push(role);
- return role;
- }
- createRoles(rolesData: RolesData[]): void {
- rolesData.forEach((role) => {
- this.createRole(role.name, role.permissions);
- });
- }
- getRole(name: string): Roles | undefined {
- return this.roles.find((role) => role.name === name);
- }
- getRoles(): Array<string> {
- const roles: Array<string> = [];
- this.roles.forEach((role) => {
- roles.push(role.name);
- });
- return roles;
- }
- hasPermission(permission: string): boolean {
- return this.roles.some((role) => role.permissions.find((access) => access === permission));
- }
- hasRole(name: string): boolean {
- return !!this.getRole(name);
- }
- removeRole(name: string): void {
- this.roles = this.roles.filter((role) => role.name !== name);
- }
- }
- export default RBAC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement