{"id":21508087,"url":"https://github.com/wtnm/nestjs-authorization-and-filtering","last_synced_at":"2025-03-17T14:46:03.762Z","repository":{"id":223171841,"uuid":"758892069","full_name":"wtnm/nestjs-authorization-and-filtering","owner":"wtnm","description":"Nestjs with flexible custom authorization and declarative (django-like) filtering","archived":false,"fork":false,"pushed_at":"2024-02-19T07:01:02.000Z","size":142,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-31T01:41:13.894Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wtnm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-02-17T11:41:52.000Z","updated_at":"2024-02-17T11:46:33.000Z","dependencies_parsed_at":"2024-02-18T19:43:49.408Z","dependency_job_id":null,"html_url":"https://github.com/wtnm/nestjs-authorization-and-filtering","commit_stats":null,"previous_names":["wtnm/nestjs-authorization-and-filtering"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtnm%2Fnestjs-authorization-and-filtering","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtnm%2Fnestjs-authorization-and-filtering/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtnm%2Fnestjs-authorization-and-filtering/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtnm%2Fnestjs-authorization-and-filtering/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wtnm","download_url":"https://codeload.github.com/wtnm/nestjs-authorization-and-filtering/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244054408,"owners_count":20390546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T20:45:43.749Z","updated_at":"2025-03-17T14:46:03.709Z","avatar_url":"https://github.com/wtnm.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nest with custom authorization and declarative filtering\n\n[Nest](https://github.com/nestjs/nest) framework TypeScript Typeorm starter repository with implemented django-like declarative (field__operator) filtering and flexible customizable authorization (with row filtering and columns exclusion)\n\n## Declarative (django-like) filtering\nGET methods should receive object whose prop names are written in the format `${field}__${operation}`, where `field` is a field in the Entity, and `operation` is a filtering method.\n\nExample:\n\n```js\n{id_in: [1,2]} // transforms into SQL 'WHERE id IN (1,2)'\n```\n### Supported operators\n\n##### in\n`field__in: value` transforms into `field IN (...value)`\n\n##### range\n`field__range: value` transforms into `value[0] \u003c= field AND field \u003c value[0]`\n\n##### gt\n`field__gt: value` transforms into `field \u003e value`\n\n##### gte\n`field__gte: value` transforms into `field \u003e= value`\n\n##### lt\n`field__lt: value` transforms into `field \u003c value`\n\n##### lte\n`field__lte: value` transforms into `field \u003c= value`\n\n##### exact\n`field__exact: value` transforms into `field = value`\n\n##### iexact\n`field__iexact: value` transforms into `field ILIKE value`\n\n##### startswith\n`field__startswith: value` transforms into `field LIKE value%`\n\n##### istartswith\n`field__istartswith: value` transforms into `field IIKE value%`\n\n##### contains\n`field__contains: value` transforms into `field LIKE %value%`\n\n##### icontains\n`field__icontains: value` transforms into `field IIKE %value%`\n\n##### endswith\n`field__endswith: value` transforms into `field LIKE %value`\n\n##### iendswith\n`field__iendswith: value` transforms into `field IIKE %value`\n\n## Customizable authorization\nEntities Role and User have field `auth`, which contains authorization information.\n\nThe initial value is taken from Role.auth (by foreign key User.role = Role.id) and then merged with User.auth.\n\nI.e. general authorization rights are assigned in the Role, and then can be further expanded (or removed) for each individual User.\n\nField `auth` has following format:\n\n```ts\ntype AuthAccessEntities = {\n  [entity: string]: Record\u003c\n    'create' | 'read' | 'update' | 'delete',\n    boolean | { filterRows: { [field__op: string]: any }; excludeCols: string[] }\n  \u003e;\n};\n```\n\nThe `filterRows` field specifies a declarative filter that will restrict access to rows.\n\nThe `excludeCols` field specifies fields to which access will be denied.\n\nExample:\n\n```ts\nconst roleAuth: AuthAccessEntities = {\n  user: {\n    read: { filterRows: { role__in: [1] }, excludeCols: ['auth'] },\n    create: { filterRows: { role__in: [1] }, excludeCols: ['auth'] },\n    update: { filterRows: { role__in: [1] }, excludeCols: ['auth'] },\n    delete: { filterRows: { role__in: [1] }, excludeCols: ['auth'] },\n  },\n};\n\n```\n#### Value USER_SELF_MAGIC_VALUE\nThe `USER_SELF_MAGIC_VALUE` value is converted to its own User.id, to limit access only to the user's own records (where necessary).\n\nExample:\n\n```ts\nimport { USER_SELF_MAGIC_VALUE } from 'src/base/modules/auth/auth.constants';\n\nconst filterRows = {user_id__in: [USER_SELF_MAGIC_VALUE]}\n```\n\n#### AuthBaseController and AuthBaseService\u003cT\u003e\nClasses that implement basic authorization management and basic CRUD. Can be inherited.\n\n#### AuthEntity and AuthOperation decorators\n`AuthEntity` decorate class to define what entity name should be used for access. \n\n`AuthOperation` decorate method to define what operation name should be used for access.\n```ts\n@Controller('user')\n@AuthEntity('user')\nexport class UserController extends AuthBaseController{\n  @Post()\n  @AuthOperation('create')\n  createOne() {}\n}\n```\n\n\n## Installation\nClone (or download), copy .env_example to .env and .env.test, run:\n\n```bash\n$ npm install\n```\n\n## Running the app\n\n```bash\n# development\n$ npm run start\n\n# watch mode\n$ npm run start:dev\n\n# production mode\n$ npm run start:prod\n```\n\n## Migrations\n\n```bash\n# generate migration with any_name\n$ npm run migration:generate --name=any_name\n\n# run\n$ npm run migration:run\n\n# revert\n$ npm run migration:revert\n```\n\n## Test\n\n```bash\n# unit tests\n$ npm test\n\n# e2e tests\n$ npm run test:e2e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwtnm%2Fnestjs-authorization-and-filtering","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwtnm%2Fnestjs-authorization-and-filtering","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwtnm%2Fnestjs-authorization-and-filtering/lists"}