Skip to content

Commit

Permalink
feat: init role / roleclasspermission / roleisntancepermission models
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlkoch committed Jun 10, 2024
1 parent e258672 commit 0667bd5
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/model/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export interface KeycloakGroupRepresentation extends ProviderGroupDetails {
clientRoles?: {
[key: string]: string[];
};
subGroups: KeycloakGroupRepresentation[];
access: {
subGroups?: KeycloakGroupRepresentation[];
access?: {
[key: string]: boolean;
};
}
Expand Down
35 changes: 35 additions & 0 deletions src/model/Role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import BaseEntity, { BaseEntityArgs } from './BaseEntity';

export interface ProviderRoleDetails {}

export interface KeycloakRoleRepresentation extends ProviderRoleDetails {
id?: string;
name?: string;
description?: string;
scopeParamRequired?: boolean;
composite?: boolean;
// TODO Recheck this type
composites?: string;
clientRole?: boolean;
containerId?: string;
attributes?: {
[key: string]: string[];
};
}

export interface RoleArgs<T extends ProviderRoleDetails = KeycloakRoleRepresentation> extends BaseEntityArgs {
authProviderId?: string;
providerDetails?: T;
}

export default class Role<T extends ProviderRoleDetails = KeycloakRoleRepresentation> extends BaseEntity {
authProviderId?: string;
providerDetails?: T;

constructor({ id, created, modified, authProviderId, providerDetails }: RoleArgs<T>) {
super({ id, created, modified });

this.authProviderId = authProviderId;
this.providerDetails = providerDetails;
}
}
16 changes: 16 additions & 0 deletions src/model/security/RoleClassPermission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Role from '../Role';
import ClassPermission, { ClassPermissionArgs } from './ClassPermission';

export interface RoleClassPermissionArgs extends ClassPermissionArgs {
role: Role;
}

export default class RoleClassPermission extends ClassPermission {
role: Role;

constructor({ id, created, modified, className, permission, role }: RoleClassPermissionArgs) {
super({ id, created, modified, className, permission });

this.role = role;
}
}
16 changes: 16 additions & 0 deletions src/model/security/RoleInstancePermission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Role from '../Role';
import InstancePermission, { InstancePermissionArgs } from './InstancePermission';

export interface RoleInstancePermissionArgs extends InstancePermissionArgs {
role: Role;
}

export default class RoleInstancePermission extends InstancePermission {
role: Role;

constructor({ id, created, modified, entityId, permission, role }: RoleInstancePermissionArgs) {
super({ id, created, modified, entityId, permission });

this.role = role;
}
}
214 changes: 212 additions & 2 deletions src/service/PermissionService/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import PermissionCollectionType from '../../model/enum/PermissionCollectionType';
import GroupClassPermission from '../../model/security/GroupClassPermission';
import GroupInstancePermission from '../../model/security/GroupInstancePermission';
import RoleClassPermission from '../../model/security/RoleClassPermission';
import RoleInstancePermission from '../../model/security/RoleInstancePermission';
import UserClassPermission from '../../model/security/UserClassPermission';
import UserInstancePermission from '../../model/security/UserInstancePermission';
import { getBearerTokenHeader } from '../../security/getBearerTokenHeader';
Expand Down Expand Up @@ -55,6 +57,26 @@ export class PermissionService extends GenericService {
}
}

async getRoleInstancePermissions(id: string | number, fetchOpts?: RequestInit): Promise<RoleInstancePermission[]> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/instance/role`, {
method: 'GET',
headers: {
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});

if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}

return await response.json();
} catch (error) {
throw new Error(`Error while requesting the role instance permissions: ${error}`);
}
}

async getUserClassPermissions(id: string | number, fetchOpts?: RequestInit): Promise<UserClassPermission[]> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/class/user`, {
Expand Down Expand Up @@ -95,6 +117,26 @@ export class PermissionService extends GenericService {
}
}

async getRoleClassPermissions(id: string | number, fetchOpts?: RequestInit): Promise<RoleClassPermission[]> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/class/role`, {
method: 'GET',
headers: {
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});

if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}

return await response.json();
} catch (error) {
throw new Error(`Error while requesting the role class permissions: ${error}`);
}
}

async getUserInstancePermission(id: string | number, userId: string | number, fetchOpts?: RequestInit):
Promise<UserInstancePermission> {
try {
Expand Down Expand Up @@ -137,8 +179,29 @@ export class PermissionService extends GenericService {
}
}

async getRoleInstancePermission(id: string | number, roleId: string | number, fetchOpts?: RequestInit):
Promise<RoleInstancePermission> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/instance/role/${roleId}`, {
method: 'GET',
headers: {
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});

if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}

