@modular-rest/server 1.5.2 → 1.6.1
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 +1 -1
- package/src/services/user_manager/db.js +77 -43
package/package.json
CHANGED
|
@@ -1,62 +1,96 @@
|
|
|
1
|
-
var mongoose = require(
|
|
1
|
+
var mongoose = require("mongoose");
|
|
2
2
|
var Schema = mongoose.Schema;
|
|
3
3
|
|
|
4
|
-
let CollectionDefinition = require(
|
|
5
|
-
let { Permission, PermissionTypes } = require(
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
title: String,
|
|
15
|
+
isAnonymous: { type: Boolean, default: false },
|
|
16
|
+
isDefault: { type: Boolean, default: false },
|
|
17
17
|
};
|
|
18
|
-
Object.keys(new PermissionTypes())
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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 (this._conditions.password) {
|
|
66
|
+
this._conditions.password = this.base64Password;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
36
70
|
module.exports = [
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
+
];
|