77 lines
No EOL
2.2 KiB
JavaScript
77 lines
No EOL
2.2 KiB
JavaScript
let satitm_directory = require('./config/ldap.js');
|
|
// Search for a user in the directory
|
|
async function queryUser(upn, attributes) {
|
|
return new Promise((resolve, reject) => {
|
|
let opts = {
|
|
filter: `(userPrincipalName=${upn})`,
|
|
scope: 'sub',
|
|
attributes: attributes
|
|
};
|
|
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));
|
|
resolve(entry.object);
|
|
});
|
|
ldapRes.on('error', function(err) {
|
|
console.error('error: ' + err.message);
|
|
reject(err);
|
|
});
|
|
ldapRes.on('end', function(result) {
|
|
console.log('status: ' + result.status);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function setAttribute(upn, attribute, value, callback) {
|
|
// First, get DN of the user from the UPN
|
|
let attributes = ['dn'];
|
|
|
|
}
|
|
|
|
// 0: Unkown, 1: Student, 2: Parent
|
|
const USER_TYPE = {
|
|
UNKNOWN: 0,
|
|
STUDENT: 1,
|
|
PARENT: 2
|
|
};
|
|
|
|
// 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) {
|
|
// The user's DN is present in the session as req.user.dn
|
|
// To convert DN to OU, remove from first CN= to first ,
|
|
let ou = req.user.dn.substring(req.user.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;
|
|
}
|
|
else if (ou === 'OU=Parents,DC=ad,DC=satitm,DC=chula,DC=ac,DC=th') {
|
|
return USER_TYPE.PARENT;
|
|
}
|
|
else {
|
|
return USER_TYPE.UNKNOWN;
|
|
}
|
|
}
|
|
|
|
async function getPrimaryParent(student_upn, callback) {
|
|
return new Promise((resolve, reject) => {
|
|
// Query primaryParent attribute in the student's LDAP entry
|
|
let attributes = ['primaryParent'];
|
|
queryUser(student_upn, attributes, function(err, student) {
|
|
if (err) {
|
|
reject(err);
|
|
} else {
|
|
let primaryParent = student.primaryParent;
|
|
resolve(primaryParent);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
queryUser: queryUser,
|
|
getUserType: getUserType,
|
|
USER_TYPE: USER_TYPE
|
|
}; |