return await response.json();
} catch (error) {
throw new Error(`Error while requesting the role instance permission: ${error}`);
}
}

async getUserClassPermission(id: string | number, userId: string | number, fetchOpts?: RequestInit):
Promise<UserInstancePermission> {
Promise<UserClassPermission> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/class/user/${userId}`, {
method: 'GET',
Expand All @@ -159,7 +222,7 @@ export class PermissionService extends GenericService {
}

async getGroupClassPermission(id: string | number, groupId: string | number, fetchOpts?: RequestInit):
Promise<UserInstancePermission> {
Promise<UserClassPermission> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/class/group/${groupId}`, {
method: 'GET',
Expand All @@ -179,6 +242,27 @@ export class PermissionService extends GenericService {
}
}

async getRoleClassPermission(id: string | number, roleId: string | number, fetchOpts?: RequestInit):
Promise<RoleClassPermission> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/class/role/${roleId}`, {
method: 'GET',
headers: {
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});

if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}

return await response.json();
} catch (error) {
throw new Error(`Error while requesting the role class permission: ${error}`);
}
}

async setUserInstancePermission(id: string | number, userId: string | number,
permissionType: PermissionCollectionType, fetchOpts?: RequestInit): Promise<void> {
try {
Expand Down Expand Up @@ -227,6 +311,30 @@ export class PermissionService extends GenericService {
}
}

async setRoleInstancePermission(id: string | number, roleId: string | number,
permissionType: PermissionCollectionType, fetchOpts?: RequestInit): Promise<void> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/instance/role/${roleId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
body: JSON.stringify({
permission: permissionType
}),
...fetchOpts
});

if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}
} catch (error) {
throw new Error(`Error while adding the role instance permission: ${error}`);
}
}

async setUserClassPermission(id: string | number, userId: string | number,
permissionType: PermissionCollectionType, fetchOpts?: RequestInit): Promise<void> {
try {
Expand Down Expand Up @@ -275,6 +383,30 @@ export class PermissionService extends GenericService {
}
}

async setRoleClassPermission(id: string | number, roleId: string | number,
permissionType: PermissionCollectionType, fetchOpts?: RequestInit): Promise<void> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/class/role/${roleId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
body: JSON.stringify({
permission: permissionType
}),
...fetchOpts
});

if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}
} catch (error) {
throw new Error(`Error while adding the role class permission: ${error}`);
}
}

async deleteUserInstancePermission(id: string | number, userId: string | number,
fetchOpts?: RequestInit): Promise<void> {
try {
Expand Down Expand Up @@ -315,6 +447,26 @@ export class PermissionService extends GenericService {
}
}

async deleteRoleInstancePermission(id: string | number, roleId: string | number,
fetchOpts?: RequestInit): Promise<void> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/instance/role/${roleId}`, {
method: 'DELETE',
headers: {
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});

if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}
} catch (error) {
throw new Error(`Error while removing the role instance permission: ${error}`);
}
}

async deleteUserClassPermission(id: string | number, userId: string | number,
fetchOpts?: RequestInit): Promise<void> {
try {
Expand Down Expand Up @@ -355,6 +507,26 @@ export class PermissionService extends GenericService {
}
}

async deleteRoleClassPermission(id: string | number, roleId: string | number,
fetchOpts?: RequestInit): Promise<void> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/class/role/${roleId}`, {
method: 'DELETE',
headers: {
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});

if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}
} catch (error) {
throw new Error(`Error while removing the role class permission: ${error}`);
}
}

async deleteUserInstancePermissions(id: string | number, fetchOpts?: RequestInit): Promise<void> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/instance/user`, {
Expand Down Expand Up @@ -393,6 +565,25 @@ export class PermissionService extends GenericService {
}
}

async deleteRoleInstancePermissions(id: string | number, fetchOpts?: RequestInit): Promise<void> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/instance/role`, {
method: 'DELETE',
headers: {
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});

if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}
} catch (error) {
throw new Error(`Error while removing all role instance permissions: ${error}`);
}
}

async deleteUserClassPermissions(id: string | number, fetchOpts?: RequestInit): Promise<void> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/class/user`, {
Expand Down Expand Up @@ -431,6 +622,25 @@ export class PermissionService extends GenericService {
}
}

async deleteRoleClassPermissions(id: string | number, fetchOpts?: RequestInit): Promise<void> {
try {
const response = await fetch(`${this.basePath}/${id}/permissions/class/role`, {
method: 'DELETE',
headers: {
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});

if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}
} catch (error) {
throw new Error(`Error while removing all role class permissions: ${error}`);
}
}

}

export default PermissionService;
Loading

0 comments on commit 0667bd5

Please sign in to comment.