@nocobase/plugin-users 0.11.1-alpha.5 → 0.12.0-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.
Files changed (108) hide show
  1. package/dist/client/index.js +11 -0
  2. package/{lib → dist}/index.d.ts +1 -0
  3. package/dist/index.js +18 -0
  4. package/dist/locale/en-US.js +12 -0
  5. package/dist/locale/es-ES.js +12 -0
  6. package/dist/locale/fr-FR.js +12 -0
  7. package/dist/locale/ja-JP.js +8 -0
  8. package/dist/locale/pt-BR.js +12 -0
  9. package/dist/locale/zh-CN.js +12 -0
  10. package/dist/server/actions/users.js +152 -0
  11. package/dist/server/authenticators/index.js +23 -0
  12. package/dist/server/authenticators/password.js +31 -0
  13. package/dist/server/collections/users.js +91 -0
  14. package/dist/server/index.d.ts +2 -0
  15. package/dist/server/index.js +18 -0
  16. package/dist/server/jwt-service.js +34 -0
  17. package/dist/server/locale/en-US.js +12 -0
  18. package/dist/server/locale/es-ES.js +12 -0
  19. package/dist/server/locale/fr-FR.js +12 -0
  20. package/dist/server/locale/index.js +26 -0
  21. package/dist/server/locale/ja-JP.js +8 -0
  22. package/dist/server/locale/pt-BR.js +12 -0
  23. package/dist/server/locale/zh-CN.js +12 -0
  24. package/dist/server/middlewares/check.js +13 -0
  25. package/dist/server/middlewares/index.js +15 -0
  26. package/{src/server/middlewares/parseToken.ts → dist/server/middlewares/parseToken.js} +11 -12
  27. package/dist/server/migrations/20220818072639-add-users-phone.js +43 -0
  28. package/dist/server/server.js +249 -0
  29. package/package.json +17 -28
  30. package/lib/client/index.js +0 -22
  31. package/lib/index.js +0 -13
  32. package/lib/locale/en-US.js +0 -15
  33. package/lib/locale/es-ES.js +0 -15
  34. package/lib/locale/fr-FR.js +0 -15
  35. package/lib/locale/ja-JP.js +0 -11
  36. package/lib/locale/pt-BR.js +0 -15
  37. package/lib/locale/zh-CN.js +0 -15
  38. package/lib/server/actions/users.js +0 -253
  39. package/lib/server/authenticators/index.js +0 -44
  40. package/lib/server/authenticators/password.js +0 -49
  41. package/lib/server/collections/users.js +0 -90
  42. package/lib/server/index.d.ts +0 -2
  43. package/lib/server/index.js +0 -16
  44. package/lib/server/jwt-service.js +0 -42
  45. package/lib/server/locale/en-US.js +0 -15
  46. package/lib/server/locale/es-ES.js +0 -15
  47. package/lib/server/locale/fr-FR.js +0 -15
  48. package/lib/server/locale/index.js +0 -27
  49. package/lib/server/locale/ja-JP.js +0 -11
  50. package/lib/server/locale/pt-BR.js +0 -15
  51. package/lib/server/locale/zh-CN.js +0 -15
  52. package/lib/server/middlewares/check.js +0 -24
  53. package/lib/server/middlewares/index.js +0 -19
  54. package/lib/server/middlewares/parseToken.js +0 -75
  55. package/lib/server/migrations/20220818072639-add-users-phone.js +0 -57
  56. package/lib/server/server.js +0 -312
  57. package/src/client/index.ts +0 -7
  58. package/src/index.ts +0 -1
  59. package/src/locale/en-US.ts +0 -10
  60. package/src/locale/es-ES.ts +0 -8
  61. package/src/locale/fr-FR.ts +0 -11
  62. package/src/locale/ja-JP.ts +0 -4
  63. package/src/locale/pt-BR.ts +0 -10
  64. package/src/locale/zh-CN.ts +0 -8
  65. package/src/server/__tests__/actions.test.ts +0 -79
  66. package/src/server/__tests__/fields.test.ts +0 -90
  67. package/src/server/__tests__/utils.ts +0 -8
  68. package/src/server/actions/users.ts +0 -148
  69. package/src/server/authenticators/index.ts +0 -26
  70. package/src/server/authenticators/password.ts +0 -34
  71. package/src/server/collections/users.ts +0 -89
  72. package/src/server/index.ts +0 -3
  73. package/src/server/jwt-service.ts +0 -34
  74. package/src/server/locale/en-US.ts +0 -10
  75. package/src/server/locale/es-ES.ts +0 -8
  76. package/src/server/locale/fr-FR.ts +0 -11
  77. package/src/server/locale/index.ts +0 -3
  78. package/src/server/locale/ja-JP.ts +0 -4
  79. package/src/server/locale/pt-BR.ts +0 -10
  80. package/src/server/locale/zh-CN.ts +0 -8
  81. package/src/server/middlewares/check.ts +0 -11
  82. package/src/server/middlewares/index.ts +0 -2
  83. package/src/server/migrations/20220818072639-add-users-phone.ts +0 -39
  84. package/src/server/server.ts +0 -253
  85. /package/{lib → dist}/client/index.d.ts +0 -0
  86. /package/{lib → dist}/locale/en-US.d.ts +0 -0
  87. /package/{lib → dist}/locale/es-ES.d.ts +0 -0
  88. /package/{lib → dist}/locale/fr-FR.d.ts +0 -0
  89. /package/{lib → dist}/locale/ja-JP.d.ts +0 -0
  90. /package/{lib → dist}/locale/pt-BR.d.ts +0 -0
  91. /package/{lib → dist}/locale/zh-CN.d.ts +0 -0
  92. /package/{lib → dist}/server/actions/users.d.ts +0 -0
  93. /package/{lib → dist}/server/authenticators/index.d.ts +0 -0
  94. /package/{lib → dist}/server/authenticators/password.d.ts +0 -0
  95. /package/{lib → dist}/server/collections/users.d.ts +0 -0
  96. /package/{lib → dist}/server/jwt-service.d.ts +0 -0
  97. /package/{lib → dist}/server/locale/en-US.d.ts +0 -0
  98. /package/{lib → dist}/server/locale/es-ES.d.ts +0 -0
  99. /package/{lib → dist}/server/locale/fr-FR.d.ts +0 -0
  100. /package/{lib → dist}/server/locale/index.d.ts +0 -0
  101. /package/{lib → dist}/server/locale/ja-JP.d.ts +0 -0
  102. /package/{lib → dist}/server/locale/pt-BR.d.ts +0 -0
  103. /package/{lib → dist}/server/locale/zh-CN.d.ts +0 -0
  104. /package/{lib → dist}/server/middlewares/check.d.ts +0 -0
  105. /package/{lib → dist}/server/middlewares/index.d.ts +0 -0
  106. /package/{lib → dist}/server/middlewares/parseToken.d.ts +0 -0
  107. /package/{lib → dist}/server/migrations/20220818072639-add-users-phone.d.ts +0 -0
  108. /package/{lib → dist}/server/server.d.ts +0 -0
