-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
151 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { IRole } from '../roles'; | ||
|
||
/** | ||
* Interface for reading roles. | ||
*/ | ||
export interface IRoleRead { | ||
/** | ||
* Retrieves a role by its id or name. | ||
* @param idOrName The id or name of the role to retrieve. | ||
* @param appId The id of the app. | ||
* @returns The role, if found. | ||
* @returns null if no role is found. | ||
* @throws If there is an error while retrieving the role. | ||
*/ | ||
getOneByIdOrName(idOrName: IRole['id'] | IRole['name'], appId: string): Promise<IRole | null>; | ||
|
||
/** | ||
* Retrieves all custom roles. | ||
* @param appId The id of the app. | ||
* @returns All custom roles. | ||
* @throws If there is an error while retrieving the roles. | ||
* @throws If the app does not have the necessary permissions. | ||
* @see IRole.protected | ||
* @see AppPermissions.role.read | ||
*/ | ||
getCustomRoles(appId: string): Promise<Array<IRole>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface IRole { | ||
description: string; | ||
mandatory2fa?: boolean; | ||
name: string; | ||
protected: boolean; | ||
scope: 'Users' | 'Subscriptions'; | ||
id: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* This module exports the IRole interface for defining roles in Rocket.Chat Apps. | ||
* @module definition/roles | ||
*/ | ||
export * from './IRole'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { IRoleRead } from '../../definition/accessors/IRoleRead'; | ||
import { IRole } from '../../definition/roles'; | ||
import { RoleBridge } from '../bridges'; | ||
|
||
export class RoleRead implements IRoleRead { | ||
constructor(private roleBridge: RoleBridge, private appId: string) {} | ||
|
||
public getOneByIdOrName(idOrName: IRole['id'] | IRole['name']): Promise<IRole | null> { | ||
return this.roleBridge.doGetOneByIdOrName(idOrName, this.appId); | ||
} | ||
|
||
public getCustomRoles(): Promise<Array<IRole>> { | ||
return this.roleBridge.doGetCustomRoles(this.appId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { IRole } from '../../definition/roles'; | ||
import { PermissionDeniedError } from '../errors/PermissionDeniedError'; | ||
import { AppPermissionManager } from '../managers/AppPermissionManager'; | ||
import { AppPermissions } from '../permissions/AppPermissions'; | ||
import { BaseBridge } from './BaseBridge'; | ||
|
||
export abstract class RoleBridge extends BaseBridge { | ||
public async doGetOneByIdOrName(idOrName: IRole['id'] | IRole['name'], appId: string): Promise<IRole | null> { | ||
if (this.hasReadPermission(appId)) { | ||
return this.getOneByIdOrName(idOrName, appId); | ||
} | ||
} | ||
|
||
public async doGetCustomRoles(appId: string): Promise<Array<IRole>> { | ||
if (this.hasReadPermission(appId)) { | ||
return this.getCustomRoles(appId); | ||
} | ||
} | ||
|
||
protected abstract getOneByIdOrName(idOrName: IRole['id'] | IRole['name'], appId: string): Promise<IRole | null>; | ||
protected abstract getCustomRoles(appId: string): Promise<Array<IRole>>; | ||
|
||
private hasReadPermission(appId: string): boolean { | ||
if (AppPermissionManager.hasPermission(appId, AppPermissions.role.read)) { | ||
return true; | ||
} | ||
|
||
AppPermissionManager.notifyAboutError( | ||
new PermissionDeniedError({ | ||
appId, | ||
missingPermissions: [AppPermissions.role.read], | ||
}), | ||
); | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { IRole } from '../../../src/definition/roles'; | ||
import { RoleBridge } from '../../../src/server/bridges'; | ||
|
||
export class TestsRoleBridge extends RoleBridge { | ||
public getOneByIdOrName(idOrName: IRole['id'] | IRole['name'], appId: string): Promise<IRole | null> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
public getCustomRoles(appId: string): Promise<Array<IRole>> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} |