@stacksjs/auth 0.70.229 → 0.70.230

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.
package/dist/rbac.d.ts CHANGED
@@ -56,7 +56,11 @@ export declare function removeRole(user: UserModel | { id: number } | number, ro
56
56
  */
57
57
  export declare function removeAllRoles(user: UserModel | { id: number } | number): Promise<void>;
58
58
  /**
59
- * Sync roles for a user (replaces all current roles)
59
+ * Sync roles for a user within one guard.
60
+ *
61
+ * Roles belonging to other guards are preserved. The previous implementation
62
+ * resolved the requested names within `guardName`, then replaced the complete
63
+ * pivot set, which silently removed assignments from every other guard.
60
64
  */
61
65
  export declare function syncRoles(user: UserModel | { id: number } | number, roleNames: string[], guardName?: string): Promise<void>;
62
66
  /**
@@ -88,7 +92,9 @@ export declare function revokePermission(user: UserModel | { id: number } | numb
88
92
  */
89
93
  export declare function revokeAllPermissions(user: UserModel | { id: number } | number): Promise<void>;
90
94
  /**
91
- * Sync direct permissions for a user
95
+ * Sync direct permissions for a user within one guard.
96
+ *
97
+ * Direct permissions belonging to other guards are preserved.
92
98
  */
93
99
  export declare function syncPermissions(user: UserModel | { id: number } | number, permissionNames: string[], guardName?: string): Promise<void>;
94
100
  /**
package/dist/rbac.js CHANGED
@@ -146,14 +146,14 @@ export async function removeAllRoles(user) {
146
146
  cache.userPermissions.delete(userId);
147
147
  }
148
148
  export async function syncRoles(user, roleNames, guardName = "web") {
149
- const userId = getUserId(user), roleIds = [];
150
- for (const name of roleNames) {
149
+ const userId = getUserId(user), preservedRoleIds = (await getUserRoles(userId)).filter((role) => role.guard_name !== guardName).map((role) => role.id), roleIds = [];
150
+ for (const name of new Set(roleNames)) {
151
151
  const role = await findRole(name, guardName);
152
152
  if (!role)
153
153
  throw Error(`Role '${name}' not found.`);
154
154
  roleIds.push(role.id);
155
155
  }
156
- await getStore().syncUserRoles(userId, roleIds);
156
+ await getStore().syncUserRoles(userId, [...preservedRoleIds, ...roleIds]);
157
157
  cache.userRoles.delete(userId);
158
158
  cache.userPermissions.delete(userId);
159
159
  }
@@ -206,14 +206,14 @@ export async function revokeAllPermissions(user) {
206
206
  cache.userPermissions.delete(userId);
207
207
  }
208
208
  export async function syncPermissions(user, permissionNames, guardName = "web") {
209
- const userId = getUserId(user), permissionIds = [];
210
- for (const name of permissionNames) {
209
+ const userId = getUserId(user), preservedPermissionIds = (await getStore().getUserDirectPermissions(userId)).filter((permission) => permission.guard_name !== guardName).map((permission) => permission.id), permissionIds = [];
210
+ for (const name of new Set(permissionNames)) {
211
211
  const perm = await findPermission(name, guardName);
212
212
  if (!perm)
213
213
  throw Error(`Permission '${name}' not found.`);
214
214
  permissionIds.push(perm.id);
215
215
  }
216
- await getStore().syncUserPermissions(userId, permissionIds);
216
+ await getStore().syncUserPermissions(userId, [...preservedPermissionIds, ...permissionIds]);
217
217
  cache.userPermissions.delete(userId);
218
218
  }
219
219
  export async function hasPermission(user, permissionName, guardName = "web") {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/auth",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.70.229",
5
+ "version": "0.70.230",
6
6
  "description": "A more simplistic way to authenticate.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -51,12 +51,12 @@
51
51
  "prepublishOnly": "bun run build"
52
52
  },
53
53
  "dependencies": {
54
- "@stacksjs/bun-router": "^0.0.20",
54
+ "@stacksjs/bun-router": "^0.0.21",
55
55
  "@stacksjs/ts-auth": "^0.4.3"
56
56
  },
57
57
  "devDependencies": {
58
58
  "better-dx": "^0.2.17",
59
- "@stacksjs/error-handling": "0.70.229",
60
- "@stacksjs/router": "0.70.229"
59
+ "@stacksjs/error-handling": "0.70.230",
60
+ "@stacksjs/router": "0.70.230"
61
61
  }
62
62
  }