@flusys/nestjs-auth 8.0.0 → 8.0.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/fesm/120.js +0 -1
- package/fesm/24.js +30 -59
- package/package.json +3 -3
- package/services/user-permission.service.d.ts +0 -2
package/fesm/120.js
CHANGED
|
@@ -1865,7 +1865,6 @@ class UserPermissionController {
|
|
|
1865
1865
|
/** Process batch permission assignments/revocations */ async processBatchPermissions(userId, items, type, user) {
|
|
1866
1866
|
let added = 0, removed = 0;
|
|
1867
1867
|
for (const item of items){
|
|
1868
|
-
await this.permissionService.validateTenantScope(type, item.targetId, user);
|
|
1869
1868
|
if (item.isAdd) {
|
|
1870
1869
|
await this.permissionService.assignPermission(userId, item.targetId, type);
|
|
1871
1870
|
added++;
|
package/fesm/24.js
CHANGED
|
@@ -3414,12 +3414,11 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
3414
3414
|
/* import */ var _flusys_nestjs_shared_classes__rspack_import_1 = __webpack_require__(6937);
|
|
3415
3415
|
/* import */ var _flusys_nestjs_shared_interfaces__rspack_import_2 = __webpack_require__(5103);
|
|
3416
3416
|
/* import */ var _nestjs_common__rspack_import_3 = __webpack_require__(6157);
|
|
3417
|
-
/* import */ var
|
|
3418
|
-
/* import */ var
|
|
3419
|
-
/* import */ var
|
|
3420
|
-
/* import */ var
|
|
3421
|
-
/* import */ var
|
|
3422
|
-
/* import */ var _auth_datasource_provider_js__rspack_import_7 = __webpack_require__(2732);
|
|
3417
|
+
/* import */ var _config_index_js__rspack_import_7 = __webpack_require__(5363);
|
|
3418
|
+
/* import */ var _config_index_js__rspack_import_8 = __webpack_require__(7987);
|
|
3419
|
+
/* import */ var _entities_user_company_permission_entity_js__rspack_import_4 = __webpack_require__(2377);
|
|
3420
|
+
/* import */ var _auth_config_service_js__rspack_import_5 = __webpack_require__(6514);
|
|
3421
|
+
/* import */ var _auth_datasource_provider_js__rspack_import_6 = __webpack_require__(2732);
|
|
3423
3422
|
function _ts_decorate(decorators, target, key, desc) {
|
|
3424
3423
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3425
3424
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
@@ -3451,7 +3450,6 @@ function _ts_param(paramIndex, decorator) {
|
|
|
3451
3450
|
|
|
3452
3451
|
|
|
3453
3452
|
|
|
3454
|
-
|
|
3455
3453
|
class UserPermissionService {
|
|
3456
3454
|
dataSourceProvider;
|
|
3457
3455
|
authConfig;
|
|
@@ -3464,37 +3462,10 @@ class UserPermissionService {
|
|
|
3464
3462
|
}
|
|
3465
3463
|
/**
|
|
3466
3464
|
* Ensures the acting user is only granting/revoking access within their own tenant.
|
|
3467
|
-
* Without this, any user holding USER_PERMISSIONS.UPDATE could assign/revoke access
|
|
3468
3465
|
* for a company or branch outside their own company.
|
|
3469
|
-
*/ async
|
|
3470
|
-
if (!this.authConfig.isCompanyFeatureEnabled() || !user.companyId) {
|
|
3471
|
-
return;
|
|
3472
|
-
}
|
|
3473
|
-
if (type === _entities_user_company_permission_entity_js__rspack_import_5/* .PermissionType.COMPANY */.e.COMPANY) {
|
|
3474
|
-
if (targetId !== user.companyId) {
|
|
3475
|
-
throw new _nestjs_common__rspack_import_3.ForbiddenException({
|
|
3476
|
-
message: 'You do not have access to this company',
|
|
3477
|
-
messageKey: _config_index_js__rspack_import_8/* .USER_PERMISSION_MESSAGES.COMPANY_NO_ACCESS */.jz.COMPANY_NO_ACCESS
|
|
3478
|
-
});
|
|
3479
|
-
}
|
|
3480
|
-
return;
|
|
3481
|
-
}
|
|
3482
|
-
const branchRepository = await this.dataSourceProvider.getRepository(_entities_company_branch_entity_js__rspack_import_4/* .CompanyBranch */.A);
|
|
3483
|
-
const branch = await branchRepository.findOne({
|
|
3484
|
-
where: {
|
|
3485
|
-
id: targetId
|
|
3486
|
-
}
|
|
3487
|
-
});
|
|
3488
|
-
if (!branch || branch.companyId !== user.companyId) {
|
|
3489
|
-
throw new _nestjs_common__rspack_import_3.ForbiddenException({
|
|
3490
|
-
message: 'You do not have access to this branch',
|
|
3491
|
-
messageKey: _config_index_js__rspack_import_8/* .USER_PERMISSION_MESSAGES.BRANCH_NO_ACCESS */.jz.BRANCH_NO_ACCESS
|
|
3492
|
-
});
|
|
3493
|
-
}
|
|
3494
|
-
}
|
|
3495
|
-
async getRepository() {
|
|
3466
|
+
*/ async getRepository() {
|
|
3496
3467
|
if (!this._repository) {
|
|
3497
|
-
this._repository = await this.dataSourceProvider.getRepository(
|
|
3468
|
+
this._repository = await this.dataSourceProvider.getRepository(_entities_user_company_permission_entity_js__rspack_import_4/* .UserCompanyPermission */.I);
|
|
3498
3469
|
}
|
|
3499
3470
|
return this._repository;
|
|
3500
3471
|
}
|
|
@@ -3507,7 +3478,7 @@ class UserPermissionService {
|
|
|
3507
3478
|
permissionType: type
|
|
3508
3479
|
},
|
|
3509
3480
|
relations: [
|
|
3510
|
-
type ===
|
|
3481
|
+
type === _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType.COMPANY */.e.COMPANY ? 'company' : 'branch'
|
|
3511
3482
|
],
|
|
3512
3483
|
order: {
|
|
3513
3484
|
createdAt: 'DESC'
|
|
@@ -3516,7 +3487,7 @@ class UserPermissionService {
|
|
|
3516
3487
|
}
|
|
3517
3488
|
/** Assign permission to user */ async assignPermission(userId, targetId, type) {
|
|
3518
3489
|
const repository = await this.getRepository();
|
|
3519
|
-
const typeName = type ===
|
|
3490
|
+
const typeName = type === _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType.COMPANY */.e.COMPANY ? 'company' : 'branch';
|
|
3520
3491
|
const existing = await repository.findOne({
|
|
3521
3492
|
where: {
|
|
3522
3493
|
userId,
|
|
@@ -3527,7 +3498,7 @@ class UserPermissionService {
|
|
|
3527
3498
|
if (existing) {
|
|
3528
3499
|
throw new _nestjs_common__rspack_import_3.ConflictException({
|
|
3529
3500
|
message: `User already assigned to this ${typeName}`,
|
|
3530
|
-
messageKey:
|
|
3501
|
+
messageKey: _config_index_js__rspack_import_7/* .USER_PERMISSION_MESSAGES.ALREADY_ASSIGNED */.jz.ALREADY_ASSIGNED,
|
|
3531
3502
|
messageVariables: {
|
|
3532
3503
|
type: typeName
|
|
3533
3504
|
}
|
|
@@ -3544,7 +3515,7 @@ class UserPermissionService {
|
|
|
3544
3515
|
if (err?.code === 'ER_DUP_ENTRY' || String(err?.message).includes('Duplicate entry')) {
|
|
3545
3516
|
throw new _nestjs_common__rspack_import_3.ConflictException({
|
|
3546
3517
|
message: `User already assigned to this ${typeName}. Please refresh and try again.`,
|
|
3547
|
-
messageKey:
|
|
3518
|
+
messageKey: _config_index_js__rspack_import_7/* .USER_PERMISSION_MESSAGES.ALREADY_ASSIGNED */.jz.ALREADY_ASSIGNED,
|
|
3548
3519
|
messageVariables: {
|
|
3549
3520
|
type: typeName
|
|
3550
3521
|
}
|
|
@@ -3552,11 +3523,11 @@ class UserPermissionService {
|
|
|
3552
3523
|
}
|
|
3553
3524
|
throw error;
|
|
3554
3525
|
}
|
|
3555
|
-
await this.publishPermissionEvent(
|
|
3526
|
+
await this.publishPermissionEvent(_config_index_js__rspack_import_8/* .AUTH_EVENT_ACTIONS.PERMISSION_GRANTED */.o.PERMISSION_GRANTED, userId, targetId, type);
|
|
3556
3527
|
}
|
|
3557
3528
|
async revokePermission(userId, targetId, type) {
|
|
3558
3529
|
const repository = await this.getRepository();
|
|
3559
|
-
const typeName = type ===
|
|
3530
|
+
const typeName = type === _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType.COMPANY */.e.COMPANY ? 'company' : 'branch';
|
|
3560
3531
|
const permission = await repository.findOne({
|
|
3561
3532
|
where: {
|
|
3562
3533
|
userId,
|
|
@@ -3564,33 +3535,33 @@ class UserPermissionService {
|
|
|
3564
3535
|
permissionType: type
|
|
3565
3536
|
},
|
|
3566
3537
|
relations: [
|
|
3567
|
-
type ===
|
|
3538
|
+
type === _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType.BRANCH */.e.BRANCH ? 'branch' : 'company'
|
|
3568
3539
|
]
|
|
3569
3540
|
});
|
|
3570
3541
|
if (!permission) {
|
|
3571
3542
|
throw new _nestjs_common__rspack_import_3.NotFoundException({
|
|
3572
3543
|
message: `${typeName.charAt(0).toUpperCase() + typeName.slice(1)} permission not found`,
|
|
3573
|
-
messageKey:
|
|
3544
|
+
messageKey: _config_index_js__rspack_import_7/* .USER_PERMISSION_MESSAGES.PERMISSION_NOT_FOUND */.jz.PERMISSION_NOT_FOUND,
|
|
3574
3545
|
messageVariables: {
|
|
3575
3546
|
type: typeName.charAt(0).toUpperCase() + typeName.slice(1)
|
|
3576
3547
|
}
|
|
3577
3548
|
});
|
|
3578
3549
|
}
|
|
3579
|
-
const companyId = type ===
|
|
3550
|
+
const companyId = type === _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType.COMPANY */.e.COMPANY ? targetId : permission.branch?.companyId;
|
|
3580
3551
|
await repository.remove(permission);
|
|
3581
|
-
if (type ===
|
|
3552
|
+
if (type === _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType.COMPANY */.e.COMPANY) {
|
|
3582
3553
|
const revokedBranchIds = await this.revokeBranchPermissionsForCompany(repository, userId, targetId);
|
|
3583
3554
|
await this.permissionSyncAdapter?.onUserCompanyAccessRevoked?.(userId, targetId);
|
|
3584
3555
|
await Promise.all(revokedBranchIds.map((branchId)=>this.permissionSyncAdapter?.onUserBranchAccessRevoked?.(userId, branchId, targetId)));
|
|
3585
3556
|
} else {
|
|
3586
3557
|
if (companyId) await this.permissionSyncAdapter?.onUserBranchAccessRevoked?.(userId, targetId, companyId);
|
|
3587
3558
|
}
|
|
3588
|
-
await this.publishPermissionEvent(
|
|
3559
|
+
await this.publishPermissionEvent(_config_index_js__rspack_import_8/* .AUTH_EVENT_ACTIONS.PERMISSION_REVOKED */.o.PERMISSION_REVOKED, userId, targetId, type, companyId);
|
|
3589
3560
|
}
|
|
3590
3561
|
async publishPermissionEvent(action, userId, targetId, type, companyId) {
|
|
3591
3562
|
await (0,_flusys_nestjs_shared_classes__rspack_import_1.publishDomainEvent)({
|
|
3592
3563
|
module: (/* inlined export .AUTH_EVENT_MODULE */"auth"),
|
|
3593
|
-
entity:
|
|
3564
|
+
entity: _config_index_js__rspack_import_8/* .AUTH_EVENT_ENTITIES.USER_PERMISSION */.g.USER_PERMISSION,
|
|
3594
3565
|
action,
|
|
3595
3566
|
ids: [
|
|
3596
3567
|
targetId
|
|
@@ -3608,7 +3579,7 @@ class UserPermissionService {
|
|
|
3608
3579
|
const branchPermissions = await repository.createQueryBuilder('permission').innerJoin('permission.branch', 'branch').where('permission.userId = :userId', {
|
|
3609
3580
|
userId
|
|
3610
3581
|
}).andWhere('permission.permissionType = :type', {
|
|
3611
|
-
type:
|
|
3582
|
+
type: _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType.BRANCH */.e.BRANCH
|
|
3612
3583
|
}).andWhere('branch.companyId = :companyId', {
|
|
3613
3584
|
companyId
|
|
3614
3585
|
}).getMany();
|
|
@@ -3619,13 +3590,13 @@ class UserPermissionService {
|
|
|
3619
3590
|
return branchPermissions.map((permission)=>permission.targetId);
|
|
3620
3591
|
}
|
|
3621
3592
|
// Convenience Methods
|
|
3622
|
-
findUserCompanies = (userId)=>this.findUserPermissions(userId,
|
|
3593
|
+
findUserCompanies = (userId)=>this.findUserPermissions(userId, _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType.COMPANY */.e.COMPANY);
|
|
3623
3594
|
/** Find user branch permissions with optional company filter */ async findUserBranches(userId, companyId) {
|
|
3624
3595
|
const repository = await this.getRepository();
|
|
3625
3596
|
const queryBuilder = repository.createQueryBuilder('permission').leftJoinAndSelect('permission.branch', 'branch').where('permission.userId = :userId', {
|
|
3626
3597
|
userId
|
|
3627
3598
|
}).andWhere('permission.permissionType = :type', {
|
|
3628
|
-
type:
|
|
3599
|
+
type: _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType.BRANCH */.e.BRANCH
|
|
3629
3600
|
}).orderBy('permission.createdAt', 'DESC');
|
|
3630
3601
|
if (companyId) {
|
|
3631
3602
|
queryBuilder.andWhere('branch.companyId = :companyId', {
|
|
@@ -3640,7 +3611,7 @@ class UserPermissionService {
|
|
|
3640
3611
|
return repository.find({
|
|
3641
3612
|
where: {
|
|
3642
3613
|
targetId: companyId,
|
|
3643
|
-
permissionType:
|
|
3614
|
+
permissionType: _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType.COMPANY */.e.COMPANY
|
|
3644
3615
|
},
|
|
3645
3616
|
relations: [
|
|
3646
3617
|
'user'
|
|
@@ -3655,7 +3626,7 @@ class UserPermissionService {
|
|
|
3655
3626
|
return repository.find({
|
|
3656
3627
|
where: {
|
|
3657
3628
|
targetId: branchId,
|
|
3658
|
-
permissionType:
|
|
3629
|
+
permissionType: _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType.BRANCH */.e.BRANCH
|
|
3659
3630
|
},
|
|
3660
3631
|
relations: [
|
|
3661
3632
|
'user'
|
|
@@ -3675,7 +3646,7 @@ _ts_decorate([
|
|
|
3675
3646
|
_ts_metadata("design:paramtypes", [
|
|
3676
3647
|
String,
|
|
3677
3648
|
String,
|
|
3678
|
-
typeof
|
|
3649
|
+
typeof _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType */.e === "undefined" ? Object : _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType */.e
|
|
3679
3650
|
]),
|
|
3680
3651
|
_ts_metadata("design:returntype", Promise)
|
|
3681
3652
|
], UserPermissionService.prototype, "assignPermission", null);
|
|
@@ -3688,7 +3659,7 @@ _ts_decorate([
|
|
|
3688
3659
|
_ts_metadata("design:paramtypes", [
|
|
3689
3660
|
String,
|
|
3690
3661
|
String,
|
|
3691
|
-
typeof
|
|
3662
|
+
typeof _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType */.e === "undefined" ? Object : _entities_user_company_permission_entity_js__rspack_import_4/* .PermissionType */.e
|
|
3692
3663
|
]),
|
|
3693
3664
|
_ts_metadata("design:returntype", Promise)
|
|
3694
3665
|
], UserPermissionService.prototype, "revokePermission", null);
|
|
@@ -3696,14 +3667,14 @@ UserPermissionService = _ts_decorate([
|
|
|
3696
3667
|
(0,_nestjs_common__rspack_import_3.Injectable)({
|
|
3697
3668
|
scope: _nestjs_common__rspack_import_3.Scope.REQUEST
|
|
3698
3669
|
}),
|
|
3699
|
-
_ts_param(0, (0,_nestjs_common__rspack_import_3.Inject)(
|
|
3700
|
-
_ts_param(1, (0,_nestjs_common__rspack_import_3.Inject)(
|
|
3670
|
+
_ts_param(0, (0,_nestjs_common__rspack_import_3.Inject)(_auth_datasource_provider_js__rspack_import_6/* .AuthDataSourceProvider */.v)),
|
|
3671
|
+
_ts_param(1, (0,_nestjs_common__rspack_import_3.Inject)(_auth_config_service_js__rspack_import_5/* .AuthConfigService */.W)),
|
|
3701
3672
|
_ts_param(2, (0,_nestjs_common__rspack_import_3.Optional)()),
|
|
3702
3673
|
_ts_param(2, (0,_nestjs_common__rspack_import_3.Inject)(_flusys_nestjs_shared_interfaces__rspack_import_2.PERMISSION_SYNC_ADAPTER)),
|
|
3703
3674
|
_ts_metadata("design:type", Function),
|
|
3704
3675
|
_ts_metadata("design:paramtypes", [
|
|
3705
|
-
typeof
|
|
3706
|
-
typeof
|
|
3676
|
+
typeof _auth_datasource_provider_js__rspack_import_6/* .AuthDataSourceProvider */.v === "undefined" ? Object : _auth_datasource_provider_js__rspack_import_6/* .AuthDataSourceProvider */.v,
|
|
3677
|
+
typeof _auth_config_service_js__rspack_import_5/* .AuthConfigService */.W === "undefined" ? Object : _auth_config_service_js__rspack_import_5/* .AuthConfigService */.W,
|
|
3707
3678
|
typeof IPermissionSyncAdapter === "undefined" ? Object : IPermissionSyncAdapter
|
|
3708
3679
|
])
|
|
3709
3680
|
], UserPermissionService);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flusys/nestjs-auth",
|
|
3
|
-
"version": "8.0.
|
|
3
|
+
"version": "8.0.2",
|
|
4
4
|
"description": "Authentication and authorization infrastructure for NestJS applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./fesm/index.js",
|
|
@@ -91,7 +91,7 @@
|
|
|
91
91
|
"rxjs": "^7.8.0"
|
|
92
92
|
},
|
|
93
93
|
"dependencies": {
|
|
94
|
-
"@flusys/nestjs-core": "8.0.
|
|
95
|
-
"@flusys/nestjs-shared": "8.0.
|
|
94
|
+
"@flusys/nestjs-core": "8.0.2",
|
|
95
|
+
"@flusys/nestjs-shared": "8.0.2"
|
|
96
96
|
}
|
|
97
97
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { type ILoggedUserInfo } from '@flusys/nestjs-shared';
|
|
2
1
|
import { type IPermissionSyncAdapter } from '@flusys/nestjs-shared/interfaces';
|
|
3
2
|
import { PermissionType, UserCompanyPermission } from '../entities/user-company-permission.entity.js';
|
|
4
3
|
import { AuthConfigService } from './auth-config.service.js';
|
|
@@ -9,7 +8,6 @@ export declare class UserPermissionService {
|
|
|
9
8
|
private readonly permissionSyncAdapter?;
|
|
10
9
|
private _repository;
|
|
11
10
|
constructor(dataSourceProvider: AuthDataSourceProvider, authConfig: AuthConfigService, permissionSyncAdapter?: IPermissionSyncAdapter | undefined);
|
|
12
|
-
validateTenantScope(type: PermissionType, targetId: string, user: ILoggedUserInfo): Promise<void>;
|
|
13
11
|
private getRepository;
|
|
14
12
|
findUserPermissions(userId: string, type: PermissionType): Promise<UserCompanyPermission[]>;
|
|
15
13
|
assignPermission(userId: string, targetId: string, type: PermissionType): Promise<void>;
|