|
|
|
|
|
|
|
|
if (req.session === undefined) |
|
|
if (req.session === undefined) |
|
|
next (common.genError (500, "Error")); |
|
|
next (common.genError (500, "Error")); |
|
|
// regenerate a new session-id with clean instance |
|
|
// regenerate a new session-id with clean instance |
|
|
req.session.regenerate (function (err) { |
|
|
|
|
|
if (user !== undefined && ! err) { |
|
|
|
|
|
common.shallowCopy (user, serverVisibleSession, {roles: true}, req.session); |
|
|
|
|
|
console.info(req.session); |
|
|
|
|
|
if (user._id) { |
|
|
|
|
|
req.session.user = user._id; |
|
|
|
|
|
} |
|
|
|
|
|
req.session.roles = roles; |
|
|
|
|
|
} else if (user === undefined && roles === undefined) { |
|
|
|
|
|
// User logged out |
|
|
|
|
|
req.session.destroy(function(err) { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
console.error(err); |
|
|
|
|
|
|
|
|
if (user !== undefined && roles !== undefined) { |
|
|
|
|
|
req.session.regenerate (function (err) { |
|
|
|
|
|
if (user !== undefined && ! err) { |
|
|
|
|
|
common.shallowCopy (user, serverVisibleSession, {roles: true}, req.session); |
|
|
|
|
|
console.info(req.session); |
|
|
|
|
|
if (user._id) { |
|
|
|
|
|
req.session.user = user._id; |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
return cb (err); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
req.session.roles = roles; |
|
|
|
|
|
} |
|
|
|
|
|
return cb (err); |
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
return cb (); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Save found user into DB, if not already exists |
|
|
// Save found user into DB, if not already exists |
|
|
function saveFoundToDB(found) { |
|
|
|
|
|
|
|
|
function saveFoundToDB(found, cb) { |
|
|
console.info(found); |
|
|
console.info(found); |
|
|
Users.findById(found.user) |
|
|
Users.findById(found.user) |
|
|
.exec(function(err, result){ |
|
|
.exec(function(err, result){ |
|
|
|
|
|
|
|
|
name: found.name, |
|
|
name: found.name, |
|
|
mail: found.mail, |
|
|
mail: found.mail, |
|
|
type: found.type, |
|
|
type: found.type, |
|
|
abos: '', |
|
|
|
|
|
bookmarks: '', |
|
|
|
|
|
roles: '', |
|
|
|
|
|
|
|
|
gender: found.gender, |
|
|
|
|
|
// abos: '', |
|
|
|
|
|
// bookmarks: '', |
|
|
|
|
|
roles: 'user', |
|
|
}, function(err, done) { |
|
|
}, function(err, done) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
console.error("User creation: Failed"); |
|
|
console.error("User creation: Failed"); |
|
|
|
|
|
|
|
|
if (done == null) { |
|
|
if (done == null) { |
|
|
console.error("Can not create user."); |
|
|
console.error("Can not create user."); |
|
|
} |
|
|
} |
|
|
|
|
|
return cb(err); |
|
|
}); |
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
return cb(err); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// check local database, then ldap |
|
|
// check local database, then ldap |
|
|
Users.findById (req.body.user) .exec (function (err, entry) { |
|
|
Users.findById (req.body.user) .exec (function (err, entry) { |
|
|
// If there is a local user AND it has a password associated, test against this, and only this |
|
|
// If there is a local user AND it has a password associated, test against this, and only this |
|
|
// if (entry != null && entry.pwd) { |
|
|
|
|
|
// if (crypto.checkLocalAuth (entry, req.body.pwd)) { |
|
|
|
|
|
// return fillSession (req, entry, common.arrayToHash(entry.roles), returnSession); |
|
|
|
|
|
// } |
|
|
|
|
|
// return returnError (); |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
if (entry != null && entry.pwd) { |
|
|
|
|
|
if (crypto.checkLocalAuth (entry, req.body.pwd)) { |
|
|
|
|
|
console.info(entry.roles); |
|
|
|
|
|
return fillSession (req, entry, JSON.stringify(entry.roles), returnSession); |
|
|
|
|
|
} |
|
|
|
|
|
return returnError (); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// check ldap |
|
|
// check ldap |
|
|
ldap.authorize (user.toLowerCase(), pwd, function (found) { |
|
|
ldap.authorize (user.toLowerCase(), pwd, function (found) { |
|
|
|
|
|
|
|
|
entry.orclgender = found.orclgender; |
|
|
entry.orclgender = found.orclgender; |
|
|
return fillSession (req, entry, entry.roles.length > 0 ? common.arrayToHash(entry.roles) : {user:true}, returnSession); |
|
|
return fillSession (req, entry, entry.roles.length > 0 ? common.arrayToHash(entry.roles) : {user:true}, returnSession); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Otherwise create standard user entry |
|
|
// Otherwise create standard user entry |
|
|
saveFoundToDB(found); |
|
|
|
|
|
return fillSession (req, found, {user:true}, returnSession); |
|
|
|
|
|
|
|
|
saveFoundToDB(found, function() { |
|
|
|
|
|
return fillSession (req, found, {user:true}, returnSession); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
logout: function (req, res, next) { |
|
|
logout: function (req, res, next) { |
|
|
fillSession (req, undefined, undefined, function (err) { |
|
|
fillSession (req, undefined, undefined, function (err) { |
|
|
|
|
|
// Session delete, exists further in db |
|
|
|
|
|
req.session.destroy(function(err) { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
console.error(err); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
console.info(req.session); |
|
|
return res.json ({}); |
|
|
return res.json ({}); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |