Init commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
const { promisify } = require('util')
|
||||
const jwt = require('jsonwebtoken')
|
||||
const User = require('../components/user/userModel')
|
||||
const AppError = require('../globalErrorHandler/AppError')
|
||||
|
||||
exports.loginProtect = async (req, res, next) => {
|
||||
// 1) Getting token and check if it's there
|
||||
let token
|
||||
|
||||
token = req.cookies.jwt || req.cookies['crm-jwt']
|
||||
|
||||
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer')) {
|
||||
token = req.headers.authorization.split(' ')[1]
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
return next(new AppError('NotLoggedIn', 401))
|
||||
}
|
||||
|
||||
// 2) Verification token
|
||||
let decodedToken
|
||||
try {
|
||||
decodedToken = await promisify(jwt.verify)(token, process.env.JWT_SECRET)
|
||||
} catch (err) {}
|
||||
|
||||
if (!decodedToken) {
|
||||
return next(new AppError('TokenUserNotExist', 401))
|
||||
}
|
||||
|
||||
// 3) check if user still exist
|
||||
const loggedInUser = await User.findById(decodedToken.id).populate('role addresses')
|
||||
|
||||
if (!loggedInUser) {
|
||||
return next(new AppError('TokenUserNotExist', 401, {}))
|
||||
}
|
||||
|
||||
// 4) check if user changed password after the token was issued
|
||||
// if (loggedInUser.changedPasswordAfter(decodedToken.iat)) {
|
||||
// return next(new AppError('UserChangedPasswordRecently', 401))
|
||||
// }
|
||||
|
||||
// GRANT ACCESS TO PROTECTED ROUTE
|
||||
req.user = loggedInUser
|
||||
next()
|
||||
}
|
||||
|
||||
exports.webServiceProtect = async (req, res, next) => {
|
||||
if (req.query.token !== process.env.WEB_SERVICE_TOKEN) {
|
||||
return next(new AppError('NoWebServiceAccess', 401))
|
||||
}
|
||||
|
||||
next()
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
const Permission = require('../components/permission/permissionModel')
|
||||
const Role = require('../components/role/roleModel')
|
||||
const AppError = require('../globalErrorHandler/AppError')
|
||||
const catchAsync = require('../globalErrorHandler/catchAsync')
|
||||
const User = require('../components/user/userModel')
|
||||
const ErrorLocalization = require('../globalErrorHandler/errorLocalization')
|
||||
const el = new ErrorLocalization()
|
||||
|
||||
let checker = (arr, target) => arr.includes(target)
|
||||
|
||||
const requires = (...permissions) => {
|
||||
return async (req, res, next) => {
|
||||
const role = await Role.findById(req.user.role)
|
||||
const perm = await Permission.findOne({ key: permissions[0] })
|
||||
const rolePerm = role?.permissions || []
|
||||
const userPerm = req.user.extraPermissions || []
|
||||
|
||||
// some features: permission denied when all roles are not same --> we want to access user with either roles
|
||||
if (
|
||||
!checker(rolePerm, perm?._id) &&
|
||||
!checker(userPerm, perm?._id) &&
|
||||
role._id.toString() !== process.env.SUPER_ADMIN_ID
|
||||
) {
|
||||
throw new AppError('NoPermissionAccess', 403, {})
|
||||
}
|
||||
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
const adminCheck = () => {
|
||||
return async (req, res, next) => {
|
||||
try {
|
||||
const user = await User.findOne({
|
||||
phoneNumber: req.body.phoneNumber,
|
||||
}).populate('role')
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('UserDoesNotExist', 403, {})
|
||||
}
|
||||
|
||||
const perm = await Permission.findOne(
|
||||
{ key: 'admindashboard' },
|
||||
null,
|
||||
null
|
||||
)
|
||||
let rolePerm = user.role?.permissions || []
|
||||
let userPerm = user.extraPermissions || []
|
||||
// some features: permission denied when all roles are not same --> we want to access user with either roles
|
||||
if (
|
||||
!checker(rolePerm, perm?._id) &&
|
||||
!checker(userPerm, perm?._id) &&
|
||||
user.role?._id.toString() !== process.env.SUPER_ADMIN_ID
|
||||
) {
|
||||
throw new AppError('NoPermissionAccess', 403, {})
|
||||
}
|
||||
|
||||
next()
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
err.message = el.getErrorText(err.name, err.parameters, 'persian')
|
||||
res.status(500).json({
|
||||
status: 'error',
|
||||
message:
|
||||
err.message ||
|
||||
'An error occurred while checking permissions.',
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
checker,
|
||||
requires,
|
||||
adminCheck,
|
||||
}
|
||||
Reference in New Issue
Block a user