nocobase/packages/plugins/users/src/server.ts

137 lines
3.7 KiB
TypeScript
Raw Normal View History

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 { JwtOptions, JwtService } from './jwt-service';
2022-04-09 07:30:43 +00:00
import * as middlewares from './middlewares';
2022-04-10 14:05:18 +00:00
import { UserModel } from './models/UserModel';
export interface UserPluginConfig {
jwt: JwtOptions;
Feat/create nocobase app (#273) * create-nocobase-app template from [develop] * change create-nocobase-app package.json config * feat: load configuration from directory * feat: configuration repository toObject * feat: create application from configuration dir * feat: application factory with plugins options * export type * feat: read application config & application with plugins options * feat: release command * fix: database release * chore: workflow package.json * feat: nocobase cli package * feat: console command * chore: load application in command * fix: load packages from process.cwd * feat: cli load env file * feat: create-nocobase-app * fix: gitignore create-nocobase-app lib * fix: sqlite path * feat: create plugin * chore: plugin files template * chore: move cli into application * chore: create-nocobase-app * fix: create plugin * chore: app-client && app-server * chore: package.json * feat: create-nocobase-app download template from npm * chore: create-nocobase-app template * fix: config of plugin-users * fix: yarn.lock * fix: database build error * fix: yarn.lock * fix: resourcer config * chore: cross-env * chore: app-client dependents * fix: env * chore: v0.6.0-alpha.1 * chore: verdaccio * chore(versions): 😊 publish v0.6.0 * chore(versions): 😊 publish v0.6.1-alpha.0 * chore(versions): 😊 publish v0.6.2-alpha.0 * chore(versions): 😊 publish v0.6.2-alpha.1 * chore: 0.6.2-alpha.2 * feat: workspaces * chore(versions): 😊 publish v0.6.2-alpha.3 * chore(versions): 😊 publish v0.6.2-alpha.4 * chore: create-nocobase-app * chore: create-nocobase-app lib * fix: update tsconfig.jest.json * chore: .env * chore(versions): 😊 publish v0.6.2-alpha.5 * chore(versions): 😊 publish v0.6.2-alpha.6 * feat: improve code * chore(versions): 😊 publish v0.6.2-alpha.7 * fix: cleanup * chore(versions): 😊 publish v0.6.2-alpha.8 * chore: tsconfig for app server package * fix: move files * fix: move files Co-authored-by: chenos <chenlinxh@gmail.com>
2022-04-17 02:00:42 +00:00
installing?: {
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 01:10:44 +00:00
async beforeLoad() {
2022-04-10 14:05:18 +00:00
this.db.registerModels({ UserModel });
this.db.on('users.afterCreateWithAssociations', async (model, options) => {
const { transaction } = options;
2022-04-10 14:54:05 +00: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 });
}
});
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,
visible: true,
2021-12-08 01:10:44 +00: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',
visible: true,
2021-12-08 01:10:44 +00:00
});
collection.setField('updatedBy', {
type: 'belongsTo',
target: 'users',
foreignKey: 'updatedById',
targetKey: 'id',
});
}
});
for (const [key, action] of Object.entries(actions)) {
this.app.resourcer.registerActionHandler(`users:${key}`, action);
2021-12-08 01:10:44 +00:00
}
2022-04-09 07:30:43 +00:00
this.app.resourcer.use(middlewares.parseToken({ plugin: this }));
const publicActions = ['check', 'signin', 'signup', 'lostpassword', 'resetpassword', 'getUserByResetToken'];
const loggedInActions = ['signout', 'updateProfile', 'changePassword', 'setDefaultRole'];
publicActions.forEach((action) => this.app.acl.skip('users', action));
loggedInActions.forEach((action) => this.app.acl.skip('users', action, 'logged-in'));
}
async load() {
await this.db.import({
2022-02-11 10:13:14 +00:00
directory: resolve(__dirname, 'collections'),
});
}
2022-02-28 14:10:04 +00:00
getRootUserInfo() {
2022-02-28 14:10:04 +00:00
const {
adminNickname = 'Super Admin',
adminEmail = 'admin@nocobase.com',
adminPassword = 'admin123',
2022-04-09 07:30:43 +00:00
} = this.options.installing || {};
2022-02-28 14:10:04 +00:00
return {
adminNickname,
adminEmail,
adminPassword,
};
}
async install() {
const { adminNickname, adminPassword, adminEmail } = this.getRootUserInfo();
2022-02-28 14:10:04 +00:00
const User = this.db.getCollection('users');
2022-04-10 14:05:18 +00:00
const user = await User.repository.create<UserModel>({
2022-02-28 14:10:04 +00:00
values: {
nickname: adminNickname,
email: adminEmail,
password: adminPassword,
roles: ['root', 'admin'],
2022-02-28 14:10:04 +00:00
},
});
2022-04-10 14:05:18 +00:00
await user.setDefaultRole('root');
2022-03-02 10:35:49 +00:00
const repo = this.db.getRepository<any>('collections');
if (repo) {
await repo.db2cm('users');
}
2022-02-28 14:10:04 +00:00
}
getName(): string {
return this.getPackageName(__dirname);
}
}