|
|
@@ -18,28 +18,25 @@ function fillSession (req, user, roles, cb) { |
|
|
|
if (req.session === undefined) |
|
|
|
next (common.genError (500, "Error")); |
|
|
|
// 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 |
|
|
|
function saveFoundToDB(found) { |
|
|
|
function saveFoundToDB(found, cb) { |
|
|
|
console.info(found); |
|
|
|
Users.findById(found.user) |
|
|
|
.exec(function(err, result){ |
|
|
@@ -54,9 +51,10 @@ function saveFoundToDB(found) { |
|
|
|
name: found.name, |
|
|
|
mail: found.mail, |
|
|
|
type: found.type, |
|
|
|
abos: '', |
|
|
|
bookmarks: '', |
|
|
|
roles: '', |
|
|
|
gender: found.gender, |
|
|
|
// abos: '', |
|
|
|
// bookmarks: '', |
|
|
|
roles: 'user', |
|
|
|
}, function(err, done) { |
|
|
|
if (err) { |
|
|
|
console.error("User creation: Failed"); |
|
|
@@ -67,7 +65,10 @@ function saveFoundToDB(found) { |
|
|
|
if (done == null) { |
|
|
|
console.error("Can not create user."); |
|
|
|
} |
|
|
|
return cb(err); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
return cb(err); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
@@ -114,12 +115,13 @@ const authorization = { |
|
|
|
// check local database, then ldap |
|
|
|
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 (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 |
|
|
|
ldap.authorize (user.toLowerCase(), pwd, function (found) { |
|
|
@@ -140,14 +142,23 @@ const authorization = { |
|
|
|
entry.orclgender = found.orclgender; |
|
|
|
return fillSession (req, entry, entry.roles.length > 0 ? common.arrayToHash(entry.roles) : {user:true}, returnSession); |
|
|
|
} |
|
|
|
|
|
|
|
// 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) { |
|
|
|
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 ({}); |
|
|
|
}); |
|
|
|
}, |