create-sipere 1.1.1 → 1.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-sipere",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "main": "create-sipere.js",
5
5
  "bin": {
6
6
  "create-sipere": "create-sipere.js"
@@ -1,6 +1,10 @@
1
1
  # sipere expressapi template
2
2
 
3
- Express based REST API template
3
+ Express based REST API template.
4
+
5
+ Website:
6
+
7
+ * [https://sipere.github.io/sipere/](https://sipere.github.io/sipere/)
4
8
 
5
9
  ## Install dependencies
6
10
 
@@ -0,0 +1,28 @@
1
+ import User from '../models/user.js'
2
+
3
+ const isAdmin = async (req, res, next) => {
4
+ try {
5
+ const user = await User.findByPk(req.userId)
6
+ if(!user) {
7
+ return res.status(404).json({
8
+ success: false,
9
+ message: 'User not found'
10
+ })
11
+ }
12
+ if(user.roleId === 1) {
13
+ next()
14
+ }else {
15
+ return res.status(403).json({
16
+ success: false,
17
+ message: 'You are not admin'
18
+ })
19
+ }
20
+ }catch(err) {
21
+ return res.status(500).json({
22
+ success: false,
23
+ message: err.message
24
+ })
25
+ }
26
+ }
27
+
28
+ export default isAdmin
@@ -0,0 +1,34 @@
1
+ import User from '../models/user.js'
2
+
3
+ const checkRole = (requiredRole) => {
4
+ return async (req, res, next) => {
5
+ try {
6
+ const user = await User.findByPk(req.userId)
7
+ if(!user) {
8
+ return res.status(404).json({
9
+ success: false,
10
+ message: 'User not found'
11
+ })
12
+ }
13
+ const roles = [0, 1, 2]
14
+ const userRoleLevel = roles.indexOf(user.roleId)
15
+ const requiredRoleLevel = roles.indexOf(requiredRole)
16
+
17
+ if(userRoleLevel >= requiredRoleLevel) {
18
+ next()
19
+ }else {
20
+ return res.status(403).json({
21
+ success: false,
22
+ message: 'You are not allowed to do this action'
23
+ })
24
+ }
25
+ }catch(err) {
26
+ return res.status(500).json({
27
+ success: false,
28
+ message: err.message
29
+ })
30
+ }
31
+ }
32
+ }
33
+
34
+ export default checkRole
@@ -2,7 +2,7 @@ const { exec } = require('child_process');
2
2
  const assert = require('assert');
3
3
 
4
4
  describe('create-sipere', function() {
5
- this.timeout(8000);
5
+ this.timeout(14000);
6
6
  it('create project', (done) => {
7
7
  const cmd = './create-sipere.js';
8
8
  const child = exec(cmd);