@spinajs/rbac-http 1.2.211 → 2.0.12
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/lib/http/src/interfaces.d.ts +461 -0
- package/lib/http/src/interfaces.js +230 -0
- package/lib/http/src/interfaces.js.map +1 -0
- package/lib/http/src/response-methods/forbidden.d.ts +11 -0
- package/lib/http/src/response-methods/forbidden.js +20 -0
- package/lib/http/src/response-methods/forbidden.js.map +1 -0
- package/lib/http/src/responses.d.ts +40 -0
- package/lib/http/src/responses.js +218 -0
- package/lib/http/src/responses.js.map +1 -0
- package/lib/rbac-http/src/augumentation.d.ts +0 -0
- package/lib/rbac-http/src/augumentation.js +9 -0
- package/lib/rbac-http/src/augumentation.js.map +1 -0
- package/lib/{config → rbac-http/src/config}/rbac-http.d.ts +0 -0
- package/lib/{config → rbac-http/src/config}/rbac-http.js +0 -0
- package/lib/rbac-http/src/config/rbac-http.js.map +1 -0
- package/lib/{controllers → rbac-http/src/controllers}/LoginController.d.ts +3 -3
- package/lib/{controllers → rbac-http/src/controllers}/LoginController.js +21 -23
- package/lib/rbac-http/src/controllers/LoginController.js.map +1 -0
- package/lib/rbac-http/src/controllers/UserAdminController.d.ts +0 -0
- package/lib/rbac-http/src/controllers/UserAdminController.js +82 -0
- package/lib/rbac-http/src/controllers/UserAdminController.js.map +1 -0
- package/lib/rbac-http/src/controllers/UserController.d.ts +10 -0
- package/lib/rbac-http/src/controllers/UserController.js +115 -0
- package/lib/rbac-http/src/controllers/UserController.js.map +1 -0
- package/lib/rbac-http/src/controllers/UserMetadata.d.ts +6 -0
- package/lib/rbac-http/src/controllers/UserMetadata.js +67 -0
- package/lib/rbac-http/src/controllers/UserMetadata.js.map +1 -0
- package/lib/{decorators.d.ts → rbac-http/src/decorators.d.ts} +8 -1
- package/lib/{decorators.js → rbac-http/src/decorators.js} +13 -3
- package/lib/rbac-http/src/decorators.js.map +1 -0
- package/lib/{dto → rbac-http/src/dto}/login-dto.d.ts +1 -1
- package/lib/{dto → rbac-http/src/dto}/login-dto.js +2 -2
- package/lib/rbac-http/src/dto/login-dto.js.map +1 -0
- package/lib/{dto → rbac-http/src/dto}/password-dto.d.ts +6 -0
- package/lib/{dto → rbac-http/src/dto}/password-dto.js +2 -1
- package/lib/rbac-http/src/dto/password-dto.js.map +1 -0
- package/lib/rbac-http/src/dto/user-dto.d.ts +21 -0
- package/lib/{dto → rbac-http/src/dto}/user-dto.js +0 -5
- package/lib/rbac-http/src/dto/user-dto.js.map +1 -0
- package/lib/{index.d.ts → rbac-http/src/index.d.ts} +1 -1
- package/lib/{index.js → rbac-http/src/index.js} +1 -1
- package/lib/rbac-http/src/index.js.map +1 -0
- package/lib/{interfaces.d.ts → rbac-http/src/interfaces.d.ts} +7 -0
- package/lib/{interfaces.js → rbac-http/src/interfaces.js} +0 -0
- package/lib/{interfaces.js.map → rbac-http/src/interfaces.js.map} +1 -1
- package/lib/rbac-http/src/middlewares.d.ts +11 -0
- package/lib/{middlewares.js → rbac-http/src/middlewares.js} +10 -14
- package/lib/rbac-http/src/middlewares.js.map +1 -0
- package/lib/{policies.d.ts → rbac-http/src/policies.d.ts} +0 -0
- package/lib/{policies.js → rbac-http/src/policies.js} +1 -1
- package/lib/rbac-http/src/policies.js.map +1 -0
- package/lib/{route-args.d.ts → rbac-http/src/route-args.d.ts} +9 -2
- package/lib/{route-args.js → rbac-http/src/route-args.js} +15 -3
- package/lib/rbac-http/src/route-args.js.map +1 -0
- package/lib/{transformers.d.ts → rbac-http/src/transformers.d.ts} +0 -0
- package/lib/{transformers.js → rbac-http/src/transformers.js} +0 -0
- package/lib/rbac-http/src/transformers.js.map +1 -0
- package/package.json +11 -10
- package/lib/augumentation.d.ts +0 -7
- package/lib/augumentation.js +0 -4
- package/lib/augumentation.js.map +0 -1
- package/lib/config/rbac-http.js.map +0 -1
- package/lib/controllers/LoginController.js.map +0 -1
- package/lib/controllers/UsersController.d.ts +0 -15
- package/lib/controllers/UsersController.js +0 -197
- package/lib/controllers/UsersController.js.map +0 -1
- package/lib/decorators.js.map +0 -1
- package/lib/dto/login-dto.js.map +0 -1
- package/lib/dto/password-dto.js.map +0 -1
- package/lib/dto/user-dto.d.ts +0 -44
- package/lib/dto/user-dto.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/middlewares.d.ts +0 -11
- package/lib/middlewares.js.map +0 -1
- package/lib/policies.js.map +0 -1
- package/lib/route-args.js.map +0 -1
- package/lib/transformers.js.map +0 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Forbidden = void 0;
|
|
4
|
+
const interfaces_1 = require("../interfaces");
|
|
5
|
+
const responses_1 = require("../responses");
|
|
6
|
+
/**
|
|
7
|
+
* Internall response function.
|
|
8
|
+
* Returns HTTP 403 FORBIDDEN ERROR
|
|
9
|
+
* @param err - error to send
|
|
10
|
+
*/
|
|
11
|
+
class Forbidden extends responses_1.Response {
|
|
12
|
+
constructor(data) {
|
|
13
|
+
super(data);
|
|
14
|
+
}
|
|
15
|
+
async execute(_req, _res) {
|
|
16
|
+
return await (0, responses_1.httpResponse)(this.responseData, interfaces_1.HTTP_STATUS_CODE.FORBIDDEN, 'responses/forbidden');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.Forbidden = Forbidden;
|
|
20
|
+
//# sourceMappingURL=forbidden.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"forbidden.js","sourceRoot":"","sources":["../../../../../http/src/response-methods/forbidden.ts"],"names":[],"mappings":";;;AACA,8CAAiD;AACjD,4CAAsD;AAEtD;;;;GAIG;AAEH,MAAa,SAAU,SAAQ,oBAAQ;IACrC,YAAY,IAAS;QACnB,KAAK,CAAC,IAAI,CAAC,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,OAAO,CAAC,IAAqB,EAAE,IAAsB;QAChE,OAAO,MAAM,IAAA,wBAAY,EAAC,IAAI,CAAC,YAAY,EAAE,6BAAgB,CAAC,SAAS,EAAE,qBAAqB,CAAC,CAAC;IAClG,CAAC;CACF;AARD,8BAQC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as express from 'express';
|
|
2
|
+
import { HTTP_STATUS_CODE } from './interfaces';
|
|
3
|
+
export declare type ResponseFunction = (req: express.Request, res: express.Response) => void;
|
|
4
|
+
export declare abstract class Response {
|
|
5
|
+
protected responseData: any;
|
|
6
|
+
constructor(responseData: any);
|
|
7
|
+
abstract execute(req: express.Request, res: express.Response, next?: express.NextFunction): Promise<ResponseFunction | void>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Sends data & sets proper header as json
|
|
11
|
+
*
|
|
12
|
+
* @param model - data to send
|
|
13
|
+
* @param status - status code
|
|
14
|
+
*/
|
|
15
|
+
export declare function jsonResponse(model: any, status?: HTTP_STATUS_CODE): (_req: express.Request, res: express.Response) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Sends data & sets proper header as json
|
|
18
|
+
*
|
|
19
|
+
* @param model - data to send
|
|
20
|
+
* @param status - status code
|
|
21
|
+
*/
|
|
22
|
+
export declare function textResponse(model: any, status?: HTTP_STATUS_CODE): (_req: express.Request, res: express.Response) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Sends html response & sets proper header. If template is not avaible, handles proper error rendering.
|
|
25
|
+
*
|
|
26
|
+
* @param file - template file path
|
|
27
|
+
* @param model - data passed to template
|
|
28
|
+
* @param status - optional status code
|
|
29
|
+
*/
|
|
30
|
+
export declare function pugResponse(file: string, model: any, status?: HTTP_STATUS_CODE): (req: express.Request, res: express.Response) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Default response handling. Checks `Accept` header & matches proper response
|
|
33
|
+
* For now its supports html & json responses
|
|
34
|
+
*
|
|
35
|
+
* @param model - data to send
|
|
36
|
+
* @param code - status code
|
|
37
|
+
* @param template - template to render without extension eg. `views/responses/ok`. It will try to match .pug, .xml or whatever to match response
|
|
38
|
+
* to `Accept` header
|
|
39
|
+
*/
|
|
40
|
+
export declare function httpResponse(model: any, code: HTTP_STATUS_CODE, template: string): (req: express.Request, res: express.Response) => void;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.httpResponse = exports.pugResponse = exports.textResponse = exports.jsonResponse = exports.Response = void 0;
|
|
27
|
+
const interfaces_1 = require("./interfaces");
|
|
28
|
+
const configuration_1 = require("@spinajs/configuration");
|
|
29
|
+
const di_1 = require("@spinajs/di");
|
|
30
|
+
const log_1 = require("@spinajs/log");
|
|
31
|
+
const pugTemplate = __importStar(require("pug"));
|
|
32
|
+
const path_1 = require("path");
|
|
33
|
+
const fs = __importStar(require("fs"));
|
|
34
|
+
const _ = __importStar(require("lodash"));
|
|
35
|
+
const exceptions_1 = require("@spinajs/exceptions");
|
|
36
|
+
const randomstring = __importStar(require("randomstring"));
|
|
37
|
+
const intl_1 = require("@spinajs/intl");
|
|
38
|
+
class Response {
|
|
39
|
+
constructor(responseData) {
|
|
40
|
+
this.responseData = responseData;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.Response = Response;
|
|
44
|
+
/**
|
|
45
|
+
* Sends data & sets proper header as json
|
|
46
|
+
*
|
|
47
|
+
* @param model - data to send
|
|
48
|
+
* @param status - status code
|
|
49
|
+
*/
|
|
50
|
+
function jsonResponse(model, status) {
|
|
51
|
+
return (_req, res) => {
|
|
52
|
+
res.status(status ? status : interfaces_1.HTTP_STATUS_CODE.OK);
|
|
53
|
+
if (model) {
|
|
54
|
+
res.json(model);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
res.json();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
exports.jsonResponse = jsonResponse;
|
|
62
|
+
/**
|
|
63
|
+
* Sends data & sets proper header as json
|
|
64
|
+
*
|
|
65
|
+
* @param model - data to send
|
|
66
|
+
* @param status - status code
|
|
67
|
+
*/
|
|
68
|
+
function textResponse(model, status) {
|
|
69
|
+
return (_req, res) => {
|
|
70
|
+
res.status(status ? status : interfaces_1.HTTP_STATUS_CODE.OK);
|
|
71
|
+
if (model) {
|
|
72
|
+
res.set('Content-Type', 'text/plain');
|
|
73
|
+
res.send(JSON.stringify(model));
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
exports.textResponse = textResponse;
|
|
78
|
+
const __translate = (lang) => {
|
|
79
|
+
return (text, ...args) => {
|
|
80
|
+
const intl = di_1.DI.get(intl_1.Intl);
|
|
81
|
+
if (typeof text === 'string') {
|
|
82
|
+
return intl.__({
|
|
83
|
+
phrase: text,
|
|
84
|
+
locale: lang,
|
|
85
|
+
}, ...args);
|
|
86
|
+
}
|
|
87
|
+
return intl.__(text, ...args);
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
const __translateNumber = (lang) => {
|
|
91
|
+
return (text, count) => {
|
|
92
|
+
const intl = di_1.DI.get(intl_1.Intl);
|
|
93
|
+
if (typeof text === 'string') {
|
|
94
|
+
return intl.__n({
|
|
95
|
+
phrase: text,
|
|
96
|
+
locale: lang,
|
|
97
|
+
}, count);
|
|
98
|
+
}
|
|
99
|
+
return intl.__n(text, count);
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
const __translateL = (text) => {
|
|
103
|
+
const intl = di_1.DI.get(intl_1.Intl);
|
|
104
|
+
return intl.__l(text);
|
|
105
|
+
};
|
|
106
|
+
const __translateH = (text) => {
|
|
107
|
+
const intl = di_1.DI.get(intl_1.Intl);
|
|
108
|
+
return intl.__h(text);
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Sends html response & sets proper header. If template is not avaible, handles proper error rendering.
|
|
112
|
+
*
|
|
113
|
+
* @param file - template file path
|
|
114
|
+
* @param model - data passed to template
|
|
115
|
+
* @param status - optional status code
|
|
116
|
+
*/
|
|
117
|
+
function pugResponse(file, model, status) {
|
|
118
|
+
const cfg = di_1.DI.get(configuration_1.Configuration);
|
|
119
|
+
return (req, res) => {
|
|
120
|
+
if (!req.accepts('html')) {
|
|
121
|
+
httpResponse({
|
|
122
|
+
error: {
|
|
123
|
+
message: 'invalid request content type',
|
|
124
|
+
code: 400,
|
|
125
|
+
},
|
|
126
|
+
}, interfaces_1.HTTP_STATUS_CODE.BAD_REQUEST, 'responses/serverError.pug')(req, res);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
res.set('Content-Type', 'text/html');
|
|
130
|
+
try {
|
|
131
|
+
try {
|
|
132
|
+
_render(file, model, status);
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
const log = di_1.DI.resolve(log_1.Log, ['http']);
|
|
136
|
+
log.warn(`Cannot render pug file ${file}, error: ${err.message}:${err.stack}`, err);
|
|
137
|
+
// try to render server error response
|
|
138
|
+
_render('responses/serverError.pug', { error: err }, interfaces_1.HTTP_STATUS_CODE.INTERNAL_ERROR);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
const log = di_1.DI.resolve(log_1.Log, ['http']);
|
|
143
|
+
// final fallback rendering error fails, we render embedded html error page
|
|
144
|
+
const ticketNo = randomstring.generate(7);
|
|
145
|
+
log.warn(`Cannot render pug file error: ${err.message}, ticket: ${ticketNo}`, err);
|
|
146
|
+
res.status(interfaces_1.HTTP_STATUS_CODE.INTERNAL_ERROR);
|
|
147
|
+
res.send(cfg.get('http.FatalTemplate').replace('{ticket}', ticketNo));
|
|
148
|
+
}
|
|
149
|
+
function _render(f, m, c) {
|
|
150
|
+
const view = getView(f);
|
|
151
|
+
const language = req.query[cfg.get('intl.queryParameter')];
|
|
152
|
+
const content = pugTemplate.renderFile(view, _.merge(m, {
|
|
153
|
+
// add i18n functions as globals
|
|
154
|
+
__: __translate(language),
|
|
155
|
+
__n: __translateNumber(language),
|
|
156
|
+
__l: __translateL,
|
|
157
|
+
__h: __translateH,
|
|
158
|
+
}));
|
|
159
|
+
res.status(c ? c : interfaces_1.HTTP_STATUS_CODE.OK);
|
|
160
|
+
res.send(content);
|
|
161
|
+
}
|
|
162
|
+
function getView(viewFile) {
|
|
163
|
+
const views = cfg
|
|
164
|
+
.get('system.dirs.views')
|
|
165
|
+
.map((p) => (0, path_1.normalize)((0, path_1.join)(p, viewFile)))
|
|
166
|
+
.filter((f) => fs.existsSync(f));
|
|
167
|
+
if (_.isEmpty(views)) {
|
|
168
|
+
throw new exceptions_1.IOFail(`View file ${viewFile} not exists.`);
|
|
169
|
+
}
|
|
170
|
+
// return last merged path, eg. if application have own view files (override standard views)
|
|
171
|
+
return views[views.length - 1];
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
exports.pugResponse = pugResponse;
|
|
176
|
+
/**
|
|
177
|
+
* Default response handling. Checks `Accept` header & matches proper response
|
|
178
|
+
* For now its supports html & json responses
|
|
179
|
+
*
|
|
180
|
+
* @param model - data to send
|
|
181
|
+
* @param code - status code
|
|
182
|
+
* @param template - template to render without extension eg. `views/responses/ok`. It will try to match .pug, .xml or whatever to match response
|
|
183
|
+
* to `Accept` header
|
|
184
|
+
*/
|
|
185
|
+
function httpResponse(model, code, template) {
|
|
186
|
+
const cfg = di_1.DI.get(configuration_1.Configuration);
|
|
187
|
+
const acceptedHeaders = cfg.get('http.AcceptHeaders');
|
|
188
|
+
const transformers = di_1.DI.resolve(Array.ofType(interfaces_1.DataTransformer));
|
|
189
|
+
return (req, res) => {
|
|
190
|
+
if (req.accepts('html') && (acceptedHeaders & interfaces_1.HttpAcceptHeaders.HTML) === interfaces_1.HttpAcceptHeaders.HTML) {
|
|
191
|
+
pugResponse(`${template}.pug`, model, code)(req, res);
|
|
192
|
+
}
|
|
193
|
+
else if (req.accepts('json') && (acceptedHeaders & interfaces_1.HttpAcceptHeaders.JSON) === interfaces_1.HttpAcceptHeaders.JSON) {
|
|
194
|
+
if (req.headers['x-data-transform']) {
|
|
195
|
+
const transformer = transformers.find((t) => t.Type === req.headers['x-data-transform']);
|
|
196
|
+
if (transformer) {
|
|
197
|
+
jsonResponse(transformer.transform(model, req), code)(req, res);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
jsonResponse({
|
|
201
|
+
error: {
|
|
202
|
+
message: "invalid data transformer, remove header 'x-data-transform' to return raw data or set proper data transformer",
|
|
203
|
+
code: 400,
|
|
204
|
+
},
|
|
205
|
+
}, interfaces_1.HTTP_STATUS_CODE.BAD_REQUEST)(req, res);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
jsonResponse(model, code)(req, res);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
textResponse(model, code)(req, res);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
exports.httpResponse = httpResponse;
|
|
218
|
+
//# sourceMappingURL=responses.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"responses.js","sourceRoot":"","sources":["../../../../http/src/responses.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,6CAAoF;AACpF,0DAAuD;AACvD,oCAAiC;AACjC,sCAAyC;AACzC,iDAAmC;AACnC,+BAAuC;AACvC,uCAAyB;AACzB,0CAA4B;AAC5B,oDAA6C;AAC7C,2DAA6C;AAC7C,wCAAyD;AAIzD,MAAsB,QAAQ;IAG5B,YAAY,YAAiB;QAC3B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;CAGF;AARD,4BAQC;AAED;;;;;GAKG;AACH,SAAgB,YAAY,CAAC,KAAU,EAAE,MAAyB;IAChE,OAAO,CAAC,IAAqB,EAAE,GAAqB,EAAE,EAAE;QACtD,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,6BAAgB,CAAC,EAAE,CAAC,CAAC;QAElD,IAAI,KAAK,EAAE;YACT,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;SACjB;aAAM;YACL,GAAG,CAAC,IAAI,EAAE,CAAC;SACZ;IACH,CAAC,CAAC;AACJ,CAAC;AAVD,oCAUC;AAED;;;;;GAKG;AACH,SAAgB,YAAY,CAAC,KAAU,EAAE,MAAyB;IAChE,OAAO,CAAC,IAAqB,EAAE,GAAqB,EAAE,EAAE;QACtD,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,6BAAgB,CAAC,EAAE,CAAC,CAAC;QAElD,IAAI,KAAK,EAAE;YACT,GAAG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WAAW,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,GAAG,CAAC,OAAO,CAAC,kBAAkB,CAAC,CAAC,CAAC;gBACzF,IAAI,WAAW,EAAE;oBACf,YAAY,CAAC,WAAW,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;iBACjE;qBAAM;oBACL,YAAY,CACV;wBACE,KAAK,EAAE;4BACL,OAAO,EAAE,8GAA8G;4BACvH,IAAI,EAAE,GAAG;yBACV;qBACF,EACD,6BAAgB,CAAC,WAAW,CAC7B,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;iBACb;aACF;iBAAM;gBACL,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;aACrC;SACF;aAAM;YACL,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;SACrC;IACH,CAAC,CAAC;AACJ,CAAC;AA9BD,oCA8BC"}
|
|
File without changes
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// import '@spinajs/http';
|
|
2
|
+
// import { ISession, User } from '@spinajs/rbac';
|
|
3
|
+
// declare module '@spinajs/http' {
|
|
4
|
+
// interface IActionLocalStoregeContext {
|
|
5
|
+
// user: User | null;
|
|
6
|
+
// session: ISession | null;
|
|
7
|
+
// }
|
|
8
|
+
// }
|
|
9
|
+
//# sourceMappingURL=augumentation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"augumentation.js","sourceRoot":"","sources":["../../../src/augumentation.ts"],"names":[],"mappings":"AAAA,0BAA0B;AAC1B,kDAAkD;AAElD,mCAAmC;AACnC,2CAA2C;AAC3C,yBAAyB;AACzB,gCAAgC;AAChC,MAAM;AACN,IAAI"}
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rbac-http.js","sourceRoot":"","sources":["../../../../src/config/rbac-http.ts"],"names":[],"mappings":";;AAAA,+BAAgD;AAEhD,SAAS,GAAG,CAAC,IAAY;IACvB,OAAO,IAAA,cAAO,EAAC,IAAA,gBAAS,EAAC,IAAA,WAAI,EAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;AACnD,CAAC;AACD,MAAM,CAAC,OAAO,GAAG;IACf,MAAM,EAAE;QACN,IAAI,EAAE;YACJ,WAAW,EAAE,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;YACtC,OAAO,EAAE,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;YAC9B,KAAK,EAAE,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;SAC3B;KACF;IACD,IAAI,EAAE;QACJ,WAAW,EAAE;QACX,0CAA0C;SAC3C;KACF;CACF,CAAC"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { LoginDto } from './../dto/login-dto';
|
|
2
|
-
import { BaseController, Ok, CookieResponse, Unauthorized } from '@spinajs/http';
|
|
3
|
-
import { AuthProvider, SessionProvider } from '@spinajs/rbac';
|
|
2
|
+
import { BaseController, Ok, CookieResponse, Unauthorized, NotAllowed } from '@spinajs/http';
|
|
3
|
+
import { AuthProvider, SessionProvider, User as UserModel } from '@spinajs/rbac';
|
|
4
4
|
import { Configuration } from '@spinajs/configuration';
|
|
5
5
|
export declare class LoginController extends BaseController {
|
|
6
6
|
protected Configuration: Configuration;
|
|
7
7
|
protected AuthProvider: AuthProvider;
|
|
8
8
|
protected SessionProvider: SessionProvider;
|
|
9
9
|
protected SessionExpirationTime: number;
|
|
10
|
-
login(credentials: LoginDto): Promise<Unauthorized | CookieResponse>;
|
|
10
|
+
login(credentials: LoginDto, logged: UserModel): Promise<Unauthorized | CookieResponse | NotAllowed>;
|
|
11
11
|
logout(ssid: string): Promise<Ok | CookieResponse>;
|
|
12
12
|
}
|
|
@@ -11,6 +11,9 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
|
+
};
|
|
14
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
18
|
exports.LoginController = void 0;
|
|
16
19
|
const login_dto_1 = require("./../dto/login-dto");
|
|
@@ -18,9 +21,13 @@ const http_1 = require("@spinajs/http");
|
|
|
18
21
|
const rbac_1 = require("@spinajs/rbac");
|
|
19
22
|
const di_1 = require("@spinajs/di");
|
|
20
23
|
const configuration_1 = require("@spinajs/configuration");
|
|
21
|
-
const
|
|
24
|
+
const decorators_1 = require("./../decorators");
|
|
25
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
22
26
|
let LoginController = class LoginController extends http_1.BaseController {
|
|
23
|
-
async login(credentials) {
|
|
27
|
+
async login(credentials, logged) {
|
|
28
|
+
if (logged) {
|
|
29
|
+
return new http_1.NotAllowed('User already logged in. Please logout before trying to authorize.');
|
|
30
|
+
}
|
|
24
31
|
const user = await this.AuthProvider.authenticate(credentials.Login, credentials.Password);
|
|
25
32
|
if (!user) {
|
|
26
33
|
return new http_1.Unauthorized({
|
|
@@ -29,29 +36,19 @@ let LoginController = class LoginController extends http_1.BaseController {
|
|
|
29
36
|
},
|
|
30
37
|
});
|
|
31
38
|
}
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
Id: user.Id,
|
|
40
|
-
};
|
|
41
|
-
const session = new rbac_1.Session({
|
|
42
|
-
Data: uObject,
|
|
43
|
-
Expiration: lifetime,
|
|
44
|
-
});
|
|
45
|
-
await this.SessionProvider.updateSession(session);
|
|
46
|
-
// sessionExpiration time is in minutes
|
|
47
|
-
// coockie maxAge is in seconds
|
|
48
|
-
return new http_1.CookieResponse('ssid', session.SessionId, this.SessionExpirationTime * 60, uObject, { httpOnly: true });
|
|
39
|
+
await user.Metadata.populate();
|
|
40
|
+
const session = new rbac_1.Session();
|
|
41
|
+
const sData = user.dehydrate();
|
|
42
|
+
session.Data.set('User', sData);
|
|
43
|
+
await this.SessionProvider.save(session);
|
|
44
|
+
// BEWARE: httpOnly coockie, only accesible via http method in browser
|
|
45
|
+
return new http_1.CookieResponse('ssid', session.SessionId, this.SessionExpirationTime, true, lodash_1.default.omit(sData, ['Id']), { httpOnly: true });
|
|
49
46
|
}
|
|
50
47
|
async logout(ssid) {
|
|
51
48
|
if (!ssid) {
|
|
52
49
|
return new http_1.Ok();
|
|
53
50
|
}
|
|
54
|
-
await this.SessionProvider.
|
|
51
|
+
await this.SessionProvider.delete(ssid);
|
|
55
52
|
// send empty cookie to confirm session deletion
|
|
56
53
|
return new http_1.CookieResponse('ssid', null, this.SessionExpirationTime);
|
|
57
54
|
}
|
|
@@ -69,14 +66,15 @@ __decorate([
|
|
|
69
66
|
__metadata("design:type", rbac_1.SessionProvider)
|
|
70
67
|
], LoginController.prototype, "SessionProvider", void 0);
|
|
71
68
|
__decorate([
|
|
72
|
-
(0, configuration_1.Config)('rbac.session.expiration',
|
|
69
|
+
(0, configuration_1.Config)('rbac.session.expiration', 120),
|
|
73
70
|
__metadata("design:type", Number)
|
|
74
71
|
], LoginController.prototype, "SessionExpirationTime", void 0);
|
|
75
72
|
__decorate([
|
|
76
73
|
(0, http_1.Post)(),
|
|
77
74
|
__param(0, (0, http_1.Body)()),
|
|
75
|
+
__param(1, (0, decorators_1.User)()),
|
|
78
76
|
__metadata("design:type", Function),
|
|
79
|
-
__metadata("design:paramtypes", [login_dto_1.LoginDto]),
|
|
77
|
+
__metadata("design:paramtypes", [login_dto_1.LoginDto, rbac_1.User]),
|
|
80
78
|
__metadata("design:returntype", Promise)
|
|
81
79
|
], LoginController.prototype, "login", null);
|
|
82
80
|
__decorate([
|
|
@@ -87,7 +85,7 @@ __decorate([
|
|
|
87
85
|
__metadata("design:returntype", Promise)
|
|
88
86
|
], LoginController.prototype, "logout", null);
|
|
89
87
|
LoginController = __decorate([
|
|
90
|
-
(0, http_1.BasePath)('auth')
|
|
88
|
+
(0, http_1.BasePath)('user/auth')
|
|
91
89
|
], LoginController);
|
|
92
90
|
exports.LoginController = LoginController;
|
|
93
91
|
//# sourceMappingURL=LoginController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LoginController.js","sourceRoot":"","sources":["../../../../src/controllers/LoginController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,kDAA8C;AAC9C,wCAAgI;AAChI,wCAA0F;AAC1F,oCAAyC;AACzC,0DAA+D;AAC/D,gDAAuC;AACvC,oDAAuB;AAGvB,IAAa,eAAe,GAA5B,MAAa,eAAgB,SAAQ,qBAAc;IAc1C,KAAK,CAAC,KAAK,CAAS,WAAqB,EAAU,MAAiB;QACzE,IAAI,MAAM,EAAE;YACV,OAAO,IAAI,iBAAU,CAAC,mEAAmE,CAAC,CAAC;SAC5F;QAED,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,WAAW,CAAC,KAAK,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC;QAE3F,IAAI,CAAC,IAAI,EAAE;YACT,OAAO,IAAI,mBAAY,CAAC;gBACtB,KAAK,EAAE;oBACL,OAAO,EAAE,6BAA6B;iBACvC;aACF,CAAC,CAAC;SACJ;QAED,MAAM,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC;QAE/B,MAAM,OAAO,GAAG,IAAI,cAAO,EAAE,CAAC;QAC9B,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;QAE/B,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CA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
// import { BaseController, Body, Get, NotFound, Ok, Patch, Post, Query, Req, Param } from '@spinajs/http';
|
|
3
|
+
// import { DateTime, RawQuery } from '@spinajs/orm';
|
|
4
|
+
// import { PasswordProvider, User } from '@spinajs/rbac';
|
|
5
|
+
// import { Permission, Resource } from './../decorators';
|
|
6
|
+
// import { UserDto } from '../dto/user-dto';
|
|
7
|
+
// import { BasePath } from 'http/lib';
|
|
8
|
+
// @Resource('user')
|
|
9
|
+
// @BasePath('user')
|
|
10
|
+
// export class UsersController extends BaseController {
|
|
11
|
+
// @Get('/')
|
|
12
|
+
// @Permission('readAny')
|
|
13
|
+
// public async listUsers(@Query() search: string, @Query({ type: 'number', minimum: 1 }) page: number, @Query({ type: 'number', minimum: 1 }) perPage: number, @Query() order: string, @Query(OrderSchema) orderDirection: SORT_ORDER, @Req() request: express.Request) {
|
|
14
|
+
// /**
|
|
15
|
+
// * implement include query param
|
|
16
|
+
// * do not return internal id
|
|
17
|
+
// *
|
|
18
|
+
// */
|
|
19
|
+
// const query = User.all()
|
|
20
|
+
// .whereNull('DeletedAt')
|
|
21
|
+
// .skip((page - 1) * perPage)
|
|
22
|
+
// .take(perPage)
|
|
23
|
+
// .order(order, orderDirection)
|
|
24
|
+
// .populate('Metadata');
|
|
25
|
+
// const countQuery = User.query().select(new RawQuery('count(*) as count')).whereNull('DeletedAt');
|
|
26
|
+
// if (search) {
|
|
27
|
+
// const searchFunc = function () {
|
|
28
|
+
// this.where('Email', 'like', `%${search}%`);
|
|
29
|
+
// this.orWhere('NiceName', 'like', `%${search}%`);
|
|
30
|
+
// };
|
|
31
|
+
// query.where(searchFunc);
|
|
32
|
+
// countQuery.where(searchFunc);
|
|
33
|
+
// }
|
|
34
|
+
// const r = await query;
|
|
35
|
+
// const c = await countQuery.asRaw<Array<{ count: number }>>();
|
|
36
|
+
// if (r.length === 0) {
|
|
37
|
+
// return new NotFound('no users met search criteria');
|
|
38
|
+
// }
|
|
39
|
+
// return new Ok(
|
|
40
|
+
// this.DataTransformer.transform(
|
|
41
|
+
// {
|
|
42
|
+
// Data: r.map((u) => u.dehydrate()),
|
|
43
|
+
// Total: c[0].count,
|
|
44
|
+
// },
|
|
45
|
+
// request,
|
|
46
|
+
// ),
|
|
47
|
+
// );
|
|
48
|
+
// }
|
|
49
|
+
// // @Post('/')
|
|
50
|
+
// // public async addUser(@Body() user: UserDto) {
|
|
51
|
+
// // const password = this._container.resolve<PasswordProvider>(PasswordProvider);
|
|
52
|
+
// // if (user.Password !== user.ConfirmPassword) {
|
|
53
|
+
// // throw new InvalidArgument('password does not match');
|
|
54
|
+
// // }
|
|
55
|
+
// // let hashedPassword = '';
|
|
56
|
+
// // let userPassword = user.Password;
|
|
57
|
+
// // if (!userPassword) {
|
|
58
|
+
// // userPassword = password.generate();
|
|
59
|
+
// // }
|
|
60
|
+
// // hashedPassword = await password.hash(userPassword);
|
|
61
|
+
// // const entity = new User({
|
|
62
|
+
// // Email: user.Email,
|
|
63
|
+
// // Login: user.Login,
|
|
64
|
+
// // NiceName: user.NiceName,
|
|
65
|
+
// // Password: hashedPassword,
|
|
66
|
+
// // CreatedAt: DateTime.now(),
|
|
67
|
+
// // Role: user.Role,
|
|
68
|
+
// // });
|
|
69
|
+
// // await entity.insert();
|
|
70
|
+
// // return new Ok({ Id: entity.Id });
|
|
71
|
+
// // }
|
|
72
|
+
// // @Patch('role/add/:login/:role')
|
|
73
|
+
// // @Permission('updateAny')
|
|
74
|
+
// // public async addRole(@Param() login: string, @Param() role: string) {}
|
|
75
|
+
// // @Patch('role/revoke/:login/:role')
|
|
76
|
+
// // @Permission('updateAny')
|
|
77
|
+
// // public async revokeRole(@Param() login: string, @Param() role: string) {}
|
|
78
|
+
// // @Patch('update/:login')
|
|
79
|
+
// // @Permission('updateAny')
|
|
80
|
+
// // public async update(@Param() login: string, @Body() data: any) {}
|
|
81
|
+
// }
|
|
82
|
+
//# sourceMappingURL=UserAdminController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UserAdminController.js","sourceRoot":"","sources":["../../../../src/controllers/UserAdminController.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,2GAA2G;AAC3G,qDAAqD;AACrD,0DAA0D;AAC1D,0DAA0D;AAC1D,6CAA6C;AAC7C,uCAAuC;AAEvC,oBAAoB;AACpB,oBAAoB;AACpB,wDAAwD;AACxD,cAAc;AACd,2BAA2B;AAC3B,4QAA4Q;AAC5Q,UAAU;AACV,uCAAuC;AACvC,mCAAmC;AACnC,SAAS;AACT,UAAU;AAEV,+BAA+B;AAC/B,gCAAgC;AAChC,oCAAoC;AACpC,uBAAuB;AACvB,sCAAsC;AACtC,+BAA+B;AAC/B,wGAAwG;AAExG,oBAAoB;AACpB,yCAAyC;AACzC,sDAAsD;AACtD,2DAA2D;AAC3D,WAAW;AAEX,iCAAiC;AACjC,sCAAsC;AACtC,QAAQ;AAER,6BAA6B;AAC7B,oEAAoE;AAEpE,4BAA4B;AAC5B,6DAA6D;AAC7D,QAAQ;AAER,qBAAqB;AACrB,wCAAwC;AACxC,YAAY;AACZ,+CAA+C;AAC/C,+BAA+B;AAC/B,aAAa;AACb,mBAAmB;AACnB,WAAW;AACX,SAAS;AACT,MAAM;AAEN,kBAAkB;AAClB,qDAAqD;AACrD,uFAAuF;AACvF,uDAAuD;AACvD,iEAAiE;AACjE,WAAW;AAEX,kCAAkC;AAClC,2CAA2C;AAE3C,8BAA8B;AAC9B,+CAA+C;AAC/C,WAAW;AAEX,6DAA6D;AAC7D,mCAAmC;AACnC,8BAA8B;AAC9B,8BAA8B;AAC9B,oCAAoC;AACpC,qCAAqC;AACrC,sCAAsC;AACtC,4BAA4B;AAC5B,aAAa;AAEb,gCAAgC;AAEhC,2CAA2C;AAC3C,SAAS;AAET,uCAAuC;AACvC,gCAAgC;AAChC,8EAA8E;AAE9E,0CAA0C;AAC1C,gCAAgC;AAChC,iFAAiF;AAEjF,+BAA+B;AAC/B,gCAAgC;AAChC,yEAAyE;AACzE,IAAI"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PasswordDto } from '../dto/password-dto';
|
|
2
|
+
import { User as UserModel, PasswordProvider, SessionProvider } from '@spinajs/rbac';
|
|
3
|
+
import { BaseController, Ok } from '@spinajs/http';
|
|
4
|
+
export declare class UserController extends BaseController {
|
|
5
|
+
protected PasswordProvider: PasswordProvider;
|
|
6
|
+
protected CoockieSecret: string;
|
|
7
|
+
protected SessionProvider: SessionProvider;
|
|
8
|
+
refresh(user: UserModel, ssid: string): Promise<Ok>;
|
|
9
|
+
newPassword(login: string, pwd: PasswordDto): Promise<Ok>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
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;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
25
|
+
if (mod && mod.__esModule) return mod;
|
|
26
|
+
var result = {};
|
|
27
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
28
|
+
__setModuleDefault(result, mod);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
32
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
33
|
+
};
|
|
34
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
35
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
36
|
+
};
|
|
37
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
38
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.UserController = void 0;
|
|
42
|
+
const password_dto_1 = require("../dto/password-dto");
|
|
43
|
+
const rbac_1 = require("@spinajs/rbac");
|
|
44
|
+
const http_1 = require("@spinajs/http");
|
|
45
|
+
const exceptions_1 = require("@spinajs/exceptions");
|
|
46
|
+
const di_1 = require("@spinajs/di");
|
|
47
|
+
const decorators_1 = require("../decorators");
|
|
48
|
+
const http_2 = require("@spinajs/http");
|
|
49
|
+
const configuration_1 = require("@spinajs/configuration");
|
|
50
|
+
const cs = __importStar(require("cookie-signature"));
|
|
51
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
52
|
+
let UserController = class UserController extends http_1.BaseController {
|
|
53
|
+
async refresh(user, ssid) {
|
|
54
|
+
// get user data from db
|
|
55
|
+
await user.refresh();
|
|
56
|
+
// refresh session data from DB
|
|
57
|
+
const sId = cs.unsign(ssid, this.CoockieSecret);
|
|
58
|
+
if (sId) {
|
|
59
|
+
const session = await this.SessionProvider.restore(sId);
|
|
60
|
+
if (session) {
|
|
61
|
+
session.Data.set('User', user.dehydrate());
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return new http_1.Ok(lodash_1.default.omit(user.dehydrate(), ['Id']));
|
|
65
|
+
}
|
|
66
|
+
async newPassword(login, pwd) {
|
|
67
|
+
if (pwd.Password !== pwd.ConfirmPassword) {
|
|
68
|
+
throw new exceptions_1.InvalidArgument('password does not match');
|
|
69
|
+
}
|
|
70
|
+
const user = await rbac_1.User.where({ Login: login }).firstOrFail();
|
|
71
|
+
const isValid = await this.PasswordProvider.verify(user.Password, pwd.OldPassword);
|
|
72
|
+
if (!isValid) {
|
|
73
|
+
throw new exceptions_1.Forbidden('Invalid login or password');
|
|
74
|
+
}
|
|
75
|
+
const hashedPassword = await this.PasswordProvider.hash(pwd.Password);
|
|
76
|
+
user.Password = hashedPassword;
|
|
77
|
+
await user.update();
|
|
78
|
+
return new http_1.Ok();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
__decorate([
|
|
82
|
+
(0, di_1.Autoinject)(),
|
|
83
|
+
__metadata("design:type", rbac_1.PasswordProvider)
|
|
84
|
+
], UserController.prototype, "PasswordProvider", void 0);
|
|
85
|
+
__decorate([
|
|
86
|
+
(0, configuration_1.Config)('http.cookie.secret'),
|
|
87
|
+
__metadata("design:type", String)
|
|
88
|
+
], UserController.prototype, "CoockieSecret", void 0);
|
|
89
|
+
__decorate([
|
|
90
|
+
(0, di_1.Autoinject)(),
|
|
91
|
+
__metadata("design:type", rbac_1.SessionProvider)
|
|
92
|
+
], UserController.prototype, "SessionProvider", void 0);
|
|
93
|
+
__decorate([
|
|
94
|
+
(0, http_1.Get)(),
|
|
95
|
+
(0, decorators_1.Permission)('readOwn'),
|
|
96
|
+
__param(0, (0, decorators_1.User)()),
|
|
97
|
+
__param(1, (0, http_2.Cookie)()),
|
|
98
|
+
__metadata("design:type", Function),
|
|
99
|
+
__metadata("design:paramtypes", [rbac_1.User, String]),
|
|
100
|
+
__metadata("design:returntype", Promise)
|
|
101
|
+
], UserController.prototype, "refresh", null);
|
|
102
|
+
__decorate([
|
|
103
|
+
(0, http_1.Patch)('/password/:login'),
|
|
104
|
+
__param(0, (0, http_1.Param)()),
|
|
105
|
+
__param(1, (0, http_1.Body)()),
|
|
106
|
+
__metadata("design:type", Function),
|
|
107
|
+
__metadata("design:paramtypes", [String, password_dto_1.PasswordDto]),
|
|
108
|
+
__metadata("design:returntype", Promise)
|
|
109
|
+
], UserController.prototype, "newPassword", null);
|
|
110
|
+
UserController = __decorate([
|
|
111
|
+
(0, http_1.BasePath)('user'),
|
|
112
|
+
(0, decorators_1.Resource)('user')
|
|
113
|
+
], UserController);
|
|
114
|
+
exports.UserController = UserController;
|
|
115
|
+
//# sourceMappingURL=UserController.js.map
|