@drax/settings-back 0.28.0 → 0.30.0

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.
@@ -1,15 +1,21 @@
1
1
  import { SettingPermissions } from "../permissions/SettingPermissions.js";
2
2
  import SettingServiceFactory from "../factory/SettingServiceFactory.js";
3
- import { UnauthorizedError, ValidationError } from "@drax/common-back";
3
+ import { NotFoundError, UnauthorizedError, ValidationError } from "@drax/common-back";
4
4
  class SettingController {
5
5
  constructor() {
6
6
  this.service = SettingServiceFactory();
7
7
  }
8
8
  async fetchAll(request, reply) {
9
9
  try {
10
- request.rbac.assertPermission(SettingPermissions.View);
11
10
  const settings = await this.service.fetchAll();
12
- return settings;
11
+ if (!request.authUser) {
12
+ return settings.filter(s => s.public === true && !s.permission);
13
+ }
14
+ else {
15
+ return settings.filter(s => {
16
+ return !s.permission || (s.permission && request.rbac.hasPermission(s.permission));
17
+ });
18
+ }
13
19
  }
14
20
  catch (e) {
15
21
  console.error(e);
@@ -25,7 +31,7 @@ class SettingController {
25
31
  }
26
32
  async fetchGrouped(request, reply) {
27
33
  try {
28
- request.rbac.assertPermission(SettingPermissions.View);
34
+ request.rbac.assertPermission(SettingPermissions.Manage);
29
35
  const settings = await this.service.fetchGrouped();
30
36
  return settings;
31
37
  }
@@ -43,9 +49,17 @@ class SettingController {
43
49
  }
44
50
  async findByKey(request, reply) {
45
51
  try {
46
- request.rbac.assertPermission(SettingPermissions.View);
47
52
  const key = request.params.key;
48
53
  const setting = await this.service.findByKey(key);
54
+ if (!setting) {
55
+ throw new NotFoundError();
56
+ }
57
+ if (setting.public === false && !request.authUser) {
58
+ throw new UnauthorizedError();
59
+ }
60
+ if (setting.permission && !request.rbac.hasPermission(setting.permission)) {
61
+ throw new UnauthorizedError();
62
+ }
49
63
  return setting;
50
64
  }
51
65
  catch (e) {
@@ -54,6 +68,10 @@ class SettingController {
54
68
  reply.statusCode = e.statusCode;
55
69
  reply.send({ error: e.message });
56
70
  }
71
+ if (e instanceof NotFoundError) {
72
+ reply.statusCode = e.statusCode;
73
+ reply.send({ error: e.message });
74
+ }
57
75
  else {
58
76
  reply.statusCode = 500;
59
77
  reply.send({ error: 'INTERNAL_SERVER_ERROR' });
@@ -3,18 +3,19 @@ import uniqueValidator from 'mongoose-unique-validator';
3
3
  import mongooseLeanVirtuals from 'mongoose-lean-virtuals';
4
4
  const SettingSchema = new mongoose.Schema({
5
5
  key: { type: String, required: true, unique: true },
6
- value: { type: mongoose.Schema.Types.Mixed, required: false, unique: false },
7
- //valueList: [{type: String, required: false, unique: false}],
8
- label: { type: String, required: false },
9
- category: { type: String, required: true },
10
- type: { type: String, default: "string", enum: ['string', 'longString', 'number', 'enum', 'boolean', 'password', 'stringList', 'numberList', 'enumList', 'ref', 'secret'], required: false, unique: false },
11
- options: [{ type: String }],
12
- regex: { type: String },
13
- entity: { type: String, required: false },
14
- entityValue: { type: String, required: false },
15
- entityText: { type: String, required: false, unique: false },
16
- prefix: { type: String, required: false },
17
- suffix: { type: String, required: false },
6
+ value: { type: mongoose.Schema.Types.Mixed, required: false, unique: false, index: false },
7
+ label: { type: String, required: false, index: false },
8
+ category: { type: String, required: true, index: false },
9
+ type: { type: String, default: "string", enum: ['string', 'longString', 'number', 'enum', 'boolean', 'password', 'stringList', 'numberList', 'enumList', 'ref', 'secret'], required: false, unique: false, index: false },
10
+ options: [{ type: String, index: false, required: false }],
11
+ regex: { type: String, required: false, index: false },
12
+ entity: { type: String, required: false, index: false },
13
+ entityValue: { type: String, required: false, index: false },
14
+ entityText: { type: String, required: false, unique: false, index: false },
15
+ prefix: { type: String, required: false, index: false },
16
+ suffix: { type: String, required: false, index: false },
17
+ permission: { type: String, required: false, index: false },
18
+ public: { type: Boolean, required: false, default: false, index: false },
18
19
  }, { timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } });
19
20
  SettingSchema.virtual("id").get(function () {
20
21
  return this._id.toString();
@@ -3,6 +3,7 @@ var SettingPermissions;
3
3
  SettingPermissions["Update"] = "setting:update";
4
4
  SettingPermissions["View"] = "setting:view";
5
5
  SettingPermissions["Manage"] = "setting:manage";
6
+ SettingPermissions["Sensitive"] = "setting:sensitive";
6
7
  })(SettingPermissions || (SettingPermissions = {}));
7
8
  export default SettingPermissions;
8
9
  export { SettingPermissions };
@@ -1,7 +1,17 @@
1
- import { object, string } from "zod";
2
- const settingSchema = object({
3
- key: string({ required_error: "validation.required" })
4
- .min(1, "validation.required"),
1
+ import z from "zod";
2
+ const settingSchema = z.object({
3
+ key: z.string({ required_error: "validation.required" }).min(1, "validation.required"),
4
+ label: z.string().optional().nullable(),
5
+ category: z.string().optional().nullable(),
6
+ type: z.enum(['string', 'longString', 'number', 'enum', 'boolean', 'password', 'stringList', 'numberList', 'enumList', 'ref', 'secret']),
7
+ regex: z.string().optional().nullable(),
8
+ entity: z.string().optional().nullable(),
9
+ entityValue: z.string().optional().nullable(),
10
+ entityText: z.string().optional().nullable(),
11
+ prefix: z.string().optional().nullable(),
12
+ suffix: z.string().optional().nullable(),
13
+ permission: z.string().optional().nullable(),
14
+ public: z.boolean().optional().nullable(),
5
15
  });
6
16
  export default settingSchema;
7
17
  export { settingSchema };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.28.0",
6
+ "version": "0.30.0",
7
7
  "description": "Setting module for nice management options.",
8
8
  "main": "dist/index.js",
9
9
  "types": "types/index.d.ts",
@@ -28,11 +28,11 @@
28
28
  "author": "Cristian Incarnato & Drax Team",
29
29
  "license": "ISC",
30
30
  "dependencies": {
31
- "@drax/common-back": "^0.28.0",
32
- "@drax/crud-back": "^0.28.0",
33
- "@drax/crud-share": "^0.28.0",
34
- "@drax/email-back": "^0.28.0",
35
- "@drax/identity-share": "^0.28.0"
31
+ "@drax/common-back": "^0.30.0",
32
+ "@drax/crud-back": "^0.30.0",
33
+ "@drax/crud-share": "^0.30.0",
34
+ "@drax/email-back": "^0.30.0",
35
+ "@drax/identity-share": "^0.30.0"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "better-sqlite3": "^11.0.0",
@@ -57,5 +57,5 @@
57
57
  "debug": "0"
58
58
  }
59
59
  },
60
- "gitHead": "062c6d15dbbf8b6e337869549ab0bdbc27200a9e"
60
+ "gitHead": "f7f06578327be29f20dcb7e2c8a2eac9e9145cab"
61
61
  }
@@ -1,12 +1,11 @@
1
1
  import {SettingPermissions} from "../permissions/SettingPermissions.js";
2
2
  import SettingService from "../services/SettingService.js";
3
3
  import SettingServiceFactory from "../factory/SettingServiceFactory.js";
4
- import {UnauthorizedError, ValidationError} from "@drax/common-back";
4
+ import {NotFoundError, UnauthorizedError, ValidationError} from "@drax/common-back";
5
5
 
6
6
  class SettingController {
7
7
 
8
8
  protected service: SettingService
9
- protected permission
10
9
 
11
10
  constructor() {
12
11
  this.service = SettingServiceFactory()
@@ -14,9 +13,16 @@ class SettingController {
14
13
 
15
14
  async fetchAll(request, reply) {
16
15
  try {
17
- request.rbac.assertPermission(SettingPermissions.View)
18
16
  const settings = await this.service.fetchAll()
19
- return settings
17
+
18
+ if(!request.authUser){
19
+ return settings.filter(s => s.public === true && !s.permission)
20
+ }else{
21
+ return settings.filter(s => {
22
+ return !s.permission || (s.permission && request.rbac.hasPermission(s.permission));
23
+ } )
24
+ }
25
+
20
26
  } catch (e) {
21
27
  console.error(e)
22
28
  if (e instanceof UnauthorizedError) {
@@ -31,8 +37,9 @@ class SettingController {
31
37
 
32
38
  async fetchGrouped(request, reply) {
33
39
  try {
34
- request.rbac.assertPermission(SettingPermissions.View)
40
+ request.rbac.assertPermission(SettingPermissions.Manage)
35
41
  const settings = await this.service.fetchGrouped()
42
+
36
43
  return settings
37
44
  } catch (e) {
38
45
  console.error(e)
@@ -48,16 +55,31 @@ class SettingController {
48
55
 
49
56
  async findByKey(request, reply) {
50
57
  try {
51
- request.rbac.assertPermission(SettingPermissions.View)
52
58
  const key = request.params.key
53
59
  const setting = await this.service.findByKey(key)
60
+
61
+ if(!setting){
62
+ throw new NotFoundError()
63
+ }
64
+
65
+ if(setting.public === false && !request.authUser ){
66
+ throw new UnauthorizedError()
67
+ }
68
+
69
+ if(setting.permission && !request.rbac.hasPermission(setting.permission)){
70
+ throw new UnauthorizedError()
71
+ }
72
+
54
73
  return setting
55
74
  } catch (e) {
56
75
  console.error(e)
57
76
  if (e instanceof UnauthorizedError) {
58
77
  reply.statusCode = e.statusCode
59
78
  reply.send({error: e.message})
60
- } else {
79
+ }if (e instanceof NotFoundError) {
80
+ reply.statusCode = e.statusCode
81
+ reply.send({error: e.message})
82
+ }else {
61
83
  reply.statusCode = 500
62
84
  reply.send({error: 'INTERNAL_SERVER_ERROR'})
63
85
  }
@@ -7,18 +7,19 @@ import {ISetting} from "@drax/settings-share";
7
7
  const SettingSchema = new mongoose.Schema<ISetting>({
8
8
 
9
9
  key: {type: String, required: true, unique: true},
10
- value: {type: mongoose.Schema.Types.Mixed, required: false, unique: false},
11
- //valueList: [{type: String, required: false, unique: false}],
12
- label: {type: String, required: false},
13
- category: {type: String, required: true},
14
- type: {type: String, default: "string", enum: ['string','longString','number','enum','boolean', 'password', 'stringList','numberList', 'enumList', 'ref', 'secret'], required: false, unique: false},
15
- options: [{type: String}],
16
- regex: {type: String},
17
- entity: {type: String, required: false},
18
- entityValue: {type: String, required: false},
19
- entityText: {type: String, required: false, unique: false},
20
- prefix: {type: String, required: false},
21
- suffix: {type: String, required: false},
10
+ value: {type: mongoose.Schema.Types.Mixed, required: false, unique: false, index: false},
11
+ label: {type: String, required: false, index: false},
12
+ category: {type: String, required: true, index: false},
13
+ type: {type: String, default: "string", enum: ['string','longString','number','enum','boolean', 'password', 'stringList','numberList', 'enumList', 'ref', 'secret'], required: false, unique: false, index: false},
14
+ options: [{type: String, index: false, required: false}],
15
+ regex: {type: String, required: false, index: false},
16
+ entity: {type: String, required: false, index: false},
17
+ entityValue: {type: String, required: false, index: false},
18
+ entityText: {type: String, required: false, unique: false, index: false},
19
+ prefix: {type: String, required: false, index: false},
20
+ suffix: {type: String, required: false, index: false},
21
+ permission: {type: String, required: false, index: false},
22
+ public: {type: Boolean, required: false, default: false, index: false},
22
23
  }, {timestamps: true, toJSON: { virtuals: true}, toObject: {virtuals: true} })
23
24
 
24
25
  SettingSchema.virtual("id").get(function () {
@@ -2,6 +2,7 @@ enum SettingPermissions {
2
2
  Update = "setting:update",
3
3
  View = "setting:view",
4
4
  Manage = "setting:manage",
5
+ Sensitive = "setting:sensitive",
5
6
 
6
7
  }
7
8
 
@@ -1,8 +1,18 @@
1
- import { object, string } from "zod"
1
+ import z from "zod"
2
2
 
3
- const settingSchema = object({
4
- key: string({ required_error: "validation.required" })
5
- .min(1, "validation.required"),
3
+ const settingSchema = z.object({
4
+ key: z.string({required_error: "validation.required"}).min(1, "validation.required"),
5
+ label: z.string().optional().nullable(),
6
+ category: z.string().optional().nullable(),
7
+ type: z.enum(['string','longString','number','enum','boolean', 'password', 'stringList','numberList', 'enumList', 'ref', 'secret']),
8
+ regex: z.string().optional().nullable(),
9
+ entity: z.string().optional().nullable(),
10
+ entityValue: z.string().optional().nullable(),
11
+ entityText: z.string().optional().nullable(),
12
+ prefix: z.string().optional().nullable(),
13
+ suffix: z.string().optional().nullable(),
14
+ permission: z.string().optional().nullable(),
15
+ public: z.boolean().optional().nullable(),
6
16
  })
7
17
 
8
18