@@ -1,253 +0,0 @@
1
- import { Collection, Op } from '@nocobase/database';
2
- import { HandlerType } from '@nocobase/resourcer';
3
- import { Plugin } from '@nocobase/server';
4
- import { Registry, parse } from '@nocobase/utils';
5
- import { resolve } from 'path';
6
-
7
- import { namespace } from './';
8
- import * as actions from './actions/users';
9
- import initAuthenticators from './authenticators';
10
- import { JwtOptions, JwtService } from './jwt-service';
11
- import { enUS, zhCN } from './locale';
12
- import { parseToken } from './middlewares';
13
-
14
- export interface UserPluginConfig {
15
- name?: string;
16
- jwt: JwtOptions;
17
- }
18
-
19
- export default class UsersPlugin extends Plugin<UserPluginConfig> {
20
- public jwtService: JwtService;
21
-
22
- public authenticators: Registry<HandlerType> = new Registry();
23
-
24
- constructor(app, options) {
25
- super(app, options);
26
- this.jwtService = new JwtService(options?.jwt || {});
27
- }
28
-
29
- async beforeLoad() {
30
- this.app.i18n.addResources('zh-CN', namespace, zhCN);
31
- this.app.i18n.addResources('en-US', namespace, enUS);
32
- const cmd = this.app.findCommand('install');
33
- if (cmd) {
34
- cmd.requiredOption('-e, --root-email <rootEmail>', '', process.env.INIT_ROOT_EMAIL);
35
- cmd.requiredOption('-p, --root-password <rootPassword>', '', process.env.INIT_ROOT_PASSWORD);
36
- cmd.option('-n, --root-nickname <rootNickname>');
37
- }
38
- this.db.registerOperators({
39
- $isCurrentUser(_, ctx) {
40
- return {
41
- [Op.eq]: ctx?.app?.ctx?.state?.currentUser?.id || -1,
42
- };
43
- },
44
- $isNotCurrentUser(_, ctx) {
45
- return {
46
- [Op.ne]: ctx?.app?.ctx?.state?.currentUser?.id || -1,
47
- };
48
- },
49
- $isVar(val, ctx) {
50
- const obj = parse({ val: `{{${val}}}` })(JSON.parse(JSON.stringify(ctx?.app?.ctx?.state)));
51
- return {
52
- [Op.eq]: obj.val,
53
- };
54
- },
55
- });
56
-
57
- this.db.on('afterDefineCollection', (collection: Collection) => {
58
- const { createdBy, updatedBy } = collection.options;
59
- if (createdBy === true) {
60
- collection.setField('createdById', {
61
- type: 'context',
62
- dataType: 'bigInt',
63
- dataIndex: 'state.currentUser.id',
64
- createOnly: true,
65
- visible: true,
66
- index: true,
67
- });
68
- collection.setField('createdBy', {
69
- type: 'belongsTo',
70
- target: 'users',
71
- foreignKey: 'createdById',
72
- targetKey: 'id',
73
- });
74
- }
75
- if (updatedBy === true) {
76
- collection.setField('updatedById', {
77
- type: 'context',
78
- dataType: 'bigInt',
79
- dataIndex: 'state.currentUser.id',
80
- visible: true,
81
- index: true,
82
- });
83
- collection.setField('updatedBy', {
84
- type: 'belongsTo',
85
- target: 'users',
86
- foreignKey: 'updatedById',
87
- targetKey: 'id',
88
- });
89
- }
90
- });
91
-
92
- for (const [key, action] of Object.entries(actions)) {
93
- this.app.resourcer.registerActionHandler(`users:${key}`, action);
94
- }
95
-
96
- // this.app.resourcer.use(parseToken, { tag: 'parseToken' });
97
-
98
- this.app.acl.addFixedParams('users', 'destroy', () => {
99
- return {
100
- filter: {
101
- 'id.$ne': 1,
102
- },
103
- };
104
- });
105
-
106
- this.app.acl.addFixedParams('collections', 'destroy', () => {
107
- return {
108
- filter: {
109
- 'name.$ne': 'users',
110
- },
111
- };
112
- });
113
-
114
- const publicActions = ['check', 'signin', 'signup', 'lostpassword', 'resetpassword', 'getUserByResetToken'];
115
- const loggedInActions = ['signout', 'updateProfile', 'changePassword'];
116
-
117
- publicActions.forEach((action) => this.app.acl.allow('users', action));
118
- loggedInActions.forEach((action) => this.app.acl.allow('users', action, 'loggedIn'));
119
-
120
- this.app.on('beforeStart', () => this.initVerification());
121
- }
122
-
123
- async load() {
124
- await this.db.import({
125
- directory: resolve(__dirname, 'collections'),
126
- });
127
-
128
- this.db.addMigrations({
129
- namespace: 'users',
130
- directory: resolve(__dirname, 'migrations'),
131
- context: {
132
- plugin: this,
133
- },
134
- });
135
-
136
- initAuthenticators(this);
137
- }
138
-
139
- getInstallingData(options: any = {}) {
140
- const { INIT_ROOT_NICKNAME, INIT_ROOT_PASSWORD, INIT_ROOT_EMAIL } = process.env;
141
- const {
142
- rootEmail = INIT_ROOT_EMAIL,
143
- rootPassword = INIT_ROOT_PASSWORD,
144
- rootNickname = INIT_ROOT_NICKNAME || 'Super Admin',
145
- } = options.users || options?.cliArgs?.[0] || {};
146
- return {
147
- rootEmail,
148
- rootPassword,
149
- rootNickname,
150
- };
151
- }
152
-
153
- async install(options) {
154
- const { rootNickname, rootPassword, rootEmail } = this.getInstallingData(options);
155
- const User = this.db.getCollection('users');
156
- if (await User.repository.findOne({ filter: { email: rootEmail } })) {
157
- return;
158
- }
159
-
160
- const user = await User.repository.create({
161
- values: {
162
- email: rootEmail,
163
- password: rootPassword,
164
- nickname: rootNickname,
165
- },
166
- });
167
-
168
- const repo = this.db.getRepository<any>('collections');
169
- if (repo) {
170
- await repo.db2cm('users');
171
- }
172
- }
173
-
174
- // TODO(module): should move to preset or dynamic configuration panel
175
- async initVerification() {
176
- const verificationPlugin = this.app.getPlugin('verification') as any;
177
- if (!verificationPlugin) {
178
- return;
179
- }
180
- const systemSettingsRepo = this.db.getRepository('systemSettings');
181
- const settings = await systemSettingsRepo.findOne();
182
- if (!settings.smsAuthEnabled) {
183
- return;
184
- }
185
-
186
- verificationPlugin.interceptors.register('users:signin', {
187
- manual: true,
188
- getReceiver(ctx) {
189
- return ctx.action.params.values.phone;
190
- },
191
- expiresIn: 120,
192
- validate: async (ctx, phone) => {
193
- if (!phone) {
194
- throw new Error(ctx.t('Not a valid cellphone number, please re-enter'));
195
- }
196
- const User = this.db.getCollection('users');
197
- const exists = await User.model.count({
198
- where: {
199
- phone,
200
- },
201
- });
202
- if (!exists) {
203
- throw new Error(ctx.t('The phone number is not registered, please register first', { ns: namespace }));
204
- }
205
-
206
- return true;
207
- },
208
- });
209
-
210
- verificationPlugin.interceptors.register('users:signup', {
211
- getReceiver(ctx) {
212
- return ctx.action.params.values.phone;
213
- },
214
- expiresIn: 120,
215
- validate: async (ctx, phone) => {
216
- if (!phone) {
217
- throw new Error(ctx.t('Not a valid cellphone number, please re-enter', { ns: namespace }));
218
- }
219
- const User = this.db.getCollection('users');
220
- const exists = await User.model.count({
221
- where: {
222
- phone,
223
- },
224
- });
225
- if (exists) {
226
- throw new Error(ctx.t('The phone number has been registered, please login directly', { ns: namespace }));
227
- }
228
-
229
- return true;
230
- },
231
- });
232
-
233
- this.authenticators.register('sms', (ctx, next) =>
234
- verificationPlugin.intercept(ctx, async () => {
235
- const { values } = ctx.action.params;
236
-
237
- const User = ctx.db.getCollection('users');
238
- const user = await User.model.findOne({
239
- where: {
240
- phone: values.phone,
241
- },
242
- });
243
- if (!user) {
244
- return ctx.throw(404, ctx.t('The phone number is incorrect, please re-enter', { ns: namespace }));
245
- }
246
-
247
- ctx.state.currentUser = user;
248
-
249
- return next();
250
- }),
251
- );
252
- }
253
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes