@modular-rest/server 1.8.0 → 1.10.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,87 +1,24 @@
1
1
  const DataProvider = require("../services/data_provider/service");
2
-
3
- function createPermissions() {
4
- let model = DataProvider.getCollection("cms", "permission");
5
-
6
- return new Promise(async (done, reject) => {
7
- // create customer permission
8
- let isAnonymousExisted = await model
9
- .countDocuments({ title: "anonymous" })
10
- .exec()
11
- .catch(reject);
12
- let isCoustomerExisted = await model
13
- .countDocuments({ title: "customer" })
14
- .exec()
15
- .catch(reject);
16
- let isAdministratorExisted = await model
17
- .countDocuments({ title: "administrator" })
18
- .exec()
19
- .catch(reject);
20
-
21
- if (!isAnonymousExisted) {
22
- await new model({
23
- anonymous_access: true,
24
- isAnonymous: true,
25
- title: "anonymous",
26
- })
27
- .save()
28
- .catch(reject);
29
- }
30
-
31
- if (!isCoustomerExisted) {
32
- await new model({
33
- customer_access: true,
34
- anonymous_access: true,
35
- upload_file_access: true,
36
- remove_file_access: true,
37
- isDefault: true,
38
- title: "customer",
39
- })
40
- .save()
41
- .catch(reject);
42
- }
43
-
44
- if (!isAdministratorExisted) {
45
- await new model({
46
- god_access: true,
47
- customer_access: true,
48
- anonymous_access: true,
49
- upload_file_access: true,
50
- remove_file_access: true,
51
- title: "administrator",
52
- })
53
- .save()
54
- .catch(reject);
55
- }
56
-
57
- done();
58
- });
59
- }
2
+ const {
3
+ getDefaultAnonymousPermissionGroup,
4
+ getDefaultAdministratorPermissionGroup,
5
+ } = require("../services/user_manager/permissionManager");
60
6
 
61
7
  async function createAdminUser({ email, password }) {
62
- let permissionModel = DataProvider.getCollection("cms", "permission");
63
8
  let authModel = DataProvider.getCollection("cms", "auth");
64
9
 
65
10
  try {
66
- let isAnonymousExisted = await authModel
11
+ const isAnonymousExisted = await authModel
67
12
  .countDocuments({ type: "anonymous" })
68
13
  .exec();
69
14
 
70
- let isAdministratorExisted = await authModel
15
+ const isAdministratorExisted = await authModel
71
16
  .countDocuments({ type: "user", email: email })
72
17
  .exec();
73
18
 
74
- let anonymousPermission = await permissionModel
75
- .findOne({ title: "anonymous" })
76
- .exec();
77
-
78
- let administratorPermission = await permissionModel
79
- .findOne({ title: "administrator" })
80
- .exec();
81
-
82
19
  if (isAnonymousExisted == 0) {
83
20
  await new authModel({
84
- permission: anonymousPermission._id,
21
+ permission: getDefaultAnonymousPermissionGroup().title,
85
22
  email: "",
86
23
  phone: "",
87
24
  password: "",
@@ -90,8 +27,12 @@ async function createAdminUser({ email, password }) {
90
27
  }
91
28
 
92
29
  if (isAdministratorExisted == 0) {
30
+ if (!email || !password) {
31
+ return Promise.reject("Invalid email or password for admin user.");
32
+ }
33
+
93
34
  await new authModel({
94
- permission: administratorPermission._id,
35
+ permission: getDefaultAdministratorPermissionGroup().title,
95
36
  email: email,
96
37
  password: password,
97
38
  type: "user",
@@ -103,6 +44,5 @@ async function createAdminUser({ email, password }) {
103
44
  }
104
45
 
105
46
  module.exports = {
106
- createPermissions,
107
47
  createAdminUser,
108
48
  };
@@ -19,22 +19,10 @@ module.exports.setup = async ({ keypair, adminUser, uploadDirectory }) => {
19
19
  /**
20
20
  * Data Insertion
21
21
  *
22
- * Insert permissions and admin user
22
+ * Insert admin user
23
23
  * for the first time
24
24
  */
25
- await DataInsertion.createPermissions().catch((err) => {
26
- console.log(
27
- "Error while creating permissions, it seems data is already inserted.",
28
- err
29
- );
30
- });
31
-
32
- await DataInsertion.createAdminUser(adminUser).catch((err) => {
33
- console.log(
34
- "Error while creating admin user, it seems data is already inserted.",
35
- err
36
- );
37
- });
25
+ await DataInsertion.createAdminUser(adminUser);
38
26
 
39
27
  /**
40
28
  * File Service