adminizer 4.1.0-build.14 → 4.1.0-build.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,7 +21,7 @@ export default async function add(req, res) {
21
21
  return res.sendStatus(403);
22
22
  }
23
23
  }
24
- let dataAccessor = new DataAccessor(req, entity, "add");
24
+ let dataAccessor = new DataAccessor(req.adminizer, req.user, entity, "add");
25
25
  let fields = dataAccessor.getFieldsConfig();
26
26
  // add deprecated 'records' to config
27
27
  fields = await FieldsHelper.loadAssociations(req, fields, "add");
@@ -30,7 +30,7 @@ export default async function edit(req, res) {
30
30
  let dataAccessor;
31
31
  try {
32
32
  const id = req.params.id;
33
- dataAccessor = new DataAccessor(req, entity, "edit");
33
+ dataAccessor = new DataAccessor(req.adminizer, req.user, entity, "edit");
34
34
  record = await entity.model.findOne({ id: id }, dataAccessor);
35
35
  if (!record)
36
36
  return res.status(404).send("Adminpanel > Record not found");
@@ -16,7 +16,7 @@ export default async function list(req, res) {
16
16
  return res.sendStatus(403);
17
17
  }
18
18
  }
19
- let dataAccessor = new DataAccessor(req, entity, "list");
19
+ let dataAccessor = new DataAccessor(req.adminizer, req.user, entity, "list");
20
20
  let fields = dataAccessor.getFieldsConfig();
21
21
  const header = inertiaListHelper(entity, req, fields);
22
22
  let start = "0";
@@ -7,7 +7,7 @@ export default async function edit(req, res) {
7
7
  }
8
8
  }
9
9
  let entity = ControllerHelper.findEntityObject(req);
10
- let dataAccessor = new DataAccessor(req, entity, "edit");
10
+ let dataAccessor = new DataAccessor(req.adminizer, req.user, entity, "edit");
11
11
  let record = await entity.model.findOne({ id: req.params.id }, dataAccessor);
12
12
  return res.redirect(`${req.adminizer.config.routePrefix}/catalog/navigation/${record.label}`);
13
13
  }
