2022-02-11 10:13:14 +00:00
|
|
|
import { Collection } from '@nocobase/database';
|
|
|
|
import { Plugin } from '@nocobase/server';
|
|
|
|
import { resolve } from 'path';
|
2021-12-08 01:10:44 +00:00
|
|
|
import * as actions from './actions/users';
|
|
|
|
import * as middlewares from './middlewares';
|
|
|
|
|
2022-02-11 10:13:14 +00:00
|
|
|
export default class UsersPlugin extends Plugin {
|
2022-02-06 17:14:00 +00:00
|
|
|
async beforeLoad() {
|
2022-02-11 10:13:14 +00:00
|
|
|
const {
|
|
|
|
adminNickname = 'Super Admin',
|
|
|
|
adminEmail = 'admin@nocobase.com',
|
|
|
|
adminPassword = 'admin123',
|
|
|
|
} = this.options;
|
|
|
|
|
2022-02-28 06:25:50 +00:00
|
|
|
this.app.on('installing', async (...args) => {
|
|
|
|
// TODO 暂时先这么写着,理想状态应该由 app.emitAsync('installing') 内部处理
|
|
|
|
await this.app.emitAsync('installing.beforeUsersPlugin', ...args);
|
2022-02-06 17:14:00 +00:00
|
|
|
const User = this.db.getCollection('users');
|
2022-02-28 06:25:50 +00:00
|
|
|
await User.repository.create({
|
|
|
|
values: {
|
|
|
|
nickname: adminNickname,
|
|
|
|
email: adminEmail,
|
|
|
|
password: adminPassword,
|
|
|
|
roles: ['admin'],
|
|
|
|
},
|
2021-12-08 01:10:44 +00:00
|
|
|
});
|
2022-02-28 06:25:50 +00:00
|
|
|
await this.app.emitAsync('installing.afterUsersPlugin', ...args);
|
2021-12-08 01:10:44 +00:00
|
|
|
});
|
|
|
|
|
2022-02-06 17:14:00 +00:00
|
|
|
this.db.on('users.afterCreateWithAssociations', async (model, options) => {
|
2022-01-30 02:37:27 +00:00
|
|
|
const { transaction } = options;
|
|
|
|
|
|
|
|
const defaultRole = await this.app.db.getRepository('roles').findOne({
|
|
|
|
filter: {
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (defaultRole && (await model.countRoles({ transaction })) == 0) {
|
|
|
|
await model.addRoles(defaultRole, { transaction });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-06 17:14:00 +00:00
|
|
|
this.db.on('afterDefineCollection', (collection: Collection) => {
|
2021-12-08 01:10:44 +00:00
|
|
|
let { createdBy, updatedBy } = collection.options;
|
|
|
|
if (createdBy === true) {
|
|
|
|
collection.setField('createdById', {
|
|
|
|
type: 'context',
|
|
|
|
dataType: 'integer',
|
|
|
|
dataIndex: 'state.currentUser.id',
|
|
|
|
createOnly: true,
|
|
|
|
});
|
|
|
|
collection.setField('createdBy', {
|
|
|
|
type: 'belongsTo',
|
|
|
|
target: 'users',
|
|
|
|
foreignKey: 'createdById',
|
|
|
|
targetKey: 'id',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (updatedBy === true) {
|
|
|
|
collection.setField('updatedById', {
|
|
|
|
type: 'context',
|
|
|
|
dataType: 'integer',
|
|
|
|
dataIndex: 'state.currentUser.id',
|
|
|
|
});
|
|
|
|
collection.setField('updatedBy', {
|
|
|
|
type: 'belongsTo',
|
|
|
|
target: 'users',
|
|
|
|
foreignKey: 'updatedById',
|
|
|
|
targetKey: 'id',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const [key, action] of Object.entries(actions)) {
|
2022-02-06 17:14:00 +00:00
|
|
|
this.app.resourcer.registerActionHandler(`users:${key}`, action);
|
2021-12-08 01:10:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 10:13:14 +00:00
|
|
|
this.app.resourcer.use(middlewares.parseToken());
|
2022-02-06 17:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
await this.db.import({
|
2022-02-11 10:13:14 +00:00
|
|
|
directory: resolve(__dirname, 'collections'),
|
2022-02-06 17:14:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|