@modular-rest/server 1.11.12 → 1.11.14

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.
Files changed (112) hide show
  1. package/.nvmrc +1 -0
  2. package/.prettierrc.json +9 -0
  3. package/.releaserc.json +24 -0
  4. package/README.md +79 -94
  5. package/dist/index.js +79 -0
  6. package/docs/.keep +0 -0
  7. package/docs/system-access-type.md +26 -0
  8. package/package.json +58 -45
  9. package/src/application.ts +206 -0
  10. package/src/class/cms_trigger.ts +68 -0
  11. package/src/class/collection_definition.ts +134 -0
  12. package/src/class/combinator.ts +176 -0
  13. package/src/class/database_trigger.ts +99 -0
  14. package/src/class/db_schemas.ts +44 -0
  15. package/src/class/{directory.js → directory.ts} +40 -18
  16. package/src/class/paginator.ts +51 -0
  17. package/src/class/reply.ts +59 -0
  18. package/src/class/security.ts +250 -0
  19. package/src/class/trigger_operator.ts +142 -0
  20. package/src/class/user.ts +199 -0
  21. package/src/class/validator.ts +123 -0
  22. package/src/config.ts +122 -0
  23. package/src/defult-permissions.ts +31 -0
  24. package/src/events.ts +59 -0
  25. package/src/helper/data_insertion.ts +94 -0
  26. package/src/helper/presetup_services.ts +96 -0
  27. package/src/index.ts +146 -0
  28. package/src/middlewares.ts +75 -0
  29. package/src/play-test.ts +8 -0
  30. package/src/services/data_provider/router.ts +191 -0
  31. package/src/services/data_provider/service.ts +305 -0
  32. package/src/services/data_provider/typeCasters.ts +15 -0
  33. package/src/services/file/db.ts +29 -0
  34. package/src/services/file/router.ts +88 -0
  35. package/src/services/file/service.ts +387 -0
  36. package/src/services/functions/router.ts +34 -0
  37. package/src/services/functions/service.ts +203 -0
  38. package/src/services/jwt/router.ts +73 -0
  39. package/src/services/jwt/service.ts +139 -0
  40. package/src/services/user_manager/db.ts +87 -0
  41. package/src/services/user_manager/permissionManager.ts +49 -0
  42. package/src/services/user_manager/router.ts +193 -0
  43. package/src/services/user_manager/service.ts +698 -0
  44. package/tsconfig.json +16 -9
  45. package/typedoc.mjs +41 -0
  46. package/LICENSE +0 -21
  47. package/package-lock.json +0 -1373
  48. package/src/application.js +0 -239
  49. package/src/class/cms_trigger.js +0 -20
  50. package/src/class/collection_definition.js +0 -33
  51. package/src/class/combinator.js +0 -133
  52. package/src/class/database_trigger.js +0 -20
  53. package/src/class/db_schemas.js +0 -18
  54. package/src/class/paginator.js +0 -31
  55. package/src/class/reply.js +0 -37
  56. package/src/class/security.js +0 -141
  57. package/src/class/trigger_operator.js +0 -39
  58. package/src/class/user.js +0 -112
  59. package/src/class/validator.js +0 -91
  60. package/src/config.js +0 -67
  61. package/src/events.js +0 -15
  62. package/src/helper/data_insertion.js +0 -64
  63. package/src/helper/presetup_services.js +0 -31
  64. package/src/index.js +0 -66
  65. package/src/middlewares.js +0 -44
  66. package/src/services/data_provider/router.js +0 -552
  67. package/src/services/data_provider/service.js +0 -262
  68. package/src/services/data_provider/typeCasters.js +0 -10
  69. package/src/services/file/db.js +0 -29
  70. package/src/services/file/router.js +0 -92
  71. package/src/services/file/service.js +0 -231
  72. package/src/services/functions/router.js +0 -37
  73. package/src/services/functions/service.js +0 -74
  74. package/src/services/jwt/router.js +0 -70
  75. package/src/services/jwt/service.js +0 -37
  76. package/src/services/user_manager/db.js +0 -83
  77. package/src/services/user_manager/permissionManager.js +0 -43
  78. package/src/services/user_manager/router.js +0 -176
  79. package/src/services/user_manager/service.js +0 -377
  80. package/types/application.d.ts +0 -97
  81. package/types/class/cms_trigger.d.ts +0 -24
  82. package/types/class/collection_definition.d.ts +0 -36
  83. package/types/class/combinator.d.ts +0 -30
  84. package/types/class/database_trigger.d.ts +0 -28
  85. package/types/class/db_schemas.d.ts +0 -2
  86. package/types/class/directory.d.ts +0 -2
  87. package/types/class/paginator.d.ts +0 -8
  88. package/types/class/reply.d.ts +0 -8
  89. package/types/class/security.d.ts +0 -109
  90. package/types/class/trigger_operator.d.ts +0 -19
  91. package/types/class/user.d.ts +0 -24
  92. package/types/class/validator.d.ts +0 -9
  93. package/types/config.d.ts +0 -101
  94. package/types/events.d.ts +0 -7
  95. package/types/helper/data_insertion.d.ts +0 -4
  96. package/types/helper/presetup_services.d.ts +0 -5
  97. package/types/index.d.ts +0 -72
  98. package/types/middlewares.d.ts +0 -9
  99. package/types/services/data_provider/router.d.ts +0 -3
  100. package/types/services/data_provider/service.d.ts +0 -40
  101. package/types/services/data_provider/typeCasters.d.ts +0 -3
  102. package/types/services/file/db.d.ts +0 -3
  103. package/types/services/file/router.d.ts +0 -3
  104. package/types/services/file/service.d.ts +0 -81
  105. package/types/services/functions/router.d.ts +0 -3
  106. package/types/services/functions/service.d.ts +0 -23
  107. package/types/services/jwt/router.d.ts +0 -3
  108. package/types/services/jwt/service.d.ts +0 -10
  109. package/types/services/user_manager/db.d.ts +0 -3
  110. package/types/services/user_manager/permissionManager.d.ts +0 -3
  111. package/types/services/user_manager/router.d.ts +0 -3
  112. package/types/services/user_manager/service.d.ts +0 -131
