@modular-rest/server 1.5.2 → 1.6.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modular-rest/server",
3
- "version": "1.5.2",
3
+ "version": "1.6.0",
4
4
  "description": "a nodejs module based on KOAJS for developing Rest-APIs in a modular solution.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,62 +1,96 @@
1
- var mongoose = require('mongoose');
1
+ var mongoose = require("mongoose");
2
2
  var Schema = mongoose.Schema;
3
3
 
4
- let CollectionDefinition = require('../../class/collection_definition');
5
- let { Permission, PermissionTypes } = require('../../class/security');
4
+ let CollectionDefinition = require("../../class/collection_definition");
5
+ let { Permission, PermissionTypes } = require("../../class/security");
6
6
 
7
7
  /**
8
8
  * Permission schema
9
- *
9
+ *
10
10
  * This schema is generated dynamically
11
11
  * by combining default & custom permissions.
12
12
  */
13
13
  let permissionSchemaConstructorOption = {
14
- title: String,
15
- isAnonymous: { type: Boolean, default: false },
16
- isDefault: { type: Boolean, default: false },
14
+ title: String,
15
+ isAnonymous: { type: Boolean, default: false },
16
+ isDefault: { type: Boolean, default: false },
17
17
  };
18
- Object.keys(new PermissionTypes())
19
- .forEach(key => {
20
- let fieldOption = { type: Boolean, default: false };
21
- permissionSchemaConstructorOption[key] = fieldOption;
22
- })
18
+ Object.keys(new PermissionTypes()).forEach((key) => {
19
+ let fieldOption = { type: Boolean, default: false };
20
+ permissionSchemaConstructorOption[key] = fieldOption;
21
+ });
23
22
 
24
23
  let permissionSchema = new Schema(permissionSchemaConstructorOption);
25
24
  permissionSchema.index({ title: 1 }, { unique: true });
26
25
 
27
26
  let authSchema = new Schema({
28
- permission: { type: Schema.Types.ObjectId, ref: 'permission', required: false },
29
- email: String,
30
- phone: String,
31
- password: String,
32
- type: { type: String, default: 'user', enum: ['user', 'anonymous'] }
27
+ permission: {
28
+ type: Schema.Types.ObjectId,
29
+ ref: "permission",
30
+ required: false,
31
+ },
32
+ email: String,
33
+ phone: String,
34
+ password: String,
35
+ type: { type: String, default: "user", enum: ["user", "anonymous"] },
33
36
  });
34
37
  authSchema.index({ email: 1 }, { unique: true });
35
38
 
39
+ authSchema
40
+ .virtual("base64Password")
41
+ .get(function () {
42
+ return Buffer.from(this.password, "base64").toString("utf-8");
43
+ })
44
+ .set(function (value) {
45
+ this.password = Buffer.from(value).toString("base64");
46
+ });
47
+
48
+ // Middleware for encoding password on save and init
49
+ authSchema.pre("save", function (next) {
50
+ // Encode the password before saving
51
+ if (this.isModified("password")) {
52
+ this.base64Password = this.password;
53
+ }
54
+ next();
55
+ });
56
+
57
+ authSchema.pre("init", function (next) {
58
+ // Decode the password on init
59
+ this.password = this.base64Password;
60
+ next();
61
+ });
62
+
63
+ // Middleware for decoding password on findOne
64
+ authSchema.post("findOne", function (doc) {
65
+ if (doc) {
66
+ doc.password = doc.base64Password;
67
+ }
68
+ });
69
+
36
70
  module.exports = [
37
- new CollectionDefinition({
38
- db: 'cms',
39
- collection: 'auth',
40
- schema: authSchema,
41
- permissions: [
42
- new Permission({
43
- type: PermissionTypes.advanced_settings,
44
- read: true,
45
- write: true
46
- }),
47
- ]
48
- }),
49
-
50
- new CollectionDefinition({
51
- db: 'cms',
52
- collection: 'permission',
53
- schema: permissionSchema,
54
- permissions: [
55
- new Permission({
56
- type: PermissionTypes.advanced_settings,
57
- read: true,
58
- write: true
59
- }),
60
- ],
61
- }),
62
- ]
71
+ new CollectionDefinition({
72
+ db: "cms",
73
+ collection: "auth",
74
+ schema: authSchema,
75
+ permissions: [
76
+ new Permission({
77
+ type: PermissionTypes.advanced_settings,
78
+ read: true,
79
+ write: true,
80
+ }),
81
+ ],
82
+ }),
83
+
84
+ new CollectionDefinition({
85
+ db: "cms",
86
+ collection: "permission",
87
+ schema: permissionSchema,
88
+ permissions: [
89
+ new Permission({
90
+ type: PermissionTypes.advanced_settings,
91
+ read: true,
92
+ write: true,
93
+ }),
94
+ ],
95
+ }),
96
+ ];