public ResponsePojo registerWPUser(String cookieLookup, String wpuser, String wpauth, String wpsetup)
{
ResponsePojo rp = new ResponsePojo();
//Step 0 Read wordpress objects
WordPressUserPojo wpu = null;
WordPressAuthPojo wpa = null;
if (null != wpsetup) {
WordPressSetupPojo setup = WordPressSetupPojo.fromApi(wpsetup, WordPressSetupPojo.class);
wpu = setup.getUser();
wpa = setup.getAuth();
if ((null == wpu) || (null == wpa)) {
rp.setResponse(new ResponseObject("WP Register User",false,"Need to specify both user and auth objects"));
return rp;
}
}//TESTED
else {
wpu = WordPressUserPojo.fromApi(wpuser,WordPressUserPojo.class);
wpa = WordPressAuthPojo.fromApi(wpauth,WordPressAuthPojo.class);
}
//Step 1 Create the person object
//NOTE we use to store subscription info (i.e. in the peoplepojo)
//but it was never used anywhere (validating subscription?)
//its in the WPUserPojo that comes across
ObjectId profileId = new ObjectId();
PersonPojo pp = new PersonPojo();
pp.set_id(profileId);
pp.setAccountStatus("active");
if ((null == wpu.getEmail()) || (0 == wpu.getEmail().size())) {
rp.setResponse(new ResponseObject("WP Register User",false,"Need to specify email"));
return rp;
}//TESTED (2c)
pp.setEmail(wpu.getEmail().get(0));
pp.setFirstName(wpu.getFirstname()); // (optional but one of this + last name must be set)
pp.setLastName(wpu.getLastname()); // (optional but one of this + first name must be set)
if ((null == wpu.getFirstname()) || wpu.getFirstname().isEmpty()){
if (null == wpu.getLastname()) {
rp.setResponse(new ResponseObject("WP Register User",false,"Need to specify one of firstname,lastname"));
return rp;
}
pp.setDisplayName(wpu.getLastname());
}//TESTED (2d)
else if ((null == wpu.getLastname()) || wpu.getLastname().isEmpty()) {
pp.setDisplayName(wpu.getFirstname());
}
else {
pp.setDisplayName(wpu.getFirstname() + " " + wpu.getLastname());
}
// Check if user is already present (+set "primary keys"):
if (null == wpu.getWPUserID()) { // WPUserId is optional, uses email if not present
wpu.setWPUserID(pp.getEmail());
}
else { // Check WPU (+email later)
PersonPojo personQuery = new PersonPojo();
personQuery.setWPUserID(wpu.getWPUserID()); // (can be null, that's fine)
DBObject dboperson = DbManager.getSocial().getPerson().findOne(personQuery.toDb());
if (null != dboperson) {
rp.setResponse(new ResponseObject("WP Register User",false,"User already exists, both WPUserId and first email must be unique"));
return rp;
}//TESTED (2e)
}
pp.setWPUserID(wpu.getWPUserID());
PersonPojo personQuery = new PersonPojo();
personQuery.setEmail(pp.getEmail());
DBObject dboperson = DbManager.getSocial().getPerson().findOne(personQuery.toDb());
if (null != dboperson) {
rp.setResponse(new ResponseObject("WP Register User",false,"User already exists, both WPUserId and first email must be unique"));
return rp;
}//TESTED (2f)
//(The rest of this code has not significantly changed)
// Optional fields:
pp.setPhone(wpu.getPhone());
pp.setSubscriptionEndDate(wpu.getSubscriptionEndDate());
pp.setSubscriptionID(wpu.getSubscriptionID());
pp.setSubscriptionStartDate(wpu.getSubscriptionStartDate());
pp.setSubscriptionTypeID(wpu.getSubscriptionTypeID());
//Step 3 add communities to my list (self and system)
List<PersonCommunityPojo> communities = new ArrayList<PersonCommunityPojo>();
pp.setCommunities(communities);
//these fields may need set one day
pp.setAvatar(null);
pp.setBiography(null);
pp.setContacts(null);
pp.setLanguages(null);
pp.setLinks(null);
pp.setLocation(null);
pp.setOrganization(null);
pp.setTags(null);
pp.setTitle(null);
//end set of fields i didn't use
//Step 4 Create the new auth object so user can login
AuthenticationPojo ap = new AuthenticationPojo();
ap.setId(profileId);
ap.setProfileId(profileId);
ap.setUsername(pp.getEmail());
ap.setAccountStatus(AccountStatus.ACTIVE);
if (null == wpa.getPassword()) { // Obligatory
rp.setResponse(new ResponseObject("WP Register User",false,"Need to specify password"));
return rp;
}
try
{
if (44 != wpa.getPassword().length()) { // hash if in the clear
wpa.setPassword(PasswordEncryption.encrypt(wpa.getPassword()));
}
ap.setPassword(wpa.getPassword());
if (null == wpa.getAccountType()) { // (optional, defaults to "user"
wpa.setAccountType("user");
}
ap.setAccountType(wpa.getAccountType());
// to create an account you must be admin, so this is fine....
ap.setWPUserID(wpa.getWPUserID());
DateFormat df = new SimpleDateFormat("MMM dd, yyyy kk:mm:ss aa");
//Handle copying dates from wordpress objects
// (These are all optional, just use now if not specified)
if (null == wpu.getCreated()) {
pp.setCreated(new Date());
}
else {
pp.setCreated(df.parse(wpu.getCreated()));
}
if (null == wpu.getModified()) {
pp.setModified(new Date());
}
else {
pp.setModified(df.parse(wpu.getModified()));
}
if (null == wpa.getCreated()) {
ap.setCreated(new Date());
}
else {