@simitgroup/simpleapp-generator 1.1.7 → 1.1.8
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/generate.js +9 -2
- package/dist/generate.js.map +1 -1
- package/package.json +1 -1
- package/src/generate.ts +15 -4
- package/templates/basic/nest/controller.ts.eta +8 -2
- package/templates/basic/nest/test.ts.eta +97 -40
- package/templates/nest/.env._eta +1 -0
- package/templates/nest/src/main.ts.eta +1 -1
- package/templates/nest/src/simpleapp/generate/apischemas/index.ts.eta +3 -3
- package/templates/nest/src/simpleapp/generate/commons/exceptions/SimpleAppExceptionFilter.ts.eta +43 -34
- package/templates/nest/src/simpleapp/generate/commons/interceptors/response.interceptor.ts.eta +110 -59
- package/templates/nest/src/simpleapp/generate/commons/middlewares/tenant.middleware.ts.eta +56 -45
- package/templates/nest/src/simpleapp/generate/commons/user.context.ts.eta +408 -344
- package/templates/nest/src/simpleapp/generate/processors/simpleapp.processor.ts.eta +6 -0
- package/templates/nest/test/app.e2e-spec.ts.eta +20 -14
- package/templates/nest/test/setting.ts.eta +78 -79
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -4,13 +4,19 @@
|
|
|
4
4
|
* last change 2023-10-28
|
|
5
5
|
* Author: Ks Tan
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
Injectable,
|
|
9
|
+
NestMiddleware,
|
|
10
|
+
Logger,
|
|
11
|
+
Scope,
|
|
12
|
+
Inject,
|
|
13
|
+
} from '@nestjs/common';
|
|
8
14
|
import { Request, Response, NextFunction } from 'express';
|
|
9
15
|
import { InjectModel } from '@nestjs/mongoose';
|
|
10
|
-
import { Model,Connection } from 'mongoose';
|
|
16
|
+
import { Model, Connection } from 'mongoose';
|
|
11
17
|
// import * as mongoose from 'mongoose';
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
18
|
+
import { User } from '../../types/user.type';
|
|
19
|
+
import { Permission } from '../../types/perm.type';
|
|
14
20
|
import { InjectConnection } from '@nestjs/mongoose';
|
|
15
21
|
|
|
16
22
|
const Base64URL = require('@darkwolf/base64url');
|
|
@@ -20,75 +26,80 @@ import { UserContext } from '../user.context';
|
|
|
20
26
|
// import {KeycloakConfigService} from "../keycloak/keycloak.service"
|
|
21
27
|
@Injectable()
|
|
22
28
|
export class TenantMiddleware implements NestMiddleware {
|
|
23
|
-
protected defaultxorg=Base64URL.encodeText('0-0-0')
|
|
24
|
-
protected excludeXorgs = ['/profile', '/profile/tenant']
|
|
25
|
-
protected logger = new Logger()
|
|
26
|
-
protected transController
|
|
27
|
-
constructor(
|
|
28
|
-
@InjectModel('User') private readonly usermodel:Model<User>,
|
|
29
|
-
@InjectModel('Permission') private readonly permmodel:Model<Permission>,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
requireXorg(baseurl:string):boolean{
|
|
29
|
+
protected defaultxorg = Base64URL.encodeText('0-0-0');
|
|
30
|
+
protected excludeXorgs = ['/profile', '/profile/tenant'];
|
|
31
|
+
protected logger = new Logger();
|
|
32
|
+
protected transController;
|
|
33
|
+
constructor(
|
|
34
|
+
@InjectModel('User') private readonly usermodel: Model<User>,
|
|
35
|
+
@InjectModel('Permission') private readonly permmodel: Model<Permission>,
|
|
36
|
+
|
|
37
|
+
) {}
|
|
38
|
+
|
|
39
|
+
requireXorg(baseurl: string): boolean {
|
|
34
40
|
// console.log('requireXorg')
|
|
35
|
-
for(let i =0; i < this.excludeXorgs.length;i++){
|
|
36
|
-
if(baseurl.includes(this.excludeXorgs[i])){
|
|
37
|
-
this.logger.verbose(
|
|
38
|
-
return false
|
|
41
|
+
for (let i = 0; i < this.excludeXorgs.length; i++) {
|
|
42
|
+
if (baseurl.includes(this.excludeXorgs[i])) {
|
|
43
|
+
this.logger.verbose('requireXorg = false');
|
|
44
|
+
return false;
|
|
39
45
|
}
|
|
40
46
|
}
|
|
41
47
|
// console.log("Require xorg")
|
|
42
|
-
return true
|
|
48
|
+
return true;
|
|
43
49
|
}
|
|
44
50
|
async use(req: Request, res: Response, next: NextFunction) {
|
|
45
51
|
if (req.baseUrl == '/oauth2-redirect.html') {
|
|
46
52
|
next();
|
|
47
53
|
return;
|
|
48
54
|
}
|
|
49
|
-
this.logger.debug(`running TenantMiddleware for ${req.baseUrl}`)
|
|
55
|
+
this.logger.debug(`running TenantMiddleware for ${req.baseUrl}`);
|
|
50
56
|
if (!req.headers['authorization']) {
|
|
51
|
-
this.logger.log(
|
|
57
|
+
this.logger.log('undefine bearer token');
|
|
52
58
|
return res.status(401).send('Undefine bearer token');
|
|
53
59
|
}
|
|
54
60
|
if (!req.headers['x-org'] && this.requireXorg(req.baseUrl)) {
|
|
55
|
-
this.logger.log(
|
|
61
|
+
this.logger.log(
|
|
62
|
+
'undefine x-org and require that at ' + req.baseUrl,
|
|
63
|
+
'TenantMiddleware',
|
|
64
|
+
);
|
|
56
65
|
return res.status(401).send('undefine header string x-org');
|
|
57
66
|
}
|
|
58
|
-
const session = await this.connection.startSession()
|
|
59
|
-
const u = new UserContext(this.usermodel,this.permmodel
|
|
67
|
+
// const session = await this.connection.startSession();
|
|
68
|
+
const u = new UserContext(this.usermodel, this.permmodel);
|
|
60
69
|
// console.log("line 43")
|
|
61
70
|
try {
|
|
62
71
|
let tokenstr: string = req.headers['authorization'];
|
|
63
72
|
tokenstr = tokenstr.replace('Bearer ', '');
|
|
64
|
-
|
|
65
|
-
const xorg = req.headers['x-org']?? this.defaultxorg
|
|
66
73
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
this.logger.verbose(
|
|
74
|
+
const xorg = req.headers['x-org'] ?? this.defaultxorg;
|
|
75
|
+
|
|
76
|
+
await u.setCurrentUserInfo(tokenstr, xorg);
|
|
77
|
+
if (u.getId() == '' && this.requireXorg(req.baseUrl)) {
|
|
78
|
+
this.logger.log('access deny of no user:', req.baseUrl);
|
|
79
|
+
return res.status(401).send('access deny');
|
|
80
|
+
} else {
|
|
81
|
+
if (u.getId() == '') {
|
|
82
|
+
this.logger.verbose(
|
|
83
|
+
`grant new user (${u.getUid()}) access ${req.baseUrl}`,
|
|
84
|
+
);
|
|
85
|
+
} else {
|
|
86
|
+
this.logger.verbose(
|
|
87
|
+
`grant user (${u.getId()}) access ${req.baseUrl}`,
|
|
88
|
+
);
|
|
76
89
|
}
|
|
77
|
-
req['sessionuser'] = u
|
|
78
|
-
this.logger.verbose(u.getRoles())
|
|
90
|
+
req['sessionuser'] = u;
|
|
91
|
+
this.logger.verbose(u.getRoles());
|
|
79
92
|
|
|
80
93
|
next();
|
|
81
94
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
this.logger.
|
|
85
|
-
|
|
86
|
-
if(err=='invalid x-org'){
|
|
95
|
+
} catch (err) {
|
|
96
|
+
this.logger.warn(err, 'invalid xorg or user info');
|
|
97
|
+
this.logger.error(err);
|
|
98
|
+
if (err == 'invalid x-org') {
|
|
87
99
|
return res.status(403).send(err);
|
|
88
|
-
}else{
|
|
100
|
+
} else {
|
|
89
101
|
return res.status(401).send(err);
|
|
90
102
|
}
|
|
91
|
-
|
|
92
103
|
}
|
|
93
104
|
}
|
|
94
105
|
}
|