Browse Source

refcator: handler to service-controller - modelvisibility and verify view belongs to the project

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5239/head
Pranav C 2 years ago
parent
commit
50f8266167
  1. 1
      packages/nocodb-sdk/src/lib/Api.ts
  2. 115
      packages/nocodb/src/lib/controllers/modelVisibilityController.ts
  3. 0
      packages/nocodb/src/lib/meta/api/tableApis.ts
  4. 1
      packages/nocodb/src/lib/services/index.ts
  5. 97
      packages/nocodb/src/lib/services/modelVisibilityService.ts
  6. 2
      packages/nocodb/src/lib/services/viewService.ts
  7. 10
      packages/nocodb/src/schema/swagger.json

1
packages/nocodb-sdk/src/lib/Api.ts

@ -871,6 +871,7 @@ export interface UserInfoType {
}
export type VisibilityRuleReqType = {
id?: string | null;
disabled?: {
commenter?: BoolType;
creator?: BoolType;

115
packages/nocodb/src/lib/controllers/modelVisibilityController.ts

@ -1,122 +1,29 @@
import Model from '../models/Model';
import ModelRoleVisibility from '../models/ModelRoleVisibility';
import { Router } from 'express';
import { Tele } from 'nc-help';
import ncMetaAclMw from '../meta/helpers/ncMetaAclMw';
import { metaApiMetrics } from '../meta/helpers/apiMetrics';
import { getAjvValidatorMw } from '../meta/api/helpers';
import { modelVisibilityService } from '../services';
async function xcVisibilityMetaSetAll(req, res) {
Tele.emit('evt', { evt_type: 'uiAcl:updated' });
for (const d of req.body) {
for (const role of Object.keys(d.disabled)) {
const dataInDb = await ModelRoleVisibility.get({
role,
// fk_model_id: d.fk_model_id,
fk_view_id: d.id,
});
if (dataInDb) {
if (d.disabled[role]) {
if (!dataInDb.disabled) {
await ModelRoleVisibility.update(d.id, role, {
disabled: d.disabled[role],
});
}
} else {
await dataInDb.delete();
}
} else if (d.disabled[role]) {
await ModelRoleVisibility.insert({
fk_view_id: d.id,
disabled: d.disabled[role],
role,
});
}
}
}
Tele.emit('evt', { evt_type: 'uiAcl:updated' });
await modelVisibilityService.xcVisibilityMetaSetAll({
visibilityRule: req.body,
projectId: req.params.projectId,
});
res.json({ msg: 'success' });
}
// @ts-ignore
export async function xcVisibilityMetaGet(
projectId,
_models: Model[] = null,
includeM2M = true
// type: 'table' | 'tableAndViews' | 'views' = 'table'
) {
// todo: move to
const roles = ['owner', 'creator', 'viewer', 'editor', 'commenter', 'guest'];
const defaultDisabled = roles.reduce((o, r) => ({ ...o, [r]: false }), {});
let models =
_models ||
(await Model.list({
project_id: projectId,
base_id: undefined,
}));
models = includeM2M ? models : (models.filter((t) => !t.mm) as Model[]);
const result = await models.reduce(async (_obj, model) => {
const obj = await _obj;
// obj[model.id] = {
// tn: model.tn,
// _tn: model._tn,
// order: model.order,
// fk_model_id: model.id,
// id: model.id,
// type: model.type,
// disabled: { ...defaultDisabled }
// };
// if (type === 'tableAndViews') {
const views = await model.getViews();
for (const view of views) {
obj[view.id] = {
ptn: model.table_name,
_ptn: model.title,
ptype: model.type,
tn: view.title,
_tn: view.title,
table_meta: model.meta,
...view,
disabled: { ...defaultDisabled },
};
// }
}
return obj;
}, Promise.resolve({}));
const disabledList = await ModelRoleVisibility.list(projectId);
for (const d of disabledList) {
// if (d.fk_model_id) result[d.fk_model_id].disabled[d.role] = !!d.disabled;
// else if (type === 'tableAndViews' && d.fk_view_id)
if (result[d.fk_view_id])
result[d.fk_view_id].disabled[d.role] = !!d.disabled;
}
return Object.values(result);
// ?.sort(
// (a: any, b: any) =>
// (a.order || 0) - (b.order || 0) ||
// (a?._tn || a?.tn)?.localeCompare(b?._tn || b?.tn)
// );
}
const router = Router({ mergeParams: true });
router.get(
'/api/v1/db/meta/projects/:projectId/visibility-rules',
metaApiMetrics,
ncMetaAclMw(async (req, res) => {
res.json(
await xcVisibilityMetaGet(
req.params.projectId,
null,
req.query.includeM2M === true || req.query.includeM2M === 'true'
)
await modelVisibilityService.xcVisibilityMetaGet({
projectId: req.params.projectId,
includeM2M:
req.query.includeM2M === true || req.query.includeM2M === 'true',
})
);
}, 'modelVisibilityList')
);

0
packages/nocodb/src/lib/meta/api/tableApis.ts

1
packages/nocodb/src/lib/services/index.ts

@ -18,3 +18,4 @@ export * as gridViewColumnService from './gridViewColumnService';
export * as viewColumnService from './viewColumnService';
export * as metaDiffService from './metaDiffService';
export * as mapViewService from './mapViewService';
export * as modelVisibilityService from './modelVisibilityService';

97
packages/nocodb/src/lib/services/modelVisibilityService.ts

@ -0,0 +1,97 @@
import { VisibilityRuleReqType } from 'nocodb-sdk';
import { NcError } from '../meta/helpers/catchError';
import Model from '../models/Model';
import ModelRoleVisibility from '../models/ModelRoleVisibility';
import { Tele } from 'nc-help';
export async function xcVisibilityMetaSetAll(param: {
visibilityRule: VisibilityRuleReqType;
projectId: string;
}) {
Tele.emit('evt', { evt_type: 'uiAcl:updated' });
for (const d of param.visibilityRule) {
for (const role of Object.keys(d.disabled)) {
const view = await Model.get(d.id);
if (view.project_id !== param.projectId) {
NcError.badRequest('View does not belong to the project');
}
const dataInDb = await ModelRoleVisibility.get({
role,
fk_view_id: d.id,
});
if (dataInDb) {
if (d.disabled[role]) {
if (!dataInDb.disabled) {
await ModelRoleVisibility.update(d.id, role, {
disabled: d.disabled[role],
});
}
} else {
await dataInDb.delete();
}
} else if (d.disabled[role]) {
await ModelRoleVisibility.insert({
fk_view_id: d.id,
disabled: d.disabled[role],
role,
});
}
}
}
Tele.emit('evt', { evt_type: 'uiAcl:updated' });
return true;
}
export async function xcVisibilityMetaGet(param: {
projectId: string;
includeM2M?: boolean;
models?: Model[];
}) {
const { includeM2M = true, projectId, models: _models } = param ?? {};
// todo: move to
const roles = ['owner', 'creator', 'viewer', 'editor', 'commenter', 'guest'];
const defaultDisabled = roles.reduce((o, r) => ({ ...o, [r]: false }), {});
let models =
_models ||
(await Model.list({
project_id: projectId,
base_id: undefined,
}));
models = includeM2M ? models : (models.filter((t) => !t.mm) as Model[]);
const result = await models.reduce(async (_obj, model) => {
const obj = await _obj;
const views = await model.getViews();
for (const view of views) {
obj[view.id] = {
ptn: model.table_name,
_ptn: model.title,
ptype: model.type,
tn: view.title,
_tn: view.title,
table_meta: model.meta,
...view,
disabled: { ...defaultDisabled },
};
}
return obj;
}, Promise.resolve({}));
const disabledList = await ModelRoleVisibility.list(projectId);
for (const d of disabledList) {
if (result[d.fk_view_id])
result[d.fk_view_id].disabled[d.role] = !!d.disabled;
}
return Object.values(result);
}

2
packages/nocodb/src/lib/services/viewService.ts

@ -1,7 +1,7 @@
import { Model, View } from '../models';
import { Tele } from 'nc-help';
import { SharedViewReqType, ViewReqType } from 'nocodb-sdk';
import { xcVisibilityMetaGet } from '../meta/api/modelVisibilityApis';
import { xcVisibilityMetaGet } from './modelVisibilityService';
export async function viewList(param: {
tableId: string;

10
packages/nocodb/src/schema/swagger.json

@ -10328,6 +10328,16 @@
"items": {
"type": "object",
"properties": {
"id": {
"oneOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"disabled": {
"type": "object",
"properties": {

Loading…
Cancel
Save