@nocobase/plugin-users 0.7.2-alpha.6 → 0.7.4-alpha.1
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.d.ts +0 -1
- package/lib/actions/users.js +0 -15
- package/lib/collections/users.js +1 -22
- package/lib/jwt-service.js +1 -1
- package/lib/middlewares/parseToken.d.ts +2 -3
- package/lib/middlewares/parseToken.js +21 -45
- package/lib/server.d.ts +1 -0
- package/lib/server.js +14 -51
- package/package.json +3 -3
- package/lib/collections/roles-users.d.ts +0 -3
- package/lib/collections/roles-users.js +0 -14
- package/lib/models/UserModel.d.ts +0 -4
- package/lib/models/UserModel.js +0 -72
package/lib/actions/users.d.ts
CHANGED
|
@@ -8,4 +8,3 @@ export declare function resetpassword(ctx: Context, next: Next): Promise<void>;
|
|
|
8
8
|
export declare function getUserByResetToken(ctx: Context, next: Next): Promise<void>;
|
|
9
9
|
export declare function updateProfile(ctx: Context, next: Next): Promise<void>;
|
|
10
10
|
export declare function changePassword(ctx: Context, next: Next): Promise<void>;
|
|
11
|
-
export declare function setDefaultRole(ctx: Context, next: Next): Promise<void>;
|
package/lib/actions/users.js
CHANGED
|
@@ -8,7 +8,6 @@ exports.check = check;
|
|
|
8
8
|
exports.getUserByResetToken = getUserByResetToken;
|
|
9
9
|
exports.lostpassword = lostpassword;
|
|
10
10
|
exports.resetpassword = resetpassword;
|
|
11
|
-
exports.setDefaultRole = setDefaultRole;
|
|
12
11
|
exports.signin = signin;
|
|
13
12
|
exports.signout = signout;
|
|
14
13
|
exports.signup = signup;
|
|
@@ -280,18 +279,4 @@ function _changePassword() {
|
|
|
280
279
|
yield next();
|
|
281
280
|
});
|
|
282
281
|
return _changePassword.apply(this, arguments);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
function setDefaultRole(_x19, _x20) {
|
|
286
|
-
return _setDefaultRole.apply(this, arguments);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
function _setDefaultRole() {
|
|
290
|
-
_setDefaultRole = _asyncToGenerator(function* (ctx, next) {
|
|
291
|
-
const roleName = ctx.action.params.values.roleName;
|
|
292
|
-
yield ctx.state.currentUser.setDefaultRole(roleName);
|
|
293
|
-
ctx.body = 'ok';
|
|
294
|
-
yield next();
|
|
295
|
-
});
|
|
296
|
-
return _setDefaultRole.apply(this, arguments);
|
|
297
282
|
}
|
package/lib/collections/users.js
CHANGED
|
@@ -43,6 +43,7 @@ var _default = {
|
|
|
43
43
|
type: 'string',
|
|
44
44
|
title: '{{t("Email")}}',
|
|
45
45
|
'x-component': 'Input',
|
|
46
|
+
'x-validator': 'email',
|
|
46
47
|
require: true
|
|
47
48
|
}
|
|
48
49
|
}, {
|
|
@@ -55,28 +56,6 @@ var _default = {
|
|
|
55
56
|
title: '{{t("Password")}}',
|
|
56
57
|
'x-component': 'Password'
|
|
57
58
|
}
|
|
58
|
-
}, {
|
|
59
|
-
interface: 'm2m',
|
|
60
|
-
type: 'belongsToMany',
|
|
61
|
-
name: 'roles',
|
|
62
|
-
target: 'roles',
|
|
63
|
-
foreignKey: 'userId',
|
|
64
|
-
otherKey: 'roleName',
|
|
65
|
-
sourceKey: 'id',
|
|
66
|
-
targetKey: 'name',
|
|
67
|
-
through: 'rolesUsers',
|
|
68
|
-
uiSchema: {
|
|
69
|
-
type: 'array',
|
|
70
|
-
title: '{{t("Roles")}}',
|
|
71
|
-
'x-component': 'RecordPicker',
|
|
72
|
-
'x-component-props': {
|
|
73
|
-
multiple: true,
|
|
74
|
-
fieldNames: {
|
|
75
|
-
label: 'title',
|
|
76
|
-
value: 'name'
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
59
|
}, {
|
|
81
60
|
type: 'string',
|
|
82
61
|
name: 'appLang'
|
package/lib/jwt-service.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MiddlewareManager } from '@nocobase/resourcer';
|
|
2
2
|
import UsersPlugin from '../server';
|
|
3
3
|
export declare function parseToken(options?: {
|
|
4
4
|
plugin: UsersPlugin;
|
|
5
|
-
}):
|
|
6
|
-
export declare function setCurrentRole(ctx: any): void;
|
|
5
|
+
}): MiddlewareManager;
|
|
@@ -4,7 +4,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.parseToken = parseToken;
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
function _resourcer() {
|
|
9
|
+
const data = require("@nocobase/resourcer");
|
|
10
|
+
|
|
11
|
+
_resourcer = function _resourcer() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
8
17
|
|
|
9
18
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
10
19
|
|
|
@@ -25,56 +34,23 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try
|
|
|
25
34
|
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); }); }; }
|
|
26
35
|
|
|
27
36
|
function parseToken(options) {
|
|
28
|
-
|
|
29
|
-
|
|
37
|
+
const middleware = new (_resourcer().MiddlewareManager)();
|
|
38
|
+
middleware.use( /*#__PURE__*/function () {
|
|
39
|
+
var _ref = _asyncToGenerator(function* (ctx, next) {
|
|
30
40
|
const user = yield findUserByToken(ctx, options.plugin);
|
|
31
41
|
|
|
32
42
|
if (user) {
|
|
33
43
|
ctx.state.currentUser = user;
|
|
34
|
-
setCurrentRole(ctx);
|
|
35
44
|
}
|
|
36
45
|
|
|
37
46
|
return next();
|
|
38
47
|
});
|
|
39
48
|
|
|
40
|
-
function
|
|
41
|
-
return
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function setCurrentRole(ctx) {
|
|
49
|
-
let currentRole = ctx.get('X-Role');
|
|
50
|
-
|
|
51
|
-
if (currentRole === 'anonymous') {
|
|
52
|
-
ctx.state.currentRole = currentRole;
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const userRoles = ctx.state.currentUser.roles;
|
|
57
|
-
|
|
58
|
-
if (userRoles.length == 1) {
|
|
59
|
-
currentRole = userRoles[0].name;
|
|
60
|
-
} else if (userRoles.length > 1) {
|
|
61
|
-
const role = userRoles.find(role => role.name === currentRole);
|
|
62
|
-
|
|
63
|
-
if (!role) {
|
|
64
|
-
var _ref;
|
|
65
|
-
|
|
66
|
-
const defaultRole = userRoles.find(role => {
|
|
67
|
-
var _role$rolesUsers;
|
|
68
|
-
|
|
69
|
-
return role === null || role === void 0 ? void 0 : (_role$rolesUsers = role.rolesUsers) === null || _role$rolesUsers === void 0 ? void 0 : _role$rolesUsers.default;
|
|
70
|
-
});
|
|
71
|
-
currentRole = (_ref = defaultRole || userRoles[0]) === null || _ref === void 0 ? void 0 : _ref.name;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (currentRole) {
|
|
76
|
-
ctx.state.currentRole = currentRole;
|
|
77
|
-
}
|
|
49
|
+
return function (_x, _x2) {
|
|
50
|
+
return _ref.apply(this, arguments);
|
|
51
|
+
};
|
|
52
|
+
}());
|
|
53
|
+
return middleware;
|
|
78
54
|
}
|
|
79
55
|
|
|
80
56
|
function findUserByToken(_x3, _x4) {
|
|
@@ -94,7 +70,7 @@ function _findUserByToken() {
|
|
|
94
70
|
userId = _yield$plugin$jwtServ.userId;
|
|
95
71
|
|
|
96
72
|
const collection = ctx.db.getCollection('users');
|
|
97
|
-
|
|
73
|
+
ctx.state.currentUserAppends = ctx.state.currentUserAppends || [];
|
|
98
74
|
|
|
99
75
|
var _iterator = _createForOfIteratorHelper(collection.fields),
|
|
100
76
|
_step;
|
|
@@ -105,7 +81,7 @@ function _findUserByToken() {
|
|
|
105
81
|
field = _step$value[1];
|
|
106
82
|
|
|
107
83
|
if (field.type === 'belongsTo') {
|
|
108
|
-
|
|
84
|
+
ctx.state.currentUserAppends.push(field.name);
|
|
109
85
|
}
|
|
110
86
|
}
|
|
111
87
|
} catch (err) {
|
|
@@ -115,7 +91,7 @@ function _findUserByToken() {
|
|
|
115
91
|
}
|
|
116
92
|
|
|
117
93
|
return yield ctx.db.getRepository('users').findOne({
|
|
118
|
-
appends,
|
|
94
|
+
appends: ctx.state.currentUserAppends,
|
|
119
95
|
filter: {
|
|
120
96
|
id: userId
|
|
121
97
|
}
|
package/lib/server.d.ts
CHANGED
package/lib/server.js
CHANGED
|
@@ -55,8 +55,6 @@ var _locale = require("./locale");
|
|
|
55
55
|
|
|
56
56
|
var middlewares = _interopRequireWildcard(require("./middlewares"));
|
|
57
57
|
|
|
58
|
-
var _UserModel = require("./models/UserModel");
|
|
59
|
-
|
|
60
58
|
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); }
|
|
61
59
|
|
|
62
60
|
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; }
|
|
@@ -83,7 +81,11 @@ class UsersPlugin extends _server().Plugin {
|
|
|
83
81
|
constructor(app, options) {
|
|
84
82
|
super(app, options);
|
|
85
83
|
this.jwtService = void 0;
|
|
84
|
+
this.tokenMiddleware = void 0;
|
|
86
85
|
this.jwtService = new _jwtService.JwtService((options === null || options === void 0 ? void 0 : options.jwt) || {});
|
|
86
|
+
this.tokenMiddleware = middlewares.parseToken({
|
|
87
|
+
plugin: this
|
|
88
|
+
});
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
beforeLoad() {
|
|
@@ -124,41 +126,6 @@ class UsersPlugin extends _server().Plugin {
|
|
|
124
126
|
|
|
125
127
|
});
|
|
126
128
|
|
|
127
|
-
_this.db.registerModels({
|
|
128
|
-
UserModel: _UserModel.UserModel
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
_this.db.on('users.afterCreateWithAssociations', /*#__PURE__*/function () {
|
|
132
|
-
var _ref = _asyncToGenerator(function* (model, options) {
|
|
133
|
-
const transaction = options.transaction;
|
|
134
|
-
|
|
135
|
-
const repository = _this.app.db.getRepository('roles');
|
|
136
|
-
|
|
137
|
-
if (!repository) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const defaultRole = yield repository.findOne({
|
|
142
|
-
filter: {
|
|
143
|
-
default: true
|
|
144
|
-
},
|
|
145
|
-
transaction
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
if (defaultRole && (yield model.countRoles({
|
|
149
|
-
transaction
|
|
150
|
-
})) == 0) {
|
|
151
|
-
yield model.addRoles(defaultRole, {
|
|
152
|
-
transaction
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
return function (_x, _x2) {
|
|
158
|
-
return _ref.apply(this, arguments);
|
|
159
|
-
};
|
|
160
|
-
}());
|
|
161
|
-
|
|
162
129
|
_this.db.on('afterDefineCollection', collection => {
|
|
163
130
|
let _collection$options = collection.options,
|
|
164
131
|
createdBy = _collection$options.createdBy,
|
|
@@ -206,12 +173,10 @@ class UsersPlugin extends _server().Plugin {
|
|
|
206
173
|
_this.app.resourcer.registerActionHandler(`users:${key}`, action);
|
|
207
174
|
}
|
|
208
175
|
|
|
209
|
-
_this.app.resourcer.use(
|
|
210
|
-
plugin: _this
|
|
211
|
-
}));
|
|
176
|
+
_this.app.resourcer.use(_this.tokenMiddleware.compose());
|
|
212
177
|
|
|
213
178
|
const publicActions = ['check', 'signin', 'signup', 'lostpassword', 'resetpassword', 'getUserByResetToken'];
|
|
214
|
-
const loggedInActions = ['signout', 'updateProfile', 'changePassword'
|
|
179
|
+
const loggedInActions = ['signout', 'updateProfile', 'changePassword'];
|
|
215
180
|
publicActions.forEach(action => _this.app.acl.allow('users', action));
|
|
216
181
|
loggedInActions.forEach(action => _this.app.acl.allow('users', action, 'loggedIn'));
|
|
217
182
|
})();
|
|
@@ -235,13 +200,13 @@ class UsersPlugin extends _server().Plugin {
|
|
|
235
200
|
INIT_ROOT_PASSWORD = _process$env.INIT_ROOT_PASSWORD,
|
|
236
201
|
INIT_ROOT_EMAIL = _process$env.INIT_ROOT_EMAIL;
|
|
237
202
|
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
rootEmail =
|
|
241
|
-
|
|
242
|
-
rootPassword =
|
|
243
|
-
|
|
244
|
-
rootNickname =
|
|
203
|
+
const _ref = options.users || (options === null || options === void 0 ? void 0 : (_options$cliArgs = options.cliArgs) === null || _options$cliArgs === void 0 ? void 0 : _options$cliArgs[0]) || {},
|
|
204
|
+
_ref$rootEmail = _ref.rootEmail,
|
|
205
|
+
rootEmail = _ref$rootEmail === void 0 ? INIT_ROOT_EMAIL : _ref$rootEmail,
|
|
206
|
+
_ref$rootPassword = _ref.rootPassword,
|
|
207
|
+
rootPassword = _ref$rootPassword === void 0 ? INIT_ROOT_PASSWORD : _ref$rootPassword,
|
|
208
|
+
_ref$rootNickname = _ref.rootNickname,
|
|
209
|
+
rootNickname = _ref$rootNickname === void 0 ? INIT_ROOT_NICKNAME || 'Super Admin' : _ref$rootNickname;
|
|
245
210
|
|
|
246
211
|
return {
|
|
247
212
|
rootEmail,
|
|
@@ -265,11 +230,9 @@ class UsersPlugin extends _server().Plugin {
|
|
|
265
230
|
values: {
|
|
266
231
|
email: rootEmail,
|
|
267
232
|
password: rootPassword,
|
|
268
|
-
nickname: rootNickname
|
|
269
|
-
roles: ['root', 'admin', 'member']
|
|
233
|
+
nickname: rootNickname
|
|
270
234
|
}
|
|
271
235
|
});
|
|
272
|
-
yield user.setDefaultRole('root');
|
|
273
236
|
|
|
274
237
|
const repo = _this3.db.getRepository('collections');
|
|
275
238
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/plugin-users",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4-alpha.1",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"licenses": [
|
|
@@ -13,9 +13,9 @@
|
|
|
13
13
|
"jsonwebtoken": "^8.5.1"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
|
-
"@nocobase/test": "0.7.
|
|
16
|
+
"@nocobase/test": "0.7.4-alpha.1",
|
|
17
17
|
"@types/jsonwebtoken": "^8.5.8",
|
|
18
18
|
"json-templates": "^4.2.0"
|
|
19
19
|
},
|
|
20
|
-
"gitHead": "
|
|
20
|
+
"gitHead": "543eb1c0308bb72a5ba54f208586cd78bfda8fa9"
|
|
21
21
|
}
|
package/lib/models/UserModel.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.UserModel = 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 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); } }
|
|
19
|
-
|
|
20
|
-
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); }); }; }
|
|
21
|
-
|
|
22
|
-
class UserModel extends _database().Model {
|
|
23
|
-
setDefaultRole(roleName, options = {}) {
|
|
24
|
-
var _this = this;
|
|
25
|
-
|
|
26
|
-
return _asyncToGenerator(function* () {
|
|
27
|
-
if (roleName == 'anonymous') {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const db = _this.constructor.database;
|
|
32
|
-
const repository = db.getRepository('rolesUsers');
|
|
33
|
-
|
|
34
|
-
if (!repository) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const transaction = options.transaction || (yield db.sequelize.transaction());
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
yield repository.update({
|
|
42
|
-
filter: {
|
|
43
|
-
userId: _this.get('id')
|
|
44
|
-
},
|
|
45
|
-
values: {
|
|
46
|
-
default: false
|
|
47
|
-
},
|
|
48
|
-
transaction
|
|
49
|
-
});
|
|
50
|
-
yield repository.update({
|
|
51
|
-
filter: {
|
|
52
|
-
userId: _this.get('id'),
|
|
53
|
-
roleName
|
|
54
|
-
},
|
|
55
|
-
values: {
|
|
56
|
-
default: true
|
|
57
|
-
},
|
|
58
|
-
transaction
|
|
59
|
-
});
|
|
60
|
-
yield transaction.commit();
|
|
61
|
-
} catch (error) {
|
|
62
|
-
yield transaction.rollback();
|
|
63
|
-
throw error;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return true;
|
|
67
|
-
})();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
exports.UserModel = UserModel;
|