As mentioned previously I'm the principal architect of Apostrophe at P'unk Avenue.
The aposUsersSafe
collection is for storing password hashes and denormalized copies of a few closely related properties only. You normally would never need to interact with it directly. Like all other docs in Apostrophe, users live in the aposDocs
collection. And it's best to interact with them via the methods provided by the module that manages that type of piece. In this case, that would be apos.users
(the apostrophe-users
module).
Check out this method; this is refactored lightly from the addFromTask
method of apostrophe-users
, which implements adding a user and also adds them to a group, something you will almost certainly want to do also.
There is no code here to hash the password because the insert
method of apos.users
will do that for us.
self.addUser = function(req, username, password, groupname, callback) {
// find the group
return self.apos.groups.find(req, { title: groupname }).permission(false).toObject(function(err, group) {
if (err) {
return callback(err);
}
if (!group) {
return callback('That group does not exist.');
}
return self.apos.users.insert(req, {
username: username,
password: password,
title: username,
firstName: username,
groupIds: [ group._id ]
}, { permissions: false }, callback);
});
};
permission(false)
is called on the cursor and an options object with { permissions: false }
is passed to insert because I am assuming you want this to happen at this point regardless of who triggers it.
I recommend reading this tutorial on Apostrophe's model layer to get a solid grounding in how to work with Apostrophe's content types without getting into trouble. You can use MongoDB directly, but you have to know when to do that and when not to.
You can pass more properties when inserting the user; this is just the bare minimum for reasonable behavior.
As for calling the method, if you were to add it to lib/modules/apostrophe-users/index.js
at project level inside construct
, then you might call it like this from middleware:
return self.apos.users.addUser(req, username, password, groupname, function(err, newUser) {
if (err) {
// Handle the error as you see fit, one way is a 403 forbidden response
res.statusCode = 403;
return res.send('forbidden');
}
// newUser is the new user. You could log them in and redirect,
// with code I gave you elsewhere, or continue request:
return next();
});
Hope this is helpful!