soul-cli 0.7.1 → 0.7.2
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/assets/css/style.scss +33 -0
- package/docs/api-examples.md +10 -9
- package/docs/ws-examples.md +3 -2
- package/package.json +1 -1
- package/src/commands.js +1 -2
- package/src/controllers/auth/tables.js +4 -4
- package/src/controllers/auth/token.js +2 -6
- package/src/controllers/auth/user.js +1 -1
- package/src/controllers/rows.js +1 -1
- package/src/middlewares/api.js +23 -1
- package/src/middlewares/auth.js +10 -5
- package/src/middlewares/validation.js +18 -0
- package/src/schemas/auth.js +21 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
---
|
|
2
|
+
---
|
|
3
|
+
@import "https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.5.1/github-markdown.min.css";
|
|
4
|
+
|
|
5
|
+
body {
|
|
6
|
+
margin: 0;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
@media (prefers-color-scheme: light) {
|
|
10
|
+
body {
|
|
11
|
+
background-color: #ffffff;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@media (prefers-color-scheme: dark) {
|
|
16
|
+
body {
|
|
17
|
+
background-color: #0d1117;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.markdown-body {
|
|
22
|
+
box-sizing: border-box;
|
|
23
|
+
min-width: 200px;
|
|
24
|
+
max-width: 980px;
|
|
25
|
+
margin: 0 auto;
|
|
26
|
+
padding: 45px;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@media (max-width: 767px) {
|
|
30
|
+
.markdown-body {
|
|
31
|
+
padding: 15px;
|
|
32
|
+
}
|
|
33
|
+
}
|
package/docs/api-examples.md
CHANGED
|
@@ -11,7 +11,9 @@ To follow the below examples we need to download a sample database and also inst
|
|
|
11
11
|
```bash
|
|
12
12
|
wget https://raw.githubusercontent.com/lerocha/chinook-database/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite # Download sample sqlite database
|
|
13
13
|
```
|
|
14
|
+
|
|
14
15
|
### Using Soul CLI
|
|
16
|
+
|
|
15
17
|
```bash
|
|
16
18
|
npm install -g soul-cli
|
|
17
19
|
soul -d ./Chinook_Sqlite.sqlite -p 8000
|
|
@@ -26,15 +28,14 @@ npm install # Install dependencies
|
|
|
26
28
|
npm link # might need `sudo`
|
|
27
29
|
soul -d ./Chinook_Sqlite.sqlite -p 8000
|
|
28
30
|
```
|
|
29
|
-
</details>
|
|
30
31
|
|
|
32
|
+
</details>
|
|
31
33
|
|
|
32
34
|
## Namespaces
|
|
33
35
|
|
|
34
|
-
1. [/api/tables](
|
|
35
|
-
2. [/api/rows](
|
|
36
|
-
3. [/api/](
|
|
37
|
-
|
|
36
|
+
1. [/api/tables](api/tables-examples.md) Examples for Tables endpoints
|
|
37
|
+
2. [/api/rows](api/rows-examples.md) Examples for Rows endpoints
|
|
38
|
+
3. [/api/](api/root-examples.md) Examples for Root endpoints
|
|
38
39
|
|
|
39
40
|
## Handling Errors
|
|
40
41
|
|
|
@@ -54,9 +55,9 @@ curl --request POST \
|
|
|
54
55
|
|
|
55
56
|
```json
|
|
56
57
|
{
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
"message": "table artists already exists",
|
|
59
|
+
"error": {
|
|
60
|
+
"code": "SQLITE_ERROR"
|
|
61
|
+
}
|
|
61
62
|
}
|
|
62
63
|
```
|
package/docs/ws-examples.md
CHANGED
|
@@ -20,6 +20,7 @@ npm i -g wscat # Install websocket client
|
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
### Using Soul CLI
|
|
23
|
+
|
|
23
24
|
```bash
|
|
24
25
|
npm install -g soul-cli
|
|
25
26
|
soul -d ./Chinook_Sqlite.sqlite -p 8000
|
|
@@ -34,9 +35,9 @@ npm install # Install dependencies
|
|
|
34
35
|
npm link # might need `sudo`
|
|
35
36
|
soul -d ./Chinook_Sqlite.sqlite -p 8000
|
|
36
37
|
```
|
|
37
|
-
</details>
|
|
38
38
|
|
|
39
|
+
</details>
|
|
39
40
|
|
|
40
41
|
## Namespaces
|
|
41
42
|
|
|
42
|
-
1. [/ws/tables](
|
|
43
|
+
1. [/ws/tables](ws/rows-examples.md) Examples for Rows websockets
|
package/package.json
CHANGED
package/src/commands.js
CHANGED
|
@@ -7,8 +7,7 @@ const runCLICommands = () => {
|
|
|
7
7
|
// if the updatesuperuser command is passed from the CLI execute the updatesuperuser function
|
|
8
8
|
if (argv._.includes('updatesuperuser')) {
|
|
9
9
|
const { id, password, is_superuser } = argv;
|
|
10
|
-
|
|
11
|
-
if (!password && !is_superuser) {
|
|
10
|
+
if (!password && is_superuser === undefined) {
|
|
12
11
|
console.log(
|
|
13
12
|
'Please provide either the --password or --is_superuser flag when using the updateuser command.',
|
|
14
13
|
);
|
|
@@ -76,10 +76,10 @@ const createDefaultTables = async () => {
|
|
|
76
76
|
permissions.push({
|
|
77
77
|
role_id: roleId,
|
|
78
78
|
table_name: table.name,
|
|
79
|
-
create:
|
|
80
|
-
read:
|
|
81
|
-
update:
|
|
82
|
-
delete:
|
|
79
|
+
create: 0,
|
|
80
|
+
read: 1,
|
|
81
|
+
update: 0,
|
|
82
|
+
delete: 0,
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
85
|
|
|
@@ -55,7 +55,7 @@ const obtainAccessToken = async (req, res) => {
|
|
|
55
55
|
*/
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
let
|
|
58
|
+
let roleIds;
|
|
59
59
|
|
|
60
60
|
// if the user is not a superuser get the role and its permission from the DB
|
|
61
61
|
if (!toBoolean(user.is_superuser)) {
|
|
@@ -64,7 +64,6 @@ const obtainAccessToken = async (req, res) => {
|
|
|
64
64
|
res,
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
-
permissions = roleData.permissions;
|
|
68
67
|
roleIds = roleData.roleIds;
|
|
69
68
|
}
|
|
70
69
|
|
|
@@ -73,7 +72,6 @@ const obtainAccessToken = async (req, res) => {
|
|
|
73
72
|
userId: user.id,
|
|
74
73
|
isSuperuser: user.is_superuser,
|
|
75
74
|
roleIds,
|
|
76
|
-
permissions,
|
|
77
75
|
};
|
|
78
76
|
|
|
79
77
|
// generate an access token
|
|
@@ -151,7 +149,7 @@ const refreshAccessToken = async (req, res) => {
|
|
|
151
149
|
*/
|
|
152
150
|
}
|
|
153
151
|
|
|
154
|
-
let
|
|
152
|
+
let roleIds;
|
|
155
153
|
const user = users[0];
|
|
156
154
|
|
|
157
155
|
// if the user is not a superuser get the role and its permission from the DB
|
|
@@ -161,7 +159,6 @@ const refreshAccessToken = async (req, res) => {
|
|
|
161
159
|
res,
|
|
162
160
|
});
|
|
163
161
|
|
|
164
|
-
permissions = roleData.permissions;
|
|
165
162
|
roleIds = roleData.roleIds;
|
|
166
163
|
}
|
|
167
164
|
|
|
@@ -170,7 +167,6 @@ const refreshAccessToken = async (req, res) => {
|
|
|
170
167
|
userId: user.id,
|
|
171
168
|
isSuperuser: user.is_superuser,
|
|
172
169
|
roleIds,
|
|
173
|
-
permissions,
|
|
174
170
|
};
|
|
175
171
|
|
|
176
172
|
// generate an access token
|
|
@@ -25,7 +25,7 @@ const updateSuperuser = async (fields) => {
|
|
|
25
25
|
|
|
26
26
|
try {
|
|
27
27
|
// find the user by using the id field
|
|
28
|
-
const users = authService.
|
|
28
|
+
const users = authService.getUsersById({ userId: id });
|
|
29
29
|
|
|
30
30
|
// abort if the id is invalid
|
|
31
31
|
if (users.length === 0) {
|
package/src/controllers/rows.js
CHANGED
package/src/middlewares/api.js
CHANGED
|
@@ -6,9 +6,16 @@ const {
|
|
|
6
6
|
responseMessages,
|
|
7
7
|
} = require('../constants/');
|
|
8
8
|
const { removeFields } = require('../utils');
|
|
9
|
+
const { customValidator } = require('../middlewares/validation');
|
|
10
|
+
const schema = require('../schemas/auth');
|
|
9
11
|
|
|
10
12
|
const { httpVerbs } = apiConstants;
|
|
11
|
-
const {
|
|
13
|
+
const {
|
|
14
|
+
reservedTableNames,
|
|
15
|
+
USERS_TABLE,
|
|
16
|
+
ROLES_PERMISSIONS_TABLE,
|
|
17
|
+
tableFields,
|
|
18
|
+
} = dbConstants;
|
|
12
19
|
const { errorMessage } = responseMessages;
|
|
13
20
|
|
|
14
21
|
const processRowRequest = async (req, res, next) => {
|
|
@@ -39,6 +46,21 @@ const processRowRequest = async (req, res, next) => {
|
|
|
39
46
|
);
|
|
40
47
|
}
|
|
41
48
|
|
|
49
|
+
// Validate fields for the _roles_permission API on POST and PUT requests
|
|
50
|
+
if (
|
|
51
|
+
resource === ROLES_PERMISSIONS_TABLE &&
|
|
52
|
+
(method === httpVerbs.POST || method === httpVerbs.PUT)
|
|
53
|
+
) {
|
|
54
|
+
const validation = customValidator(schema.updateRolePermissions)(req);
|
|
55
|
+
|
|
56
|
+
if (validation.errorStatus) {
|
|
57
|
+
return res.status(400).json({
|
|
58
|
+
message: validation.message,
|
|
59
|
+
error: validation.details,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
42
64
|
next();
|
|
43
65
|
};
|
|
44
66
|
|
package/src/middlewares/auth.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const config = require('../config');
|
|
2
2
|
const { decodeToken, toBoolean } = require('../utils/index');
|
|
3
3
|
const { apiConstants, responseMessages } = require('../constants');
|
|
4
|
+
const { authService } = require('../services');
|
|
4
5
|
|
|
5
6
|
const { errorMessage } = responseMessages;
|
|
6
7
|
|
|
@@ -42,12 +43,16 @@ const hasAccess = async (req, res, next) => {
|
|
|
42
43
|
.send({ message: errorMessage.NOT_AUTHORIZED_ERROR });
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
// if the user is not a super user,
|
|
46
|
-
const
|
|
47
|
-
|
|
46
|
+
// if the user is not a super user, fetch the permission of the user from the DB
|
|
47
|
+
const rolePermissions = authService.getPermissionByRoleIds({
|
|
48
|
+
roleIds: payload.roleIds,
|
|
48
49
|
});
|
|
49
50
|
|
|
50
|
-
|
|
51
|
+
const resourcePermission = rolePermissions.filter(
|
|
52
|
+
(row) => row.table_name === tableName,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
if (resourcePermission.length <= 0) {
|
|
51
56
|
return res
|
|
52
57
|
.status(403)
|
|
53
58
|
.send({ message: errorMessage.PERMISSION_NOT_DEFINED_ERROR });
|
|
@@ -56,7 +61,7 @@ const hasAccess = async (req, res, next) => {
|
|
|
56
61
|
// If the user has permission on the table in at least in one of the roles then allow access on the table
|
|
57
62
|
let hasPermission = false;
|
|
58
63
|
|
|
59
|
-
|
|
64
|
+
resourcePermission.some((resource) => {
|
|
60
65
|
const httpMethod =
|
|
61
66
|
apiConstants.httpMethodDefinitions[verb].toLowerCase();
|
|
62
67
|
|
|
@@ -19,6 +19,24 @@ const validator = (schema) => (req, res, next) => {
|
|
|
19
19
|
}
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
+
const customValidator = (schema) => (req) => {
|
|
23
|
+
const response = { errorStatus: false, message: '', error: '' };
|
|
24
|
+
|
|
25
|
+
const { body, params, query, cookies } = req;
|
|
26
|
+
const data = { body, params, query, cookies };
|
|
27
|
+
|
|
28
|
+
const { error } = schema.validate(data);
|
|
29
|
+
|
|
30
|
+
if (error) {
|
|
31
|
+
response.errorStatus = true;
|
|
32
|
+
response.message = error.message;
|
|
33
|
+
response.error = error.details;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return response;
|
|
37
|
+
};
|
|
38
|
+
|
|
22
39
|
module.exports = {
|
|
23
40
|
validator,
|
|
41
|
+
customValidator,
|
|
24
42
|
};
|
package/src/schemas/auth.js
CHANGED
|
@@ -59,9 +59,30 @@ const registerUser = Joi.object({
|
|
|
59
59
|
}).required(),
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
+
const updateRolePermissions = Joi.object({
|
|
63
|
+
query: Joi.object({}).required(),
|
|
64
|
+
params: Joi.object({ name: Joi.string(), pks: Joi.string() }).required(),
|
|
65
|
+
body: Joi.object({
|
|
66
|
+
fields: Joi.object({
|
|
67
|
+
role_id: Joi.number().required(),
|
|
68
|
+
table_name: Joi.string().required(),
|
|
69
|
+
create: Joi.number().valid(0, 1).required(),
|
|
70
|
+
read: Joi.number().valid(0, 1).required(),
|
|
71
|
+
update: Joi.number().valid(0, 1).required(),
|
|
72
|
+
delete: Joi.number().valid(0, 1).required(),
|
|
73
|
+
}).required(),
|
|
74
|
+
}).required(),
|
|
75
|
+
|
|
76
|
+
cookies: Joi.object({
|
|
77
|
+
accessToken: Joi.string().required(),
|
|
78
|
+
refreshToken: Joi.string().optional(),
|
|
79
|
+
}).required(),
|
|
80
|
+
});
|
|
81
|
+
|
|
62
82
|
module.exports = {
|
|
63
83
|
obtainAccessToken,
|
|
64
84
|
refreshAccessToken,
|
|
65
85
|
changePassword,
|
|
66
86
|
registerUser,
|
|
87
|
+
updateRolePermissions,
|
|
67
88
|
};
|