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 Update User",false,"Need to specify both user and auth objects"));
return rp;
}
}
else {
wpu = WordPressUserPojo.fromApi(wpuser,WordPressUserPojo.class);
wpa = WordPressAuthPojo.fromApi(wpauth,WordPressAuthPojo.class);
}
//Save both these objects to the DB
try
{
PersonPojo personQuery = new PersonPojo();
if (null != personIdStr) {
personQuery.set_id(new ObjectId(personIdStr));
}
else {
if (null == wpu.getWPUserID()) {
if ((null == wpu.getEmail()) || wpu.getEmail().isEmpty()) {
rp.setResponse(new ResponseObject("WP Update User",false,"Need to specify WPUserID (or email address if not integrated via WordPress)"));
return rp;
}
else { // If authentication username is set, use that because it means that we're trying to change
// the email (and we're an admin user)
if (null != wpa.getUsername()) { // I may be changing someone's name
personQuery.setEmail(wpa.getUsername());
}
else { // I'm not changing anybody's name
personQuery.setEmail(wpu.getEmail().get(0));
}
}
}
else {
personQuery.setWPUserID(wpu.getWPUserID());
}
}
BasicDBObject dboperson = (BasicDBObject) DbManager.getSocial().getPerson().findOne(personQuery.toDb());
if (null == dboperson) {
rp.setResponse(new ResponseObject("WP Update User",false,"Can't find user specified by WPUserID"));
return rp;
}
PersonPojo pp = PersonPojo.fromDb(dboperson,PersonPojo.class);
if ((null != wpu.getEmail()) && !wpu.getEmail().isEmpty()) {
if (!pp.getEmail().equalsIgnoreCase(wpu.getEmail().get(0))) { // Email has changed...
pp.setEmail(wpu.getEmail().get(0));
// Check this is allowed (ie haven't taken a username already in use):
personQuery = new PersonPojo();
personQuery.setEmail(pp.getEmail());
dboperson = (BasicDBObject) DbManager.getSocial().getPerson().findOne(personQuery.toDb());
if (null != dboperson) {
rp.setResponse(new ResponseObject("WP Update User",false,"This primary email address is not unique"));
return rp;
}//TOTEST
bNeedToUpdateCommunities = true;
}
}
if (null != wpu.getFirstname()) {
if ((null == pp.getFirstName()) || !pp.getFirstName().equals(wpu.getFirstname())) {
pp.setFirstName(wpu.getFirstname());
bNeedToUpdateCommunities = true;
}
}
if (null != wpu.getLastname()) {
if ((null == pp.getLastName()) || !pp.getLastName().equals(wpu.getLastname())) {
pp.setLastName(wpu.getLastname());
bNeedToUpdateCommunities = true;
}
}
// Update display name
StringBuffer displayName = new StringBuffer();
if ((null != pp.getFirstName()) && !pp.getFirstName().isEmpty()) {
displayName.append(pp.getFirstName());
}
if ((null != pp.getLastName()) && !pp.getLastName().isEmpty()) {
if (displayName.length() > 0) {
displayName.append(' ');
}
displayName.append(pp.getLastName());
}//TOTESTx2
pp.setDisplayName(displayName.toString());
if (null != wpu.getPhone()) {
pp.setPhone(wpu.getPhone());
}
if (null != wpu.getSubscriptionEndDate()) {
pp.setSubscriptionEndDate(wpu.getSubscriptionEndDate());
}
if (null != wpu.getSubscriptionID()) {
pp.setSubscriptionID(wpu.getSubscriptionID());
}
if (null != wpu.getSubscriptionStartDate()) {
pp.setSubscriptionStartDate(wpu.getSubscriptionStartDate());
}
if (null != wpu.getSubscriptionTypeID()) {
pp.setSubscriptionTypeID(wpu.getSubscriptionTypeID());
}
// (can't change WPUserId obv)
AuthenticationPojo authQuery = new AuthenticationPojo();
if (null != pp.get_id()) {
authQuery.setProfileId(pp.get_id());
}
else {
rp.setResponse(new ResponseObject("WP Update User",false,"Internal authentication error 1"));
return rp;
}
DBObject dboauth = DbManager.getSocial().getAuthentication().findOne(authQuery.toDb());
if (null == dboauth) {
rp.setResponse(new ResponseObject("WP Update User",false,"Internal authentication error 2"));
return rp;
}
AuthenticationPojo ap = AuthenticationPojo.fromDb(dboauth, AuthenticationPojo.class);
if ((null != wpu.getEmail()) && !wpu.getEmail().isEmpty()) {
ap.setUsername(wpu.getEmail().get(0)); // (ap.username == email address, make life easy when resetting password)
}
if (null != wpa.getPassword()) {
if (44 != wpa.getPassword().length()) { // hash if in the clear
wpa.setPassword(PasswordEncryption.encrypt(wpa.getPassword()));
}
ap.setPassword(wpa.getPassword());
}
if (null != wpa.getAccountType()) {
if (null == personIdStr) { // (this means you're admin and hence can upgrade users to admins)
ap.setAccountType(wpa.getAccountType());
}
}
// (can't change WPUserId obv)
//Handle dates (just update modified times)
pp.setModified(new Date());
ap.setModified(new Date());
if ((null != wpa.getApiKey()) && (0 == wpa.getApiKey().length()) && (null != ap.getApiKey()))
{
// Delete existing API key
// (We'll allow a user to update their own API key - just not create it, see below)
CookiePojo removeMe = new CookiePojo();
removeMe.setApiKey(ap.getApiKey());
ap.setApiKey(null);
DbManager.getSocial().getCookies().remove(removeMe.toDb());
}
else if (null != wpa.getApiKey()) {
// Change or create API key
// Only admins can do this:
if (null != personIdStr) { // (this is != null iff user isn't admin)
// Check security settings
PropertiesManager pm = new PropertiesManager();
if (pm.getHarvestSecurity()) {
rp.setResponse(new ResponseObject("WP Update User",false,"You must be admin in secure mode to set an API key"));
return rp;
}
}//TESTED (admin, admin-enabled, non-admin - harvest.secure on and off)
ap.setApiKey(wpa.getApiKey());
CookiePojo cp = new CookiePojo();
cp.set_id(ap.getProfileId());
cp.setCookieId(cp.get_id());
cp.setApiKey(wpa.getApiKey());
cp.setStartDate(ap.getCreated());
cp.setProfileId(ap.getProfileId());
DbManager.getSocial().getCookies().save(cp.toDb());
}//TESTED
//else if api key is null then leave alone, assume hasn't changed
//update old entries
DbManager.getSocial().getPerson().update(new BasicDBObject("_id", pp.get_id()), pp.toDb());
DbManager.getSocial().getAuthentication().update(authQuery.toDb(), ap.toDb());
rp.setResponse(new ResponseObject("WP Update User",true,"User Updated Successfully"));
rp.setData(ap, new AuthenticationPojoApiMap());
//update communities if necessary
if (bNeedToUpdateCommunities)
{
//set community members name and email, if they match on id
BasicDBObject query = new BasicDBObject("members._id", pp.get_id());
BasicDBObject update = new BasicDBObject("members.$.email", pp.getEmail());
update.put("members.$.displayName", pp.getDisplayName());
DbManager.getSocial().getCommunity().update(query, new BasicDBObject("$set", update), false, true);
// (don't upsert, many times)
//INF-1314 if the ownerid == pp_id, set new username
BasicDBObject query1 = new BasicDBObject("ownerId", pp.get_id());
BasicDBObject update1 = new BasicDBObject("ownerDisplayName", pp.getDisplayName());
DbManager.getSocial().getCommunity().update(query1, new BasicDBObject("$set", update1), false, true);
}//TOTEST
// Just recreate personal community if necessary (means if something goes wrong can always just update user...)
if (null != pp.get_id()) {
GenericProcessingController.createCommunityDocIndex(pp.get_id().toString(), null, true, false, false);
}
//TESTED
rp.setResponse(new ResponseObject("WP Update User",true,"User Updated Successfully"));
}
catch (Exception ex )
{
logger.error("Exception Message: " + ex.getMessage(), ex);
rp.setResponse(new ResponseObject("WP Update User",false,"error while updating wp objects"));
}
return rp;
}//TOTEST