2022-08-20 18:06:12 +08:00
|
|
|
import parse from 'json-templates';
|
2022-09-29 21:05:31 +08:00
|
|
|
import { resolve } from 'path';
|
2022-08-20 18:06:12 +08:00
|
|
|
|
2022-04-19 17:04:54 +08:00
|
|
|
import { Collection, Op } from '@nocobase/database';
|
2022-11-20 14:40:41 +08:00
|
|
|
import { HandlerType } from '@nocobase/resourcer';
|
2022-02-11 18:13:14 +08:00
|
|
|
import { Plugin } from '@nocobase/server';
|
2022-08-20 18:06:12 +08:00
|
|
|
import { Registry } from '@nocobase/utils';
|
|
|
|
|
2022-05-24 14:14:19 +08:00
|
|
|
import { namespace } from './';
|
2021-12-08 09:10:44 +08:00
|
|
|
import * as actions from './actions/users';
|
2022-09-29 21:05:31 +08:00
|
|
|
import initAuthenticators from './authenticators';
|
2022-04-09 14:54:46 +08:00
|
|
|
import { JwtOptions, JwtService } from './jwt-service';
|
2022-05-24 14:14:19 +08:00
|
|
|
import { enUS, zhCN } from './locale';
|
2022-09-13 23:27:19 +08:00
|
|
|
import { parseToken } from './middlewares';
|
2022-04-09 14:54:46 +08:00
|
|
|
|
|
|
|
export interface UserPluginConfig {
|
2022-10-27 13:00:16 +08:00
|
|
|
name?: string;
|
2022-04-09 14:54:46 +08:00
|
|
|
jwt: JwtOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class UsersPlugin extends Plugin<UserPluginConfig> {
|
|
|
|
public jwtService: JwtService;
|
|
|
|
|
2022-08-20 18:06:12 +08:00
|
|
|
public authenticators: Registry<HandlerType> = new Registry();
|
2022-07-25 19:33:23 +08:00
|
|
|
|
2022-04-09 14:54:46 +08:00
|
|
|
constructor(app, options) {
|
|
|
|
super(app, options);
|
2022-05-19 00:40:55 +08:00
|
|
|
this.jwtService = new JwtService(options?.jwt || {});
|
2022-04-09 14:54:46 +08:00
|
|
|
}
|
2021-12-08 09:10:44 +08:00
|
|
|
|
2022-02-28 21:49:50 +08:00
|
|
|
async beforeLoad() {
|
2022-05-24 14:14:19 +08:00
|
|
|
this.app.i18n.addResources('zh-CN', namespace, zhCN);
|
|
|
|
this.app.i18n.addResources('en-US', namespace, enUS);
|
2022-05-19 00:40:55 +08:00
|
|
|
const cmd = this.app.findCommand('install');
|
|
|
|
if (cmd) {
|
|
|
|
cmd.requiredOption('-e, --root-email <rootEmail>', '', process.env.INIT_ROOT_EMAIL);
|
|
|
|
cmd.requiredOption('-p, --root-password <rootPassword>', '', process.env.INIT_ROOT_PASSWORD);
|
2022-08-20 18:06:12 +08:00
|
|
|
cmd.option('-n, --root-nickname <rootNickname>');
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
2022-04-19 17:04:54 +08:00
|
|
|
this.db.registerOperators({
|
|
|
|
$isCurrentUser(_, ctx) {
|
|
|
|
return {
|
|
|
|
[Op.eq]: ctx?.app?.ctx?.state?.currentUser?.id || -1,
|
|
|
|
};
|
|
|
|
},
|
2022-07-04 17:50:18 +08:00
|
|
|
$isVar(val, ctx) {
|
|
|
|
const obj = parse({ val: `{{${val}}}` })(JSON.parse(JSON.stringify(ctx?.app?.ctx?.state)));
|
|
|
|
return {
|
|
|
|
[Op.eq]: obj.val,
|
|
|
|
};
|
|
|
|
},
|
2022-04-19 17:04:54 +08:00
|
|
|
});
|
2022-01-30 10:37:27 +08:00
|
|
|
|
2022-02-07 01:14:00 +08:00
|
|
|
this.db.on('afterDefineCollection', (collection: Collection) => {
|
2021-12-08 09:10:44 +08:00
|
|
|
let { createdBy, updatedBy } = collection.options;
|
|
|
|
if (createdBy === true) {
|
|
|
|
collection.setField('createdById', {
|
|
|
|
type: 'context',
|
2022-11-20 14:40:41 +08:00
|
|
|
dataType: 'bigInt',
|
2021-12-08 09:10:44 +08:00
|
|
|
dataIndex: 'state.currentUser.id',
|
|
|
|
createOnly: true,
|
2022-04-12 12:02:58 +08:00
|
|
|
visible: true,
|
2022-07-01 09:33:05 +08:00
|
|
|
index: true,
|
2021-12-08 09:10:44 +08:00
|
|
|
});
|
|
|
|
collection.setField('createdBy', {
|
|
|
|
type: 'belongsTo',
|
|
|
|
target: 'users',
|
|
|
|
foreignKey: 'createdById',
|
|
|
|
targetKey: 'id',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (updatedBy === true) {
|
|
|
|
collection.setField('updatedById', {
|
|
|
|
type: 'context',
|
2022-11-20 14:40:41 +08:00
|
|
|
dataType: 'bigInt',
|
2021-12-08 09:10:44 +08:00
|
|
|
dataIndex: 'state.currentUser.id',
|
2022-04-12 12:02:58 +08:00
|
|
|
visible: true,
|
2022-07-01 09:33:05 +08:00
|
|
|
index: true,
|
2021-12-08 09:10:44 +08:00
|
|
|
});
|
|
|
|
collection.setField('updatedBy', {
|
|
|
|
type: 'belongsTo',
|
|
|
|
target: 'users',
|
|
|
|
foreignKey: 'updatedById',
|
|
|
|
targetKey: 'id',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const [key, action] of Object.entries(actions)) {
|
2022-02-07 01:14:00 +08:00
|
|
|
this.app.resourcer.registerActionHandler(`users:${key}`, action);
|
2021-12-08 09:10:44 +08:00
|
|
|
}
|
|
|
|
|
2022-09-29 21:05:31 +08:00
|
|
|
this.app.resourcer.use(parseToken, { tag: 'parseToken' });
|
2022-03-11 10:10:57 +08:00
|
|
|
|
|
|
|
const publicActions = ['check', 'signin', 'signup', 'lostpassword', 'resetpassword', 'getUserByResetToken'];
|
2022-07-25 19:33:23 +08:00
|
|
|
const loggedInActions = ['signout', 'updateProfile', 'changePassword'];
|
2022-03-11 10:10:57 +08:00
|
|
|
|
2022-04-24 10:14:46 +08:00
|
|
|
publicActions.forEach((action) => this.app.acl.allow('users', action));
|
|
|
|
loggedInActions.forEach((action) => this.app.acl.allow('users', action, 'loggedIn'));
|
2022-11-28 16:41:58 +08:00
|
|
|
|
|
|
|
this.app.on('beforeStart', () => this.initVerification());
|
2022-02-07 01:14:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
await this.db.import({
|
2022-02-11 18:13:14 +08:00
|
|
|
directory: resolve(__dirname, 'collections'),
|
2022-02-07 01:14:00 +08:00
|
|
|
});
|
2022-08-20 18:06:12 +08:00
|
|
|
|
|
|
|
this.db.addMigrations({
|
|
|
|
namespace: 'users',
|
|
|
|
directory: resolve(__dirname, 'migrations'),
|
|
|
|
context: {
|
|
|
|
plugin: this,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
initAuthenticators(this);
|
2022-02-07 01:14:00 +08:00
|
|
|
}
|
2022-02-28 22:10:04 +08:00
|
|
|
|
2022-05-19 00:40:55 +08:00
|
|
|
getInstallingData(options: any = {}) {
|
|
|
|
const { INIT_ROOT_NICKNAME, INIT_ROOT_PASSWORD, INIT_ROOT_EMAIL } = process.env;
|
2022-02-28 22:10:04 +08:00
|
|
|
const {
|
2022-05-19 00:40:55 +08:00
|
|
|
rootEmail = INIT_ROOT_EMAIL,
|
|
|
|
rootPassword = INIT_ROOT_PASSWORD,
|
|
|
|
rootNickname = INIT_ROOT_NICKNAME || 'Super Admin',
|
|
|
|
} = options.users || options?.cliArgs?.[0] || {};
|
2022-03-11 10:10:57 +08:00
|
|
|
return {
|
2022-05-19 00:40:55 +08:00
|
|
|
rootEmail,
|
|
|
|
rootPassword,
|
|
|
|
rootNickname,
|
2022-03-11 10:10:57 +08:00
|
|
|
};
|
|
|
|
}
|
2022-04-09 14:54:46 +08:00
|
|
|
|
2022-05-19 00:40:55 +08:00
|
|
|
async install(options) {
|
|
|
|
const { rootNickname, rootPassword, rootEmail } = this.getInstallingData(options);
|
2022-02-28 22:10:04 +08:00
|
|
|
const User = this.db.getCollection('users');
|
2022-07-25 19:33:23 +08:00
|
|
|
const user = await User.repository.create({
|
2022-02-28 22:10:04 +08:00
|
|
|
values: {
|
2022-05-19 00:40:55 +08:00
|
|
|
email: rootEmail,
|
|
|
|
password: rootPassword,
|
2022-09-29 21:05:31 +08:00
|
|
|
nickname: rootNickname,
|
2022-02-28 22:10:04 +08:00
|
|
|
},
|
|
|
|
});
|
2022-04-09 14:54:46 +08:00
|
|
|
|
2022-03-02 18:35:49 +08:00
|
|
|
const repo = this.db.getRepository<any>('collections');
|
|
|
|
if (repo) {
|
|
|
|
await repo.db2cm('users');
|
|
|
|
}
|
2022-02-28 22:10:04 +08:00
|
|
|
}
|
2022-11-28 16:41:58 +08:00
|
|
|
|
|
|
|
// TODO(module): should move to preset or dynamic configuration panel
|
|
|
|
async initVerification() {
|
|
|
|
const verificationPlugin = this.app.getPlugin('verification') as any;
|
|
|
|
if (!verificationPlugin) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
verificationPlugin.interceptors.register('users:signin', {
|
|
|
|
manual: true,
|
|
|
|
getReceiver(ctx) {
|
|
|
|
return ctx.action.params.values.phone;
|
|
|
|
},
|
|
|
|
expiresIn: 120,
|
|
|
|
validate: async (ctx, phone) => {
|
|
|
|
if (!phone) {
|
|
|
|
throw new Error(ctx.t('Not a valid cellphone number, please re-enter'));
|
|
|
|
}
|
|
|
|
const User = this.db.getCollection('users');
|
|
|
|
const exists = await User.model.count({
|
|
|
|
where: {
|
|
|
|
phone,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!exists) {
|
|
|
|
throw new Error(ctx.t('The phone number is not registered, please register first', { ns: namespace }));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
verificationPlugin.interceptors.register('users:signup', {
|
|
|
|
getReceiver(ctx) {
|
|
|
|
return ctx.action.params.values.phone;
|
|
|
|
},
|
|
|
|
expiresIn: 120,
|
|
|
|
validate: async (ctx, phone) => {
|
|
|
|
if (!phone) {
|
|
|
|
throw new Error(ctx.t('Not a valid cellphone number, please re-enter', { ns: namespace }));
|
|
|
|
}
|
|
|
|
const User = this.db.getCollection('users');
|
|
|
|
const exists = await User.model.count({
|
|
|
|
where: {
|
|
|
|
phone,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (exists) {
|
|
|
|
throw new Error(ctx.t('The phone number has been registered, please login directly', { ns: namespace }));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this.authenticators.register('sms', (ctx, next) =>
|
|
|
|
verificationPlugin.intercept(ctx, async () => {
|
|
|
|
const { values } = ctx.action.params;
|
|
|
|
|
|
|
|
const User = ctx.db.getCollection('users');
|
|
|
|
const user = await User.model.findOne({
|
|
|
|
where: {
|
|
|
|
phone: values.phone,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!user) {
|
|
|
|
return ctx.throw(404, ctx.t('The phone number is incorrect, please re-enter', { ns: namespace }));
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.state.currentUser = user;
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2022-02-07 01:14:00 +08:00
|
|
|
}
|