working ish website

This commit is contained in:
Siwat Sirichai 2024-05-05 18:13:58 +07:00
parent 5540ac6d81
commit b194d2031d
8 changed files with 271 additions and 6 deletions

View file

@ -67,10 +67,19 @@ const USER_TYPE = {
// Determine the type of user
// Student is in OU=Students,OU=Users,DC=ad,DC=satitm,DC=chula,DC=ac,DC=th
// Parent is in OU=Parents,OU=Users,DC=ad,DC=satitm,DC=chula,DC=ac,DC=th
function getUserType(req, res) {
function getUserType(req) {
// The user's DN is present in the session as req.user.dn
if (req.user) {
return getUserTypeFromDN(req.user.dn);
}
else {
return USER_TYPE.UNKNOWN;
}
}
function getUserTypeFromDN(dn) {
// To convert DN to OU, remove from first CN= to first ,
let ou = req.user.dn.substring(req.user.dn.indexOf(',') + 1);
let ou = dn.substring(dn.indexOf(',') + 1);
console.log('OU:', ou);
if (ou === 'OU=Students,DC=ad,DC=satitm,DC=chula,DC=ac,DC=th') {
return USER_TYPE.STUDENT;
@ -96,9 +105,38 @@ async function getPrimaryParent(student_upn) {
}
}
async function listStudents(upn) {
// Search for students with the parent's UPN in their primaryParent attribute
let opts = {
filter: `(primaryParent=${upn})`,
scope: 'sub',
attributes: ['userPrincipalName']
};
let students = [];
return new Promise((resolve, reject) => {
satitm_directory.search('DC=ad,DC=satitm,DC=chula,DC=ac,DC=th', opts, function(err, ldapRes) {
ldapRes.on('searchEntry', function(entry) {
console.log('entry: ' + JSON.stringify(entry.object));
students.push(entry.object.userPrincipalName);
});
ldapRes.on('error', function(err) {
console.error('error: ' + err.message);
reject(err);
});
ldapRes.on('end', function(result) {
console.log('status: ' + result.status);
resolve(students);
});
});
});
}
module.exports = {
queryUser: queryUser,
getUserType: getUserType,
getUserTypeFromDN: getUserTypeFromDN,
setPrimaryParent: setPrimaryParent,
listStudents: listStudents,
getPrimaryParent: getPrimaryParent,
USER_TYPE: USER_TYPE
};