@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.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { IRequest, IHandler, IFactory, UserContext, Optional, ResultType, ICommand, RequestType, IStrategy, IContextAccessor, RequestContext, Identity, IValidatorService, IPipelineBehavior, Delegate, IQuery, ICacheableOptions, ISpecification, XenoRegistry, IXenoApplication, IServiceContainer, ServicesProxy, XenoIncomingRequest, ResponseDto, NativeListenOptions, IAppBuilder, IConfigurationService, Nullable, HttpServerPlugin, NativeRouteDefinition, SetupAction, MiddlewareConfig, ContextPlugin, LoggerPlugin, LoggerConfig, CachePlugin, CacheConfig, AuthPlugin, AuthModuleOptions, DbPlugin, PipelinePlugin, HttpPlugin, HttpConfig, IModule, IRequestContext, ITransactionalDb, DbModuleOptions, IServiceScope, IUnitOfWork, IDisposable, AuthorizationOptions, ConcurrencyOptions, XenoPlugin, IdempotencyOptions, LogLevel, PerformanceOptions, IReadDao, IReadDataSource, IMapper, IRepository, IWriteDataSource, IController, IMediator, Dictionary, AppError } from '@xeno-js/shared';
1
+ import { IRequest, IHandler, IFactory, UserContext, Optional, ResultType, ICommand, RequestType, IStrategy, IPolicyRegistry, IContextAccessor, RequestContext, Identity, IValidatorService, IPipelineBehavior, Delegate, IQuery, ICacheableOptions, ISpecification, XenoRegistry, IXenoApplication, IServiceContainer, ServicesProxy, XenoIncomingRequest, ResponseDto, NativeListenOptions, IAppBuilder, IConfigurationService, Nullable, HttpServerPlugin, NativeRouteDefinition, SetupAction, MiddlewareConfig, ContextPlugin, LoggerPlugin, LoggerConfig, CachePlugin, CacheConfig, AuthPlugin, AuthModuleOptions, DbPlugin, PipelinePlugin, HttpPlugin, HttpConfig, IModule, IRequestContext, ITransactionalDb, DbModuleOptions, IServiceScope, IUnitOfWork, IDisposable, AuthorizationOptions, ConcurrencyOptions, XenoPlugin, IdempotencyOptions, LogLevel, PerformanceOptions, IReadDao, IReadDataSource, IMapper, IRepository, IWriteDataSource, IController, IMediator, Dictionary, AppError } from '@xeno-js/shared';
2
2
  export * from '@xeno-js/shared';
3
3
 
4
4
  /**
@@ -69,6 +69,7 @@ declare abstract class Command<TResponse = unknown> implements ICommand<TRespons
69
69
  * @link https://github.com/xeno-js/xeno-js
70
70
  */
