From d7d83fc95db5a9b353958f256b96490b88318584 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sun, 5 Jan 2025 12:24:25 +0500 Subject: [PATCH] patch: add patch for restricted properties Signed-off-by: Muhammad Aaqil --- ...ion+0.15.9+001+restricted-properties.patch | 12 ++++-- src/commands/authorization.ts | 38 +++++++++++++++++++ test/commands/authorization.test.ts | 17 +++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 src/commands/authorization.ts create mode 100644 test/commands/authorization.test.ts diff --git a/patches/@loopback+authorization+0.15.9+001+restricted-properties.patch b/patches/@loopback+authorization+0.15.9+001+restricted-properties.patch index c357e17..2ef4859 100644 --- a/patches/@loopback+authorization+0.15.9+001+restricted-properties.patch +++ b/patches/@loopback+authorization+0.15.9+001+restricted-properties.patch @@ -1,13 +1,19 @@ diff --git a/node_modules/@loopback/authorization/dist/authorize-interceptor.js b/node_modules/@loopback/authorization/dist/authorize-interceptor.js -index 1927892..c37343f 100644 +index 1927892..6b76f3c 100644 --- a/node_modules/@loopback/authorization/dist/authorize-interceptor.js +++ b/node_modules/@loopback/authorization/dist/authorize-interceptor.js -@@ -84,7 +84,20 @@ let AuthorizationInterceptor = class AuthorizationInterceptor { +@@ -84,7 +84,26 @@ let AuthorizationInterceptor = class AuthorizationInterceptor { error.statusCode = this.options.defaultStatusCodeForDeny; throw error; } - return next(); -+ const restrictedProperties = await invocationCtx.get(keys_1.AuthorizationTags.RESTRICTED_FIELDS); ++ const restrictedProperties = await invocationCtx.get(keys_1.AuthorizationTags.RESTRICTED_FIELDS) || []; ++ if ( ++ metadata.restrictedProperties && ++ metadata.restrictedProperties.length ++ ) { ++ restrictedProperties.push(...metadata.restrictedProperties); ++ } + let result = await next(); + if (result && restrictedProperties) { + restrictedProperties.forEach(property => { diff --git a/src/commands/authorization.ts b/src/commands/authorization.ts new file mode 100644 index 0000000..018b31e --- /dev/null +++ b/src/commands/authorization.ts @@ -0,0 +1,38 @@ +import {Command, Flags} from '@oclif/core' +import { Project } from 'ts-morph'; +import chalk from 'chalk'; +import fs from 'fs'; +import { execute, isLoopBackApp, processOptions } from '../utils/index.js'; + +export default class Authorization extends Command { + + static override description = 'add authorization layer.'; + + static override flags = { + config: Flags.string({ char: 'c', description: 'Config JSON object' }), + acls: Flags.string({ description: 'array of acls.' }), + } + + public async run(): Promise { + const parsed = await this.parse(Authorization) + let options = processOptions(parsed.flags); + const { acls } = options; + console.log(acls); + const project = new Project({ + tsConfigFilePath: 'tsconfig.json', + compilerOptions: { allowJs: true, checkJs: true } + }); + const invokedFrom = process.cwd(); + + console.log(chalk.blue('Confirming if this is a LoopBack 4 project.')); + let packageJson: any = fs.readFileSync(`${invokedFrom}/package.json`, { encoding: 'utf8' }) + packageJson = JSON.parse(packageJson); + + if (!isLoopBackApp(packageJson)) throw Error('Not a loopback project'); + console.log(chalk.bold(chalk.green('OK.'))); + + const deps = packageJson.dependencies; + const pkg = '@loopbacl/authorization'; + if (!deps[pkg]) { await execute(`npm i ${pkg}`, `Installing ${pkg}`); } + } +} diff --git a/test/commands/authorization.test.ts b/test/commands/authorization.test.ts new file mode 100644 index 0000000..092924a --- /dev/null +++ b/test/commands/authorization.test.ts @@ -0,0 +1,17 @@ +import {expect, test} from '@oclif/test' + +describe('authorization', () => { + test + .stdout() + .command(['authorization']) + .it('runs hello', ctx => { + expect(ctx.stdout).to.contain('hello world') + }) + + test + .stdout() + .command(['authorization', '--name', 'jeff']) + .it('runs hello --name jeff', ctx => { + expect(ctx.stdout).to.contain('hello jeff') + }) +})