najm-auth 1.1.1 → 1.1.2

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/index.js CHANGED
@@ -1849,12 +1849,16 @@ __export(roles_exports, {
1849
1849
  RoleValidator: () => RoleValidator,
1850
1850
  assignRoleDto: () => assignRoleDto,
1851
1851
  createRoleDto: () => createRoleDto,
1852
+ defineRoles: () => defineRoles,
1852
1853
  isAdmin: () => isAdmin,
1853
1854
  isAdministrator: () => isAdministrator,
1854
1855
  roleIdParam: () => roleIdParam,
1855
1856
  updateRoleDto: () => updateRoleDto
1856
1857
  });
1857
1858
 
1859
+ // src/roles/defineRoles.ts
1860
+ import { composeGuards as composeGuards2, createGuard as createGuard3 } from "najm-guard";
1861
+
1858
1862
  // src/roles/RoleGuards.ts
1859
1863
  import { Service as Service3 } from "najm-core";
1860
1864
  import { GuardParams, User as User2 } from "najm-core";
@@ -1902,6 +1906,35 @@ var Role = createGuard2(RoleGuard);
1902
1906
  var isAdmin = composeGuards(isAuth(), Role(ROLES.ADMIN));
1903
1907
  var isAdministrator = composeGuards(isAuth(), Role(ROLE_GROUPS.ADMINISTRATORS));
1904
1908
 
1909
+ // src/roles/defineRoles.ts
1910
+ var Role2 = createGuard3(RoleGuard);
1911
+ function defineRoles(roles) {
1912
+ const ROLES2 = roles;
1913
+ const guards2 = {};
1914
+ for (const [key, value] of Object.entries(roles)) {
1915
+ const name = `is${key.charAt(0).toUpperCase()}${key.slice(1).toLowerCase()}`;
1916
+ guards2[name] = composeGuards2(isAuth(), Role2(value));
1917
+ }
1918
+ function createGroupGuard(keys) {
1919
+ const values = keys.map((k) => roles[k]);
1920
+ return composeGuards2(isAuth(), Role2(values));
1921
+ }
1922
+ __name(createGroupGuard, "createGroupGuard");
1923
+ function hasRole(userRole, ...keys) {
1924
+ if (!userRole)
1925
+ return false;
1926
+ const normalized = userRole.toLowerCase();
1927
+ return keys.some((k) => roles[k] === normalized);
1928
+ }
1929
+ __name(hasRole, "hasRole");
1930
+ function isInGroup(userRole, keys) {
1931
+ return hasRole(userRole, ...keys);
1932
+ }
1933
+ __name(isInGroup, "isInGroup");
1934
+ return { ROLES: ROLES2, createGroupGuard, hasRole, isInGroup, ...guards2 };
1935
+ }
1936
+ __name(defineRoles, "defineRoles");
1937
+
1905
1938
  // src/roles/RoleController.ts
1906
1939
  import { Controller as Controller2 } from "najm-core";
1907
1940
  import { Get as Get2, Post as Post2, Put, Delete, ResMsg as ResMsg2 } from "najm-core";
