@flusys/nestjs-auth 1.1.0-beta → 1.1.0
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/README.md +633 -468
- package/cjs/config/auth.constants.js +8 -0
- package/cjs/controllers/authentication.controller.js +175 -167
- package/cjs/controllers/branch.controller.js +44 -1
- package/cjs/controllers/company-selection.controller.js +27 -20
- package/cjs/controllers/company.controller.js +44 -1
- package/cjs/controllers/email-verification.controller.js +220 -0
- package/cjs/controllers/index.js +1 -0
- package/cjs/controllers/user-permission.controller.js +82 -154
- package/cjs/controllers/user.controller.js +68 -15
- package/cjs/docs/auth-swagger.config.js +14 -7
- package/cjs/dtos/authentication.dto.js +16 -43
- package/cjs/dtos/index.js +0 -1
- package/cjs/dtos/user-permission.dto.js +28 -0
- package/cjs/dtos/user.dto.js +19 -4
- package/cjs/entities/index.js +16 -17
- package/cjs/entities/user-company-permission.entity.js +3 -9
- package/cjs/interceptors/clear-token.interceptor.js +10 -23
- package/cjs/interceptors/set-token.interceptor.js +15 -55
- package/cjs/interfaces/auth-email-provider.interface.js +1 -1
- package/cjs/modules/auth.module.js +79 -154
- package/cjs/services/auth-config.service.js +33 -35
- package/cjs/services/auth-datasource.provider.js +18 -42
- package/cjs/services/auth-email.service.js +89 -85
- package/cjs/services/authentication.service.js +248 -414
- package/cjs/services/branch.service.js +9 -4
- package/cjs/services/company-selection-session.service.js +1 -1
- package/cjs/services/user-permission.service.js +43 -73
- package/cjs/services/user.service.js +78 -64
- package/cjs/strategies/jwt.strategy.js +44 -44
- package/cjs/validators/password.validator.js +45 -0
- package/config/auth.constants.d.ts +2 -0
- package/controllers/authentication.controller.d.ts +4 -10
- package/controllers/company-selection.controller.d.ts +6 -2
- package/controllers/email-verification.controller.d.ts +20 -0
- package/controllers/index.d.ts +1 -0
- package/controllers/user-permission.controller.d.ts +5 -4
- package/controllers/user.controller.d.ts +6 -0
- package/dtos/authentication.dto.d.ts +0 -4
- package/dtos/company-branch.dto.d.ts +0 -1
- package/dtos/company.dto.d.ts +0 -1
- package/dtos/index.d.ts +0 -1
- package/dtos/user-permission.dto.d.ts +4 -0
- package/dtos/user.dto.d.ts +1 -3
- package/entities/index.d.ts +5 -10
- package/entities/user-company-permission.entity.d.ts +1 -3
- package/fesm/config/auth.constants.js +6 -13
- package/fesm/controllers/authentication.controller.js +178 -170
- package/fesm/controllers/branch.controller.js +45 -2
- package/fesm/controllers/company-selection.controller.js +28 -21
- package/fesm/controllers/company.controller.js +45 -2
- package/fesm/controllers/email-verification.controller.js +210 -0
- package/fesm/controllers/index.js +1 -0
- package/fesm/controllers/user-permission.controller.js +84 -156
- package/fesm/controllers/user.controller.js +71 -18
- package/fesm/docs/auth-swagger.config.js +14 -7
- package/fesm/dtos/authentication.dto.js +17 -44
- package/fesm/dtos/index.js +0 -1
- package/fesm/dtos/user-permission.dto.js +28 -0
- package/fesm/dtos/user.dto.js +20 -5
- package/fesm/entities/index.js +17 -9
- package/fesm/entities/user-company-permission.entity.js +3 -9
- package/fesm/interceptors/clear-token.interceptor.js +9 -22
- package/fesm/interceptors/set-token.interceptor.js +14 -54
- package/fesm/interfaces/auth-email-provider.interface.js +1 -1
- package/fesm/modules/auth.module.js +82 -157
- package/fesm/services/auth-config.service.js +34 -36
- package/fesm/services/auth-datasource.provider.js +19 -43
- package/fesm/services/auth-email.service.js +90 -86
- package/fesm/services/authentication.service.js +248 -414
- package/fesm/services/branch.service.js +9 -4
- package/fesm/services/company-selection-session.service.js +1 -1
- package/fesm/services/user-permission.service.js +44 -74
- package/fesm/services/user.service.js +79 -65
- package/fesm/strategies/jwt.strategy.js +44 -44
- package/fesm/validators/password.validator.js +48 -0
- package/interceptors/clear-token.interceptor.d.ts +0 -1
- package/interceptors/set-token.interceptor.d.ts +1 -1
- package/interfaces/authentication.interface.d.ts +0 -4
- package/interfaces/user-enricher.interface.d.ts +1 -4
- package/modules/auth.module.d.ts +9 -4
- package/package.json +4 -4
- package/services/auth-config.service.d.ts +7 -8
- package/services/auth-datasource.provider.d.ts +3 -4
- package/services/auth-email.service.d.ts +8 -4
- package/services/authentication.service.d.ts +16 -16
- package/services/company-selection-session.service.d.ts +1 -1
- package/services/user-permission.service.d.ts +6 -9
- package/services/user.service.d.ts +3 -0
- package/strategies/jwt.strategy.d.ts +4 -1
- package/validators/password.validator.d.ts +10 -0
- package/cjs/dtos/user-company-permission.dto.js +0 -110
- package/dtos/user-company-permission.dto.d.ts +0 -10
- package/fesm/dtos/user-company-permission.dto.js +0 -98
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "EmailVerificationController", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return EmailVerificationController;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _dtos = require("@flusys/nestjs-shared/dtos");
|
|
12
|
+
const _common = require("@nestjs/common");
|
|
13
|
+
const _swagger = require("@nestjs/swagger");
|
|
14
|
+
const _throttler = require("@nestjs/throttler");
|
|
15
|
+
const _express = require("express");
|
|
16
|
+
const _authenticationdto = require("../dtos/authentication.dto");
|
|
17
|
+
const _settokeninterceptor = require("../interceptors/set-token.interceptor");
|
|
18
|
+
const _authconfigservice = require("../services/auth-config.service");
|
|
19
|
+
const _authdatasourceprovider = require("../services/auth-datasource.provider");
|
|
20
|
+
const _authemailservice = require("../services/auth-email.service");
|
|
21
|
+
const _authenticationservice = require("../services/authentication.service");
|
|
22
|
+
function _define_property(obj, key, value) {
|
|
23
|
+
if (key in obj) {
|
|
24
|
+
Object.defineProperty(obj, key, {
|
|
25
|
+
value: value,
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true
|
|
29
|
+
});
|
|
30
|
+
} else {
|
|
31
|
+
obj[key] = value;
|
|
32
|
+
}
|
|
33
|
+
return obj;
|
|
34
|
+
}
|
|
35
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
36
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
37
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
38
|
+
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;
|
|
39
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
40
|
+
}
|
|
41
|
+
function _ts_metadata(k, v) {
|
|
42
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
43
|
+
}
|
|
44
|
+
function _ts_param(paramIndex, decorator) {
|
|
45
|
+
return function(target, key) {
|
|
46
|
+
decorator(target, key, paramIndex);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
let EmailVerificationController = class EmailVerificationController {
|
|
50
|
+
ensureEmailVerificationEnabled() {
|
|
51
|
+
if (!this.dataSourceProvider.isEmailVerificationEnabled()) {
|
|
52
|
+
throw new _common.BadRequestException('Email verification is not enabled for this tenant');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
buildAuthUrl(req, path) {
|
|
56
|
+
const frontendUrl = this.authConfig.getFrontendUrl();
|
|
57
|
+
return frontendUrl ? `${frontendUrl}${path}` : `${req.protocol}://${req.get('host')}${path}`;
|
|
58
|
+
}
|
|
59
|
+
async forgotPassword(req, dto) {
|
|
60
|
+
this.ensureEmailVerificationEnabled();
|
|
61
|
+
return this.authEmailService.forgotPassword(dto.email, this.buildAuthUrl(req, '/auth/reset-password'));
|
|
62
|
+
}
|
|
63
|
+
async resetPassword(dto) {
|
|
64
|
+
this.ensureEmailVerificationEnabled();
|
|
65
|
+
return this.authEmailService.resetPassword(dto.token, dto.newPassword);
|
|
66
|
+
}
|
|
67
|
+
async resendVerification(req, dto) {
|
|
68
|
+
this.ensureEmailVerificationEnabled();
|
|
69
|
+
return this.authEmailService.resendVerificationEmail(dto.email, this.buildAuthUrl(req, '/auth/verify-email'));
|
|
70
|
+
}
|
|
71
|
+
async verifyEmail(dto) {
|
|
72
|
+
this.ensureEmailVerificationEnabled();
|
|
73
|
+
const result = await this.authEmailService.verifyEmail(dto.token);
|
|
74
|
+
if (result.type === 'pending') {
|
|
75
|
+
return this.authService.createUserFromPendingRegistration(result.pending);
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
success: true,
|
|
79
|
+
message: result.message
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
constructor(authService, authEmailService, authConfig, dataSourceProvider){
|
|
83
|
+
_define_property(this, "authService", void 0);
|
|
84
|
+
_define_property(this, "authEmailService", void 0);
|
|
85
|
+
_define_property(this, "authConfig", void 0);
|
|
86
|
+
_define_property(this, "dataSourceProvider", void 0);
|
|
87
|
+
this.authService = authService;
|
|
88
|
+
this.authEmailService = authEmailService;
|
|
89
|
+
this.authConfig = authConfig;
|
|
90
|
+
this.dataSourceProvider = dataSourceProvider;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
_ts_decorate([
|
|
94
|
+
(0, _common.Post)('forgot-password'),
|
|
95
|
+
(0, _common.HttpCode)(_common.HttpStatus.OK),
|
|
96
|
+
(0, _throttler.Throttle)({
|
|
97
|
+
default: {
|
|
98
|
+
limit: 3,
|
|
99
|
+
ttl: 300000
|
|
100
|
+
}
|
|
101
|
+
}),
|
|
102
|
+
(0, _swagger.ApiOperation)({
|
|
103
|
+
summary: 'Request password reset email'
|
|
104
|
+
}),
|
|
105
|
+
(0, _swagger.ApiBody)({
|
|
106
|
+
type: _authenticationdto.ForgotPasswordDto
|
|
107
|
+
}),
|
|
108
|
+
(0, _swagger.ApiResponse)({
|
|
109
|
+
status: 200,
|
|
110
|
+
type: _dtos.MessageResponseDto
|
|
111
|
+
}),
|
|
112
|
+
_ts_param(0, (0, _common.Req)()),
|
|
113
|
+
_ts_param(1, (0, _common.Body)()),
|
|
114
|
+
_ts_metadata("design:type", Function),
|
|
115
|
+
_ts_metadata("design:paramtypes", [
|
|
116
|
+
typeof _express.Request === "undefined" ? Object : _express.Request,
|
|
117
|
+
typeof _authenticationdto.ForgotPasswordDto === "undefined" ? Object : _authenticationdto.ForgotPasswordDto
|
|
118
|
+
]),
|
|
119
|
+
_ts_metadata("design:returntype", Promise)
|
|
120
|
+
], EmailVerificationController.prototype, "forgotPassword", null);
|
|
121
|
+
_ts_decorate([
|
|
122
|
+
(0, _common.Post)('reset-password'),
|
|
123
|
+
(0, _common.HttpCode)(_common.HttpStatus.OK),
|
|
124
|
+
(0, _throttler.Throttle)({
|
|
125
|
+
default: {
|
|
126
|
+
limit: 5,
|
|
127
|
+
ttl: 900000
|
|
128
|
+
}
|
|
129
|
+
}),
|
|
130
|
+
(0, _swagger.ApiOperation)({
|
|
131
|
+
summary: 'Reset password using token'
|
|
132
|
+
}),
|
|
133
|
+
(0, _swagger.ApiBody)({
|
|
134
|
+
type: _authenticationdto.ResetPasswordDto
|
|
135
|
+
}),
|
|
136
|
+
(0, _swagger.ApiResponse)({
|
|
137
|
+
status: 200,
|
|
138
|
+
type: _dtos.MessageResponseDto
|
|
139
|
+
}),
|
|
140
|
+
_ts_param(0, (0, _common.Body)()),
|
|
141
|
+
_ts_metadata("design:type", Function),
|
|
142
|
+
_ts_metadata("design:paramtypes", [
|
|
143
|
+
typeof _authenticationdto.ResetPasswordDto === "undefined" ? Object : _authenticationdto.ResetPasswordDto
|
|
144
|
+
]),
|
|
145
|
+
_ts_metadata("design:returntype", Promise)
|
|
146
|
+
], EmailVerificationController.prototype, "resetPassword", null);
|
|
147
|
+
_ts_decorate([
|
|
148
|
+
(0, _common.Post)('resend-verification'),
|
|
149
|
+
(0, _common.HttpCode)(_common.HttpStatus.OK),
|
|
150
|
+
(0, _throttler.Throttle)({
|
|
151
|
+
default: {
|
|
152
|
+
limit: 3,
|
|
153
|
+
ttl: 300000
|
|
154
|
+
}
|
|
155
|
+
}),
|
|
156
|
+
(0, _swagger.ApiOperation)({
|
|
157
|
+
summary: 'Resend email verification link'
|
|
158
|
+
}),
|
|
159
|
+
(0, _swagger.ApiBody)({
|
|
160
|
+
type: _authenticationdto.ResendVerificationDto
|
|
161
|
+
}),
|
|
162
|
+
(0, _swagger.ApiResponse)({
|
|
163
|
+
status: 200,
|
|
164
|
+
type: _dtos.MessageResponseDto
|
|
165
|
+
}),
|
|
166
|
+
_ts_param(0, (0, _common.Req)()),
|
|
167
|
+
_ts_param(1, (0, _common.Body)()),
|
|
168
|
+
_ts_metadata("design:type", Function),
|
|
169
|
+
_ts_metadata("design:paramtypes", [
|
|
170
|
+
typeof _express.Request === "undefined" ? Object : _express.Request,
|
|
171
|
+
typeof _authenticationdto.ResendVerificationDto === "undefined" ? Object : _authenticationdto.ResendVerificationDto
|
|
172
|
+
]),
|
|
173
|
+
_ts_metadata("design:returntype", Promise)
|
|
174
|
+
], EmailVerificationController.prototype, "resendVerification", null);
|
|
175
|
+
_ts_decorate([
|
|
176
|
+
(0, _common.Post)('verify-email'),
|
|
177
|
+
(0, _common.HttpCode)(_common.HttpStatus.OK),
|
|
178
|
+
(0, _throttler.Throttle)({
|
|
179
|
+
default: {
|
|
180
|
+
limit: 10,
|
|
181
|
+
ttl: 60000
|
|
182
|
+
}
|
|
183
|
+
}),
|
|
184
|
+
(0, _common.UseInterceptors)(_settokeninterceptor.SetToken),
|
|
185
|
+
(0, _swagger.ApiOperation)({
|
|
186
|
+
summary: 'Verify email using token'
|
|
187
|
+
}),
|
|
188
|
+
(0, _swagger.ApiBody)({
|
|
189
|
+
type: _authenticationdto.VerifyEmailDto
|
|
190
|
+
}),
|
|
191
|
+
(0, _swagger.ApiResponse)({
|
|
192
|
+
status: 200,
|
|
193
|
+
type: _dtos.MessageResponseDto
|
|
194
|
+
}),
|
|
195
|
+
(0, _swagger.ApiResponse)({
|
|
196
|
+
status: 200,
|
|
197
|
+
type: _authenticationdto.RegistrationResponseDto
|
|
198
|
+
}),
|
|
199
|
+
_ts_param(0, (0, _common.Body)()),
|
|
200
|
+
_ts_metadata("design:type", Function),
|
|
201
|
+
_ts_metadata("design:paramtypes", [
|
|
202
|
+
typeof _authenticationdto.VerifyEmailDto === "undefined" ? Object : _authenticationdto.VerifyEmailDto
|
|
203
|
+
]),
|
|
204
|
+
_ts_metadata("design:returntype", Promise)
|
|
205
|
+
], EmailVerificationController.prototype, "verifyEmail", null);
|
|
206
|
+
EmailVerificationController = _ts_decorate([
|
|
207
|
+
(0, _swagger.ApiTags)('Authentication'),
|
|
208
|
+
(0, _common.Controller)('auth'),
|
|
209
|
+
_ts_param(0, (0, _common.Inject)(_authenticationservice.AuthenticationService)),
|
|
210
|
+
_ts_param(1, (0, _common.Inject)(_authemailservice.AuthEmailService)),
|
|
211
|
+
_ts_param(2, (0, _common.Inject)(_authconfigservice.AuthConfigService)),
|
|
212
|
+
_ts_param(3, (0, _common.Inject)(_authdatasourceprovider.AuthDataSourceProvider)),
|
|
213
|
+
_ts_metadata("design:type", Function),
|
|
214
|
+
_ts_metadata("design:paramtypes", [
|
|
215
|
+
typeof _authenticationservice.AuthenticationService === "undefined" ? Object : _authenticationservice.AuthenticationService,
|
|
216
|
+
typeof _authemailservice.AuthEmailService === "undefined" ? Object : _authemailservice.AuthEmailService,
|
|
217
|
+
typeof _authconfigservice.AuthConfigService === "undefined" ? Object : _authconfigservice.AuthConfigService,
|
|
218
|
+
typeof _authdatasourceprovider.AuthDataSourceProvider === "undefined" ? Object : _authdatasourceprovider.AuthDataSourceProvider
|
|
219
|
+
])
|
|
220
|
+
], EmailVerificationController);
|
package/cjs/controllers/index.js
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
});
|
|
5
5
|
_export_star(require("./authentication.controller"), exports);
|
|
6
6
|
_export_star(require("./company-selection.controller"), exports);
|
|
7
|
+
_export_star(require("./email-verification.controller"), exports);
|
|
7
8
|
_export_star(require("./user.controller"), exports);
|
|
8
9
|
_export_star(require("./company.controller"), exports);
|
|
9
10
|
_export_star(require("./branch.controller"), exports);
|
|
@@ -8,10 +8,11 @@ Object.defineProperty(exports, "UserPermissionController", {
|
|
|
8
8
|
return UserPermissionController;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
+
const _nestjsshared = require("@flusys/nestjs-shared");
|
|
11
12
|
const _common = require("@nestjs/common");
|
|
12
13
|
const _swagger = require("@nestjs/swagger");
|
|
13
|
-
const _guards = require("@flusys/nestjs-shared/guards");
|
|
14
14
|
const _userpermissiondto = require("../dtos/user-permission.dto");
|
|
15
|
+
const _usercompanypermissionentity = require("../entities/user-company-permission.entity");
|
|
15
16
|
const _userpermissionservice = require("../services/user-permission.service");
|
|
16
17
|
function _define_property(obj, key, value) {
|
|
17
18
|
if (key in obj) {
|
|
@@ -41,123 +42,67 @@ function _ts_param(paramIndex, decorator) {
|
|
|
41
42
|
};
|
|
42
43
|
}
|
|
43
44
|
let UserPermissionController = class UserPermissionController {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (item.isAdd) {
|
|
50
|
-
await this.userPermissionService.assignUserToCompany({
|
|
51
|
-
userId: dto.userId,
|
|
52
|
-
companyId: item.targetId
|
|
53
|
-
});
|
|
54
|
-
addedCount++;
|
|
55
|
-
} else {
|
|
56
|
-
await this.userPermissionService.revokeCompanyAccess(dto.userId, item.targetId);
|
|
57
|
-
removedCount++;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
45
|
+
async assignUserCompany(dto) {
|
|
46
|
+
return this.processBatchPermissions(dto.userId, dto.items, _usercompanypermissionentity.PermissionType.COMPANY);
|
|
47
|
+
}
|
|
48
|
+
async getUserCompanies(userId) {
|
|
49
|
+
const permissions = await this.permissionService.findUserCompanies(userId);
|
|
60
50
|
return {
|
|
61
51
|
success: true,
|
|
62
|
-
message:
|
|
52
|
+
message: 'User companies retrieved successfully',
|
|
53
|
+
data: permissions.map((p)=>({
|
|
54
|
+
id: p.id,
|
|
55
|
+
userId: p.userId,
|
|
56
|
+
companyId: p.company?.id || p.targetId,
|
|
57
|
+
companyName: p.company?.name || '',
|
|
58
|
+
companySlug: p.company?.slug || ''
|
|
59
|
+
}))
|
|
63
60
|
};
|
|
64
61
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
userId: permission.userId,
|
|
71
|
-
companyId: permission.company?.id || permission.targetId,
|
|
72
|
-
companyName: permission.company?.name || '',
|
|
73
|
-
companySlug: permission.company?.slug || ''
|
|
74
|
-
}));
|
|
62
|
+
async assignUserBranch(dto) {
|
|
63
|
+
return this.processBatchPermissions(dto.userId, dto.items, _usercompanypermissionentity.PermissionType.BRANCH);
|
|
64
|
+
}
|
|
65
|
+
async getUserBranches(userId, companyId) {
|
|
66
|
+
const permissions = await this.permissionService.findUserBranches(userId, companyId);
|
|
75
67
|
return {
|
|
76
|
-
|
|
68
|
+
success: true,
|
|
69
|
+
message: 'User branches retrieved successfully',
|
|
70
|
+
data: permissions.map((p)=>({
|
|
71
|
+
id: p.id,
|
|
72
|
+
userId: p.userId,
|
|
73
|
+
branchId: p.branch?.id || p.targetId,
|
|
74
|
+
branchName: p.branch?.name || '',
|
|
75
|
+
branchSlug: p.branch?.slug || '',
|
|
76
|
+
companyId: p.branch?.companyId || ''
|
|
77
|
+
}))
|
|
77
78
|
};
|
|
78
79
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
let removedCount = 0;
|
|
83
|
-
for (const item of dto.items){
|
|
80
|
+
/** Process batch permission assignments/revocations */ async processBatchPermissions(userId, items, type) {
|
|
81
|
+
let added = 0, removed = 0;
|
|
82
|
+
for (const item of items){
|
|
84
83
|
if (item.isAdd) {
|
|
85
|
-
await this.
|
|
86
|
-
|
|
87
|
-
branchId: item.targetId
|
|
88
|
-
});
|
|
89
|
-
addedCount++;
|
|
84
|
+
await this.permissionService.assignPermission(userId, item.targetId, type);
|
|
85
|
+
added++;
|
|
90
86
|
} else {
|
|
91
|
-
await this.
|
|
92
|
-
|
|
87
|
+
await this.permissionService.revokePermission(userId, item.targetId, type);
|
|
88
|
+
removed++;
|
|
93
89
|
}
|
|
94
90
|
}
|
|
95
91
|
return {
|
|
96
92
|
success: true,
|
|
97
|
-
message: `Processed ${
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
/** Get user's branches */ async getUserBranches(userId) {
|
|
101
|
-
const permissions = await this.userPermissionService.findUserBranches(userId);
|
|
102
|
-
// Map to DTO format with populated branch details
|
|
103
|
-
const data = permissions.map((permission)=>({
|
|
104
|
-
id: permission.id,
|
|
105
|
-
userId: permission.userId,
|
|
106
|
-
branchId: permission.branch?.id || permission.targetId,
|
|
107
|
-
branchName: permission.branch?.name || '',
|
|
108
|
-
branchSlug: permission.branch?.slug || '',
|
|
109
|
-
companyId: permission.branch?.companyId || ''
|
|
110
|
-
}));
|
|
111
|
-
return {
|
|
112
|
-
data
|
|
93
|
+
message: `Processed ${added} additions and ${removed} removals`
|
|
113
94
|
};
|
|
114
95
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
this.userPermissionService = userPermissionService;
|
|
96
|
+
constructor(permissionService){
|
|
97
|
+
_define_property(this, "permissionService", void 0);
|
|
98
|
+
this.permissionService = permissionService;
|
|
119
99
|
}
|
|
120
100
|
};
|
|
121
101
|
_ts_decorate([
|
|
122
102
|
(0, _common.Post)('user-company/assign'),
|
|
103
|
+
(0, _nestjsshared.RequirePermission)(_nestjsshared.USER_PERMISSIONS.UPDATE),
|
|
123
104
|
(0, _swagger.ApiOperation)({
|
|
124
|
-
summary: 'Batch assign/revoke user-company permissions'
|
|
125
|
-
description: `
|
|
126
|
-
Batch operation to assign or revoke user permissions to companies.
|
|
127
|
-
|
|
128
|
-
**Example: Add company permissions**
|
|
129
|
-
\`\`\`json
|
|
130
|
-
{
|
|
131
|
-
"userId": "user-id",
|
|
132
|
-
"items": [
|
|
133
|
-
{ "targetId": "company-id-1", "isAdd": true },
|
|
134
|
-
{ "targetId": "company-id-2", "isAdd": true }
|
|
135
|
-
]
|
|
136
|
-
}
|
|
137
|
-
\`\`\`
|
|
138
|
-
|
|
139
|
-
**Example: Remove company permissions**
|
|
140
|
-
\`\`\`json
|
|
141
|
-
{
|
|
142
|
-
"userId": "user-id",
|
|
143
|
-
"items": [
|
|
144
|
-
{ "targetId": "company-id-1", "isAdd": false },
|
|
145
|
-
{ "targetId": "company-id-2", "isAdd": false }
|
|
146
|
-
]
|
|
147
|
-
}
|
|
148
|
-
\`\`\`
|
|
149
|
-
|
|
150
|
-
**Example: Mix add and remove**
|
|
151
|
-
\`\`\`json
|
|
152
|
-
{
|
|
153
|
-
"userId": "user-id",
|
|
154
|
-
"items": [
|
|
155
|
-
{ "targetId": "company-id-1", "isAdd": true },
|
|
156
|
-
{ "targetId": "company-id-2", "isAdd": false }
|
|
157
|
-
]
|
|
158
|
-
}
|
|
159
|
-
\`\`\`
|
|
160
|
-
`
|
|
105
|
+
summary: 'Batch assign/revoke user-company permissions'
|
|
161
106
|
}),
|
|
162
107
|
(0, _swagger.ApiResponse)({
|
|
163
108
|
status: 200,
|
|
@@ -174,22 +119,28 @@ Batch operation to assign or revoke user permissions to companies.
|
|
|
174
119
|
_ts_metadata("design:returntype", Promise)
|
|
175
120
|
], UserPermissionController.prototype, "assignUserCompany", null);
|
|
176
121
|
_ts_decorate([
|
|
177
|
-
(0, _common.
|
|
122
|
+
(0, _common.Post)('get-user-company'),
|
|
123
|
+
(0, _nestjsshared.RequirePermission)(_nestjsshared.USER_PERMISSIONS.READ),
|
|
178
124
|
(0, _swagger.ApiOperation)({
|
|
179
|
-
summary: 'Get user companies'
|
|
180
|
-
description: 'Returns all companies the user has access to.'
|
|
125
|
+
summary: 'Get user companies'
|
|
181
126
|
}),
|
|
182
|
-
(0, _swagger.
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
127
|
+
(0, _swagger.ApiBody)({
|
|
128
|
+
schema: {
|
|
129
|
+
properties: {
|
|
130
|
+
userId: {
|
|
131
|
+
type: 'string'
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
required: [
|
|
135
|
+
'userId'
|
|
136
|
+
]
|
|
137
|
+
}
|
|
187
138
|
}),
|
|
188
139
|
(0, _swagger.ApiResponse)({
|
|
189
140
|
status: 200,
|
|
190
141
|
type: _userpermissiondto.UserCompaniesResponseDto
|
|
191
142
|
}),
|
|
192
|
-
_ts_param(0, (0, _common.
|
|
143
|
+
_ts_param(0, (0, _common.Body)('userId')),
|
|
193
144
|
_ts_metadata("design:type", Function),
|
|
194
145
|
_ts_metadata("design:paramtypes", [
|
|
195
146
|
String
|
|
@@ -198,44 +149,9 @@ _ts_decorate([
|
|
|
198
149
|
], UserPermissionController.prototype, "getUserCompanies", null);
|
|
199
150
|
_ts_decorate([
|
|
200
151
|
(0, _common.Post)('user-branch/assign'),
|
|
152
|
+
(0, _nestjsshared.RequirePermission)(_nestjsshared.USER_PERMISSIONS.UPDATE),
|
|
201
153
|
(0, _swagger.ApiOperation)({
|
|
202
|
-
summary: 'Batch assign/revoke user-branch permissions'
|
|
203
|
-
description: `
|
|
204
|
-
Batch operation to assign or revoke user permissions to branches.
|
|
205
|
-
|
|
206
|
-
**Example: Add branch permissions**
|
|
207
|
-
\`\`\`json
|
|
208
|
-
{
|
|
209
|
-
"userId": "user-id",
|
|
210
|
-
"items": [
|
|
211
|
-
{ "targetId": "branch-id-1", "isAdd": true },
|
|
212
|
-
{ "targetId": "branch-id-2", "isAdd": true }
|
|
213
|
-
]
|
|
214
|
-
}
|
|
215
|
-
\`\`\`
|
|
216
|
-
|
|
217
|
-
**Example: Remove branch permissions**
|
|
218
|
-
\`\`\`json
|
|
219
|
-
{
|
|
220
|
-
"userId": "user-id",
|
|
221
|
-
"items": [
|
|
222
|
-
{ "targetId": "branch-id-1", "isAdd": false },
|
|
223
|
-
{ "targetId": "branch-id-2", "isAdd": false }
|
|
224
|
-
]
|
|
225
|
-
}
|
|
226
|
-
\`\`\`
|
|
227
|
-
|
|
228
|
-
**Example: Mix add and remove**
|
|
229
|
-
\`\`\`json
|
|
230
|
-
{
|
|
231
|
-
"userId": "user-id",
|
|
232
|
-
"items": [
|
|
233
|
-
{ "targetId": "branch-id-1", "isAdd": true },
|
|
234
|
-
{ "targetId": "branch-id-2", "isAdd": false }
|
|
235
|
-
]
|
|
236
|
-
}
|
|
237
|
-
\`\`\`
|
|
238
|
-
`
|
|
154
|
+
summary: 'Batch assign/revoke user-branch permissions'
|
|
239
155
|
}),
|
|
240
156
|
(0, _swagger.ApiResponse)({
|
|
241
157
|
status: 200,
|
|
@@ -252,24 +168,36 @@ Batch operation to assign or revoke user permissions to branches.
|
|
|
252
168
|
_ts_metadata("design:returntype", Promise)
|
|
253
169
|
], UserPermissionController.prototype, "assignUserBranch", null);
|
|
254
170
|
_ts_decorate([
|
|
255
|
-
(0, _common.
|
|
171
|
+
(0, _common.Post)('get-user-branch'),
|
|
172
|
+
(0, _nestjsshared.RequirePermission)(_nestjsshared.USER_PERMISSIONS.READ),
|
|
256
173
|
(0, _swagger.ApiOperation)({
|
|
257
|
-
summary: 'Get user branches'
|
|
258
|
-
description: 'Returns all branches the user has access to.'
|
|
174
|
+
summary: 'Get user branches'
|
|
259
175
|
}),
|
|
260
|
-
(0, _swagger.
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
176
|
+
(0, _swagger.ApiBody)({
|
|
177
|
+
schema: {
|
|
178
|
+
properties: {
|
|
179
|
+
userId: {
|
|
180
|
+
type: 'string'
|
|
181
|
+
},
|
|
182
|
+
companyId: {
|
|
183
|
+
type: 'string',
|
|
184
|
+
description: 'Optional company ID to filter branches'
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
required: [
|
|
188
|
+
'userId'
|
|
189
|
+
]
|
|
190
|
+
}
|
|
265
191
|
}),
|
|
266
192
|
(0, _swagger.ApiResponse)({
|
|
267
193
|
status: 200,
|
|
268
194
|
type: _userpermissiondto.UserBranchesResponseDto
|
|
269
195
|
}),
|
|
270
|
-
_ts_param(0, (0, _common.
|
|
196
|
+
_ts_param(0, (0, _common.Body)('userId')),
|
|
197
|
+
_ts_param(1, (0, _common.Body)('companyId')),
|
|
271
198
|
_ts_metadata("design:type", Function),
|
|
272
199
|
_ts_metadata("design:paramtypes", [
|
|
200
|
+
String,
|
|
273
201
|
String
|
|
274
202
|
]),
|
|
275
203
|
_ts_metadata("design:returntype", Promise)
|
|
@@ -278,7 +206,7 @@ UserPermissionController = _ts_decorate([
|
|
|
278
206
|
(0, _swagger.ApiTags)('User Permissions'),
|
|
279
207
|
(0, _swagger.ApiBearerAuth)(),
|
|
280
208
|
(0, _common.Controller)('administration/permissions'),
|
|
281
|
-
(0, _common.UseGuards)(
|
|
209
|
+
(0, _common.UseGuards)(_nestjsshared.JwtAuthGuard),
|
|
282
210
|
_ts_param(0, (0, _common.Inject)(_userpermissionservice.UserPermissionService)),
|
|
283
211
|
_ts_metadata("design:type", Function),
|
|
284
212
|
_ts_metadata("design:paramtypes", [
|