2022-04-19 17:04:54 +08:00
|
|
|
import { Collection, Op } from '@nocobase/database';
|
2022-02-11 18:13:14 +08:00
|
|
|
import { Plugin } from '@nocobase/server';
|
|
|
|
import { resolve } from 'path';
|
2021-12-08 09:10:44 +08:00
|
|
|
import * as actions from './actions/users';
|
2022-04-09 14:54:46 +08:00
|
|
|
import { JwtOptions, JwtService } from './jwt-service';
|
2022-04-09 15:30:43 +08:00
|
|
|
import * as middlewares from './middlewares';
|
2022-04-10 22:05:18 +08:00
|
|
|
import { UserModel } from './models/UserModel';
|
2022-04-09 14:54:46 +08:00
|
|
|
|
|
|
|
export interface UserPluginConfig {
|
|
|
|
jwt: JwtOptions;
|
|
|
|
|
2022-04-17 10:00:42 +08:00
|
|
|
installing?: {
|
2022-04-09 14:54:46 +08:00
|
|
|
adminNickname: string;
|
|
|
|
adminEmail: string;
|
|
|
|
adminPassword: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class UsersPlugin extends Plugin<UserPluginConfig> {
|
|
|
|
public jwtService: JwtService;
|
|
|
|
|
|
|
|
constructor(app, options) {
|
|
|
|
super(app, options);
|
|
|
|
this.jwtService = new JwtService(options?.jwt);
|
|
|
|
}
|
2021-12-08 09:10:44 +08:00
|
|
|
|
2022-02-28 21:49:50 +08:00
|
|
|
async beforeLoad() {
|
2022-04-19 17:04:54 +08:00
|
|
|
this.db.registerOperators({
|
|
|
|
$isCurrentUser(_, ctx) {
|
|
|
|
return {
|
|
|
|
[Op.eq]: ctx?.app?.ctx?.state?.currentUser?.id || -1,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
2022-04-10 22:05:18 +08:00
|
|
|
this.db.registerModels({ UserModel });
|
2022-02-07 01:14:00 +08:00
|
|
|
this.db.on('users.afterCreateWithAssociations', async (model, options) => {
|
2022-01-30 10:37:27 +08:00
|
|
|
const { transaction } = options;
|
2022-04-10 22:54:05 +08:00
|
|
|
const repository = this.app.db.getRepository('roles');
|
|
|
|
if (!repository) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const defaultRole = await repository.findOne({
|
|
|
|
filter: {
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
if (defaultRole && (await model.countRoles({ transaction })) == 0) {
|
|
|
|
await model.addRoles(defaultRole, { transaction });
|
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',
|
|
|
|
dataType: 'integer',
|
|
|
|
dataIndex: 'state.currentUser.id',
|
|
|
|
createOnly: true,
|
2022-04-12 12:02:58 +08:00
|
|
|
visible: true,
|
2022-04-21 18:08:33 +08:00
|
|
|
onDelete: 'SET NULL',
|
|
|
|
onUpdate: 'CASCADE',
|
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',
|
|
|
|
dataType: 'integer',
|
|
|
|
dataIndex: 'state.currentUser.id',
|
2022-04-12 12:02:58 +08:00
|
|
|
visible: true,
|
2022-04-21 18:08:33 +08:00
|
|
|
onDelete: 'SET NULL',
|
|
|
|
onUpdate: 'CASCADE',
|
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-04-09 15:30:43 +08:00
|
|
|
this.app.resourcer.use(middlewares.parseToken({ plugin: this }));
|
2022-03-11 10:10:57 +08:00
|
|
|
|
|
|
|
const publicActions = ['check', 'signin', 'signup', 'lostpassword', 'resetpassword', 'getUserByResetToken'];
|
|
|
|
const loggedInActions = ['signout', 'updateProfile', 'changePassword', 'setDefaultRole'];
|
|
|
|
|
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-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-02-28 22:10:04 +08:00
|
|
|
|
2022-03-11 10:10:57 +08:00
|
|
|
getRootUserInfo() {
|
2022-02-28 22:10:04 +08:00
|
|
|
const {
|
|
|
|
adminNickname = 'Super Admin',
|
|
|
|
adminEmail = 'admin@nocobase.com',
|
|
|
|
adminPassword = 'admin123',
|
2022-04-09 15:30:43 +08:00
|
|
|
} = this.options.installing || {};
|
2022-02-28 22:10:04 +08:00
|
|
|
|
2022-03-11 10:10:57 +08:00
|
|
|
return {
|
|
|
|
adminNickname,
|
|
|
|
adminEmail,
|
|
|
|
adminPassword,
|
|
|
|
};
|
|
|
|
}
|
2022-04-09 14:54:46 +08:00
|
|
|
|
2022-03-11 10:10:57 +08:00
|
|
|
async install() {
|
|
|
|
const { adminNickname, adminPassword, adminEmail } = this.getRootUserInfo();
|
|
|
|
|
2022-02-28 22:10:04 +08:00
|
|
|
const User = this.db.getCollection('users');
|
2022-04-10 22:05:18 +08:00
|
|
|
const user = await User.repository.create<UserModel>({
|
2022-02-28 22:10:04 +08:00
|
|
|
values: {
|
|
|
|
nickname: adminNickname,
|
|
|
|
email: adminEmail,
|
|
|
|
password: adminPassword,
|
2022-04-10 19:22:39 +08:00
|
|
|
roles: ['root', 'admin'],
|
2022-02-28 22:10:04 +08:00
|
|
|
},
|
|
|
|
});
|
2022-04-09 14:54:46 +08:00
|
|
|
|
2022-04-10 22:05:18 +08:00
|
|
|
await user.setDefaultRole('root');
|
|
|
|
|
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-03-28 22:01:10 +08:00
|
|
|
|
|
|
|
getName(): string {
|
|
|
|
return this.getPackageName(__dirname);
|
|
|
|
}
|
2022-02-07 01:14:00 +08:00
|
|
|
}
|