@develit-services/rbac 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -480,8 +480,15 @@ let RbacServiceBase = class extends backendSdk.develitWorker(cloudflare_workers.
480
480
  { successMessage: "Scope successfully granted to user." },
481
481
  async ({ userId, scope, resourceId }) => {
482
482
  this.validateScope(scope);
483
- const userScope = await getScopesByUserQuery({ db: this.db, userId });
484
- if (userScope.some((s) => s.scope === scope)) {
483
+ const userScopes = await getScopesByUserQuery({ db: this.db, userId });
484
+ const matchingScopes = userScopes.filter((s) => s.scope === scope);
485
+ if (!resourceId && matchingScopes.find((s) => !s.resourceId)) {
486
+ throw backendSdk.createInternalError(null, {
487
+ message: "Scope already assigned to user.",
488
+ status: 409
489
+ });
490
+ }
491
+ if (resourceId && matchingScopes.some((s) => s.resourceId === resourceId)) {
485
492
  throw backendSdk.createInternalError(null, {
486
493
  message: "Scope already assigned to user.",
487
494
  status: 409
@@ -506,7 +513,16 @@ let RbacServiceBase = class extends backendSdk.develitWorker(cloudflare_workers.
506
513
  for (const scope of scopes) {
507
514
  this.validateScope(scope.scope);
508
515
  const userScopes = await getScopesByUserQuery({ db: this.db, userId });
509
- if (userScopes.some((s) => s.scope === scope.scope)) {
516
+ const matchingScopes = userScopes.filter(
517
+ (s) => s.scope === scope.scope
518
+ );
519
+ if (!scope.resourceId && matchingScopes.find((s) => !s.resourceId)) {
520
+ throw backendSdk.createInternalError(null, {
521
+ message: "Scope already assigned to user.",
522
+ status: 409
523
+ });
524
+ }
525
+ if (scope.resourceId && matchingScopes.some((s) => s.resourceId === scope.resourceId)) {
510
526
  throw backendSdk.createInternalError(null, {
511
527
  message: "Scope already assigned to user.",
512
528
  status: 409
@@ -476,8 +476,15 @@ let RbacServiceBase = class extends develitWorker(WorkerEntrypoint) {
476
476
  { successMessage: "Scope successfully granted to user." },
477
477
  async ({ userId, scope, resourceId }) => {
478
478
  this.validateScope(scope);
479
- const userScope = await getScopesByUserQuery({ db: this.db, userId });
480
- if (userScope.some((s) => s.scope === scope)) {
479
+ const userScopes = await getScopesByUserQuery({ db: this.db, userId });
480
+ const matchingScopes = userScopes.filter((s) => s.scope === scope);
481
+ if (!resourceId && matchingScopes.find((s) => !s.resourceId)) {
482
+ throw createInternalError(null, {
483
+ message: "Scope already assigned to user.",
484
+ status: 409
485
+ });
486
+ }
487
+ if (resourceId && matchingScopes.some((s) => s.resourceId === resourceId)) {
481
488
  throw createInternalError(null, {
482
489
  message: "Scope already assigned to user.",
483
490
  status: 409
@@ -502,7 +509,16 @@ let RbacServiceBase = class extends develitWorker(WorkerEntrypoint) {
502
509
  for (const scope of scopes) {
503
510
  this.validateScope(scope.scope);
504
511
  const userScopes = await getScopesByUserQuery({ db: this.db, userId });
505
- if (userScopes.some((s) => s.scope === scope.scope)) {
512
+ const matchingScopes = userScopes.filter(
513
+ (s) => s.scope === scope.scope
514
+ );
515
+ if (!scope.resourceId && matchingScopes.find((s) => !s.resourceId)) {
516
+ throw createInternalError(null, {
517
+ message: "Scope already assigned to user.",
518
+ status: 409
519
+ });
520
+ }
521
+ if (scope.resourceId && matchingScopes.some((s) => s.resourceId === scope.resourceId)) {
506
522
  throw createInternalError(null, {
507
523
  message: "Scope already assigned to user.",
508
524
  status: 409
@@ -10,10 +10,15 @@ function defineRbacServiceWrangler(config) {
10
10
  name
11
11
  }),
12
12
  vars: {
13
- // Variables
14
13
  ...envs.local.vars,
15
14
  ENVIRONMENT: "localhost"
16
15
  },
16
+ services: [
17
+ {
18
+ binding: "SECRETS_STORE",
19
+ service: `${project}-secrets-store`
20
+ }
21
+ ],
17
22
  d1_databases: [
18
23
  {
19
24
  binding: "RBAC_D1",
@@ -5,6 +5,10 @@ declare function defineRbacServiceWrangler(config: RbacServiceWranglerConfig): {
5
5
  ENVIRONMENT: string;
6
6
  SERVICE_CONFIG_INCLUDE_CONFIRMATION: boolean;
7
7
  };
8
+ services: {
9
+ binding: string;
10
+ service: string;
11
+ }[];
8
12
  d1_databases: {
9
13
  binding: string;
10
14
  database_name: string;
@@ -5,6 +5,10 @@ declare function defineRbacServiceWrangler(config: RbacServiceWranglerConfig): {
5
5
  ENVIRONMENT: string;
6
6
  SERVICE_CONFIG_INCLUDE_CONFIRMATION: boolean;
7
7
  };
8
+ services: {
9
+ binding: string;
10
+ service: string;
11
+ }[];
8
12
  d1_databases: {
9
13
  binding: string;
10
14
  database_name: string;
@@ -5,6 +5,10 @@ declare function defineRbacServiceWrangler(config: RbacServiceWranglerConfig): {
5
5
  ENVIRONMENT: string;
6
6
  SERVICE_CONFIG_INCLUDE_CONFIRMATION: boolean;
7
7
  };
8
+ services: {
9
+ binding: string;
10
+ service: string;
11
+ }[];
8
12
  d1_databases: {
9
13
  binding: string;
10
14
  database_name: string;
@@ -8,10 +8,15 @@ function defineRbacServiceWrangler(config) {
8
8
  name
9
9
  }),
10
10
  vars: {
11
- // Variables
12
11
  ...envs.local.vars,
13
12
  ENVIRONMENT: "localhost"
14
13
  },
14
+ services: [
15
+ {
16
+ binding: "SECRETS_STORE",
17
+ service: `${project}-secrets-store`
18
+ }
19
+ ],
15
20
  d1_databases: [
16
21
  {
17
22
  binding: "RBAC_D1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@develit-services/rbac",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "author": "Develit.io s.r.o.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -30,13 +30,13 @@
30
30
  "./dist"
31
31
  ],
32
32
  "scripts": {
33
- "dev": "wrangler dev --port 9237 --persist-to ../../.wrangler/state",
33
+ "dev": "wrangler dev --port 9237 --persist-to ../../.wrangler/state -c ./wrangler.jsonc -c ../../apps/secrets-store/wrangler.jsonc",
34
34
  "wrangler:generate": "bunx develit wrangler:generate --types",
35
35
  "db:init": "wrangler d1 execute develit-rbac --local --persist-to ../../.wrangler/state --command=\"SELECT 'Creating database...' AS status;\"",
36
36
  "db:generate": "drizzle-kit generate",
37
37
  "db:migrate": "drizzle-kit migrate",
38
38
  "db:explore": "drizzle-kit studio",
39
- "types": "wrangler types --env-interface RbacEnv --include-runtime false",
39
+ "types": "bash typegen.sh",
40
40
  "lint": "biome check",
41
41
  "lint:fix": "biome check --fix",
42
42
  "test": "vitest",