@@ -2332,7 +2365,7 @@ PermissionRepository = __decorate17([
2332
2365
  // src/permissions/PermissionGuards.ts
2333
2366
  import { Injectable as Injectable8 } from "najm-core";
2334
2367
  import { GuardParams as GuardParams2, User as User3 } from "najm-core";
2335
- import { createGuard as createGuard3, composeGuards as composeGuards2 } from "najm-guard";
2368
+ import { createGuard as createGuard4, composeGuards as composeGuards3 } from "najm-guard";
2336
2369
  var __decorate18 = function(decorators, target, key, desc) {
2337
2370
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2338
2371
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -2385,8 +2418,8 @@ __decorate18([
2385
2418
  PermissionGuard = __decorate18([
2386
2419
  Injectable8()
2387
2420
  ], PermissionGuard);
2388
- var Permission = createGuard3(PermissionGuard);
2389
- var Can = /* @__PURE__ */ __name((permission) => composeGuards2(isAuth(), Permission(permission))(), "Can");
2421
+ var Permission = createGuard4(PermissionGuard);
2422
+ var Can = /* @__PURE__ */ __name((permission) => composeGuards3(isAuth(), Permission(permission))(), "Can");
2390
2423
  var canCreate = /* @__PURE__ */ __name((resource) => Can(`create:${resource}`), "canCreate");
2391
2424
  var canRead = /* @__PURE__ */ __name((resource) => Can(`read:${resource}`), "canRead");
2392
2425
  var canUpdate = /* @__PURE__ */ __name((resource) => Can(`update:${resource}`), "canUpdate");
@@ -2980,6 +3013,85 @@ var selectSchema = /* @__PURE__ */ __name((config) => {
2980
3013
  }, "selectSchema");
2981
3014
  var auth = /* @__PURE__ */ __name((config) => plugin("auth").version("1.0.0").depends(cache(), cookies(), i18n(), guards(), validation(config?.validation), rateLimit(config?.rateLimit), email()).requires("database").contributes(I18N_CONTRIBUTIONS, AUTH_LOCALES).services(auth_exports, users_exports, roles_exports, permissions_exports, tokens_exports).config(AUTH_CONFIG, mergeConfig(config)).set(AUTH_SCHEMA, selectSchema(config)).build(), "auth");
2982
3015
 
3016
+ // src/ownership/createResourceGuards.ts
3017
+ import { Injectable as Injectable11, Inject as Inject9, User as User4, Params as Params4 } from "najm-core";
3018
+ import { createGuard as createGuard5, composeGuards as composeGuards4 } from "najm-guard";
3019
+ var __decorate22 = function(decorators, target, key, desc) {
3020
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3021
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
3022
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3023
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
3024
+ };
3025
+ var __metadata22 = function(k, v) {
3026
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
3027
+ };
3028
+ var __param9 = function(paramIndex, decorator) {
3029
+ return function(target, key) {
3030
+ decorator(target, key, paramIndex);
3031
+ };
3032
+ };
3033
+ function createResourceGuards(ownershipClass, resourceType, resource, options) {
3034
+ var _a15, _b9;
3035
+ const writeGuard = options?.adminGuard ?? isAdmin;
3036
+ let AccessGuard = class AccessGuard {
3037
+ static {
3038
+ __name(this, "AccessGuard");
3039
+ }
3040
+ ownership;
3041
+ async canActivate(user, id) {
3042
+ const allowed = await this.ownership.canAccess(user, resourceType, id);
3043
+ return allowed ? { owner: user } : false;
3044
+ }
3045
+ };
3046
+ __decorate22([
3047
+ Inject9(ownershipClass),
3048
+ __metadata22("design:type", Object)
3049
+ ], AccessGuard.prototype, "ownership", void 0);
3050
+ __decorate22([
3051
+ __param9(0, User4()),
3052
+ __param9(1, Params4("id")),
3053
+ __metadata22("design:type", Function),
3054
+ __metadata22("design:paramtypes", [Object, String]),
3055
+ __metadata22("design:returntype", typeof (_a15 = typeof Promise !== "undefined" && Promise) === "function" ? _a15 : Object)
3056
+ ], AccessGuard.prototype, "canActivate", null);
3057
+ AccessGuard = __decorate22([
3058
+ Injectable11()
3059
+ ], AccessGuard);
3060
+ let ListGuard = class ListGuard {
3061
+ static {
3062
+ __name(this, "ListGuard");
3063
+ }
3064
+ ownership;
3065
+ async canActivate(user) {
3066
+ const ids = await this.ownership.getAccessibleIds(user, resourceType);
3067
+ return { filter: ids };
3068
+ }
3069
+ };
3070
+ __decorate22([
3071
+ Inject9(ownershipClass),
3072
+ __metadata22("design:type", Object)
3073
+ ], ListGuard.prototype, "ownership", void 0);
3074
+ __decorate22([
3075
+ __param9(0, User4()),
3076
+ __metadata22("design:type", Function),
3077
+ __metadata22("design:paramtypes", [Object]),
3078
+ __metadata22("design:returntype", typeof (_b9 = typeof Promise !== "undefined" && Promise) === "function" ? _b9 : Object)
3079
+ ], ListGuard.prototype, "canActivate", null);
3080
+ ListGuard = __decorate22([
3081
+ Injectable11()
3082
+ ], ListGuard);
3083
+ const access = createGuard5(AccessGuard);
3084
+ const list = createGuard5(ListGuard);
3085
+ return {
3086
+ canRead: composeGuards4(Can(`read:${resource}`), access()),
3087
+ canUpdate: composeGuards4(Can(`update:${resource}`), access()),
3088
+ canCreate: composeGuards4(Can(`create:${resource}`), writeGuard()),
3089
+ canDelete: composeGuards4(Can(`delete:${resource}`), writeGuard()),
3090
+ canList: composeGuards4(Can(`read:${resource}`), list())
3091
+ };
3092
+ }
3093
+ __name(createResourceGuards, "createResourceGuards");
3094
+
2983
3095
  // src/seed.ts
2984
3096
  import { hash } from "bcryptjs";
2985
3097
  var toSeedId = /* @__PURE__ */ __name((prefix, value) => {
@@ -3162,9 +3274,11 @@ export {
3162
3274
  clean,
3163
3275
  confirmResetPasswordDto,
3164
3276
  createPermissionDto,
3277
+ createResourceGuards,
3165
3278
  createRoleDto,
3166
3279
  createTokenDto,
3167
3280
  createUserDto,
3281
+ defineRoles,
3168
3282
  emailParam,
3169
3283
  formatDate,
3170
3284
  getAuthLocale,
@@ -3189,6 +3303,8 @@ export {
3189
3303
  rolesTable,
3190
3304
  seedAuthData,
3191
3305
  tokenIdParam,
3306
+ tokenStatusEnum,
3307
+ tokenTypeEnum,
3192
3308
  tokensTable,
3193
3309
  updatePermissionDto,
3194
3310
  updateRoleDto,
@@ -3196,6 +3312,7 @@ export {
3196
3312
  updateUserDto,
3197
3313
  userIdInParam,
3198
3314
  userIdParam,
3315
+ userStatusEnum,
3199
3316
  usersTable,
3200
3317
  verifyTokenDto
3201
3318
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "najm-auth",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Authentication and authorization library for najm framework",
5
5
  "type": "module",
6
6
  "files": [
@@ -64,7 +64,7 @@
64
64
  "dependencies": {
65
65
  "najm-cookies": "^1.1.1",
66
66
  "najm-core": "^1.1.1",
67
- "najm-database": "^1.1.1",
67
+ "najm-database": "^1.1.2",
68
68
  "najm-guard": "^1.1.1",
69
69
  "najm-i18n": "^1.1.1",
70
70
  "najm-cache": "^1.1.1",