User creation on login (server-seitig)
This commit is contained in:
+51
-7
@@ -17,18 +17,61 @@ const clientVisibleSession = { user: true, name: true, type: true, mail: true, r
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
return cb (err);
|
||||
});
|
||||
}
|
||||
|
||||
// Save found user into DB, if not already exists
|
||||
function saveFoundToDB(found) {
|
||||
console.info(found);
|
||||
Users.findById(found.user)
|
||||
.exec(function(err, result){
|
||||
if (err) {
|
||||
console.error("Error: Users collection.");
|
||||
console.error(err);
|
||||
}
|
||||
// User doesn't exist
|
||||
if (found !== undefined && !result) {
|
||||
Users.create({
|
||||
_id: found.user,
|
||||
name: found.name,
|
||||
mail: found.mail,
|
||||
type: found.type,
|
||||
abos: '',
|
||||
bookmarks: '',
|
||||
roles: '',
|
||||
}, function(err, done) {
|
||||
if (err) {
|
||||
console.error("User creation: Failed");
|
||||
console.error(err);
|
||||
} else {
|
||||
console.info("New User created!");
|
||||
}
|
||||
if (done == null) {
|
||||
console.error("Can not create user.");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const authorization = {
|
||||
// Generate Error object suitible for throwing or next()ing
|
||||
genCheckAuthorized: function (group) {
|
||||
@@ -62,7 +105,7 @@ const authorization = {
|
||||
|
||||
// Check whether to just validate current session ID
|
||||
if (user === '' && pwd === '') {
|
||||
console.log ("auth revalidate: " + req.session.user);
|
||||
console.log ("auth revalidate: " + req.session._id);
|
||||
if (req.session.user === undefined)
|
||||
return returnError();
|
||||
return returnSession ();
|
||||
@@ -71,12 +114,12 @@ 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)) {
|
||||
// return fillSession (req, entry, common.arrayToHash(entry.roles), returnSession);
|
||||
// }
|
||||
// return returnError ();
|
||||
// }
|
||||
|
||||
// check ldap
|
||||
ldap.authorize (user.toLowerCase(), pwd, function (found) {
|
||||
@@ -98,6 +141,7 @@ const authorization = {
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
+23
-44
@@ -217,25 +217,22 @@ const dbs = {
|
||||
* -> User schema
|
||||
* Create a new user
|
||||
*/
|
||||
post: function (req, res) {
|
||||
post: function(req, res) {
|
||||
// console.info(req.body);
|
||||
model.Users.create({
|
||||
_id: req.body.id,
|
||||
_id: req.body._id,
|
||||
name: req.body.name,
|
||||
mail: req.body.mail,
|
||||
type: req.body.type,
|
||||
roles: req.body.roles,
|
||||
hash: req.body.hash,
|
||||
salt: req.body.salt,
|
||||
pwd: req.body.pwd,
|
||||
abos: req.body.abos,
|
||||
bookmarks: req.body.bookmarks,
|
||||
}, function (err, result) {
|
||||
roles: req.body.roles,
|
||||
}, function(err, result) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return res.status(401).json(err.message);
|
||||
} else {
|
||||
res.json({
|
||||
message: "User created!!"
|
||||
});
|
||||
res.json({message: "User created!", user: req.body});
|
||||
}
|
||||
if (result == null) {
|
||||
return res.status(500).json("Can not create user.")
|
||||
@@ -365,42 +362,24 @@ const dbs = {
|
||||
model.Tags._list = [""];
|
||||
|
||||
var userSchema = common.mongoose.Schema({
|
||||
/*_id: {
|
||||
type: String
|
||||
},*/
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
_comment: ""
|
||||
},
|
||||
pwd: {
|
||||
type: String,
|
||||
_comment: ""
|
||||
},
|
||||
hash: {
|
||||
type: String
|
||||
},
|
||||
salt: {
|
||||
type: String
|
||||
},
|
||||
type: {
|
||||
type: String
|
||||
},
|
||||
roles: {
|
||||
type: [String],
|
||||
required: true,
|
||||
_comment: ""
|
||||
},
|
||||
abos: {
|
||||
type: [String],
|
||||
_comment: ""
|
||||
},
|
||||
_id: { type: String, required: true },
|
||||
name: { type: String,
|
||||
_comment: "" },
|
||||
mail: { type: String },
|
||||
type: { type: String },
|
||||
pwd: { type: String,
|
||||
_comment: "" },
|
||||
hash: { type: String },
|
||||
salt: { type: String },
|
||||
abos: { type: [String],
|
||||
_comment: "" },
|
||||
//deactivated: { type: Boolean },
|
||||
//host: { type: Boolean },
|
||||
bookmarks: {
|
||||
type: [String],
|
||||
_comment: ""
|
||||
},
|
||||
bookmarks: { type: [String],
|
||||
_comment: "" },
|
||||
sessionid: { type: String },
|
||||
roles: { type: [String], required: true,
|
||||
_comment: "" },
|
||||
});
|
||||
model.Users = common.mongoose.model('users', userSchema);
|
||||
model.Users._list = [""];
|
||||
|
||||
Reference in New Issue
Block a user