@@ -31,7 +31,7 @@ export default async function remove(req, res) {
31
31
  let record;
32
32
  let dataAccessor;
33
33
  try {
34
- dataAccessor = new DataAccessor(req, entity, "remove");
34
+ dataAccessor = new DataAccessor(req.adminizer, req.user, entity, "remove");
35
35
  record = await entity.model.findOne({ id: req.params.id }, dataAccessor);
36
36
  }
37
37
  catch (e) {
@@ -25,7 +25,7 @@ export default async function view(req, res) {
25
25
  return res.sendStatus(403);
26
26
  }
27
27
  }
28
- let dataAccessor = new DataAccessor(req, entity, "view");
28
+ let dataAccessor = new DataAccessor(req.adminizer, req.user, entity, "view");
29
29
  let fields = dataAccessor.getFieldsConfig();
30
30
  let record;
31
31
  try {
@@ -6,6 +6,7 @@ export class AccessRightsHelper {
6
6
  this.adminizer = adminizer;
7
7
  }
8
8
  registerToken(accessRightsToken) {
9
+ accessRightsToken.id = accessRightsToken.id.toLowerCase();
9
10
  if (!accessRightsToken.id || !accessRightsToken.name || !accessRightsToken.description || !accessRightsToken.department) {
10
11
  throw new Error("Adminpanel > Can not register token: Missed one or more required parameters");
11
12
  }
@@ -42,6 +43,11 @@ export class AccessRightsHelper {
42
43
  return tokens.some((token) => this.hasPermission(token, user));
43
44
  }
44
45
  hasPermission(tokenId, user) {
46
+ if (!tokenId) {
47
+ Adminizer.log.error(`AccessRightsHelper > hasPermission no tokenId: ${tokenId}`);
48
+ return false;
49
+ }
50
+ tokenId = tokenId.toLowerCase();
45
51
  if (!this.adminizer.config.auth.enable) {
46
52
  return true;
47
53
  }
@@ -42,7 +42,7 @@ export class FieldsHelper {
42
42
  name: modelName, config: req.adminizer.config.models[modelName],
43
43
  model: Model, uri: `${req.adminizer.config.routePrefix}/model/${modelName}`, type: "model"
44
44
  };
45
- let dataAccessor = new DataAccessor(req, entity, action);
45
+ let dataAccessor = new DataAccessor(req.adminizer, req.user, entity, action);
46
46
  list = await Model.find({}, dataAccessor);
47
47
  }
48
48
  catch (e) {
@@ -249,7 +249,7 @@ export interface ModelConfig {
249
249
  * May be association or association-many to UserAP or GroupAP */
250
250
  userAccessRelation?: {
251
251
  field: string;
252
- via: string;
252
+ via?: string;
253
253
  } | string;
254
254
  userAccessRelationCallback?: (userWithGroups: UserWithGroups, record: any) => boolean;
255
255
  }
@@ -390,7 +390,7 @@ export interface HrefConfig {
390
390
  /**
391
391
  * Only for view, controller still uses his own access rights token
392
392
  * */
393
- accessRightsToken?: string;
393
+ accessRightsToken?: string | undefined;
394
394
  /**
395
395
  * For menu items only
396
396
  * */
@@ -4,6 +4,7 @@
4
4
  import { Entity } from "../../interfaces/types";
5
5
  import { ActionType } from "../../interfaces/adminpanelConfig";
6
6
  import { Fields } from "../../helpers/fieldsHelper";
7
+ import { Adminizer } from "../Adminizer";
7
8
  import { UserAP } from "models/UserAP";
8
9
  export declare class DataAccessor {
9
10
  private readonly adminizer;
@@ -11,7 +12,8 @@ export declare class DataAccessor {
11
12
  entity: Entity;
12
13
  action: ActionType;
13
14
  private fields;
14
- constructor(req: ReqType, entity: Entity, action: ActionType);
15
+ private actionVerb;
16
+ constructor(adminizer: Adminizer, user: UserAP, entity: Entity, action: ActionType);
15
17
  /**
16
18
  * Retrieves the fields for the given entity based on action type,
17
19
  * taking into account access rights and configuration settings.
@@ -6,12 +6,14 @@ export class DataAccessor {
6
6
  entity;
7
7
  action;
8
8
  fields = null;
9
+ actionVerb;
9
10
  // TODO: change req to adminizr + user
10
- constructor(req, entity, action) {
11
- this.adminizer = req.adminizer;
12
- this.user = req.user;
11
+ constructor(adminizer, user, entity, action) {
12
+ this.adminizer = adminizer;
13
+ this.user = user;
13
14
  this.entity = entity;
14
15
  this.action = action;
16
+ this.actionVerb = getTokenAction(this.action);
15
17
  }
16
18
  /**
17
19
  * Retrieves the fields for the given entity based on action type,
@@ -29,6 +31,11 @@ export class DataAccessor {
29
31
  const actionConfig = ControllerHelper.findActionConfig(this.entity, this.action);
30
32
  const fieldsConfig = this.entity.config?.fields || {};
31
33
  const modelAttributes = this.entity.model.attributes;
34
+ const tokenId = `${this.actionVerb}-${this.entity.model.modelname}-${this.entity.type}`;
35
+ if (!this.adminizer.accessRightsHelper.hasPermission(tokenId, this.user)) {
36
+ Adminizer.log.debug(`No access rights to ${this.entity.type}: ${this.entity.model.modelname}`);
37
+ return undefined;
38
+ }
32
39
  const result = {};
33
40
  Object.entries(modelAttributes).forEach(([key, modelField]) => {
34
41
  // Checks for short type in Waterline: fieldName: 'string'
@@ -67,6 +74,12 @@ export class DataAccessor {
67
74
  let populatedModelFieldsConfig = {};
68
75
  if (modelField.type === "association" || modelField.type === "association-many") {
69
76
  const modelName = modelField.model || modelField.collection;
77
+ // Todo: test > hide relation without model token
78
+ const tokenId = `${this.actionVerb}-${modelName}-${this.entity.type}`;
79
+ if (!this.adminizer.accessRightsHelper.hasPermission(tokenId, this.user)) {
80
+ Adminizer.log.silly(`No access rights to ${this.entity.type}: ${this.entity.model.modelname}`);
81
+ return undefined;
82
+ }
70
83
  if (modelName) {
71
84
  const model = this.adminizer.modelHandler.model.get(modelName);
72
85
  if (model) {
@@ -95,10 +108,9 @@ export class DataAccessor {
95
108
  return undefined;
96
109
  }
97
110
  // Check if user has access to the associated model
98
- const actionVerb = getTokenAction(this.action);
99
- const tokenId = `${actionVerb}-${modelName}-${this.entity.type}`;
111
+ const tokenId = `${this.actionVerb}-${modelName}-${this.entity.type}`;
100
112
  if (!this.adminizer.accessRightsHelper.hasPermission(tokenId, this.user)) {
101
- Adminizer.log.warn(`No access rights to ${this.entity.type}: ${modelName}`);
113
+ Adminizer.log.debug(`No access rights to ${this.entity.type}: ${modelName}`);
102
114
  return undefined;
103
115
  }
104
116
  const associatedFields = {};
package/lib/v4/I18n.js CHANGED
@@ -104,7 +104,9 @@ export class I18n {
104
104
  return;
105
105
  const file = this.locateFile(locale);
106
106
  const tmpFile = `${file}.tmp`;
107
+ const dir = path.dirname(file);
107
108
  try {
109
+ fs.mkdirSync(dir, { recursive: true });
108
110
  fs.writeFileSync(tmpFile, JSON.stringify(I18n.locales[locale], null, this.indent), "utf8");
109
111
  fs.renameSync(tmpFile, file);
110
112
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "adminizer",
3
3
  "type": "module",
4
- "version": "4.1.0-build.14",
4
+ "version": "4.1.0-build.16",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "vitest --reporter verbose",