tachybase_todo/packages/plugin-pages/src/server.ts
Junyi bd756a6a5c
Feature: add permission plugin api (#57)
* fix: update method of permission

* 页面访问权限

* feat: add tabs update in role

* refactor: change main action permission check into single file

* feat: add pluginLoaded event listener for init page permissions

* refactor: change to can api

* refactor: use constants for role types

* refactor: can api

* 只处理 actions 表里的权限,其余跳过

* 注释掉 fields

* bugfix

* add: 详情模块编辑按钮权限判断

* test: add cases

* fix: add association permission judgment

* fix: add disabled property to drawer select

Co-authored-by: chenos <chenlinxh@gmail.com>
2021-01-20 08:33:47 +08:00

152 lines
4.3 KiB
TypeScript

import path from 'path';
import Database from '@nocobase/database';
import Resourcer from '@nocobase/resourcer';
import getCollection from './actions/getCollection';
import getView from './actions/getView';
import getRoutes from './actions/getRoutes';
import getPageInfo from './actions/getPageInfo';
import * as rolesPagesActions from './actions/roles.pages';
export default async function (options = {}) {
const database: Database = this.database;
const resourcer: Resourcer = this.resourcer;
database.import({
directory: path.resolve(__dirname, 'collections'),
});
resourcer.registerActionHandler('getCollection', getCollection);
resourcer.registerActionHandler('getView', getView);
resourcer.registerActionHandler('getPageInfo', getPageInfo);
resourcer.registerActionHandler('pages:getRoutes', getRoutes);
Object.keys(rolesPagesActions).forEach(actionName => {
resourcer.registerActionHandler(`roles.pages:${actionName}`, rolesPagesActions[actionName]);
});
/*
const [Collection, Page, View] = database.getModels(['collections', 'pages', 'views']);
async function createCollectionPage(model, options) {
// const {
// transaction = await database.sequelize.transaction(),
// } = options;
if (model.get('internal')) {
return;
}
const transaction = await database.sequelize.transaction();
const parent = await Page.findOne({
transaction,
where: {
path: '/collections',
}
});
let page = await Page.findOne({
transaction,
where: {
collection: model.get('name'),
path: `/collections/${model.get('name')}`,
},
});
if (!page) {
page = await Page.create({
type: 'collection',
collection: model.get('name'),
path: `/collections/${model.get('name')}`,
sort: 100,
parent_id: parent.id,
}, {
transaction,
});
}
page.set({
title: model.get('title'),
icon: model.get('icon'),
showInMenu: !!model.get('showInDataMenu'),
});
await page.save({
transaction,
});
await Page.collectionPagesResort({transaction});
await transaction.commit();
}
Collection.addHook('afterCreate', createCollectionPage);
Collection.addHook('afterUpdate', createCollectionPage);
Collection.addHook('afterDestroy', async (model, options) => {
const { transaction } = options;
// console.log('afterDestroy', model);
await Page.destroy({
transaction,
where: {
path: `/collections/${model.get('name')}`,
},
});
await Page.collectionPagesResort({transaction});
});
async function syncViewCollectionPage(model, options) {
const transaction = await database.sequelize.transaction();
const parentPath = `/collections/${model.get('collection_name')}`;
const currentPath = `${parentPath}/views/${model.get('name')}`;
try {
const parent = await Page.findOne({
transaction,
where: {
path: parentPath,
},
});
if (!parent) {
await transaction.rollback();
return;
}
let page = await Page.findOne({
transaction,
where: {
collection: model.get('collection_name'),
path: currentPath,
},
});
if (!page) {
page = await Page.create({
type: 'collection',
collection: model.get('collection_name'),
path: currentPath,
sort: 100,
parent_id: parent.id,
}, {
transaction,
});
}
page.set({
title: model.get('title'),
viewName: model.get('name'),
viewId: model.get('id'),
// icon: model.get('icon'),
showInMenu: !!model.get('showInDataMenu'),
});
await page.save({
transaction,
});
} catch (error) {
await transaction.rollback();
console.error(error);
}
await transaction.commit();
}
View.addHook('beforeValidate', (model) => {
if (model.get('default')) {
model.set('showInDataMenu', true);
}
});
View.addHook('afterCreate', syncViewCollectionPage);
View.addHook('afterUpdate', syncViewCollectionPage);
View.addHook('afterDestroy', async (model, options) => {
await Page.destroy({
where: {
viewId: model.get('id'),
},
});
});
*/
}