create-sipere 1.1.1 → 1.2.1
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 +1 -1
- package/templates/js-rest-api/README.md +5 -1
- package/templates/js-rest-api/app/app.js +2 -0
- package/templates/js-rest-api/app/middleware/authAdmin.js +28 -0
- package/templates/js-rest-api/app/middleware/checkRole.js +34 -0
- package/templates/js-rest-api/app/utils/paths.js +10 -0
- package/test/create-project.spec.js +1 -1
package/package.json
CHANGED
|
@@ -4,11 +4,13 @@ import cors from 'cors'
|
|
|
4
4
|
import fs from 'fs'
|
|
5
5
|
import router from './routes/api.js'
|
|
6
6
|
import './models/modrels.js'
|
|
7
|
+
import { UPLOAD_PATH } from './utils/paths.js'
|
|
7
8
|
|
|
8
9
|
const app = express()
|
|
9
10
|
|
|
10
11
|
const logfile = 'access.log'
|
|
11
12
|
var accessLogStream = fs.createWriteStream(logfile, { flags: 'a' })
|
|
13
|
+
app.use('/images', express.static(UPLOAD_PATH))
|
|
12
14
|
app.use(morgan('dev', { stream: accessLogStream }))
|
|
13
15
|
app.use(cors())
|
|
14
16
|
app.use(express.json())
|
|
@@ -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
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { fileURLToPath } from 'url';
|
|
2
|
+
import { dirname, resolve, join } from 'path';
|
|
3
|
+
|
|
4
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
5
|
+
const __dirname = dirname(__filename);
|
|
6
|
+
|
|
7
|
+
const ROOT_DIR = resolve(__dirname, '../..');
|
|
8
|
+
const UPLOAD_PATH = join(ROOT_DIR, 'public', 'uploads');
|
|
9
|
+
|
|
10
|
+
export { ROOT_DIR, UPLOAD_PATH }
|