@@ -1,239 +0,0 @@
1
- const koa = require("koa");
2
- const cors = require("@koa/cors");
3
- const koaBody = require("koa-body");
4
- const koaStatic = require("koa-static");
5
- const mount = require("koa-mount");
6
- const path = require("path");
7
- const Combination = require("./class/combinator");
8
- const DataProvider = require("./services/data_provider/service");
9
- const UserService = require("./services/user_manager/service");
10
-
11
- const defaultServiceRoot = __dirname + "/services";
12
-
13
- /**
14
- * @typedef {import('koa')} Koa
15
- * @typedef {import('http').Server} server
16
- * @typedef {import('@koa/cors').Options} Cors
17
- * @typedef {import('./class/security.js').PermissionGroup} PermissionGroup
18
- * @typedef {import('./class/cms_trigger.js')} CmsTrigger
19
- */
20
-
21
- const {
22
- config,
23
- setConfig
24
- } = require("./config");
25
-
26
- /**
27
- * Create a modular REST instance with Koa and MongoDB support.
28
- * @param {{
29
- * cors?: Cors; // CORS options.
30
- * modulesPath?: string; // Root directory of your router.js/db.js files.
31
- * uploadDirectory?: string; // Root directory of your uploaded files.
32
- * koaBodyOptions?: object; // Options for koa-body.
33
- * staticPath?: {
34
- * rootDir: string; // Root directory of your static files.
35
- * rootPath: string; // Root path of your static files, defaults to '/assets'.
36
- * maxage?: number; // Browser cache max-age in milliseconds. Defaults to 0.
37
- * hidden?: boolean; // Allow transfer of hidden files. Defaults to false.
38
- * index?: string; // Default file name. Defaults to 'index.html'.
39
- * defer?: boolean; // If true, serves after return next(), allowing any downstream middleware to respond first. Defaults to false.
40
- * gzip?: boolean; // Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file with .gz extension exists. Defaults to true.
41
- * br?: boolean; // Try to serve the brotli version of a file automatically when brotli is supported by a client and if the requested file with .br extension exists. Note that brotli is only accepted over https. Defaults to false.
42
- * setHeaders?: Function; // Function to set custom headers on response.
43
- * extensions?: boolean|Array; // Try to match extensions from passed array to search for file when no extension is suffixed in URL. First found is served. Defaults to false.
44
- * };
45
- * onBeforeInit?: (koaApp:Koa) => void; // A callback called before initializing the Koa server.
46
- * onAfterInit?: (koaApp:Koa) => void; // A callback called after server initialization.
47
- * port?: number; // Server port.
48
- * dontListen?: boolean; // If true, the server will not run and will only return the Koa app object.
49
- * mongo?: {
50
- * dbPrefix: string; // A prefix for your database name.
51
- * mongoBaseAddress: string; // The address of your MongoDB server without any database specification.
52
- * addressMap?: string; // Specific addresses for each database.
53
- * };
54
- * keypair?: {
55
- * private: string; // Private key for RSA authentication.
56
- * public: string; // Public key for RSA authentication.
57
- * };
58
- * adminUser?: {
59
- * email: string; // Admin user email.
60
- * password: string; // Admin user password.
61
- * };
62
- * verificationCodeGeneratorMethod: () => string; // A method to return a verification code when registering a new user.
63
- * collectionDefinitions?: CollectionDefinition[]; // An array of additional collection definitions.
64
- * permissionGroups?: PermissionGroup[]; // An array of additional permission groups.
65
- * authTriggers?: CmsTrigger[]; // An array of additional database triggers for the auth collection.
66
- * fileTriggers?: CmsTrigger[]; // An array of additional database triggers for the auth collection.
67
- * }} options
68
- * @returns {Promise<{app: Koa, server: Server}>}
69
- */
70
- async function createRest(options) {
71
- setConfig({
72
- port: 3000,
73
- dontListen: false,
74
- mongo: {
75
- atlas: false,
76
- mongoBaseAddress: "mongodb://localhost:27017",
77
- dbPrefix: "mrest_",
78
- },
79
- adminUser: {
80
- email: "admin@email.com",
81
- password: "@dmin",
82
- },
83
-
84
- ...options,
85
- });
86
-
87
- const app = new koa();
88
-
89
- /**
90
- * Plug in Cors
91
- */
92
- app.use(cors(config.cors || {}));
93
-
94
- /**
95
- * Plug in BodyParser
96
- */
97
- const bodyParserOptions = {
98
- multipart: true,
99
- ...(config.koaBodyOptions || {}),
100
- };
101
- app.use(koaBody(bodyParserOptions));
102
-
103
- /**
104
- * Plug In KoaStatic
105
- */
106
- if (config.staticPath) {
107
- const defaultStaticPath = config.staticPath.rootDir;
108
- const defaultStaticRootPath = config.staticPath.rootPath || "/assets";
109
-
110
- delete config.staticPath.rootDir;
111
- delete config.staticPath.rootPath;
112
-
113
- app.use(
114
- mount(
115
- defaultStaticRootPath,
116
- koaStatic(defaultStaticPath, config.staticPath)
117
- )
118
- );
119
- }
120
-
121
- /**
122
- * Run before hook
123
- */
124
- if (config.onBeforeInit) config.onBeforeInit(app);
125
-
126
- /**
127
- * Setup default services
128
- *
129
- * - Collect and plug in router.js/db.js of default services
130
- * - Setting up default services
131
- */
132
-
133
- // 1. Plug in default routes
134
- await Combination.combineRoutesByFilePath(path.join(defaultServiceRoot), app);
135
-
136
- // Collect default databaseDefinitions
137
- const defaultDatabaseDefinitionList =
138
- await Combination.combineModulesByFilePath({
139
- rootDirectory: defaultServiceRoot,
140
- filename: {
141
- name: "db",
142
- extension: ".js",
143
- },
144
- combineWithRoot: true,
145
- });
146
-
147
- // 2. Plug in default databaseDefinitions
148
- await DataProvider.addCollectionDefinitionByList({
149
- list: defaultDatabaseDefinitionList,
150
- mongoOption: config.mongo,
151
- });
152
-
153
- /**
154
- * User Services
155
- *
156
- * 3. Plug in routes and database
157
- */
158
- if (config.modulesPath) {
159
- // Plug in user routes
160
- await Combination.combineRoutesByFilePath(config.modulesPath, app);
161
-
162
- // Collect user CollectionDefinitions (db.js files)
163
- let userDatabaseDetail = [];
164
- userDatabaseDetail = await Combination.combineModulesByFilePath({
165
- rootDirectory: config.modulesPath,
166
- filename: {
167
- name: "db",
168
- extension: ".js",
169
- },
170
- combineWithRoot: true,
171
- });
172
-
173
- // Combine additional CollectionDefinitions
174
- if (config.collectionDefinitions) {
175
- userDatabaseDetail.concat(config.collectionDefinitions);
176
- }
177
-
178
- // Plug in user CollectionDefinitions
179
- await DataProvider.addCollectionDefinitionByList({
180
- list: userDatabaseDetail || [],
181
- mongoOption: config.mongo,
182
- });
183
-
184
- // Plug in Verification method
185
- if (typeof config.verificationCodeGeneratorMethod == "function") {
186
- UserService.main.setCustomVerificationCodeGeneratorMethod(
187
- config.verificationCodeGeneratorMethod
188
- );
189
- }
190
-
191
- // 4. plug in modular functions
192
- await Combination.combineFunctionsByFilePath({
193
- rootDirectory: config.modulesPath,
194
- filename: {
195
- name: "functions",
196
- extension: ".js",
197
- },
198
- });
199
- }
200
-
201
- // 4. Setting up default services
202
- try {
203
- await require("./helper/presetup_services").setup(options);
204
- } catch (e) {
205
- return Promise.reject(e);
206
- }
207
-
208
- /**
209
- * Run the server
210
- *
211
- * return KOA app object
212
- */
213
- return new Promise((done, reject) => {
214
- try {
215
- let server;
216
-
217
- if (!config.dontListen) {
218
- server = app.listen(config.port);
219
-
220
- console.log(
221
- "\x1b[35m",
222
- `KOAS has been launched on: localhost:${config.port}`
223
- );
224
- }
225
-
226
- // on after init
227
- if (config.onAfterInit) config.onAfterInit(app);
228
-
229
- done({
230
- app,
231
- server,
232
- });
233
- } catch (err) {
234
- reject(err);
235
- }
236
- });
237
- }
238
-
239
- module.exports = createRest;
@@ -1,20 +0,0 @@
1
- /**
2
- * `CmsTrigger` is a class that defines a callback to be called on a specific database transaction.
3
- *
4
- * @class
5
- */
6
- class CmsTrigger {
7
- /**
8
- * Creates a new instance of `CmsTrigger`.
9
- *
10
- * @param {'update-one' | 'insert-one' | 'remove-one' } operation - The operation to be triggered.
11
- * @param {function({query: any, queryResult: any}): void} [callback=(context) => {}] - The callback to be called when the operation is executed. The callback function takes an object as parameter with two properties: 'query' and 'queryResult'.
12
- * @constructor
13
- */
14
- constructor(operation, callback = (context) => {}) {
15
- this.operation = operation;
16
- this.callback = callback;
17
- }
18
- }
19
-
20
- module.exports = CmsTrigger;
@@ -1,33 +0,0 @@
1
- /**
2
- * @typedef {import('./security.js').Permission} Permission
3
- * @typedef {import('./database_trigger.js')} DatabaseTrigger
4
- */
5
-
6
- class CollectionDefinition {
7
- /**
8
- * This class helps to create a mongoose collection
9
- * associated with permissions and triggers.
10
- *
11
- * @class
12
- * @param {Object} option
13
- * @param {string} option.db - Database name
14
- * @param {string} option.collection - Collection name
15
- * @param {Object} option.schema - Mongoose schema
16
- * @param {Array<Permission>} option.permissions - A list of permissions for this collection
17
- * @param {Array<DatabaseTrigger>=} option.triggers - A database trigger
18
- */
19
- constructor({ db, collection, schema, permissions, triggers }) {
20
- // string
21
- this.database = db;
22
- // string
23
- this.collection = collection;
24
- // schema object of mongoose
25
- this.schema = schema;
26
- // a list of Permission for this collection
27
- this.permissions = permissions;
28
-
29
- this.triggers = triggers;
30
- }
31
- }
32
-
33
- module.exports = CollectionDefinition;
@@ -1,133 +0,0 @@
1
- const Router = require("koa-router");
2
- const directory = require("./directory.js");
3
- const { addFunction } = require("./../services/functions/service.js");
4
-
5
- class Combinator {
6
- async combineRoutesByFilePath(rootDirectory, app) {
7
- // find route paths
8
- let option = { name: "router", filter: [".js"] };
9
- let routerPaths = await directory
10
- .find(rootDirectory, option)
11
- .then()
12
- .catch((e) => {
13
- console.log(e);
14
- });
15
-
16
- // create and combine routes into the app
17
- for (let i = 0; i < routerPaths.length; i++) {
18
- let service = require(routerPaths[i]);
19
- let name = service.name;
20
-
21
- var serviceRouter = new Router();
22
- serviceRouter.use(`/${name}`, service.main.routes());
23
-
24
- app.use(serviceRouter.routes());
25
- }
26
- }
27
-
28
- /**
29
- *
30
- * @param {object} option
31
- * @param {string} option.rootDirectory root directory of files
32
- * @param {object} option.filename an object of {name, extension}
33
- * @param {string} option.filename.name name of file
34
- * @param {string} option.filename.extension the extension of the file
35
- * @param {boolean} option.combineWithRoot combine all file content and return theme as a object
36
- * @param {boolean} option.convertToArray return file content as an array instead an object
37
- */
38
- async combineModulesByFilePath({
39
- rootDirectory,
40
- filename,
41
- combineWithRoot,
42
- convertToArray,
43
- }) {
44
- // find route paths
45
- let rootObject_temp;
46
- const option = { name: filename.name, filter: [filename.extension] };
47
- const modulesPath = await directory
48
- .find(rootDirectory, option)
49
- .then()
50
- .catch((e) => {
51
- console.log(e);
52
- });
53
-
54
- // create and combine routes into the app
55
- for (let i = 0; i < modulesPath.length; i++) {
56
- const moduleObject = require(modulesPath[i]);
57
-
58
- // act by otherOption
59
- if (combineWithRoot) {
60
- if (moduleObject.name) delete moduleObject.name;
61
-
62
- if (moduleObject.length) {
63
- if (!rootObject_temp) rootObject_temp = [];
64
-
65
- rootObject_temp = [...rootObject_temp, ...moduleObject];
66
- } else {
67
- rootObject_temp = this.extendObj(rootObject_temp, moduleObject);
68
- }
69
- // else if (typeof)
70
- }
71
- // default act
72
- else {
73
- const name = moduleObject.name;
74
- rootObject_temp[name] = moduleObject;
75
- }
76
- }
77
-
78
- // options
79
- // convertToArray
80
- if (convertToArray) {
81
- rootObject_temp = Object.values(rootObject_temp);
82
- }
83
-
84
- // set result to main rootObject
85
- return rootObject_temp;
86
- }
87
-
88
- async combineFunctionsByFilePath({ rootDirectory, filename }) {
89
- // find route paths
90
- const option = { name: filename.name, filter: [filename.extension] };
91
- const functionsPaths = await directory
92
- .find(rootDirectory, option)
93
- .then()
94
- .catch((e) => {
95
- console.log(e);
96
- });
97
-
98
- // create and combine routes into the app
99
- for (let i = 0; i < functionsPaths.length; i++) {
100
- const modularFunctions = require(functionsPaths[i]);
101
-
102
- if (!modularFunctions.functions) {
103
- throw new Error(
104
- `Module file ${functionsPaths[i]} does not have functions property.`
105
- );
106
- }
107
-
108
- // if array
109
- if (modularFunctions.functions.length) {
110
- for (const moduleFunction of modularFunctions.functions) {
111
- addFunction(moduleFunction);
112
- }
113
- } else {
114
- addFunction(modularFunctions.functions);
115
- }
116
- }
117
- }
118
-
119
- extendObj(obj, src) {
120
- for (var key in src) {
121
- if (src.hasOwnProperty(key)) obj[key] = src[key];
122
- }
123
- return obj;
124
- }
125
-
126
- static get instance() {
127
- return instance;
128
- }
129
- }
130
-
131
- const instance = new Combinator();
132
-
133
- module.exports = Combinator.instance;
@@ -1,20 +0,0 @@
1
- /**
2
- * `DatabaseTrigger` is a class that defines a callback to be called on a specific database transaction.
3
- *
4
- * @class
5
- */
6
- class DatabaseTrigger {
7
- /**
8
- * Creates a new instance of `DatabaseTrigger`.
9
- *
10
- * @param {'find' | 'find-one' | 'count' | 'update-one' | 'insert-one' | 'remove-one' | 'aggregate'} operation - The operation to be triggered. Supported operations are: 'find', 'find-one', 'count', 'update-one', 'insert-one', 'remove-one', 'aggregate'.
11
- * @param {function({query: Object.<string, any>, queryResult: any | any[]}): void} [callback=(context) => {}] - The callback to be called when the operation is executed. The callback function takes an object as parameter with two properties: 'query' and 'queryResult'.
12
- * @constructor
13
- */
14
- constructor(operation, callback = (context) => {}) {
15
- this.operation = operation;
16
- this.callback = callback;
17
- }
18
- }
19
-
20
- module.exports = DatabaseTrigger;
@@ -1,18 +0,0 @@
1
- var mongoose = require("mongoose");
2
- var Schema = mongoose.Schema;
3
-
4
- module.exports = {
5
- file: new Schema(
6
- {
7
- originalName: String,
8
- fileName: String,
9
- owner: String,
10
- format: String,
11
- // Tag being used as the parent dir for files
12
- // uploadDir/$format/$tag/timestamp.format
13
- tag: String,
14
- size: Number,
15
- },
16
- { timestamps: true }
17
- ),
18
- };
@@ -1,31 +0,0 @@
1
- /**
2
- * Creates a pagination object based on the given parameters.
3
- * @param {number} count - The total number of items to paginate.
4
- * @param {number} perPage - The number of items to display per page.
5
- * @param {number} page - The current page number.
6
- * @returns {Object} - An object containing pagination information.
7
- */
8
- function create(count, perPage, page) {
9
- const totalPages = Math.ceil(count / perPage);
10
-
11
- if (page > totalPages) page = 1;
12
-
13
- let from = 0;
14
- if (perPage == 1) from = page - 1;
15
- else from = (perPage * page) - perPage;
16
-
17
- if (page <= 1) from = 0;
18
-
19
- let result = {
20
- 'pages': totalPages,
21
- 'page': page,
22
- 'from': from,
23
- 'to': perPage
24
- };
25
-
26
- return result;
27
- };
28
-
29
- module.exports = {
30
- create,
31
- }
@@ -1,37 +0,0 @@
1
- /**
2
- * Creates a response object with the given status and detail.
3
- *
4
- * @param {string} status - The status of the response. Can be "s" for success, "f" for fail, or "e" for error.
5
- * @param {Object} [detail={}] - The detail of the response. Can contain any additional information about the response.
6
- * @returns {Object} - The response object with the given status and detail.
7
- */
8
- function create(status, detail = {}) {
9
-
10
- let result = detail || {};
11
-
12
- // define status
13
- switch (status) {
14
- case 's':
15
- result['status'] = 'success';
16
- break;
17
-
18
- case 'f':
19
- result['status'] = 'fail';
20
- break;
21
-
22
- case 'e':
23
- result['status'] = 'error';
24
- break;
25
-
26
- default:
27
- result['status'] = 'success';
28
- break;
29
- }
30
-
31
- // return
32
- return result;
33
- }
34
-
35
- module.exports = {
36
- create
37
- }
@@ -1,141 +0,0 @@
1
- /**
2
- * Class representing an access definition.
3
- */
4
- class AccessDefinition {
5
- /**
6
- * Create an access definition.
7
- * @param {Object} options - The options for the access definition.
8
- * @param {string} options.database - The name of the database.
9
- * @param {string} options.collection - The name of the collection.
10
- * @param {Array.<Permission>} options.permissionList - The list of permissions.
11
- */
12
- constructor({ database, collection, permissionList }) {
13
- this.database = database;
14
- this.collection = collection;
15
- this.permissionList = permissionList;
16
- }
17
- }
18
-
19
- /**
20
- * @typedef {('god_access'|'user_access'|'upload_file_access'|'remove_file_access'|'anonymous_access'|'advanced_settings'|string)} PermissionType
21
- */
22
-
23
- /**
24
- * Class representing a permission.
25
- */
26
- class Permission {
27
- /**
28
- * Create a permission.
29
- * @param {Object} options - The options for the permission.
30
- * @param {PermissionType} options.type - The type of the permission.
31
- * @param {boolean} [options.read=false] - The read access of the permission.
32
- * @param {boolean} [options.write=false] - The write access of the permission.
33
- * @param {boolean} [options.onlyOwnData=false] - If true, users can perform CRUD on documents that they created already.
34
- * @param {string} [options.ownerIdField='refId'] - The name of the field that contains the owner's id of the document.
35
- */
36
- constructor({
37
- type,
38
- read = false,
39
- write = false,
40
- onlyOwnData = false,
41
- ownerIdField = "refId",
42
- }) {
43
- this.type = type;
44
- this.read = read;
45
- this.write = write;
46
- this.onlyOwnData = onlyOwnData;
47
- this.ownerIdField = ownerIdField;
48
- }
49
- }
50
-
51
- /**
52
- * Class representing different types of permissions.
53
- * Each static getter returns a string that represents a specific type of permission.
54
- */
55
- class PermissionTypes {
56
- /**
57
- * Get the string representing god access permission type.
58
- * @return {string} The god access permission type.
59
- */
60
- static get god_access() {
61
- return "god_access";
62
- }
63
-
64
- /**
65
- * Get the string representing advanced settings permission type.
66
- * @return {string} The advanced settings permission type.
67
- */
68
- static get advanced_settings() {
69
- return "advanced_settings";
70
- }
71
-
72
- /**
73
- * Get the string representing user access permission type.
74
- * @return {string} The user access permission type.
75
- */
76
- static get user_access() {
77
- return "user_access";
78
- }
79
-
80
- /**
81
- * Get the string representing upload file access permission type.
82
- * @return {string} The upload file access permission type.
83
- */
84
- static get upload_file_access() {
85
- return "upload_file_access";
86
- }
87
-
88
- /**
89
- * Get the string representing remove file access permission type.
90
- * @return {string} The remove file access permission type.
91
- */
92
- static get remove_file_access() {
93
- return "remove_file_access";
94
- }
95
- }
96
-
97
- class PermissionGroup {
98
- /**
99
- * Create a permission group.
100
- * @param {Object} options - The options for the permission group.
101
- * @param {string} options.title - The title of the permission group.
102
- * @param {boolean} [options.isDefault=false] - If true, the permission group is the default permission group.
103
- * @param {boolean} [options.isAnonymous=false] - If true, the permission group is the anonymous permission group.
104
- * @param {Array.<PermissionType>} [options.validPermissionTypes=[]] - The valid permission types of the permission group.
105
- * @return {PermissionGroup} The created permission group.
106
- */
107
- constructor({
108
- title,
109
- isDefault = false,
110
- isAnonymous = false,
111
- validPermissionTypes = [],
112
- }) {
113
- //
114
- this.title = title;
115
-
116
- this.isDefault = isDefault;
117
- this.isAnonymous = isAnonymous;
118
-
119
- this.validPermissionTypes = validPermissionTypes;
120
- }
121
- }
122
-
123
- /**
124
- * Class representing access types.
125
- */
126
- class AccessTypes {
127
- static get read() {
128
- return "read";
129
- }
130
- static get write() {
131
- return "write";
132
- }
133
- }
134
-
135
- module.exports = {
136
- AccessDefinition,
137
- Permission,
138
- PermissionTypes,
139
- PermissionGroup,
140
- AccessTypes,
141
- };