@nocobase/plugin-users 0.7.0-alpha.7 → 0.7.0-alpha.72
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/actions/users.js +279 -163
- package/lib/collections/roles-users.js +12 -5
- package/lib/collections/users.js +87 -86
- package/lib/index.d.ts +1 -0
- package/lib/index.js +19 -8
- package/lib/jwt-service.js +50 -29
- package/lib/locale/en-US.d.ts +5 -0
- package/lib/locale/en-US.js +11 -0
- package/lib/locale/index.d.ts +2 -0
- package/lib/locale/index.js +23 -0
- package/lib/locale/zh-CN.d.ts +6 -0
- package/lib/locale/zh-CN.js +12 -0
- package/lib/middlewares/check.js +28 -23
- package/lib/middlewares/index.js +20 -7
- package/lib/middlewares/parseToken.js +86 -62
- package/lib/models/UserModel.d.ts +2 -2
- package/lib/models/UserModel.js +68 -53
- package/lib/server.d.ts +5 -10
- package/lib/server.js +261 -149
- package/package.json +3 -8
- package/esm/actions/users.d.ts +0 -11
- package/esm/actions/users.js +0 -165
- package/esm/actions/users.js.map +0 -1
- package/esm/collections/roles-users.d.ts +0 -3
- package/esm/collections/roles-users.js +0 -5
- package/esm/collections/roles-users.js.map +0 -1
- package/esm/collections/users.d.ts +0 -3
- package/esm/collections/users.js +0 -86
- package/esm/collections/users.js.map +0 -1
- package/esm/index.d.ts +0 -1
- package/esm/index.js +0 -2
- package/esm/index.js.map +0 -1
- package/esm/jwt-service.d.ts +0 -12
- package/esm/jwt-service.js +0 -26
- package/esm/jwt-service.js.map +0 -1
- package/esm/middlewares/check.d.ts +0 -1
- package/esm/middlewares/check.js +0 -22
- package/esm/middlewares/check.js.map +0 -1
- package/esm/middlewares/index.d.ts +0 -2
- package/esm/middlewares/index.js +0 -3
- package/esm/middlewares/index.js.map +0 -1
- package/esm/middlewares/parseToken.d.ts +0 -6
- package/esm/middlewares/parseToken.js +0 -64
- package/esm/middlewares/parseToken.js.map +0 -1
- package/esm/models/UserModel.d.ts +0 -4
- package/esm/models/UserModel.js +0 -53
- package/esm/models/UserModel.js.map +0 -1
- package/esm/server.d.ts +0 -23
- package/esm/server.js +0 -133
- package/esm/server.js.map +0 -1
- package/lib/actions/users.js.map +0 -1
- package/lib/collections/roles-users.js.map +0 -1
- package/lib/collections/users.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/jwt-service.js.map +0 -1
- package/lib/middlewares/check.js.map +0 -1
- package/lib/middlewares/index.js.map +0 -1
- package/lib/middlewares/parseToken.js.map +0 -1
- package/lib/models/UserModel.js.map +0 -1
- package/lib/server.js.map +0 -1
- package/tsconfig.build.json +0 -9
package/lib/server.js
CHANGED
|
@@ -1,155 +1,267 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
13
5
|
});
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
function _database() {
|
|
9
|
+
const data = require("@nocobase/database");
|
|
10
|
+
|
|
11
|
+
_database = function _database() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function _server() {
|
|
19
|
+
const data = require("@nocobase/server");
|
|
20
|
+
|
|
21
|
+
_server = function _server() {
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function _path() {
|
|
29
|
+
const data = require("path");
|
|
30
|
+
|
|
31
|
+
_path = function _path() {
|
|
32
|
+
return data;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return data;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var _2 = require("./");
|
|
39
|
+
|
|
40
|
+
var actions = _interopRequireWildcard(require("./actions/users"));
|
|
41
|
+
|
|
42
|
+
var _jwtService = require("./jwt-service");
|
|
43
|
+
|
|
44
|
+
var _locale = require("./locale");
|
|
45
|
+
|
|
46
|
+
var middlewares = _interopRequireWildcard(require("./middlewares"));
|
|
47
|
+
|
|
48
|
+
var _UserModel = require("./models/UserModel");
|
|
49
|
+
|
|
50
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
51
|
+
|
|
52
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
53
|
+
|
|
54
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
55
|
+
|
|
56
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
57
|
+
|
|
58
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
59
|
+
|
|
60
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
61
|
+
|
|
62
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
63
|
+
|
|
64
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
65
|
+
|
|
66
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
67
|
+
|
|
68
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
69
|
+
|
|
70
|
+
class UsersPlugin extends _server().Plugin {
|
|
71
|
+
constructor(app, options) {
|
|
72
|
+
super(app, options);
|
|
73
|
+
this.jwtService = void 0;
|
|
74
|
+
this.jwtService = new _jwtService.JwtService((options === null || options === void 0 ? void 0 : options.jwt) || {});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
beforeLoad() {
|
|
78
|
+
var _this = this;
|
|
79
|
+
|
|
80
|
+
return _asyncToGenerator(function* () {
|
|
81
|
+
_this.app.i18n.addResources('zh-CN', _2.namespace, _locale.zhCN);
|
|
82
|
+
|
|
83
|
+
_this.app.i18n.addResources('en-US', _2.namespace, _locale.enUS);
|
|
84
|
+
|
|
85
|
+
const cmd = _this.app.findCommand('install');
|
|
86
|
+
|
|
87
|
+
if (cmd) {
|
|
88
|
+
cmd.requiredOption('-e, --root-email <rootEmail>', '', process.env.INIT_ROOT_EMAIL);
|
|
89
|
+
cmd.requiredOption('-p, --root-password <rootPassword>', '', process.env.INIT_ROOT_PASSWORD);
|
|
90
|
+
cmd.option('-n, --root-nickname [rootNickname]');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
_this.db.registerOperators({
|
|
94
|
+
$isCurrentUser(_, ctx) {
|
|
95
|
+
var _ctx$app, _ctx$app$ctx, _ctx$app$ctx$state, _ctx$app$ctx$state$cu;
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
[_database().Op.eq]: (ctx === null || ctx === void 0 ? void 0 : (_ctx$app = ctx.app) === null || _ctx$app === void 0 ? void 0 : (_ctx$app$ctx = _ctx$app.ctx) === null || _ctx$app$ctx === void 0 ? void 0 : (_ctx$app$ctx$state = _ctx$app$ctx.state) === null || _ctx$app$ctx$state === void 0 ? void 0 : (_ctx$app$ctx$state$cu = _ctx$app$ctx$state.currentUser) === null || _ctx$app$ctx$state$cu === void 0 ? void 0 : _ctx$app$ctx$state$cu.id) || -1
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
_this.db.registerModels({
|
|
105
|
+
UserModel: _UserModel.UserModel
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
_this.db.on('users.afterCreateWithAssociations', /*#__PURE__*/function () {
|
|
109
|
+
var _ref = _asyncToGenerator(function* (model, options) {
|
|
110
|
+
const transaction = options.transaction;
|
|
111
|
+
|
|
112
|
+
const repository = _this.app.db.getRepository('roles');
|
|
113
|
+
|
|
114
|
+
if (!repository) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const defaultRole = yield repository.findOne({
|
|
119
|
+
filter: {
|
|
120
|
+
default: true
|
|
121
|
+
},
|
|
122
|
+
transaction
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
if (defaultRole && (yield model.countRoles({
|
|
126
|
+
transaction
|
|
127
|
+
})) == 0) {
|
|
128
|
+
yield model.addRoles(defaultRole, {
|
|
129
|
+
transaction
|
|
120
130
|
});
|
|
131
|
+
}
|
|
121
132
|
});
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return {
|
|
126
|
-
adminNickname,
|
|
127
|
-
adminEmail,
|
|
128
|
-
adminPassword,
|
|
133
|
+
|
|
134
|
+
return function (_x, _x2) {
|
|
135
|
+
return _ref.apply(this, arguments);
|
|
129
136
|
};
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
137
|
+
}());
|
|
138
|
+
|
|
139
|
+
_this.db.on('afterDefineCollection', collection => {
|
|
140
|
+
let _collection$options = collection.options,
|
|
141
|
+
createdBy = _collection$options.createdBy,
|
|
142
|
+
updatedBy = _collection$options.updatedBy;
|
|
143
|
+
|
|
144
|
+
if (createdBy === true) {
|
|
145
|
+
collection.setField('createdById', {
|
|
146
|
+
type: 'context',
|
|
147
|
+
dataType: 'integer',
|
|
148
|
+
dataIndex: 'state.currentUser.id',
|
|
149
|
+
createOnly: true,
|
|
150
|
+
visible: true,
|
|
151
|
+
onDelete: 'SET NULL',
|
|
152
|
+
onUpdate: 'CASCADE'
|
|
153
|
+
});
|
|
154
|
+
collection.setField('createdBy', {
|
|
155
|
+
type: 'belongsTo',
|
|
156
|
+
target: 'users',
|
|
157
|
+
foreignKey: 'createdById',
|
|
158
|
+
targetKey: 'id'
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (updatedBy === true) {
|
|
163
|
+
collection.setField('updatedById', {
|
|
164
|
+
type: 'context',
|
|
165
|
+
dataType: 'integer',
|
|
166
|
+
dataIndex: 'state.currentUser.id',
|
|
167
|
+
visible: true,
|
|
168
|
+
onDelete: 'SET NULL',
|
|
169
|
+
onUpdate: 'CASCADE'
|
|
170
|
+
});
|
|
171
|
+
collection.setField('updatedBy', {
|
|
172
|
+
type: 'belongsTo',
|
|
173
|
+
target: 'users',
|
|
174
|
+
foreignKey: 'updatedById',
|
|
175
|
+
targetKey: 'id'
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
for (var _i = 0, _Object$entries = Object.entries(actions); _i < _Object$entries.length; _i++) {
|
|
181
|
+
const _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
182
|
+
key = _Object$entries$_i[0],
|
|
183
|
+
action = _Object$entries$_i[1];
|
|
184
|
+
|
|
185
|
+
_this.app.resourcer.registerActionHandler(`users:${key}`, action);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
_this.app.resourcer.use(middlewares.parseToken({
|
|
189
|
+
plugin: _this
|
|
190
|
+
}));
|
|
191
|
+
|
|
192
|
+
const publicActions = ['check', 'signin', 'signup', 'lostpassword', 'resetpassword', 'getUserByResetToken'];
|
|
193
|
+
const loggedInActions = ['signout', 'updateProfile', 'changePassword', 'setDefaultRole'];
|
|
194
|
+
publicActions.forEach(action => _this.app.acl.allow('users', action));
|
|
195
|
+
loggedInActions.forEach(action => _this.app.acl.allow('users', action, 'loggedIn'));
|
|
196
|
+
})();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
load() {
|
|
200
|
+
var _this2 = this;
|
|
201
|
+
|
|
202
|
+
return _asyncToGenerator(function* () {
|
|
203
|
+
yield _this2.db.import({
|
|
204
|
+
directory: (0, _path().resolve)(__dirname, 'collections')
|
|
205
|
+
});
|
|
206
|
+
})();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
getInstallingData(options = {}) {
|
|
210
|
+
var _options$cliArgs;
|
|
211
|
+
|
|
212
|
+
const _process$env = process.env,
|
|
213
|
+
INIT_ROOT_NICKNAME = _process$env.INIT_ROOT_NICKNAME,
|
|
214
|
+
INIT_ROOT_PASSWORD = _process$env.INIT_ROOT_PASSWORD,
|
|
215
|
+
INIT_ROOT_EMAIL = _process$env.INIT_ROOT_EMAIL;
|
|
216
|
+
|
|
217
|
+
const _ref2 = options.users || (options === null || options === void 0 ? void 0 : (_options$cliArgs = options.cliArgs) === null || _options$cliArgs === void 0 ? void 0 : _options$cliArgs[0]) || {},
|
|
218
|
+
_ref2$rootEmail = _ref2.rootEmail,
|
|
219
|
+
rootEmail = _ref2$rootEmail === void 0 ? INIT_ROOT_EMAIL : _ref2$rootEmail,
|
|
220
|
+
_ref2$rootPassword = _ref2.rootPassword,
|
|
221
|
+
rootPassword = _ref2$rootPassword === void 0 ? INIT_ROOT_PASSWORD : _ref2$rootPassword,
|
|
222
|
+
_ref2$rootNickname = _ref2.rootNickname,
|
|
223
|
+
rootNickname = _ref2$rootNickname === void 0 ? INIT_ROOT_NICKNAME || 'Super Admin' : _ref2$rootNickname;
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
rootEmail,
|
|
227
|
+
rootPassword,
|
|
228
|
+
rootNickname
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
install(options) {
|
|
233
|
+
var _this3 = this;
|
|
234
|
+
|
|
235
|
+
return _asyncToGenerator(function* () {
|
|
236
|
+
const _this3$getInstallingD = _this3.getInstallingData(options),
|
|
237
|
+
rootNickname = _this3$getInstallingD.rootNickname,
|
|
238
|
+
rootPassword = _this3$getInstallingD.rootPassword,
|
|
239
|
+
rootEmail = _this3$getInstallingD.rootEmail;
|
|
240
|
+
|
|
241
|
+
const User = _this3.db.getCollection('users');
|
|
242
|
+
|
|
243
|
+
const user = yield User.repository.create({
|
|
244
|
+
values: {
|
|
245
|
+
email: rootEmail,
|
|
246
|
+
password: rootPassword,
|
|
247
|
+
nickname: rootNickname,
|
|
248
|
+
roles: ['root', 'admin', 'member']
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
yield user.setDefaultRole('root');
|
|
252
|
+
|
|
253
|
+
const repo = _this3.db.getRepository('collections');
|
|
254
|
+
|
|
255
|
+
if (repo) {
|
|
256
|
+
yield repo.db2cm('users');
|
|
257
|
+
}
|
|
258
|
+
})();
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
getName() {
|
|
262
|
+
return this.getPackageName(__dirname);
|
|
263
|
+
}
|
|
264
|
+
|
|
153
265
|
}
|
|
154
|
-
|
|
155
|
-
|
|
266
|
+
|
|
267
|
+
exports.default = UsersPlugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/plugin-users",
|
|
3
|
-
"version": "0.7.0-alpha.
|
|
3
|
+
"version": "0.7.0-alpha.72",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"licenses": [
|
|
@@ -9,17 +9,12 @@
|
|
|
9
9
|
"url": "http://www.apache.org/licenses/LICENSE-2.0"
|
|
10
10
|
}
|
|
11
11
|
],
|
|
12
|
-
"scripts": {
|
|
13
|
-
"build": "rimraf -rf lib esm dist && npm run build:cjs && npm run build:esm",
|
|
14
|
-
"build:cjs": "tsc --project tsconfig.build.json",
|
|
15
|
-
"build:esm": "tsc --project tsconfig.build.json --module es2015 --outDir esm"
|
|
16
|
-
},
|
|
17
12
|
"dependencies": {
|
|
18
13
|
"jsonwebtoken": "^8.5.1"
|
|
19
14
|
},
|
|
20
15
|
"devDependencies": {
|
|
21
|
-
"@nocobase/test": "0.7.0-alpha.
|
|
16
|
+
"@nocobase/test": "0.7.0-alpha.72",
|
|
22
17
|
"@types/jsonwebtoken": "^8.5.8"
|
|
23
18
|
},
|
|
24
|
-
"gitHead": "
|
|
19
|
+
"gitHead": "db24b85879b0f911458914b49ea9644fd7eb7562"
|
|
25
20
|
}
|
package/esm/actions/users.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Context, Next } from '@nocobase/actions';
|
|
2
|
-
export declare function check(ctx: Context, next: Next): Promise<void>;
|
|
3
|
-
export declare function signin(ctx: Context, next: Next): Promise<void>;
|
|
4
|
-
export declare function signout(ctx: Context, next: Next): Promise<void>;
|
|
5
|
-
export declare function signup(ctx: Context, next: Next): Promise<void>;
|
|
6
|
-
export declare function lostpassword(ctx: Context, next: Next): Promise<void>;
|
|
7
|
-
export declare function resetpassword(ctx: Context, next: Next): Promise<void>;
|
|
8
|
-
export declare function getUserByResetToken(ctx: Context, next: Next): Promise<void>;
|
|
9
|
-
export declare function updateProfile(ctx: Context, next: Next): Promise<void>;
|
|
10
|
-
export declare function changePassword(ctx: Context, next: Next): Promise<void>;
|
|
11
|
-
export declare function setDefaultRole(ctx: Context, next: Next): Promise<void>;
|
package/esm/actions/users.js
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import crypto from 'crypto';
|
|
11
|
-
export function check(ctx, next) {
|
|
12
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
13
|
-
if (ctx.state.currentUser) {
|
|
14
|
-
const user = ctx.state.currentUser.toJSON();
|
|
15
|
-
ctx.body = user;
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
ctx.body = {};
|
|
19
|
-
}
|
|
20
|
-
yield next();
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
export function signin(ctx, next) {
|
|
24
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
const { uniqueField = 'email', values } = ctx.action.params;
|
|
26
|
-
if (!values[uniqueField]) {
|
|
27
|
-
ctx.throw(401, '请填写邮箱账号');
|
|
28
|
-
}
|
|
29
|
-
const User = ctx.db.getCollection('users');
|
|
30
|
-
const user = yield User.model.findOne({
|
|
31
|
-
where: {
|
|
32
|
-
[uniqueField]: values[uniqueField],
|
|
33
|
-
},
|
|
34
|
-
});
|
|
35
|
-
if (!user) {
|
|
36
|
-
ctx.throw(401, '邮箱账号未注册');
|
|
37
|
-
}
|
|
38
|
-
const pwd = User.getField('password');
|
|
39
|
-
const isValid = yield pwd.verify(values.password, user.password);
|
|
40
|
-
if (!isValid) {
|
|
41
|
-
ctx.throw(401, '密码错误,请您重新输入');
|
|
42
|
-
}
|
|
43
|
-
const pluginUser = ctx.app.getPlugin('@nocobase/plugin-users');
|
|
44
|
-
ctx.body = Object.assign(Object.assign({}, user.toJSON()), { token: pluginUser.jwtService.sign({
|
|
45
|
-
userId: user.get('id'),
|
|
46
|
-
}) });
|
|
47
|
-
yield next();
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
export function signout(ctx, next) {
|
|
51
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
ctx.body = ctx.state.currentUser;
|
|
53
|
-
yield next();
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
export function signup(ctx, next) {
|
|
57
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
-
const User = ctx.db.getRepository('users');
|
|
59
|
-
const { values } = ctx.action.params;
|
|
60
|
-
const user = yield User.create({ values });
|
|
61
|
-
ctx.body = user;
|
|
62
|
-
yield next();
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
export function lostpassword(ctx, next) {
|
|
66
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
const { values: { email }, } = ctx.action.params;
|
|
68
|
-
if (!email) {
|
|
69
|
-
ctx.throw(401, '请填写邮箱账号');
|
|
70
|
-
}
|
|
71
|
-
const User = ctx.db.getCollection('users');
|
|
72
|
-
const user = yield User.model.findOne({
|
|
73
|
-
where: {
|
|
74
|
-
email,
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
if (!user) {
|
|
78
|
-
ctx.throw(401, '邮箱账号未注册');
|
|
79
|
-
}
|
|
80
|
-
user.resetToken = crypto.randomBytes(20).toString('hex');
|
|
81
|
-
yield user.save();
|
|
82
|
-
ctx.body = user;
|
|
83
|
-
yield next();
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
export function resetpassword(ctx, next) {
|
|
87
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
-
const { values: { email, password, resetToken }, } = ctx.action.params;
|
|
89
|
-
const User = ctx.db.getCollection('users');
|
|
90
|
-
const user = yield User.model.findOne({
|
|
91
|
-
where: {
|
|
92
|
-
email,
|
|
93
|
-
resetToken,
|
|
94
|
-
},
|
|
95
|
-
});
|
|
96
|
-
if (!user) {
|
|
97
|
-
ctx.throw(401, 'Unauthorized');
|
|
98
|
-
}
|
|
99
|
-
user.token = null;
|
|
100
|
-
user.resetToken = null;
|
|
101
|
-
user.password = password;
|
|
102
|
-
yield user.save();
|
|
103
|
-
ctx.body = user;
|
|
104
|
-
yield next();
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
export function getUserByResetToken(ctx, next) {
|
|
108
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
-
const { token } = ctx.action.params;
|
|
110
|
-
const User = ctx.db.getCollection('users');
|
|
111
|
-
const user = yield User.model.findOne({
|
|
112
|
-
where: {
|
|
113
|
-
resetToken: token,
|
|
114
|
-
},
|
|
115
|
-
});
|
|
116
|
-
if (!user) {
|
|
117
|
-
ctx.throw(401, 'Unauthorized');
|
|
118
|
-
}
|
|
119
|
-
ctx.body = user;
|
|
120
|
-
yield next();
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
export function updateProfile(ctx, next) {
|
|
124
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
-
const { values } = ctx.action.params;
|
|
126
|
-
if (!ctx.state.currentUser) {
|
|
127
|
-
ctx.throw(401, 'Unauthorized');
|
|
128
|
-
}
|
|
129
|
-
yield ctx.state.currentUser.update(values);
|
|
130
|
-
ctx.body = ctx.state.currentUser;
|
|
131
|
-
yield next();
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
export function changePassword(ctx, next) {
|
|
135
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
-
const { values: { oldPassword, newPassword }, } = ctx.action.params;
|
|
137
|
-
if (!ctx.state.currentUser) {
|
|
138
|
-
ctx.throw(401, 'Unauthorized');
|
|
139
|
-
}
|
|
140
|
-
const User = ctx.db.getCollection('users');
|
|
141
|
-
const user = yield User.model.findOne({
|
|
142
|
-
where: {
|
|
143
|
-
email: ctx.state.currentUser.email,
|
|
144
|
-
},
|
|
145
|
-
});
|
|
146
|
-
const pwd = User.getField('password');
|
|
147
|
-
const isValid = yield pwd.verify(oldPassword, user.password);
|
|
148
|
-
if (!isValid) {
|
|
149
|
-
ctx.throw(401, '密码错误,请您重新输入');
|
|
150
|
-
}
|
|
151
|
-
user.password = newPassword;
|
|
152
|
-
user.save();
|
|
153
|
-
ctx.body = ctx.state.currentUser.toJSON();
|
|
154
|
-
yield next();
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
export function setDefaultRole(ctx, next) {
|
|
158
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
-
const { values: { roleName }, } = ctx.action.params;
|
|
160
|
-
yield ctx.state.currentUser.setDefaultRole(roleName);
|
|
161
|
-
ctx.body = 'ok';
|
|
162
|
-
yield next();
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
//# sourceMappingURL=users.js.map
|
package/esm/actions/users.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"users.js","sourceRoot":"","sources":["../../src/actions/users.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,UAAgB,KAAK,CAAC,GAAY,EAAE,IAAU;;QAClD,IAAI,GAAG,CAAC,KAAK,CAAC,WAAW,EAAE;YACzB,MAAM,IAAI,GAAG,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC;YAC5C,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;SACjB;aAAM;YACL,GAAG,CAAC,IAAI,GAAG,EAAE,CAAC;SACf;QACD,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CAAA;AAED,MAAM,UAAgB,MAAM,CAAC,GAAY,EAAE,IAAU;;QACnD,MAAM,EAAE,WAAW,GAAG,OAAO,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC;QAE5D,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE;YACxB,GAA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C,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CAAA;AAED,MAAM,UAAgB,cAAc,CAAC,GAAY,EAAE,IAAU;;QAC3D,MAAM,EACJ,MAAM,EAAE,EAAE,QAAQ,EAAE,GACrB,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC;QAEtB,MAAM,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QAErD,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;QAEhB,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CAAA","sourcesContent":["import { Context, Next } from '@nocobase/actions';\nimport { PasswordField } from '@nocobase/database';\nimport crypto from 'crypto';\n\nexport async function check(ctx: Context, next: Next) {\n if (ctx.state.currentUser) {\n const user = ctx.state.currentUser.toJSON();\n ctx.body = user;\n } else {\n ctx.body = {};\n }\n await next();\n}\n\nexport async function signin(ctx: Context, next: Next) {\n const { uniqueField = 'email', values } = ctx.action.params;\n\n if (!values[uniqueField]) {\n ctx.throw(401, '请填写邮箱账号');\n }\n const User = ctx.db.getCollection('users');\n const user = await User.model.findOne<any>({\n where: {\n [uniqueField]: values[uniqueField],\n },\n });\n if (!user) {\n ctx.throw(401, '邮箱账号未注册');\n }\n const pwd = User.getField<PasswordField>('password');\n const isValid = await pwd.verify(values.password, user.password);\n if (!isValid) {\n ctx.throw(401, '密码错误,请您重新输入');\n }\n\n const pluginUser = ctx.app.getPlugin('@nocobase/plugin-users');\n\n ctx.body = {\n ...user.toJSON(),\n token: pluginUser.jwtService.sign({\n userId: user.get('id'),\n }),\n };\n await next();\n}\n\nexport async function signout(ctx: Context, next: Next) {\n ctx.body = ctx.state.currentUser;\n await next();\n}\n\nexport async function signup(ctx: Context, next: Next) {\n const User = ctx.db.getRepository('users');\n const { values } = ctx.action.params;\n const user = await User.create({ values });\n ctx.body = user;\n await next();\n}\n\nexport async function lostpassword(ctx: Context, next: Next) {\n const {\n values: { email },\n } = ctx.action.params;\n if (!email) {\n ctx.throw(401, '请填写邮箱账号');\n }\n const User = ctx.db.getCollection('users');\n const user = await User.model.findOne<any>({\n where: {\n email,\n },\n });\n if (!user) {\n ctx.throw(401, '邮箱账号未注册');\n }\n user.resetToken = crypto.randomBytes(20).toString('hex');\n await user.save();\n ctx.body = user;\n await next();\n}\n\nexport async function resetpassword(ctx: Context, next: Next) {\n const {\n values: { email, password, resetToken },\n } = ctx.action.params;\n const User = ctx.db.getCollection('users');\n const user = await User.model.findOne<any>({\n where: {\n email,\n resetToken,\n },\n });\n if (!user) {\n ctx.throw(401, 'Unauthorized');\n }\n user.token = null;\n user.resetToken = null;\n user.password = password;\n await user.save();\n ctx.body = user;\n await next();\n}\n\nexport async function getUserByResetToken(ctx: Context, next: Next) {\n const { token } = ctx.action.params;\n const User = ctx.db.getCollection('users');\n const user = await User.model.findOne({\n where: {\n resetToken: token,\n },\n });\n if (!user) {\n ctx.throw(401, 'Unauthorized');\n }\n ctx.body = user;\n await next();\n}\n\nexport async function updateProfile(ctx: Context, next: Next) {\n const { values } = ctx.action.params;\n if (!ctx.state.currentUser) {\n ctx.throw(401, 'Unauthorized');\n }\n await ctx.state.currentUser.update(values);\n ctx.body = ctx.state.currentUser;\n await next();\n}\n\nexport async function changePassword(ctx: Context, next: Next) {\n const {\n values: { oldPassword, newPassword },\n } = ctx.action.params;\n if (!ctx.state.currentUser) {\n ctx.throw(401, 'Unauthorized');\n }\n const User = ctx.db.getCollection('users');\n const user = await User.model.findOne<any>({\n where: {\n email: ctx.state.currentUser.email,\n },\n });\n const pwd = User.getField<PasswordField>('password');\n const isValid = await pwd.verify(oldPassword, user.password);\n if (!isValid) {\n ctx.throw(401, '密码错误,请您重新输入');\n }\n user.password = newPassword;\n user.save();\n ctx.body = ctx.state.currentUser.toJSON();\n await next();\n}\n\nexport async function setDefaultRole(ctx: Context, next: Next) {\n const {\n values: { roleName },\n } = ctx.action.params;\n\n await ctx.state.currentUser.setDefaultRole(roleName);\n\n ctx.body = 'ok';\n\n await next();\n}\n"]}
|