@xeno-js/core 4.1.4 → 4.2.1

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.cjs CHANGED
@@ -5,8 +5,13 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __esm = (fn, res) => function __init() {
9
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
8
+ var __esm = (fn, res, err) => function __init() {
9
+ if (err) throw err[0];
10
+ try {
11
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
12
+ } catch (e) {
13
+ throw err = [e], e;
14
+ }
10
15
  };
11
16
  var __export = (target, all) => {
12
17
  for (var name in all)
@@ -647,15 +652,24 @@ var BaseAuthorizationStrategy = class {
647
652
  * @since 2025-09-30
648
653
  * @link https://github.com/xeno-js/xeno-js
649
654
  */
650
- constructor(_requestContext) {
655
+ constructor(_policy, _requestContext) {
656
+ this._policy = _policy;
651
657
  this._requestContext = _requestContext;
652
658
  }
659
+ _policy;
653
660
  _requestContext;
654
661
  async execute(request) {
655
662
  const context = this._requestContext.getContext();
656
663
  if (!import_shared14.Guards.isDefined(context)) {
657
664
  return import_shared13.Result.fail(import_shared13.AppError.unauthorized(request.intent, "User is not authenticated."));
658
665
  }
666
+ const superadmin = this._policy.getPolicy("superadmin");
667
+ if (!import_shared14.Guards.isNullOrEmpty(superadmin?.roles)) {
668
+ const auth = context?.identity;
669
+ const roles = auth?.roles ?? [];
670
+ const hasRequiredRole = superadmin.roles?.some((role) => roles.includes(role));
671
+ if (hasRequiredRole) return import_shared13.Result.ok();
672
+ }
659
673
  return await this.performAuthorizationCheck(request, context.identity);
660
674
  }
661
675
  };
@@ -673,11 +687,9 @@ var PermissionAuthorizationStrategy = class extends BaseAuthorizationStrategy {
673
687
  * @since 2025-09-30
674
688
  * @link https://github.com/xeno-js/xeno-js
675
689
  */
676
- constructor(_policy, requestContext) {
677
- super(requestContext);
678
- this._policy = _policy;
690
+ constructor(policy, requestContext) {
691
+ super(policy, requestContext);
679
692
  }
680
- _policy;
681
693
  async performAuthorizationCheck(command, auth) {
682
694
  const policy = this._policy.getPolicy(command.intent);
683
695
  if (!import_shared16.Guards.isNullOrEmpty(policy?.permissions)) {
@@ -706,11 +718,9 @@ var RoleAuthorizationStrategy = class extends BaseAuthorizationStrategy {
706
718
  * @since 2025-09-30
707
719
  * @link https://github.com/xeno-js/xeno-js
708
720
  */
709
- constructor(_policy, requestContext) {
710
- super(requestContext);
711
- this._policy = _policy;
721
+ constructor(policy, requestContext) {
722
+ super(policy, requestContext);
712
723
  }
713
- _policy;
714
724
  async performAuthorizationCheck(command, auth) {
715
725
  const policy = this._policy.getPolicy(command.intent);
716
726
  if (!import_shared18.Guards.isNullOrEmpty(policy?.roles)) {
@@ -737,11 +747,9 @@ var TenantAuthorizationStrategy = class extends BaseAuthorizationStrategy {
737
747
  * @since 2025-09-30
738
748
  * @link https://github.com/xeno-js/xeno-js
739
749
  */
740
- constructor(_policy, requestContext) {
741
- super(requestContext);
742
- this._policy = _policy;
750
+ constructor(policy, requestContext) {
751
+ super(policy, requestContext);
743
752
  }
744
- _policy;
745
753
  async performAuthorizationCheck(command, auth) {
746
754
  const policy = this._policy.getPolicy(command.intent);
747
755
  if (import_shared20.Guards.isDefined(policy?.tenantId)) {
@@ -764,11 +772,9 @@ var UserAuthorizationStrategy = class extends BaseAuthorizationStrategy {
764
772
  * @since 2025-09-30
765
773
  * @link https://github.com/xeno-js/xeno-js
766
774
  */
767
- constructor(_policy, requestContext) {
768
- super(requestContext);
769
- this._policy = _policy;
775
+ constructor(policy, requestContext) {
776
+ super(policy, requestContext);
770
777
  }
771
- _policy;
772
778
  async performAuthorizationCheck(command, auth) {
773
779
  const policy = this._policy.getPolicy(command.intent);
774
780
  if (import_shared22.Guards.isDefined(policy?.userId)) {
@@ -1090,6 +1096,11 @@ var PolicyRegistry = class {
1090
1096
  this._cache.set(key, policy);
1091
1097
  return this;
1092
1098
  }
1099
+ setSuperadminRoles(roles) {
1100
+ const key = this.getKey("superadmin");
1101
+ this._cache.set(key, { roles });
1102
+ return this;
1103
+ }
1093
1104
  getPolicy(intent) {
1094
1105
  const key = this.getKey(intent);
1095
1106
  return this._cache.get(key);
@@ -2049,6 +2060,10 @@ var AuthUtils = Object.freeze({
2049
2060
  const registryInstance = new PolicyRegistry();
2050
2061
  const ctxAccessor = container.resolve(import_shared52.TOKENS.CONTEXT_ACCESSOR);
2051
2062
  const Policies = /* @__PURE__ */ new Set();
2063
+ if (import_shared51.Guards.isDefined(opts.superadmin)) {
2064
+ const superadminRoles = import_shared51.Guards.isArray(opts.superadmin) ? opts.superadmin : [opts.superadmin];
2065
+ registryInstance.setSuperadminRoles(superadminRoles);
2066
+ }
2052
2067
  for (const [intent, policy] of Object.entries(policyRegistry)) {
2053
2068
  const intentLower = intent.toLowerCase();
2054
2069
  registryInstance.addPolicy(intentLower, policy);