soul-cli 0.6.0 → 0.7.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/.all-contributorsrc +73 -0
- package/.editorconfig +26 -0
- package/.env.sample +9 -0
- package/.eslintignore +2 -0
- package/.eslintrc.js +24 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/.github/workflows/npm-publish.yml +23 -0
- package/.husky/pre-commit +5 -0
- package/CONTRIBUTING.md +92 -0
- package/LICENSE +21 -0
- package/README.md +186 -1
- package/docs/api/root-examples.md +41 -0
- package/docs/api/rows-examples.md +225 -0
- package/docs/api/tables-examples.md +217 -0
- package/docs/api-examples.md +62 -0
- package/docs/extensions-examples.md +167 -0
- package/docs/logo.png +0 -0
- package/docs/soul-studio.png +0 -0
- package/docs/ws/rows-examples.md +122 -0
- package/docs/ws-examples.md +42 -0
- package/package.json +17 -2
- package/src/cli.js +59 -1
- package/src/commands.js +22 -0
- package/src/config/index.js +48 -3
- package/src/constants/api.js +26 -0
- package/src/constants/auth.js +5 -0
- package/src/constants/index.js +13 -0
- package/src/constants/messages.js +41 -0
- package/src/constants/roles.js +3 -0
- package/src/constants/tables.js +47 -0
- package/src/controllers/auth/common.js +16 -0
- package/src/controllers/auth/index.js +5 -0
- package/src/controllers/auth/tables.js +96 -0
- package/src/controllers/auth/token.js +243 -0
- package/src/controllers/auth/user.js +394 -0
- package/src/controllers/auth.test.js +372 -0
- package/src/controllers/rows.js +30 -31
- package/src/controllers/rows.test.js +209 -18
- package/src/controllers/tables.test.js +58 -28
- package/src/db/schema.js +118 -0
- package/src/index.js +32 -7
- package/src/middlewares/api.js +79 -0
- package/src/middlewares/auth.js +85 -0
- package/src/middlewares/broadcast.js +1 -1
- package/src/middlewares/validation.js +3 -2
- package/src/routes/auth.js +29 -0
- package/src/routes/rows.js +17 -5
- package/src/routes/tables.js +26 -4
- package/src/schemas/auth.js +67 -0
- package/src/schemas/index.js +6 -1
- package/src/schemas/rows.js +20 -0
- package/src/schemas/tables.js +18 -2
- package/src/server.js +3 -2
- package/src/services/authService.js +78 -0
- package/src/services/index.js +2 -1
- package/src/services/rowService.js +36 -7
- package/src/services/tableService.js +127 -1
- package/src/swagger/index.js +67 -0
- package/src/swagger/swagger.json +292 -60
- package/src/tests/index.js +4 -5
- package/src/tests/testData.js +72 -27
- package/src/utils/index.js +78 -0
- /package/{src/services/.gitkeep → docs/extensions/api-examples.js} +0 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const config = require('../config');
|
|
2
|
+
const { registerUser } = require('../controllers/auth');
|
|
3
|
+
const {
|
|
4
|
+
apiConstants,
|
|
5
|
+
dbConstants,
|
|
6
|
+
responseMessages,
|
|
7
|
+
} = require('../constants/');
|
|
8
|
+
const { removeFields } = require('../utils');
|
|
9
|
+
|
|
10
|
+
const { httpVerbs } = apiConstants;
|
|
11
|
+
const { reservedTableNames, USERS_TABLE, tableFields } = dbConstants;
|
|
12
|
+
const { errorMessage } = responseMessages;
|
|
13
|
+
|
|
14
|
+
const processRowRequest = async (req, res, next) => {
|
|
15
|
+
const resource = req.params.name;
|
|
16
|
+
const { method } = req;
|
|
17
|
+
|
|
18
|
+
// If the user sends a request to the auth tables while AUTH is set to false, throw an error
|
|
19
|
+
if (apiConstants.authEndpoints.includes(resource) && !config.auth) {
|
|
20
|
+
return res.status(403).send({
|
|
21
|
+
message: errorMessage.AUTH_SET_TO_FALSE_ERROR,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Redirect this request to the registerUser controller => POST /api/tables/_users/rows
|
|
26
|
+
if (resource === USERS_TABLE && method === httpVerbs.POST) {
|
|
27
|
+
return registerUser(req, res);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Remove some fields for this request and check the username field => PUT /api/tables/_users/rows
|
|
31
|
+
if (resource === USERS_TABLE && method === httpVerbs.PUT) {
|
|
32
|
+
/**
|
|
33
|
+
* remove some user fields from the request like (is_superuser, hashed_password, salt).
|
|
34
|
+
* NOTE: password can be updated via the /change-password API and superuser status can be only updated from the CLI
|
|
35
|
+
*/
|
|
36
|
+
removeFields(
|
|
37
|
+
[req.body.fields],
|
|
38
|
+
[tableFields.SALT, tableFields.IS_SUPERUSER, tableFields.HASHED_PASSWORD],
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
next();
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const processRowResponse = async (req, res, next) => {
|
|
46
|
+
// Extract payload data
|
|
47
|
+
const resource = req.params.name;
|
|
48
|
+
const status = req.response.status;
|
|
49
|
+
const payload = req.response.payload;
|
|
50
|
+
|
|
51
|
+
// Remove some fields from the response
|
|
52
|
+
if (resource === USERS_TABLE) {
|
|
53
|
+
removeFields(payload.data, [tableFields.SALT, tableFields.HASHED_PASSWORD]);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
res.status(status).send(payload);
|
|
57
|
+
next();
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const processTableRequest = async (req, res, next) => {
|
|
61
|
+
const { method, body, baseUrl } = req;
|
|
62
|
+
|
|
63
|
+
// if the user tries to create a table with the reserved table names throw an error. Request => POST /api/tables
|
|
64
|
+
if (baseUrl === apiConstants.baseTableUrl && method === httpVerbs.POST) {
|
|
65
|
+
if (reservedTableNames.includes(body.name)) {
|
|
66
|
+
return res.status(409).send({
|
|
67
|
+
message: errorMessage.RESERVED_TABLE_NAME_ERROR,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
next();
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
module.exports = {
|
|
76
|
+
processRowRequest,
|
|
77
|
+
processRowResponse,
|
|
78
|
+
processTableRequest,
|
|
79
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const config = require('../config');
|
|
2
|
+
const { decodeToken, toBoolean } = require('../utils/index');
|
|
3
|
+
const { apiConstants, responseMessages } = require('../constants');
|
|
4
|
+
|
|
5
|
+
const { errorMessage } = responseMessages;
|
|
6
|
+
|
|
7
|
+
const hasAccess = async (req, res, next) => {
|
|
8
|
+
let payload;
|
|
9
|
+
const { name: tableName } = req.params;
|
|
10
|
+
const verb = req.method;
|
|
11
|
+
const originalURL = req.originalUrl;
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
if (config.auth) {
|
|
15
|
+
// extract the payload from the token and verify it
|
|
16
|
+
try {
|
|
17
|
+
payload = await decodeToken(
|
|
18
|
+
req.cookies.accessToken,
|
|
19
|
+
config.tokenSecret,
|
|
20
|
+
);
|
|
21
|
+
req.user = payload;
|
|
22
|
+
} catch (error) {
|
|
23
|
+
return res
|
|
24
|
+
.status(401)
|
|
25
|
+
.send({ message: errorMessage.INVALID_ACCESS_TOKEN_ERROR });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// if the user is a super_user, allow access on the resource
|
|
29
|
+
if (toBoolean(payload.isSuperuser)) {
|
|
30
|
+
return next();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// if the endpoint is set to be accessed by any user regardless of there roles, then allow access
|
|
34
|
+
if (apiConstants.universalAccessEndpoints.includes(originalURL)) {
|
|
35
|
+
return next();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// if table_name is not passed from the router throw unauthorized error
|
|
39
|
+
if (!tableName) {
|
|
40
|
+
return res
|
|
41
|
+
.status(403)
|
|
42
|
+
.send({ message: errorMessage.NOT_AUTHORIZED_ERROR });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// if the user is not a super user, check the users permission on the resource
|
|
46
|
+
const permissions = payload.permissions.filter((row) => {
|
|
47
|
+
return row.table_name === tableName;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (permissions.length <= 0) {
|
|
51
|
+
return res
|
|
52
|
+
.status(403)
|
|
53
|
+
.send({ message: errorMessage.PERMISSION_NOT_DEFINED_ERROR });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// If the user has permission on the table in at least in one of the roles then allow access on the table
|
|
57
|
+
let hasPermission = false;
|
|
58
|
+
|
|
59
|
+
permissions.some((resource) => {
|
|
60
|
+
const httpMethod =
|
|
61
|
+
apiConstants.httpMethodDefinitions[verb].toLowerCase();
|
|
62
|
+
|
|
63
|
+
if (toBoolean(resource[httpMethod])) {
|
|
64
|
+
hasPermission = true;
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
if (hasPermission) {
|
|
70
|
+
next();
|
|
71
|
+
} else {
|
|
72
|
+
return res
|
|
73
|
+
.status(403)
|
|
74
|
+
.send({ message: errorMessage.NOT_AUTHORIZED_ERROR });
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
next();
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.log(error);
|
|
81
|
+
res.status(401).send({ message: error.message });
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
module.exports = { hasAccess };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const validator = (schema) => (req, res, next) => {
|
|
2
|
-
const { body, params, query } = req;
|
|
3
|
-
const data = { body, params, query };
|
|
2
|
+
const { body, params, query, cookies } = req;
|
|
3
|
+
const data = { body, params, query, cookies };
|
|
4
4
|
|
|
5
5
|
const { value, error } = schema.validate(data);
|
|
6
6
|
|
|
@@ -13,6 +13,7 @@ const validator = (schema) => (req, res, next) => {
|
|
|
13
13
|
req.body = value.body;
|
|
14
14
|
req.params = value.params;
|
|
15
15
|
req.query = value.query;
|
|
16
|
+
req.cookies = value.cookies;
|
|
16
17
|
|
|
17
18
|
next();
|
|
18
19
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
|
|
3
|
+
const controllers = require('../controllers/auth');
|
|
4
|
+
const { validator } = require('../middlewares/validation');
|
|
5
|
+
const schema = require('../schemas/auth');
|
|
6
|
+
const { hasAccess } = require('../middlewares/auth');
|
|
7
|
+
|
|
8
|
+
const router = express.Router();
|
|
9
|
+
|
|
10
|
+
router.post(
|
|
11
|
+
'/token/obtain',
|
|
12
|
+
validator(schema.obtainAccessToken),
|
|
13
|
+
controllers.obtainAccessToken,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
router.get(
|
|
17
|
+
'/token/refresh',
|
|
18
|
+
validator(schema.refreshAccessToken),
|
|
19
|
+
controllers.refreshAccessToken,
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
router.put(
|
|
23
|
+
'/change-password',
|
|
24
|
+
hasAccess,
|
|
25
|
+
validator(schema.changePassword),
|
|
26
|
+
controllers.changePassword,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
module.exports = router;
|
package/src/routes/rows.js
CHANGED
|
@@ -3,37 +3,49 @@ const express = require('express');
|
|
|
3
3
|
const controllers = require('../controllers/rows');
|
|
4
4
|
const { broadcast } = require('../middlewares/broadcast');
|
|
5
5
|
const { validator } = require('../middlewares/validation');
|
|
6
|
+
const { processRowRequest, processRowResponse } = require('../middlewares/api');
|
|
7
|
+
const { hasAccess } = require('../middlewares/auth');
|
|
6
8
|
const schema = require('../schemas/rows');
|
|
7
9
|
|
|
8
10
|
const router = express.Router();
|
|
9
11
|
|
|
10
12
|
router.get(
|
|
11
13
|
'/:name/rows',
|
|
14
|
+
hasAccess,
|
|
12
15
|
validator(schema.listTableRows),
|
|
13
|
-
|
|
16
|
+
processRowRequest,
|
|
17
|
+
controllers.listTableRows,
|
|
18
|
+
processRowResponse,
|
|
14
19
|
);
|
|
15
20
|
router.post(
|
|
16
21
|
'/:name/rows',
|
|
22
|
+
hasAccess,
|
|
17
23
|
validator(schema.insertRowInTable),
|
|
24
|
+
processRowRequest,
|
|
18
25
|
controllers.insertRowInTable,
|
|
19
|
-
broadcast
|
|
26
|
+
broadcast,
|
|
20
27
|
);
|
|
21
28
|
router.get(
|
|
22
29
|
'/:name/rows/:pks',
|
|
30
|
+
hasAccess,
|
|
23
31
|
validator(schema.getRowInTableByPK),
|
|
24
|
-
controllers.getRowInTableByPK
|
|
32
|
+
controllers.getRowInTableByPK,
|
|
33
|
+
processRowResponse,
|
|
25
34
|
);
|
|
26
35
|
router.put(
|
|
27
36
|
'/:name/rows/:pks',
|
|
37
|
+
hasAccess,
|
|
28
38
|
validator(schema.updateRowInTableByPK),
|
|
39
|
+
processRowRequest,
|
|
29
40
|
controllers.updateRowInTableByPK,
|
|
30
|
-
broadcast
|
|
41
|
+
broadcast,
|
|
31
42
|
);
|
|
32
43
|
router.delete(
|
|
33
44
|
'/:name/rows/:pks',
|
|
45
|
+
hasAccess,
|
|
34
46
|
validator(schema.deleteRowInTableByPK),
|
|
35
47
|
controllers.deleteRowInTableByPK,
|
|
36
|
-
broadcast
|
|
48
|
+
broadcast,
|
|
37
49
|
);
|
|
38
50
|
|
|
39
51
|
module.exports = router;
|
package/src/routes/tables.js
CHANGED
|
@@ -3,16 +3,38 @@ const express = require('express');
|
|
|
3
3
|
const controllers = require('../controllers/tables');
|
|
4
4
|
const { validator } = require('../middlewares/validation');
|
|
5
5
|
const schema = require('../schemas/tables');
|
|
6
|
+
const { hasAccess } = require('../middlewares/auth');
|
|
7
|
+
const { processTableRequest } = require('../middlewares/api');
|
|
6
8
|
|
|
7
9
|
const router = express.Router();
|
|
8
10
|
|
|
9
|
-
router.get(
|
|
10
|
-
|
|
11
|
+
router.get(
|
|
12
|
+
'/',
|
|
13
|
+
hasAccess,
|
|
14
|
+
validator(schema.listTables),
|
|
15
|
+
controllers.listTables,
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
router.post(
|
|
19
|
+
'/',
|
|
20
|
+
processTableRequest,
|
|
21
|
+
hasAccess,
|
|
22
|
+
validator(schema.createTable),
|
|
23
|
+
controllers.createTable,
|
|
24
|
+
);
|
|
25
|
+
|
|
11
26
|
router.get(
|
|
12
27
|
'/:name',
|
|
28
|
+
hasAccess,
|
|
13
29
|
validator(schema.getTableSchema),
|
|
14
|
-
controllers.getTableSchema
|
|
30
|
+
controllers.getTableSchema,
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
router.delete(
|
|
34
|
+
'/:name',
|
|
35
|
+
hasAccess,
|
|
36
|
+
validator(schema.deleteTable),
|
|
37
|
+
controllers.deleteTable,
|
|
15
38
|
);
|
|
16
|
-
router.delete('/:name', validator(schema.deleteTable), controllers.deleteTable);
|
|
17
39
|
|
|
18
40
|
module.exports = router;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const Joi = require('joi');
|
|
2
|
+
|
|
3
|
+
const obtainAccessToken = Joi.object({
|
|
4
|
+
query: Joi.object().required(),
|
|
5
|
+
params: Joi.object({}).required(),
|
|
6
|
+
|
|
7
|
+
body: Joi.object({
|
|
8
|
+
fields: Joi.object({
|
|
9
|
+
username: Joi.string().required(),
|
|
10
|
+
password: Joi.string().required(),
|
|
11
|
+
}).required(),
|
|
12
|
+
}).required(),
|
|
13
|
+
|
|
14
|
+
cookies: Joi.object({
|
|
15
|
+
refreshToken: Joi.string().optional(),
|
|
16
|
+
accessToken: Joi.string().optional(),
|
|
17
|
+
}),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const refreshAccessToken = Joi.object({
|
|
21
|
+
query: Joi.object().required(),
|
|
22
|
+
params: Joi.object({}).required(),
|
|
23
|
+
body: Joi.object({}).required(),
|
|
24
|
+
cookies: Joi.object({
|
|
25
|
+
refreshToken: Joi.string().required(),
|
|
26
|
+
accessToken: Joi.string().optional(),
|
|
27
|
+
}).required(),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const changePassword = Joi.object({
|
|
31
|
+
query: Joi.object().required(),
|
|
32
|
+
params: Joi.object().required(),
|
|
33
|
+
|
|
34
|
+
body: Joi.object({
|
|
35
|
+
fields: Joi.object({
|
|
36
|
+
currentPassword: Joi.string().required(),
|
|
37
|
+
newPassword: Joi.string().required(),
|
|
38
|
+
}).required(),
|
|
39
|
+
}).required(),
|
|
40
|
+
cookies: Joi.object({
|
|
41
|
+
accessToken: Joi.string().required(),
|
|
42
|
+
refreshToken: Joi.string().optional(),
|
|
43
|
+
}).required(),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const registerUser = Joi.object({
|
|
47
|
+
query: Joi.object().required(),
|
|
48
|
+
params: Joi.object({}).required(),
|
|
49
|
+
body: Joi.object({
|
|
50
|
+
fields: Joi.object({
|
|
51
|
+
username: Joi.string().required(),
|
|
52
|
+
password: Joi.string().required(),
|
|
53
|
+
}).required(),
|
|
54
|
+
}).required(),
|
|
55
|
+
|
|
56
|
+
cookies: Joi.object({
|
|
57
|
+
accessToken: Joi.string().required(),
|
|
58
|
+
refreshToken: Joi.string().optional(),
|
|
59
|
+
}).required(),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
module.exports = {
|
|
63
|
+
obtainAccessToken,
|
|
64
|
+
refreshAccessToken,
|
|
65
|
+
changePassword,
|
|
66
|
+
registerUser,
|
|
67
|
+
};
|
package/src/schemas/index.js
CHANGED
|
@@ -12,10 +12,15 @@ const transaction = Joi.object({
|
|
|
12
12
|
}),
|
|
13
13
|
Joi.object({
|
|
14
14
|
query: Joi.string().required(),
|
|
15
|
-
})
|
|
15
|
+
}),
|
|
16
16
|
)
|
|
17
17
|
.required(),
|
|
18
18
|
}).required(),
|
|
19
|
+
|
|
20
|
+
cookies: Joi.object({
|
|
21
|
+
refreshToken: Joi.string().optional(),
|
|
22
|
+
accessToken: Joi.string().optional(),
|
|
23
|
+
}),
|
|
19
24
|
});
|
|
20
25
|
|
|
21
26
|
module.exports = {
|
package/src/schemas/rows.js
CHANGED
|
@@ -14,6 +14,10 @@ const listTableRows = Joi.object({
|
|
|
14
14
|
name: Joi.string(),
|
|
15
15
|
}).required(),
|
|
16
16
|
body: Joi.object().required(),
|
|
17
|
+
cookies: Joi.object({
|
|
18
|
+
refreshToken: Joi.string().optional(),
|
|
19
|
+
accessToken: Joi.string().optional(),
|
|
20
|
+
}),
|
|
17
21
|
});
|
|
18
22
|
|
|
19
23
|
const insertRowInTable = Joi.object({
|
|
@@ -28,6 +32,10 @@ const insertRowInTable = Joi.object({
|
|
|
28
32
|
body: Joi.object({
|
|
29
33
|
fields: Joi.object().required(),
|
|
30
34
|
}).required(),
|
|
35
|
+
cookies: Joi.object({
|
|
36
|
+
refreshToken: Joi.string().optional(),
|
|
37
|
+
accessToken: Joi.string().optional(),
|
|
38
|
+
}),
|
|
31
39
|
});
|
|
32
40
|
|
|
33
41
|
const getRowInTableByPK = Joi.object({
|
|
@@ -48,6 +56,10 @@ const getRowInTableByPK = Joi.object({
|
|
|
48
56
|
pks: Joi.string().required(),
|
|
49
57
|
}).required(),
|
|
50
58
|
body: Joi.object().required(),
|
|
59
|
+
cookies: Joi.object({
|
|
60
|
+
refreshToken: Joi.string().optional(),
|
|
61
|
+
accessToken: Joi.string().optional(),
|
|
62
|
+
}),
|
|
51
63
|
});
|
|
52
64
|
|
|
53
65
|
const updateRowInTableByPK = Joi.object({
|
|
@@ -68,6 +80,10 @@ const updateRowInTableByPK = Joi.object({
|
|
|
68
80
|
body: Joi.object({
|
|
69
81
|
fields: Joi.object().required(),
|
|
70
82
|
}).required(),
|
|
83
|
+
cookies: Joi.object({
|
|
84
|
+
refreshToken: Joi.string().optional(),
|
|
85
|
+
accessToken: Joi.string().optional(),
|
|
86
|
+
}),
|
|
71
87
|
});
|
|
72
88
|
|
|
73
89
|
const deleteRowInTableByPK = Joi.object({
|
|
@@ -86,6 +102,10 @@ const deleteRowInTableByPK = Joi.object({
|
|
|
86
102
|
pks: Joi.string().required(),
|
|
87
103
|
}).required(),
|
|
88
104
|
body: Joi.object().required(),
|
|
105
|
+
cookies: Joi.object({
|
|
106
|
+
refreshToken: Joi.string().optional(),
|
|
107
|
+
accessToken: Joi.string().optional(),
|
|
108
|
+
}),
|
|
89
109
|
});
|
|
90
110
|
|
|
91
111
|
module.exports = {
|
package/src/schemas/tables.js
CHANGED
|
@@ -7,6 +7,10 @@ const listTables = Joi.object({
|
|
|
7
7
|
}).required(),
|
|
8
8
|
params: Joi.object().required(),
|
|
9
9
|
body: Joi.object().required(),
|
|
10
|
+
cookies: Joi.object({
|
|
11
|
+
refreshToken: Joi.string().optional(),
|
|
12
|
+
accessToken: Joi.string().optional(),
|
|
13
|
+
}),
|
|
10
14
|
});
|
|
11
15
|
|
|
12
16
|
const createTable = Joi.object({
|
|
@@ -38,7 +42,7 @@ const createTable = Joi.object({
|
|
|
38
42
|
'BLOB',
|
|
39
43
|
'BOOLEAN',
|
|
40
44
|
'DATE',
|
|
41
|
-
'DATETIME'
|
|
45
|
+
'DATETIME',
|
|
42
46
|
)
|
|
43
47
|
.insensitive()
|
|
44
48
|
.required(),
|
|
@@ -67,10 +71,14 @@ const createTable = Joi.object({
|
|
|
67
71
|
.default('RESTRICT'),
|
|
68
72
|
}),
|
|
69
73
|
index: Joi.boolean(),
|
|
70
|
-
})
|
|
74
|
+
}),
|
|
71
75
|
)
|
|
72
76
|
.required(),
|
|
73
77
|
}),
|
|
78
|
+
cookies: Joi.object({
|
|
79
|
+
refreshToken: Joi.string().optional(),
|
|
80
|
+
accessToken: Joi.string().optional(),
|
|
81
|
+
}),
|
|
74
82
|
});
|
|
75
83
|
|
|
76
84
|
const getTableSchema = Joi.object({
|
|
@@ -83,6 +91,10 @@ const getTableSchema = Joi.object({
|
|
|
83
91
|
.required(),
|
|
84
92
|
}),
|
|
85
93
|
body: Joi.object().required(),
|
|
94
|
+
cookies: Joi.object({
|
|
95
|
+
refreshToken: Joi.string().optional(),
|
|
96
|
+
accessToken: Joi.string().optional(),
|
|
97
|
+
}),
|
|
86
98
|
});
|
|
87
99
|
|
|
88
100
|
const deleteTable = Joi.object({
|
|
@@ -95,6 +107,10 @@ const deleteTable = Joi.object({
|
|
|
95
107
|
.required(),
|
|
96
108
|
}),
|
|
97
109
|
body: Joi.object().required(),
|
|
110
|
+
cookies: Joi.object({
|
|
111
|
+
refreshToken: Joi.string().optional(),
|
|
112
|
+
accessToken: Joi.string().optional(),
|
|
113
|
+
}),
|
|
98
114
|
});
|
|
99
115
|
|
|
100
116
|
module.exports = {
|
package/src/server.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
const http = require('http');
|
|
4
|
-
const express = require('express');
|
|
5
4
|
|
|
6
5
|
const app = require('./index');
|
|
7
6
|
const { wss } = require('./websocket');
|
|
@@ -9,7 +8,9 @@ const config = require('./config/index');
|
|
|
9
8
|
|
|
10
9
|
if (config.startWithStudio) {
|
|
11
10
|
(async () => {
|
|
12
|
-
const { handler: soulStudioHandler } = await import(
|
|
11
|
+
const { handler: soulStudioHandler } = await import(
|
|
12
|
+
'soul-studio/build/handler.js'
|
|
13
|
+
);
|
|
13
14
|
app.use('/studio', soulStudioHandler);
|
|
14
15
|
})();
|
|
15
16
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const db = require('../db');
|
|
2
|
+
const rowService = require('./rowService')(db);
|
|
3
|
+
|
|
4
|
+
const { constantRoles, dbConstants } = require('../constants');
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
USERS_TABLE,
|
|
8
|
+
ROLES_TABLE,
|
|
9
|
+
USERS_ROLES_TABLE,
|
|
10
|
+
ROLES_PERMISSIONS_TABLE,
|
|
11
|
+
tableFields,
|
|
12
|
+
} = dbConstants;
|
|
13
|
+
|
|
14
|
+
module.exports = () => {
|
|
15
|
+
return {
|
|
16
|
+
getUsersByUsername({ username }) {
|
|
17
|
+
const users = rowService.get({
|
|
18
|
+
tableName: USERS_TABLE,
|
|
19
|
+
whereString: `WHERE ${tableFields.USERNAME} =?`,
|
|
20
|
+
whereStringValues: [username],
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return users;
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
getUsersById({ userId }) {
|
|
27
|
+
const users = rowService.get({
|
|
28
|
+
tableName: USERS_TABLE,
|
|
29
|
+
whereString: `WHERE ${tableFields.ID}=?`,
|
|
30
|
+
whereStringValues: [userId],
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return users;
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
getAllUsers() {
|
|
37
|
+
const users = rowService.get({
|
|
38
|
+
tableName: USERS_TABLE,
|
|
39
|
+
whereString: '',
|
|
40
|
+
whereStringValues: [],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return users;
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
getPermissionByRoleIds({ roleIds }) {
|
|
47
|
+
const permissions = rowService.get({
|
|
48
|
+
tableName: ROLES_PERMISSIONS_TABLE,
|
|
49
|
+
whereString: `WHERE ${tableFields.ROLE_ID} IN (${roleIds.map(
|
|
50
|
+
() => '?',
|
|
51
|
+
)})`,
|
|
52
|
+
whereStringValues: [...roleIds],
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
return permissions;
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
getUserRoleByUserId({ userId }) {
|
|
59
|
+
const userRoles = rowService.get({
|
|
60
|
+
tableName: USERS_ROLES_TABLE,
|
|
61
|
+
whereString: `WHERE ${tableFields.USER_ID} =?`,
|
|
62
|
+
whereStringValues: [userId],
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return userRoles;
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
getDefaultRole() {
|
|
69
|
+
const defaultRole = rowService.get({
|
|
70
|
+
tableName: ROLES_TABLE,
|
|
71
|
+
whereString: `WHERE ${tableFields.ROLE_NAME}=?`,
|
|
72
|
+
whereStringValues: [constantRoles.DEFAULT_ROLE],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return defaultRole;
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
};
|
package/src/services/index.js
CHANGED
|
@@ -2,5 +2,6 @@ const db = require('../db');
|
|
|
2
2
|
|
|
3
3
|
const rowService = require('./rowService')(db);
|
|
4
4
|
const tableService = require('./tableService')(db);
|
|
5
|
+
const authService = require('./authService')(db);
|
|
5
6
|
|
|
6
|
-
module.exports = { rowService, tableService };
|
|
7
|
+
module.exports = { rowService, tableService, authService };
|