@modular-rest/server 1.7.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.
package/src/config.js ADDED
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @typedef {import('koa')} Koa
3
+ * @typedef {import('@koa/cors').Options} Cors
4
+ * @typedef {import('./class/collection_definition')} CollectionDefinition
5
+ * @typedef {import('./class/security').PermissionGroup} PermissionGroup
6
+ */
7
+
8
+ /**
9
+ * @typedef {{
10
+ * cors?: Cors; // CORS options.
11
+ * modulesPath?: string; // Root directory of your router.js/db.js files.
12
+ * staticPath?: {
13
+ * rootDir?: string; // Root directory of your static files.
14
+ * rootPath?: string; // Root path of your static files.
15
+ * notFoundFile?: string; // Not found file.
16
+ * log?: boolean; // Log requests to console.
17
+ * last?: boolean; // Don't execute any downstream middleware.
18
+ * maxage?: number; // Browser cache max-age in milliseconds.
19
+ * hidden?: boolean; // Allow transfer of hidden files.
20
+ * gzip?: boolean; // Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file exists.
21
+ * brotli?: boolean; // Try to serve the brotli version of a file automatically when brotli is supported by a client and if the requested file exists.
22
+ * index?: string; // Index file.
23
+ * };
24
+ * onBeforeInit?: (koaApp:Koa) => void; // A callback called before initializing the Koa server.
25
+ * onAfterInit?: (koaApp:Koa) => void; // A callback called after server initialization.
26
+ * port?: number; // Server port.
27
+ * dontListen?: boolean; // If true, the server will not run and will only return the Koa app object.
28
+ * mongo?: {
29
+ * dbPrefix: string; // A prefix for your database name.
30
+ * mongoBaseAddress: string; // The address of your MongoDB server without any database specification.
31
+ * addressMap?: string; // Specific addresses for each database.
32
+ * };
33
+ * keypair?: {
34
+ * private: string; // Private key for RSA authentication.
35
+ * public: string; // Public key for RSA authentication.
36
+ * };
37
+ * adminUser?: {
38
+ * email: string; // Admin user email.
39
+ * password: string; // Admin user password.
40
+ * };
41
+ * verificationCodeGeneratorMethod: () => string; // A method to return a verification code when registering a new user.
42
+ * collectionDefinitions?: CollectionDefinition[]; // An array of additional collection definitions.
43
+ * permissionGroups?: PermissionGroup[]; // An array of additional permission groups.
44
+ * }} Config
45
+ * @exports Config
46
+ */
47
+
48
+ /**
49
+ * @param {Config} options
50
+ */
51
+ function setConfig(options) {
52
+ Object.assign(config, options);
53
+ }
54
+
55
+ /**
56
+ * @type {Config}
57
+ */
58
+ const config = {};
59
+
60
+ module.exports = {
61
+ setConfig,
62
+ config,
63
+ };
@@ -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
package/src/index.js CHANGED
@@ -19,16 +19,24 @@ const userManager = require("./services/user_manager/service");
19
19
 
20
20
  module.exports = {
21
21
  createRest,
22
+
23
+ // Route utilities
22
24
  reply,
23
25
  TypeCasters,
24
26
  paginator,
25
27
  validator,
28
+
29
+ // Service utilities
26
30
  getCollection,
31
+
32
+ // Database
27
33
  CollectionDefinition,
28
34
  Schemas,
29
35
  Schema,
30
36
  DatabaseTrigger,
31
37
  ...SecurityClass,
38
+
39
+ // Middlewares
32
40
  middleware,
33
41
  userManager: userManager.main,
34
42
  };