-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
611 additions
and
694 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,61 @@ | ||
const axios = require('axios'); | ||
const axios = require('axios'); | ||
const config = require('./config') | ||
exports.statusUser = async (session) => { | ||
let userConnected = { | ||
auth : false, | ||
userPermissions : -1 | ||
auth: false, | ||
userPermissions: -1 | ||
} | ||
|
||
if(!session || !session.user) { //User non connecté | ||
if (!session || !session.user) { //User non connecté | ||
return userConnected = { | ||
auth : false, | ||
userPermissions : -1, | ||
auth: false, | ||
userPermissions: -1, | ||
id: -1, | ||
userAvatar : "default.png" | ||
userAvatar: "default.png" | ||
} | ||
}else{ | ||
const msgs = await axios.get(`${config.urlAPI}dm/nb-msgs-new/${session.user.id}`,{ headers : { 'Authorization' : `token ${session.user.token}`}}) | ||
} else { | ||
const msgs = await axios.get(`${config.urlAPI}dm/nb-msgs-new/${session.user.id}`, { headers: { 'Authorization': `token ${session.user.token}` } }) | ||
return userConnected = { | ||
auth : true, | ||
auth: true, | ||
id: session.user.id, | ||
userPermissions : session.user.userPermissions, | ||
userAvatar : session.user.userAvatar, | ||
nbMsgs : msgs.data.result.nb_msgs || 0 | ||
userPermissions: session.user.userPermissions, | ||
permissions: this.convertPermissions(session.user.userPermissions), | ||
userAvatar: session.user.userAvatar, | ||
nbMsgs: msgs.data.result.nb_msgs || 0 | ||
} | ||
} | ||
} | ||
|
||
exports.convertPermissions = (userPermsNumber) => { | ||
let userPermissions = []; | ||
const permissions = [ | ||
{ value: 256, permission: 'MANAGE_ALBUM' }, | ||
{ value: 128, permission: 'MANAGE_ANNOUNCEMENTS' }, | ||
{ value: 64, permission: 'MANAGE_REQUESTS' }, | ||
{ value: 32, permission: 'MANAGE_FORUM' }, | ||
{ value: 16, permission: 'MANAGE_ARTICLES' }, | ||
{ value: 8, permission: 'MANAGE_MEMBERS' }, | ||
{ value: 4, permission: 'MODERATOR' }, | ||
{ value: 2, permission: 'STAFF' }, | ||
{ value: 1, permission: 'ADMINISTRATOR' }, | ||
] | ||
for (let permission of permissions) { | ||
const rest = userPermsNumber % permission.value; | ||
if (rest == 0 && userPermsNumber != 0) { | ||
userPermissions.push(permission.permission); | ||
break; | ||
} | ||
if (rest < userPermsNumber) { | ||
userPermissions.push(permission.permission); | ||
userPermsNumber = rest | ||
} | ||
} | ||
return userPermissions; | ||
} | ||
exports.hasPermissions = (userPermissions, permissionsRequested) => { | ||
if (userPermissions[0].permission === 'ADMINISTRATOR') return true; | ||
for (permRequested of permissionsRequested) { | ||
if (!permissionsRequested.includes(permRequested)) return false; | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
|
||
const { convertPermissions } = require('../functions') | ||
module.exports = async (req, res, next) => { | ||
try{ | ||
if(!req.session || !req.session.user) throw 'Missing session' | ||
else if(req.session.user && req.session.user.userPermissions < 3) throw 'Missing permissions' | ||
next() | ||
}catch(err){ | ||
try { | ||
const userPermissions = convertPermissions(req.session.user.userPermissions) | ||
req.user = { | ||
permissions: userPermissions | ||
} | ||
if (userPermissions.length) next() | ||
else throw 'Bad permissions' | ||
} catch (err) { | ||
res.status(401).redirect('/') | ||
//json(checkAndChange(new Error('Requete non authentifiée'))); | ||
} | ||
}; |
Oops, something went wrong.