71
71
  declare abstract class BaseAuthorizationStrategy<TInput extends IRequest> implements IStrategy<TInput> {
72
+ protected readonly _policy: IPolicyRegistry;
72
73
  private readonly _requestContext;
73
74
  /**
74
75
  * @description Constructs a new instance of the BaseAuthorizationStrategy class, which serves as an abstract base for specific authorization strategies in the CQRS pipeline. It takes an IRequestContext as a parameter, which is used to retrieve the identity of the currently authenticated user during the authorization process. This context is essential for performing the authorization checks based on the user's identity when executing requests that require specific permissions.
@@ -79,7 +80,7 @@ declare abstract class BaseAuthorizationStrategy<TInput extends IRequest> implem
79
80
  * @since 2025-09-30
80
81
  * @link https://github.com/xeno-js/xeno-js
81
82
  */
82
- constructor(_requestContext: IContextAccessor<RequestContext>);
83
+ constructor(_policy: IPolicyRegistry, _requestContext: IContextAccessor<RequestContext>);
83
84
  execute(request: IRequest): Promise<ResultType<void>>;
84
85
  /**
85
86
  * @description Abstract method that must be implemented by concrete authorization strategies to perform the actual authorization check. This method is called after the user's identity has been retrieved and verified. It receives the request and the user's identity as parameters and should return a Result indicating whether the authorization check passed or failed.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IRequest, IHandler, IFactory, UserContext, Optional, ResultType, ICommand, RequestType, IStrategy, IContextAccessor, RequestContext, Identity, IValidatorService, IPipelineBehavior, Delegate, IQuery, ICacheableOptions, ISpecification, XenoRegistry, IXenoApplication, IServiceContainer, ServicesProxy, XenoIncomingRequest, ResponseDto, NativeListenOptions, IAppBuilder, IConfigurationService, Nullable, HttpServerPlugin, NativeRouteDefinition, SetupAction, MiddlewareConfig, ContextPlugin, LoggerPlugin, LoggerConfig, CachePlugin, CacheConfig, AuthPlugin, AuthModuleOptions, DbPlugin, PipelinePlugin, HttpPlugin, HttpConfig, IModule, IRequestContext, ITransactionalDb, DbModuleOptions, IServiceScope, IUnitOfWork, IDisposable, AuthorizationOptions, ConcurrencyOptions, XenoPlugin, IdempotencyOptions, LogLevel, PerformanceOptions, IReadDao, IReadDataSource, IMapper, IRepository, IWriteDataSource, IController, IMediator, Dictionary, AppError } from '@xeno-js/shared';
1
+ import { IRequest, IHandler, IFactory, UserContext, Optional, ResultType, ICommand, RequestType, IStrategy, IPolicyRegistry, IContextAccessor, RequestContext, Identity, IValidatorService, IPipelineBehavior, Delegate, IQuery, ICacheableOptions, ISpecification, XenoRegistry, IXenoApplication, IServiceContainer, ServicesProxy, XenoIncomingRequest, ResponseDto, NativeListenOptions, IAppBuilder, IConfigurationService, Nullable, HttpServerPlugin, NativeRouteDefinition, SetupAction, MiddlewareConfig, ContextPlugin, LoggerPlugin, LoggerConfig, CachePlugin, CacheConfig, AuthPlugin, AuthModuleOptions, DbPlugin, PipelinePlugin, HttpPlugin, HttpConfig, IModule, IRequestContext, ITransactionalDb, DbModuleOptions, IServiceScope, IUnitOfWork, IDisposable, AuthorizationOptions, ConcurrencyOptions, XenoPlugin, IdempotencyOptions, LogLevel, PerformanceOptions, IReadDao, IReadDataSource, IMapper, IRepository, IWriteDataSource, IController, IMediator, Dictionary, AppError } from '@xeno-js/shared';
2
2
  export * from '@xeno-js/shared';
3
3
 
4
4
  /**
@@ -69,6 +69,7 @@ declare abstract class Command<TResponse = unknown> implements ICommand<TRespons
69
69
  * @link https://github.com/xeno-js/xeno-js
70
70
  */
71
71
  declare abstract class BaseAuthorizationStrategy<TInput extends IRequest> implements IStrategy<TInput> {
72
+ protected readonly _policy: IPolicyRegistry;
72
73
  private readonly _requestContext;
73
74
  /**
74
75
  * @description Constructs a new instance of the BaseAuthorizationStrategy class, which serves as an abstract base for specific authorization strategies in the CQRS pipeline. It takes an IRequestContext as a parameter, which is used to retrieve the identity of the currently authenticated user during the authorization process. This context is essential for performing the authorization checks based on the user's identity when executing requests that require specific permissions.
@@ -79,7 +80,7 @@ declare abstract class BaseAuthorizationStrategy<TInput extends IRequest> implem
79
80
  * @since 2025-09-30
80
81
  * @link https://github.com/xeno-js/xeno-js
81
82
  */
82
- constructor(_requestContext: IContextAccessor<RequestContext>);
83
+ constructor(_policy: IPolicyRegistry, _requestContext: IContextAccessor<RequestContext>);
83
84
  execute(request: IRequest): Promise<ResultType<void>>;
84
85
  /**
85
86
  * @description Abstract method that must be implemented by concrete authorization strategies to perform the actual authorization check. This method is called after the user's identity has been retrieved and verified. It receives the request and the user's identity as parameters and should return a Result indicating whether the authorization check passed or failed.
package/dist/index.js CHANGED
@@ -1,7 +1,12 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __getOwnPropNames = Object.getOwnPropertyNames;
3
- var __esm = (fn, res) => function __init() {
4
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
3
+ var __esm = (fn, res, err) => function __init() {
4
+ if (err) throw err[0];
5
+ try {
6
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
7
+ } catch (e) {
8
+ throw err = [e], e;
9
+ }
5
10
  };
6
11
  var __export = (target, all) => {
7
12
  for (var name in all)
@@ -595,15 +600,24 @@ var BaseAuthorizationStrategy = class {
595
600
  * @since 2025-09-30
596
601
  * @link https://github.com/xeno-js/xeno-js
597
602
  */
598
- constructor(_requestContext) {
603
+ constructor(_policy, _requestContext) {
604
+ this._policy = _policy;
599
605
  this._requestContext = _requestContext;
600
606
  }
607
+ _policy;
601
608
  _requestContext;
602
609
  async execute(request) {
603
610
  const context = this._requestContext.getContext();
604
611
  if (!Guards4.isDefined(context)) {
605
612
  return Result6.fail(AppError6.unauthorized(request.intent, "User is not authenticated."));
606
613
  }
614
+ const superadmin = this._policy.getPolicy("superadmin");
615
+ if (!Guards4.isNullOrEmpty(superadmin?.roles)) {
616
+ const auth = context?.identity;
617
+ const roles = auth?.roles ?? [];
618
+ const hasRequiredRole = superadmin.roles?.some((role) => roles.includes(role));
619
+ if (hasRequiredRole) return Result6.ok();
620
+ }
607
621
  return await this.performAuthorizationCheck(request, context.identity);
608
622
  }
609
623
  };
@@ -621,11 +635,9 @@ var PermissionAuthorizationStrategy = class extends BaseAuthorizationStrategy {
621
635
  * @since 2025-09-30
622
636
  * @link https://github.com/xeno-js/xeno-js
623
637
  */
624
- constructor(_policy, requestContext) {
625
- super(requestContext);
626
- this._policy = _policy;
638
+ constructor(policy, requestContext) {
639
+ super(policy, requestContext);
627
640
  }
628
- _policy;
629
641
  async performAuthorizationCheck(command, auth) {
630
642
  const policy = this._policy.getPolicy(command.intent);
631
643
  if (!Guards5.isNullOrEmpty(policy?.permissions)) {
@@ -654,11 +666,9 @@ var RoleAuthorizationStrategy = class extends BaseAuthorizationStrategy {
654
666
  * @since 2025-09-30
655
667
  * @link https://github.com/xeno-js/xeno-js
656
668
  */
657
- constructor(_policy, requestContext) {
658
- super(requestContext);
659
- this._policy = _policy;
669
+ constructor(policy, requestContext) {
670
+ super(policy, requestContext);
660
671
  }
661
- _policy;
662
672
  async performAuthorizationCheck(command, auth) {
663
673
  const policy = this._policy.getPolicy(command.intent);
664
674
  if (!Guards6.isNullOrEmpty(policy?.roles)) {
@@ -685,11 +695,9 @@ var TenantAuthorizationStrategy = class extends BaseAuthorizationStrategy {
685
695
  * @since 2025-09-30
686
696
  * @link https://github.com/xeno-js/xeno-js
687
697
  */
688
- constructor(_policy, requestContext) {
689
- super(requestContext);
690
- this._policy = _policy;
698
+ constructor(policy, requestContext) {
699
+ super(policy, requestContext);
691
700
  }
692
- _policy;
693
701
  async performAuthorizationCheck(command, auth) {
694
702
  const policy = this._policy.getPolicy(command.intent);
695
703
  if (Guards7.isDefined(policy?.tenantId)) {
@@ -712,11 +720,9 @@ var UserAuthorizationStrategy = class extends BaseAuthorizationStrategy {
712
720
  * @since 2025-09-30
713
721
  * @link https://github.com/xeno-js/xeno-js
714
722
  */
715
- constructor(_policy, requestContext) {
716
- super(requestContext);
717
- this._policy = _policy;
723
+ constructor(policy, requestContext) {
724
+ super(policy, requestContext);
718
725
  }
719
- _policy;
720
726
  async performAuthorizationCheck(command, auth) {
721
727
  const policy = this._policy.getPolicy(command.intent);
722
728
  if (Guards8.isDefined(policy?.userId)) {
@@ -1038,6 +1044,11 @@ var PolicyRegistry = class {
1038
1044
  this._cache.set(key, policy);
1039
1045
  return this;
1040
1046
  }
1047
+ setSuperadminRoles(roles) {
1048
+ const key = this.getKey("superadmin");
1049
+ this._cache.set(key, { roles });
1050
+ return this;
1051
+ }
1041
1052
  getPolicy(intent) {
1042
1053
  const key = this.getKey(intent);
1043
1054
  return this._cache.get(key);
@@ -1998,6 +2009,10 @@ var AuthUtils = Object.freeze({
1998
2009
  const registryInstance = new PolicyRegistry();
1999
2010
  const ctxAccessor = container.resolve(TOKENS3.CONTEXT_ACCESSOR);
2000
2011
  const Policies = /* @__PURE__ */ new Set();
2012
+ if (Guards27.isDefined(opts.superadmin)) {
2013
+ const superadminRoles = Guards27.isArray(opts.superadmin) ? opts.superadmin : [opts.superadmin];
2014
+ registryInstance.setSuperadminRoles(superadminRoles);
2015
+ }
2001
2016
  for (const [intent, policy] of Object.entries(policyRegistry)) {
2002
2017
  const intentLower = intent.toLowerCase();
2003
2018
  registryInstance.addPolicy(intentLower, policy);