PR comments.

This commit is contained in:
mike12345567 2023-06-27 12:45:00 +01:00
parent 7e79c827a7
commit 4d2aa2a67b
5 changed files with 19 additions and 15 deletions

View File

@ -25,11 +25,11 @@ const EXTERNAL_BUILTIN_ROLE_IDS = [
BUILTIN_IDS.PUBLIC, BUILTIN_IDS.PUBLIC,
] ]
export const RoleVersion = { export const RoleIDVersion = {
// original version, with a UUID based ID // original version, with a UUID based ID
VERSION_1: undefined, UUID: undefined,
// new version - with name based ID // new version - with name based ID
VERSION_2: 2, NAME: "name",
} }
export class Role implements RoleDoc { export class Role implements RoleDoc {
@ -38,7 +38,7 @@ export class Role implements RoleDoc {
name: string name: string
permissionId: string permissionId: string
inherits?: string inherits?: string
version?: number version?: string
permissions = {} permissions = {}
constructor(id: string, name: string, permissionId: string) { constructor(id: string, name: string, permissionId: string) {
@ -46,7 +46,7 @@ export class Role implements RoleDoc {
this.name = name this.name = name
this.permissionId = permissionId this.permissionId = permissionId
// version for managing the ID - removing the role_ when responding // version for managing the ID - removing the role_ when responding
this.version = RoleVersion.VERSION_2 this.version = RoleIDVersion.NAME
} }
addInheritance(inherits: string) { addInheritance(inherits: string) {
@ -408,11 +408,11 @@ export function getDBRoleID(roleName: string) {
/** /**
* Remove the "role_" from builtin role IDs that have been written to the DB (for permissions). * Remove the "role_" from builtin role IDs that have been written to the DB (for permissions).
*/ */
export function getExternalRoleID(roleId: string, version?: number) { export function getExternalRoleID(roleId: string, version?: string) {
// for built-in roles we want to remove the DB role ID element (role_) // for built-in roles we want to remove the DB role ID element (role_)
if ( if (
(roleId.startsWith(DocumentType.ROLE) && isBuiltin(roleId)) || (roleId.startsWith(DocumentType.ROLE) && isBuiltin(roleId)) ||
version === RoleVersion.VERSION_2 version === RoleIDVersion.NAME
) { ) {
return roleId.split(`${DocumentType.ROLE}${SEPARATOR}`)[1] return roleId.split(`${DocumentType.ROLE}${SEPARATOR}`)[1]
} }

View File

@ -15,7 +15,7 @@ async function updateRolesOnUserTable(
db: Database, db: Database,
roleId: string, roleId: string,
updateOption: string, updateOption: string,
roleVersion?: number roleVersion: string | undefined
) { ) {
const table = await db.get(InternalTables.USER_METADATA) const table = await db.get(InternalTables.USER_METADATA)
const schema = table.schema const schema = table.schema
@ -26,7 +26,7 @@ async function updateRolesOnUserTable(
updated = true updated = true
const constraints = schema[prop].constraints const constraints = schema[prop].constraints
const updatedRoleId = const updatedRoleId =
roleVersion === roles.RoleVersion.VERSION_2 roleVersion === roles.RoleIDVersion.NAME
? roles.getExternalRoleID(roleId, roleVersion) ? roles.getExternalRoleID(roleId, roleVersion)
: roleId : roleId
const indexOf = constraints.inclusion.indexOf(updatedRoleId) const indexOf = constraints.inclusion.indexOf(updatedRoleId)
@ -66,7 +66,7 @@ export async function save(ctx: UserCtx) {
isCreate = true isCreate = true
} }
// version 2 roles need updated to add back role_ // version 2 roles need updated to add back role_
else if (version === roles.RoleVersion.VERSION_2) { else if (version === roles.RoleIDVersion.NAME) {
_id = generateRoleID(name) _id = generateRoleID(name)
} }

View File

@ -22,12 +22,12 @@ import tar from "tar"
const MemoryStream = require("memorystream") const MemoryStream = require("memorystream")
interface DBDumpOpts { export interface DBDumpOpts {
filter?: any filter?: any
exportPath?: string exportPath?: string
} }
interface ExportOpts extends DBDumpOpts { export interface ExportOpts extends DBDumpOpts {
tar?: boolean tar?: boolean
excludeRows?: boolean excludeRows?: boolean
excludeLogs?: boolean excludeLogs?: boolean
@ -57,7 +57,10 @@ function tarFilesToTmp(tmpDir: string, files: string[]) {
* a filter function or the name of the export. * a filter function or the name of the export.
* @return {*} either a readable stream or a string * @return {*} either a readable stream or a string
*/ */
export async function exportDB(dbName: string, opts: DBDumpOpts = {}) { export async function exportDB(
dbName: string,
opts: DBDumpOpts = {}
): Promise<DBDumpOpts> {
const exportOpts = { const exportOpts = {
filter: opts?.filter, filter: opts?.filter,
batch_size: 1000, batch_size: 1000,
@ -178,6 +181,7 @@ export async function exportApp(appId: string, config?: ExportOpts) {
* Streams a backup of the database state for an app * Streams a backup of the database state for an app
* @param {string} appId The ID of the app which is to be backed up. * @param {string} appId The ID of the app which is to be backed up.
* @param {boolean} excludeRows Flag to state whether the export should include data. * @param {boolean} excludeRows Flag to state whether the export should include data.
* @param {string} encryptPassword password for encrypting the export.
* @returns {*} a readable stream of the backup which is written in real time * @returns {*} a readable stream of the backup which is written in real time
*/ */
export async function streamExportApp({ export async function streamExportApp({

View File

@ -266,7 +266,7 @@ export function basicRole() {
name: `NewRole_${utils.newid()}`, name: `NewRole_${utils.newid()}`,
inherits: roles.BUILTIN_ROLE_IDS.BASIC, inherits: roles.BUILTIN_ROLE_IDS.BASIC,
permissionId: permissions.BuiltinPermissionID.READ_ONLY, permissionId: permissions.BuiltinPermissionID.READ_ONLY,
version: 2, version: "name",
} }
} }

View File

@ -4,5 +4,5 @@ export interface Role extends Document {
permissionId: string permissionId: string
inherits?: string inherits?: string
permissions: { [key: string]: string[] } permissions: { [key: string]: string[] }
version?: number version?: string
} }