- aggiunto Elena come admin di tutti i circuiti ...
shared_consts.USER_ADMIN_CIRCUITS
This commit is contained in:
@@ -777,19 +777,60 @@ UserSchema.statics.isFacilitatore = function (perm) {
|
||||
* The status code reflects the validity of the token: valid, expired, or invalid.
|
||||
*/
|
||||
|
||||
// Funzione helper separata per trovare l'utente
|
||||
async function findUserByTokenAndAccess(User, decoded, token, typeaccess, withuser, withlean, project) {
|
||||
try {
|
||||
const query = {
|
||||
_id: decoded._id,
|
||||
tokens: {
|
||||
$elemMatch: {
|
||||
token,
|
||||
access: typeaccess,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (withuser && !withlean) {
|
||||
return await User.findOne(query, project);
|
||||
}
|
||||
|
||||
return await User.findOne(query, project).lean();
|
||||
} catch (err) {
|
||||
console.warn('Errore con decoded._id, provo con decoded.smart:', err.message);
|
||||
|
||||
// Fallback: usa decoded.smart
|
||||
const query = {
|
||||
_id: decoded.smart,
|
||||
tokens: {
|
||||
$elemMatch: {
|
||||
token,
|
||||
access: typeaccess,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (withuser && !withlean) {
|
||||
return await User.findOne(query, project);
|
||||
}
|
||||
|
||||
return await User.findOne(query, project).lean();
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione principale refactored
|
||||
UserSchema.statics.findByToken = async function (token, typeaccess, con_auth, withuser, withlean = false) {
|
||||
const User = this;
|
||||
let code = server_constants.RIS_CODE_HTTP_INVALID_TOKEN;
|
||||
let user = null;
|
||||
let decoded;
|
||||
const start = process.hrtime.bigint();
|
||||
const start_jwt = process.hrtime.bigint();
|
||||
|
||||
// Validazione token
|
||||
if (!token) {
|
||||
console.warn('TOKEN VUOTO ! ');
|
||||
console.warn('TOKEN VUOTO!');
|
||||
return { user, code };
|
||||
}
|
||||
|
||||
// Verifica JWT
|
||||
try {
|
||||
decoded = jwt.verify(token, process.env.SIGNCODE);
|
||||
code = server_constants.RIS_CODE_OK;
|
||||
@@ -803,83 +844,32 @@ UserSchema.statics.findByToken = async function (token, typeaccess, con_auth, wi
|
||||
return { user: null, code };
|
||||
}
|
||||
|
||||
const end_jwt = process.hrtime.bigint();
|
||||
// console.log(` jwt.verify impiega ${Math.round(Number(end_jwt - start_jwt) / 1e6) / 1000} secondi.`);
|
||||
// Definizione projection
|
||||
const project = withuser ? undefined : {
|
||||
perm: 1,
|
||||
_id: 1,
|
||||
idapp: 1,
|
||||
username: 1,
|
||||
deleted: 1,
|
||||
aportador_solidario: 1,
|
||||
aportador_solidario_nome_completo: 1,
|
||||
'profile.socioresidente': 1,
|
||||
};
|
||||
|
||||
let project = undefined;
|
||||
|
||||
if (withuser) {
|
||||
const start_find = process.hrtime.bigint();
|
||||
if (withlean) {
|
||||
user = await User.findOne(
|
||||
{
|
||||
_id: decoded._id,
|
||||
tokens: {
|
||||
$elemMatch: {
|
||||
token,
|
||||
access: typeaccess,
|
||||
},
|
||||
},
|
||||
},
|
||||
project
|
||||
).lean();
|
||||
} else {
|
||||
user = await User.findOne(
|
||||
{
|
||||
_id: decoded._id,
|
||||
tokens: {
|
||||
$elemMatch: {
|
||||
token,
|
||||
access: typeaccess,
|
||||
},
|
||||
},
|
||||
},
|
||||
project
|
||||
);
|
||||
}
|
||||
const end_find = process.hrtime.bigint();
|
||||
// console.log(` User.findOne impiega ${Math.round(Number(end_find - start_find) / 1e6) / 1000} secondi.`);
|
||||
} else {
|
||||
project = {
|
||||
perm: 1,
|
||||
_id: 1,
|
||||
idapp: 1,
|
||||
username: 1,
|
||||
deleted: 1,
|
||||
aportador_solidario: 1,
|
||||
aportador_solidario_nome_completo: 1,
|
||||
'profile.socioresidente': 1,
|
||||
};
|
||||
|
||||
const start_find = process.hrtime.bigint();
|
||||
user = await User.findOne(
|
||||
{
|
||||
_id: decoded._id,
|
||||
tokens: {
|
||||
$elemMatch: {
|
||||
token,
|
||||
access: typeaccess,
|
||||
},
|
||||
},
|
||||
},
|
||||
project
|
||||
).lean();
|
||||
const end_find = process.hrtime.bigint();
|
||||
// console.log(` User.findOne LEAN impiega ${Math.round(Number(end_find - start_find) / 1e6) / 1000} secondi.`);
|
||||
}
|
||||
// Ricerca utente con funzione separata
|
||||
user = await findUserByTokenAndAccess(User, decoded, token, typeaccess, withuser, withlean, project);
|
||||
|
||||
// Verifica scadenza token per idapp specifici
|
||||
if (user) {
|
||||
const checkExpiry = tools.getEnableTokenExpiredByIdApp(user.idapp);
|
||||
const currentTime = Date.now() / 1000;
|
||||
|
||||
if (checkExpiry && decoded.exp < currentTime) {
|
||||
console.log('Il token è scaduto, generazione del nuovo token...');
|
||||
code = server_constants.RIS_CODE_HTTP_FORBIDDEN_TOKEN_EXPIRED;
|
||||
}
|
||||
}
|
||||
|
||||
// const end = process.hrtime.bigint();
|
||||
// console.log(` findByToken impiega ${Math.round(Number(end - start) / 1e6) / 1000} secondi.`);
|
||||
|
||||
return { user, code };
|
||||
};
|
||||
|
||||
@@ -900,7 +890,7 @@ UserSchema.statics.findByTokenAnyAccess = function (token) {
|
||||
}).lean();
|
||||
};
|
||||
|
||||
UserSchema.statics.findByRefreshTokenAnyAccess = function (refreshToken) {
|
||||
UserSchema.statics.findByRefreshTokenAnyAccess = async function (refreshToken) {
|
||||
const User = this;
|
||||
let decoded;
|
||||
|
||||
@@ -911,12 +901,25 @@ UserSchema.statics.findByRefreshTokenAnyAccess = function (refreshToken) {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
|
||||
return User.findOne({
|
||||
_id: decoded._id,
|
||||
'tokens.refreshToken': refreshToken,
|
||||
});
|
||||
let ris = null;
|
||||
|
||||
if (decoded) {
|
||||
try {
|
||||
ris = await User.findOne({
|
||||
_id: decoded._id,
|
||||
'tokens.refreshToken': refreshToken,
|
||||
});
|
||||
} catch (e) {
|
||||
ris = await User.findOne({
|
||||
_id: decoded.smart,
|
||||
'tokens.refreshToken': refreshToken,
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
UserSchema.statics.findByCredentials = async function (idapp, username, password, pwdcrypted) {
|
||||
const User = this;
|
||||
let pwd = '';
|
||||
|
||||
Reference in New Issue
Block a user