package fi.otavanopisto.pyramus.json.users; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.commons.lang.StringUtils; import fi.internetix.smvc.SmvcRuntimeException; import fi.internetix.smvc.controllers.JSONRequestContext; import fi.otavanopisto.pyramus.I18N.Messages; import fi.otavanopisto.pyramus.dao.DAOFactory; import fi.otavanopisto.pyramus.dao.base.AddressDAO; import fi.otavanopisto.pyramus.dao.base.ContactTypeDAO; import fi.otavanopisto.pyramus.dao.base.EmailDAO; import fi.otavanopisto.pyramus.dao.base.PersonDAO; import fi.otavanopisto.pyramus.dao.base.PhoneNumberDAO; import fi.otavanopisto.pyramus.dao.base.TagDAO; import fi.otavanopisto.pyramus.dao.users.StaffMemberDAO; import fi.otavanopisto.pyramus.dao.users.UserIdentificationDAO; import fi.otavanopisto.pyramus.domainmodel.base.ContactType; import fi.otavanopisto.pyramus.domainmodel.base.Person; import fi.otavanopisto.pyramus.domainmodel.base.Tag; import fi.otavanopisto.pyramus.domainmodel.users.Role; import fi.otavanopisto.pyramus.domainmodel.users.StaffMember; import fi.otavanopisto.pyramus.framework.JSONRequestController; import fi.otavanopisto.pyramus.framework.PyramusStatusCode; import fi.otavanopisto.pyramus.framework.UserRole; import fi.otavanopisto.pyramus.framework.UserUtils; import fi.otavanopisto.pyramus.plugin.auth.AuthenticationProviderVault; import fi.otavanopisto.pyramus.plugin.auth.InternalAuthenticationProvider; /** * The controller responsible of creating a new Pyramus user. * * @see fi.otavanopisto.pyramus.views.users.CreateUserViewController */ public class CreateUserJSONRequestController extends JSONRequestController { /** * Processes the request to create a new user. Simply gathers the fields submitted from the * web page and adds the user to the database. * * @param requestContext The JSON request context */ public void process(JSONRequestContext requestContext) { StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO(); AddressDAO addressDAO = DAOFactory.getInstance().getAddressDAO(); EmailDAO emailDAO = DAOFactory.getInstance().getEmailDAO(); PhoneNumberDAO phoneNumberDAO = DAOFactory.getInstance().getPhoneNumberDAO(); TagDAO tagDAO = DAOFactory.getInstance().getTagDAO(); ContactTypeDAO contactTypeDAO = DAOFactory.getInstance().getContactTypeDAO(); PersonDAO personDAO = DAOFactory.getInstance().getPersonDAO(); UserIdentificationDAO userIdentificationDAO = DAOFactory.getInstance().getUserIdentificationDAO(); Long personId = requestContext.getLong("personId"); int emailCount2 = requestContext.getInteger("emailTable.rowCount"); for (int i = 0; i < emailCount2; i++) { String colPrefix = "emailTable." + i; String email = requestContext.getString(colPrefix + ".email"); ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId")); if (email != null) { if (!UserUtils.isAllowedEmail(email, contactType, personId)) { throw new RuntimeException(Messages.getInstance().getText(requestContext.getRequest().getLocale(), "generic.errors.emailInUse")); } } } // Fields from the web page String firstName = requestContext.getString("firstName"); String lastName = requestContext.getString("lastName"); String title = requestContext.getString("title"); Role role = Role.getRole(requestContext.getInteger("role")); String tagsText = requestContext.getString("tags"); String username = requestContext.getString("username"); String password = requestContext.getString("password1"); String password2 = requestContext.getString("password2"); Set<Tag> tagEntities = new HashSet<>(); if (!StringUtils.isBlank(tagsText)) { List<String> tags = Arrays.asList(tagsText.split("[\\ ,]")); for (String tag : tags) { if (!StringUtils.isBlank(tag)) { Tag tagEntity = tagDAO.findByText(tag.trim()); if (tagEntity == null) tagEntity = tagDAO.create(tag); tagEntities.add(tagEntity); } } } // User Person person = personId != null ? personDAO.findById(personId) : personDAO.create(null, null, null, null, Boolean.FALSE); StaffMember user = userDAO.create(firstName, lastName, role, person, false); if (title != null) userDAO.updateTitle(user, title); if(person.getDefaultUser() == null){ personDAO.updateDefaultUser(person, user); } // Authentication if (AuthenticationProviderVault.getInstance().hasInternalStrategies()) { boolean usernameBlank = StringUtils.isBlank(username); boolean passwordBlank = StringUtils.isBlank(password); // TODO: Support multiple internal authentication sources if (!usernameBlank) { InternalAuthenticationProvider internalAuthenticationProvider = AuthenticationProviderVault.getInstance().getInternalAuthenticationProviders().get(0); if (!passwordBlank) { if (!password.equals(password2)) throw new SmvcRuntimeException(PyramusStatusCode.PASSWORD_MISMATCH, "Passwords don't match"); } String externalId = internalAuthenticationProvider.createCredentials(username, password); userIdentificationDAO.create(person, internalAuthenticationProvider.getName(), externalId); } } // Tags userDAO.updateTags(user, tagEntities); // Addresses int addressCount = requestContext.getInteger("addressTable.rowCount"); for (int i = 0; i < addressCount; i++) { String colPrefix = "addressTable." + i; Boolean defaultAddress = requestContext.getBoolean(colPrefix + ".defaultAddress"); ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId")); String name = requestContext.getString(colPrefix + ".name"); String street = requestContext.getString(colPrefix + ".street"); String postal = requestContext.getString(colPrefix + ".postal"); String city = requestContext.getString(colPrefix + ".city"); String country = requestContext.getString(colPrefix + ".country"); boolean hasAddress = name != null || street != null || postal != null || city != null || country != null; if (hasAddress) { addressDAO.create(user.getContactInfo(), contactType, name, street, postal, city, country, defaultAddress); } } // Email addresses int emailCount = requestContext.getInteger("emailTable.rowCount"); for (int i = 0; i < emailCount; i++) { String colPrefix = "emailTable." + i; Boolean defaultAddress = requestContext.getBoolean(colPrefix + ".defaultAddress"); ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId")); String email = requestContext.getString(colPrefix + ".email"); // Trim the email address email = email != null ? email.trim() : null; if (email != null) { emailDAO.create(user.getContactInfo(), contactType, defaultAddress, email); } } // Phone numbers int phoneCount = requestContext.getInteger("phoneTable.rowCount"); for (int i = 0; i < phoneCount; i++) { String colPrefix = "phoneTable." + i; Boolean defaultNumber = requestContext.getBoolean(colPrefix + ".defaultNumber"); ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId")); String number = requestContext.getString(colPrefix + ".phone"); if (number != null) { phoneNumberDAO.create(user.getContactInfo(), contactType, defaultNumber, number); } } // Redirect to the Edit User view requestContext.setRedirectURL(requestContext.getRequest().getContextPath() + "/users/edituser.page?userId=" + user.getId()); } public UserRole[] getAllowedRoles() { return new UserRole[] { UserRole.MANAGER, UserRole.STUDY_PROGRAMME_LEADER, UserRole.ADMINISTRATOR }; } }