@Test public final void testProcessFormSubmission() { try { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); BindException aError = new BindException(new CreateUserCommand(), CreateUserCommand.ACTION_EDIT); testSetAgencyUserManager(); testSetAuthorityManager(); testSetMessageSource(); testSetPasswordEncoder(); testSetSaltSource(); CreateUserCommand aCommand = new CreateUserCommand(); aCommand.setAction(CreateUserCommand.ACTION_NEW); ModelAndView mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("newUser")); aCommand = new CreateUserCommand(); aCommand.setAction(CreateUserCommand.ACTION_VIEW); aCommand.setOid(new Long(1000)); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("newUser")); CreateUserCommand newCommand = (CreateUserCommand)mav.getModel().get("command"); assertTrue(newCommand != null); assertTrue(newCommand.getMode().equals("view")); aCommand = new CreateUserCommand(); aCommand.setAction(CreateUserCommand.ACTION_EDIT); aCommand.setOid(new Long(1000)); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("newUser")); newCommand = (CreateUserCommand)mav.getModel().get("command"); assertTrue(newCommand != null); assertTrue(newCommand.getMode().equals("edit")); aCommand = new CreateUserCommand(); aCommand.setAction(CreateUserCommand.ACTION_SAVE); aCommand.setOid(1000L); aCommand.setFirstname("Test"); aCommand.setLastname("User"); aCommand.setUsername("TestUserName"); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("viewUsers")); List<Agency> agencies = (List<Agency>)mav.getModel().get("agencies"); assertTrue(agencies != null); assertTrue(agencies.size() > 0); String message = (String)mav.getModel().get("page_message"); assertTrue(message != null); assertTrue(message.startsWith("user.updated")); aCommand = new CreateUserCommand(); aCommand.setAction(CreateUserCommand.ACTION_SAVE); aCommand.setFirstname("Test"); aCommand.setLastname("User"); aCommand.setUsername("TestUserName"); aCommand.setPassword("Password"); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("viewUsers")); agencies = (List<Agency>)mav.getModel().get("agencies"); assertTrue(agencies != null); assertTrue(agencies.size() > 0); message = (String)mav.getModel().get("page_message"); assertTrue(message != null); assertTrue(message.startsWith("user.created")); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }
@Override protected ModelAndView processFormSubmission(HttpServletRequest aReq, HttpServletResponse aRes, Object aCommand, BindException aError) throws Exception { CreateUserCommand userCmd = (CreateUserCommand) aCommand; ModelAndView mav = new ModelAndView(); mav.addObject(UserCommand.MDL_LOGGED_IN_USER, AuthUtil.getRemoteUserObject()); if (userCmd != null) { if (aError.hasErrors()) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); String mode = userCmd.getMode(); if (CreateUserCommand.ACTION_EDIT.equals(mode)) { mav.addObject(CreateUserCommand.ACTION_EDIT, mode); } mav.addObject(Constants.GBL_CMD_DATA, aError.getTarget()); mav.addObject(Constants.GBL_ERRORS, aError); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_NEW.equals(userCmd.getAction())) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_VIEW.equals(userCmd.getAction()) || CreateUserCommand.ACTION_EDIT.equals(userCmd.getAction())) { Long userOid = userCmd.getOid(); User user = agencyUserManager.getUserByOid(userOid); CreateUserCommand editCmd = new CreateUserCommand(); editCmd.setOid(userOid); editCmd.setActive(user.isActive()); editCmd.setAddress(user.getAddress()); editCmd.setAgencyOid(user.getAgency().getOid()); editCmd.setUsername(user.getUsername()); editCmd.setFirstname(user.getFirstname()); editCmd.setLastname(user.getLastname()); editCmd.setTitle(user.getTitle()); editCmd.setPhone(user.getPhone()); editCmd.setEmail(user.getEmail()); editCmd.setNotificationsByEmail(user.isNotificationsByEmail()); editCmd.setTasksByEmail(user.isTasksByEmail()); editCmd.setExternalAuth(user.isExternalAuth()); editCmd.setNotifyOnGeneral(user.isNotifyOnGeneral()); editCmd.setNotifyOnHarvestWarnings(user.isNotifyOnHarvestWarnings()); editCmd.setMode(userCmd.getAction()); List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); List assignedRoles = agencyUserManager.getAssociatedRolesForUser(userCmd.getOid()); mav.addObject(CreateUserCommand.MDL_ASSIGNED_ROLES, assignedRoles); mav.addObject(Constants.GBL_CMD_DATA, editCmd); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_SAVE.equals(userCmd.getAction())) { try { User user = new User(); boolean update = (userCmd.getOid() != null); if (update == true) { user = agencyUserManager.getUserByOid(userCmd.getOid()); } else { user.setActive(true); Long agencyOid = userCmd.getAgencyOid(); Agency agency = agencyUserManager.getAgencyByOid(agencyOid); user.setAgency(agency); user.setExternalAuth(userCmd.isExternalAuth()); if (userCmd.isExternalAuth() == false) { String pwd = userCmd.getPassword(); String encodedPwd =passwordEncoder.encodePassword(pwd, saltSource.getSystemWideSalt()); user.setPassword(encodedPwd); user.setForcePasswordChange(true); } user.setRoles(null); } user.setAddress(userCmd.getAddress()); user.setEmail(userCmd.getEmail()); user.setFirstname(userCmd.getFirstname()); user.setLastname(userCmd.getLastname()); user.setNotificationsByEmail(userCmd.isNotificationsByEmail()); user.setTasksByEmail(userCmd.isTasksByEmail()); user.setPhone(userCmd.getPhone()); user.setTitle(userCmd.getTitle()); user.setUsername(userCmd.getUsername()); user.setNotifyOnGeneral(userCmd.isNotifyOnGeneral()); user.setNotifyOnHarvestWarnings(userCmd.isNotifyOnHarvestWarnings()); agencyUserManager.updateUser(user, update); List userDTOs = agencyUserManager.getUserDTOsForLoggedInUser(); List agencies = null; if (authorityManager.hasPrivilege(Privilege.MANAGE_USERS, Privilege.SCOPE_ALL)) { agencies = agencyUserManager.getAgencies(); } else { User loggedInUser = AuthUtil.getRemoteUserObject(); Agency usersAgency = loggedInUser.getAgency(); agencies = new ArrayList<Agency>(); agencies.add(usersAgency); } String message; if (update == true) { message = messageSource.getMessage("user.updated", new Object[] { userCmd.getUsername() }, Locale.getDefault()); } else { message = messageSource.getMessage("user.created", new Object[] { userCmd.getUsername() }, Locale.getDefault()); } String agencyFilter = (String)aReq.getSession().getAttribute(UserCommand.MDL_AGENCYFILTER); if(agencyFilter == null) { agencyFilter = AuthUtil.getRemoteUserObject().getAgency().getName(); } mav.addObject(UserCommand.MDL_AGENCYFILTER, agencyFilter); mav.addObject(UserCommand.MDL_USERS, userDTOs); mav.addObject(UserCommand.MDL_AGENCIES, agencies); mav.addObject(Constants.GBL_MESSAGES, message ); mav.setViewName("viewUsers"); } catch (DataAccessException e) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); String mode = userCmd.getMode(); if (CreateUserCommand.ACTION_EDIT.equals(mode)) { mav.addObject(CreateUserCommand.ACTION_EDIT, mode); } aError.reject("user.duplicate"); mav.addObject(Constants.GBL_CMD_DATA, aError.getTarget()); mav.addObject(Constants.GBL_ERRORS, aError); mav.setViewName("newUser"); } } } else { log.warn("No Action provided for CreateUserController."); return null; } return mav; }
CreateUserController extends AbstractFormController { @Override protected ModelAndView processFormSubmission(HttpServletRequest aReq, HttpServletResponse aRes, Object aCommand, BindException aError) throws Exception { CreateUserCommand userCmd = (CreateUserCommand) aCommand; ModelAndView mav = new ModelAndView(); mav.addObject(UserCommand.MDL_LOGGED_IN_USER, AuthUtil.getRemoteUserObject()); if (userCmd != null) { if (aError.hasErrors()) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); String mode = userCmd.getMode(); if (CreateUserCommand.ACTION_EDIT.equals(mode)) { mav.addObject(CreateUserCommand.ACTION_EDIT, mode); } mav.addObject(Constants.GBL_CMD_DATA, aError.getTarget()); mav.addObject(Constants.GBL_ERRORS, aError); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_NEW.equals(userCmd.getAction())) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_VIEW.equals(userCmd.getAction()) || CreateUserCommand.ACTION_EDIT.equals(userCmd.getAction())) { Long userOid = userCmd.getOid(); User user = agencyUserManager.getUserByOid(userOid); CreateUserCommand editCmd = new CreateUserCommand(); editCmd.setOid(userOid); editCmd.setActive(user.isActive()); editCmd.setAddress(user.getAddress()); editCmd.setAgencyOid(user.getAgency().getOid()); editCmd.setUsername(user.getUsername()); editCmd.setFirstname(user.getFirstname()); editCmd.setLastname(user.getLastname()); editCmd.setTitle(user.getTitle()); editCmd.setPhone(user.getPhone()); editCmd.setEmail(user.getEmail()); editCmd.setNotificationsByEmail(user.isNotificationsByEmail()); editCmd.setTasksByEmail(user.isTasksByEmail()); editCmd.setExternalAuth(user.isExternalAuth()); editCmd.setNotifyOnGeneral(user.isNotifyOnGeneral()); editCmd.setNotifyOnHarvestWarnings(user.isNotifyOnHarvestWarnings()); editCmd.setMode(userCmd.getAction()); List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); List assignedRoles = agencyUserManager.getAssociatedRolesForUser(userCmd.getOid()); mav.addObject(CreateUserCommand.MDL_ASSIGNED_ROLES, assignedRoles); mav.addObject(Constants.GBL_CMD_DATA, editCmd); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_SAVE.equals(userCmd.getAction())) { try { User user = new User(); boolean update = (userCmd.getOid() != null); if (update == true) { user = agencyUserManager.getUserByOid(userCmd.getOid()); } else { user.setActive(true); Long agencyOid = userCmd.getAgencyOid(); Agency agency = agencyUserManager.getAgencyByOid(agencyOid); user.setAgency(agency); user.setExternalAuth(userCmd.isExternalAuth()); if (userCmd.isExternalAuth() == false) { String pwd = userCmd.getPassword(); String encodedPwd =passwordEncoder.encodePassword(pwd, saltSource.getSystemWideSalt()); user.setPassword(encodedPwd); user.setForcePasswordChange(true); } user.setRoles(null); } user.setAddress(userCmd.getAddress()); user.setEmail(userCmd.getEmail()); user.setFirstname(userCmd.getFirstname()); user.setLastname(userCmd.getLastname()); user.setNotificationsByEmail(userCmd.isNotificationsByEmail()); user.setTasksByEmail(userCmd.isTasksByEmail()); user.setPhone(userCmd.getPhone()); user.setTitle(userCmd.getTitle()); user.setUsername(userCmd.getUsername()); user.setNotifyOnGeneral(userCmd.isNotifyOnGeneral()); user.setNotifyOnHarvestWarnings(userCmd.isNotifyOnHarvestWarnings()); agencyUserManager.updateUser(user, update); List userDTOs = agencyUserManager.getUserDTOsForLoggedInUser(); List agencies = null; if (authorityManager.hasPrivilege(Privilege.MANAGE_USERS, Privilege.SCOPE_ALL)) { agencies = agencyUserManager.getAgencies(); } else { User loggedInUser = AuthUtil.getRemoteUserObject(); Agency usersAgency = loggedInUser.getAgency(); agencies = new ArrayList<Agency>(); agencies.add(usersAgency); } String message; if (update == true) { message = messageSource.getMessage("user.updated", new Object[] { userCmd.getUsername() }, Locale.getDefault()); } else { message = messageSource.getMessage("user.created", new Object[] { userCmd.getUsername() }, Locale.getDefault()); } String agencyFilter = (String)aReq.getSession().getAttribute(UserCommand.MDL_AGENCYFILTER); if(agencyFilter == null) { agencyFilter = AuthUtil.getRemoteUserObject().getAgency().getName(); } mav.addObject(UserCommand.MDL_AGENCYFILTER, agencyFilter); mav.addObject(UserCommand.MDL_USERS, userDTOs); mav.addObject(UserCommand.MDL_AGENCIES, agencies); mav.addObject(Constants.GBL_MESSAGES, message ); mav.setViewName("viewUsers"); } catch (DataAccessException e) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); String mode = userCmd.getMode(); if (CreateUserCommand.ACTION_EDIT.equals(mode)) { mav.addObject(CreateUserCommand.ACTION_EDIT, mode); } aError.reject("user.duplicate"); mav.addObject(Constants.GBL_CMD_DATA, aError.getTarget()); mav.addObject(Constants.GBL_ERRORS, aError); mav.setViewName("newUser"); } } } else { log.warn("No Action provided for CreateUserController."); return null; } return mav; } }
CreateUserController extends AbstractFormController { @Override protected ModelAndView processFormSubmission(HttpServletRequest aReq, HttpServletResponse aRes, Object aCommand, BindException aError) throws Exception { CreateUserCommand userCmd = (CreateUserCommand) aCommand; ModelAndView mav = new ModelAndView(); mav.addObject(UserCommand.MDL_LOGGED_IN_USER, AuthUtil.getRemoteUserObject()); if (userCmd != null) { if (aError.hasErrors()) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); String mode = userCmd.getMode(); if (CreateUserCommand.ACTION_EDIT.equals(mode)) { mav.addObject(CreateUserCommand.ACTION_EDIT, mode); } mav.addObject(Constants.GBL_CMD_DATA, aError.getTarget()); mav.addObject(Constants.GBL_ERRORS, aError); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_NEW.equals(userCmd.getAction())) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_VIEW.equals(userCmd.getAction()) || CreateUserCommand.ACTION_EDIT.equals(userCmd.getAction())) { Long userOid = userCmd.getOid(); User user = agencyUserManager.getUserByOid(userOid); CreateUserCommand editCmd = new CreateUserCommand(); editCmd.setOid(userOid); editCmd.setActive(user.isActive()); editCmd.setAddress(user.getAddress()); editCmd.setAgencyOid(user.getAgency().getOid()); editCmd.setUsername(user.getUsername()); editCmd.setFirstname(user.getFirstname()); editCmd.setLastname(user.getLastname()); editCmd.setTitle(user.getTitle()); editCmd.setPhone(user.getPhone()); editCmd.setEmail(user.getEmail()); editCmd.setNotificationsByEmail(user.isNotificationsByEmail()); editCmd.setTasksByEmail(user.isTasksByEmail()); editCmd.setExternalAuth(user.isExternalAuth()); editCmd.setNotifyOnGeneral(user.isNotifyOnGeneral()); editCmd.setNotifyOnHarvestWarnings(user.isNotifyOnHarvestWarnings()); editCmd.setMode(userCmd.getAction()); List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); List assignedRoles = agencyUserManager.getAssociatedRolesForUser(userCmd.getOid()); mav.addObject(CreateUserCommand.MDL_ASSIGNED_ROLES, assignedRoles); mav.addObject(Constants.GBL_CMD_DATA, editCmd); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_SAVE.equals(userCmd.getAction())) { try { User user = new User(); boolean update = (userCmd.getOid() != null); if (update == true) { user = agencyUserManager.getUserByOid(userCmd.getOid()); } else { user.setActive(true); Long agencyOid = userCmd.getAgencyOid(); Agency agency = agencyUserManager.getAgencyByOid(agencyOid); user.setAgency(agency); user.setExternalAuth(userCmd.isExternalAuth()); if (userCmd.isExternalAuth() == false) { String pwd = userCmd.getPassword(); String encodedPwd =passwordEncoder.encodePassword(pwd, saltSource.getSystemWideSalt()); user.setPassword(encodedPwd); user.setForcePasswordChange(true); } user.setRoles(null); } user.setAddress(userCmd.getAddress()); user.setEmail(userCmd.getEmail()); user.setFirstname(userCmd.getFirstname()); user.setLastname(userCmd.getLastname()); user.setNotificationsByEmail(userCmd.isNotificationsByEmail()); user.setTasksByEmail(userCmd.isTasksByEmail()); user.setPhone(userCmd.getPhone()); user.setTitle(userCmd.getTitle()); user.setUsername(userCmd.getUsername()); user.setNotifyOnGeneral(userCmd.isNotifyOnGeneral()); user.setNotifyOnHarvestWarnings(userCmd.isNotifyOnHarvestWarnings()); agencyUserManager.updateUser(user, update); List userDTOs = agencyUserManager.getUserDTOsForLoggedInUser(); List agencies = null; if (authorityManager.hasPrivilege(Privilege.MANAGE_USERS, Privilege.SCOPE_ALL)) { agencies = agencyUserManager.getAgencies(); } else { User loggedInUser = AuthUtil.getRemoteUserObject(); Agency usersAgency = loggedInUser.getAgency(); agencies = new ArrayList<Agency>(); agencies.add(usersAgency); } String message; if (update == true) { message = messageSource.getMessage("user.updated", new Object[] { userCmd.getUsername() }, Locale.getDefault()); } else { message = messageSource.getMessage("user.created", new Object[] { userCmd.getUsername() }, Locale.getDefault()); } String agencyFilter = (String)aReq.getSession().getAttribute(UserCommand.MDL_AGENCYFILTER); if(agencyFilter == null) { agencyFilter = AuthUtil.getRemoteUserObject().getAgency().getName(); } mav.addObject(UserCommand.MDL_AGENCYFILTER, agencyFilter); mav.addObject(UserCommand.MDL_USERS, userDTOs); mav.addObject(UserCommand.MDL_AGENCIES, agencies); mav.addObject(Constants.GBL_MESSAGES, message ); mav.setViewName("viewUsers"); } catch (DataAccessException e) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); String mode = userCmd.getMode(); if (CreateUserCommand.ACTION_EDIT.equals(mode)) { mav.addObject(CreateUserCommand.ACTION_EDIT, mode); } aError.reject("user.duplicate"); mav.addObject(Constants.GBL_CMD_DATA, aError.getTarget()); mav.addObject(Constants.GBL_ERRORS, aError); mav.setViewName("newUser"); } } } else { log.warn("No Action provided for CreateUserController."); return null; } return mav; } CreateUserController(); }
CreateUserController extends AbstractFormController { @Override protected ModelAndView processFormSubmission(HttpServletRequest aReq, HttpServletResponse aRes, Object aCommand, BindException aError) throws Exception { CreateUserCommand userCmd = (CreateUserCommand) aCommand; ModelAndView mav = new ModelAndView(); mav.addObject(UserCommand.MDL_LOGGED_IN_USER, AuthUtil.getRemoteUserObject()); if (userCmd != null) { if (aError.hasErrors()) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); String mode = userCmd.getMode(); if (CreateUserCommand.ACTION_EDIT.equals(mode)) { mav.addObject(CreateUserCommand.ACTION_EDIT, mode); } mav.addObject(Constants.GBL_CMD_DATA, aError.getTarget()); mav.addObject(Constants.GBL_ERRORS, aError); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_NEW.equals(userCmd.getAction())) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_VIEW.equals(userCmd.getAction()) || CreateUserCommand.ACTION_EDIT.equals(userCmd.getAction())) { Long userOid = userCmd.getOid(); User user = agencyUserManager.getUserByOid(userOid); CreateUserCommand editCmd = new CreateUserCommand(); editCmd.setOid(userOid); editCmd.setActive(user.isActive()); editCmd.setAddress(user.getAddress()); editCmd.setAgencyOid(user.getAgency().getOid()); editCmd.setUsername(user.getUsername()); editCmd.setFirstname(user.getFirstname()); editCmd.setLastname(user.getLastname()); editCmd.setTitle(user.getTitle()); editCmd.setPhone(user.getPhone()); editCmd.setEmail(user.getEmail()); editCmd.setNotificationsByEmail(user.isNotificationsByEmail()); editCmd.setTasksByEmail(user.isTasksByEmail()); editCmd.setExternalAuth(user.isExternalAuth()); editCmd.setNotifyOnGeneral(user.isNotifyOnGeneral()); editCmd.setNotifyOnHarvestWarnings(user.isNotifyOnHarvestWarnings()); editCmd.setMode(userCmd.getAction()); List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); List assignedRoles = agencyUserManager.getAssociatedRolesForUser(userCmd.getOid()); mav.addObject(CreateUserCommand.MDL_ASSIGNED_ROLES, assignedRoles); mav.addObject(Constants.GBL_CMD_DATA, editCmd); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_SAVE.equals(userCmd.getAction())) { try { User user = new User(); boolean update = (userCmd.getOid() != null); if (update == true) { user = agencyUserManager.getUserByOid(userCmd.getOid()); } else { user.setActive(true); Long agencyOid = userCmd.getAgencyOid(); Agency agency = agencyUserManager.getAgencyByOid(agencyOid); user.setAgency(agency); user.setExternalAuth(userCmd.isExternalAuth()); if (userCmd.isExternalAuth() == false) { String pwd = userCmd.getPassword(); String encodedPwd =passwordEncoder.encodePassword(pwd, saltSource.getSystemWideSalt()); user.setPassword(encodedPwd); user.setForcePasswordChange(true); } user.setRoles(null); } user.setAddress(userCmd.getAddress()); user.setEmail(userCmd.getEmail()); user.setFirstname(userCmd.getFirstname()); user.setLastname(userCmd.getLastname()); user.setNotificationsByEmail(userCmd.isNotificationsByEmail()); user.setTasksByEmail(userCmd.isTasksByEmail()); user.setPhone(userCmd.getPhone()); user.setTitle(userCmd.getTitle()); user.setUsername(userCmd.getUsername()); user.setNotifyOnGeneral(userCmd.isNotifyOnGeneral()); user.setNotifyOnHarvestWarnings(userCmd.isNotifyOnHarvestWarnings()); agencyUserManager.updateUser(user, update); List userDTOs = agencyUserManager.getUserDTOsForLoggedInUser(); List agencies = null; if (authorityManager.hasPrivilege(Privilege.MANAGE_USERS, Privilege.SCOPE_ALL)) { agencies = agencyUserManager.getAgencies(); } else { User loggedInUser = AuthUtil.getRemoteUserObject(); Agency usersAgency = loggedInUser.getAgency(); agencies = new ArrayList<Agency>(); agencies.add(usersAgency); } String message; if (update == true) { message = messageSource.getMessage("user.updated", new Object[] { userCmd.getUsername() }, Locale.getDefault()); } else { message = messageSource.getMessage("user.created", new Object[] { userCmd.getUsername() }, Locale.getDefault()); } String agencyFilter = (String)aReq.getSession().getAttribute(UserCommand.MDL_AGENCYFILTER); if(agencyFilter == null) { agencyFilter = AuthUtil.getRemoteUserObject().getAgency().getName(); } mav.addObject(UserCommand.MDL_AGENCYFILTER, agencyFilter); mav.addObject(UserCommand.MDL_USERS, userDTOs); mav.addObject(UserCommand.MDL_AGENCIES, agencies); mav.addObject(Constants.GBL_MESSAGES, message ); mav.setViewName("viewUsers"); } catch (DataAccessException e) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); String mode = userCmd.getMode(); if (CreateUserCommand.ACTION_EDIT.equals(mode)) { mav.addObject(CreateUserCommand.ACTION_EDIT, mode); } aError.reject("user.duplicate"); mav.addObject(Constants.GBL_CMD_DATA, aError.getTarget()); mav.addObject(Constants.GBL_ERRORS, aError); mav.setViewName("newUser"); } } } else { log.warn("No Action provided for CreateUserController."); return null; } return mav; } CreateUserController(); @Override void initBinder(HttpServletRequest request, ServletRequestDataBinder binder); void setPasswordEncoder(PasswordEncoder passwordEncoder); void setSaltSource(SystemWideSaltSource saltSource); void setAgencyUserManager(AgencyUserManager agencyUserManager); void setMessageSource(MessageSource messageSource); void setAuthorityManager(AuthorityManager authorityManager); }
CreateUserController extends AbstractFormController { @Override protected ModelAndView processFormSubmission(HttpServletRequest aReq, HttpServletResponse aRes, Object aCommand, BindException aError) throws Exception { CreateUserCommand userCmd = (CreateUserCommand) aCommand; ModelAndView mav = new ModelAndView(); mav.addObject(UserCommand.MDL_LOGGED_IN_USER, AuthUtil.getRemoteUserObject()); if (userCmd != null) { if (aError.hasErrors()) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); String mode = userCmd.getMode(); if (CreateUserCommand.ACTION_EDIT.equals(mode)) { mav.addObject(CreateUserCommand.ACTION_EDIT, mode); } mav.addObject(Constants.GBL_CMD_DATA, aError.getTarget()); mav.addObject(Constants.GBL_ERRORS, aError); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_NEW.equals(userCmd.getAction())) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_VIEW.equals(userCmd.getAction()) || CreateUserCommand.ACTION_EDIT.equals(userCmd.getAction())) { Long userOid = userCmd.getOid(); User user = agencyUserManager.getUserByOid(userOid); CreateUserCommand editCmd = new CreateUserCommand(); editCmd.setOid(userOid); editCmd.setActive(user.isActive()); editCmd.setAddress(user.getAddress()); editCmd.setAgencyOid(user.getAgency().getOid()); editCmd.setUsername(user.getUsername()); editCmd.setFirstname(user.getFirstname()); editCmd.setLastname(user.getLastname()); editCmd.setTitle(user.getTitle()); editCmd.setPhone(user.getPhone()); editCmd.setEmail(user.getEmail()); editCmd.setNotificationsByEmail(user.isNotificationsByEmail()); editCmd.setTasksByEmail(user.isTasksByEmail()); editCmd.setExternalAuth(user.isExternalAuth()); editCmd.setNotifyOnGeneral(user.isNotifyOnGeneral()); editCmd.setNotifyOnHarvestWarnings(user.isNotifyOnHarvestWarnings()); editCmd.setMode(userCmd.getAction()); List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); List assignedRoles = agencyUserManager.getAssociatedRolesForUser(userCmd.getOid()); mav.addObject(CreateUserCommand.MDL_ASSIGNED_ROLES, assignedRoles); mav.addObject(Constants.GBL_CMD_DATA, editCmd); mav.setViewName("newUser"); } else if (CreateUserCommand.ACTION_SAVE.equals(userCmd.getAction())) { try { User user = new User(); boolean update = (userCmd.getOid() != null); if (update == true) { user = agencyUserManager.getUserByOid(userCmd.getOid()); } else { user.setActive(true); Long agencyOid = userCmd.getAgencyOid(); Agency agency = agencyUserManager.getAgencyByOid(agencyOid); user.setAgency(agency); user.setExternalAuth(userCmd.isExternalAuth()); if (userCmd.isExternalAuth() == false) { String pwd = userCmd.getPassword(); String encodedPwd =passwordEncoder.encodePassword(pwd, saltSource.getSystemWideSalt()); user.setPassword(encodedPwd); user.setForcePasswordChange(true); } user.setRoles(null); } user.setAddress(userCmd.getAddress()); user.setEmail(userCmd.getEmail()); user.setFirstname(userCmd.getFirstname()); user.setLastname(userCmd.getLastname()); user.setNotificationsByEmail(userCmd.isNotificationsByEmail()); user.setTasksByEmail(userCmd.isTasksByEmail()); user.setPhone(userCmd.getPhone()); user.setTitle(userCmd.getTitle()); user.setUsername(userCmd.getUsername()); user.setNotifyOnGeneral(userCmd.isNotifyOnGeneral()); user.setNotifyOnHarvestWarnings(userCmd.isNotifyOnHarvestWarnings()); agencyUserManager.updateUser(user, update); List userDTOs = agencyUserManager.getUserDTOsForLoggedInUser(); List agencies = null; if (authorityManager.hasPrivilege(Privilege.MANAGE_USERS, Privilege.SCOPE_ALL)) { agencies = agencyUserManager.getAgencies(); } else { User loggedInUser = AuthUtil.getRemoteUserObject(); Agency usersAgency = loggedInUser.getAgency(); agencies = new ArrayList<Agency>(); agencies.add(usersAgency); } String message; if (update == true) { message = messageSource.getMessage("user.updated", new Object[] { userCmd.getUsername() }, Locale.getDefault()); } else { message = messageSource.getMessage("user.created", new Object[] { userCmd.getUsername() }, Locale.getDefault()); } String agencyFilter = (String)aReq.getSession().getAttribute(UserCommand.MDL_AGENCYFILTER); if(agencyFilter == null) { agencyFilter = AuthUtil.getRemoteUserObject().getAgency().getName(); } mav.addObject(UserCommand.MDL_AGENCYFILTER, agencyFilter); mav.addObject(UserCommand.MDL_USERS, userDTOs); mav.addObject(UserCommand.MDL_AGENCIES, agencies); mav.addObject(Constants.GBL_MESSAGES, message ); mav.setViewName("viewUsers"); } catch (DataAccessException e) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(CreateUserCommand.MDL_AGENCIES, agencies); String mode = userCmd.getMode(); if (CreateUserCommand.ACTION_EDIT.equals(mode)) { mav.addObject(CreateUserCommand.ACTION_EDIT, mode); } aError.reject("user.duplicate"); mav.addObject(Constants.GBL_CMD_DATA, aError.getTarget()); mav.addObject(Constants.GBL_ERRORS, aError); mav.setViewName("newUser"); } } } else { log.warn("No Action provided for CreateUserController."); return null; } return mav; } CreateUserController(); @Override void initBinder(HttpServletRequest request, ServletRequestDataBinder binder); void setPasswordEncoder(PasswordEncoder passwordEncoder); void setSaltSource(SystemWideSaltSource saltSource); void setAgencyUserManager(AgencyUserManager agencyUserManager); void setMessageSource(MessageSource messageSource); void setAuthorityManager(AuthorityManager authorityManager);  }