32 lines
812 B
JavaScript
32 lines
812 B
JavaScript
const { UserTypes, Errors } = require('./constants'),
|
|
{ User } = require('../model'),
|
|
{ AppError, AppAuthError } = require('./app_error');
|
|
|
|
function getPuid(req, isObject = true, roles = []) {
|
|
let puid;
|
|
if (req.ut === UserTypes.APP) {
|
|
puid = req.uid;
|
|
} else if (roles.includes(req.ut)) {
|
|
if (!req.userInfo?.puid) {
|
|
AppError.throw(Errors.PARENT_NOT_EXIST);
|
|
}
|
|
puid = req.userInfo?.puid;
|
|
}
|
|
if (puid) return isObject ? { byPuid: puid } : puid;
|
|
AppError.throw(Errors.NO_ACCESS);
|
|
}
|
|
|
|
async function checkUserClient(userId, req) {
|
|
const user = await User.findOne({
|
|
_id: userId,
|
|
kind: UserTypes.CLIENT,
|
|
parent: getPuid(req, false),
|
|
});
|
|
if (!user) {
|
|
AppAuthError.throw(Errors.USER_NOT_FOUND);
|
|
}
|
|
return user;
|
|
}
|
|
|
|
module.exports = { getPuid, checkUserClient };
|