Search in sources :

Example 1 with UserType

use of org.akaza.openclinica.bean.core.UserType in project OpenClinica by OpenClinica.

the class CreateUserAccountServlet method processRequest.

@Override
protected void processRequest() throws Exception {
    FormProcessor fp = new FormProcessor(request);
    StudyDAO sdao = new StudyDAO(sm.getDataSource());
    // YW 11-28-2007 << list sites under their studies
    ArrayList<StudyBean> all = (ArrayList<StudyBean>) sdao.findAll();
    ArrayList<StudyBean> finalList = new ArrayList<StudyBean>();
    for (StudyBean sb : all) {
        if (!(sb.getParentStudyId() > 0)) {
            finalList.add(sb);
            finalList.addAll(sdao.findAllByParent(sb.getId()));
        }
    }
    addEntityList("studies", finalList, respage.getString("a_user_cannot_be_created_no_study_as_active"), Page.ADMIN_SYSTEM);
    // YW >>
    Map roleMap = new LinkedHashMap();
    for (Iterator it = getRoles().iterator(); it.hasNext(); ) {
        Role role = (Role) it.next();
        // I added the below if statement , to exclude displaying on study level the newly added 'ReseachAssisstant2' role by default.
        if (role.getId() != 7)
            roleMap.put(role.getId(), role.getDescription());
    //            roleMap.put(role.getId(), role.getDescription());
    }
    // addEntityList("roles", getRoles(), respage.getString("a_user_cannot_be_created_no_roles_as_role"), Page.ADMIN_SYSTEM);
    request.setAttribute("roles", roleMap);
    ArrayList types = UserType.toArrayList();
    types.remove(UserType.INVALID);
    if (!ub.isTechAdmin()) {
        types.remove(UserType.TECHADMIN);
    }
    addEntityList("types", types, respage.getString("a_user_cannot_be_created_no_user_types_for"), Page.ADMIN_SYSTEM);
    Boolean changeRoles = request.getParameter("changeRoles") == null ? false : Boolean.parseBoolean(request.getParameter("changeRoles"));
    int activeStudy = fp.getInt(INPUT_STUDY);
    if (changeRoles) {
        StudyBean study = (StudyBean) sdao.findByPK(activeStudy);
        roleMap = new LinkedHashMap();
        ResourceBundle resterm = org.akaza.openclinica.i18n.util.ResourceBundleProvider.getTermsBundle();
        if (study.getParentStudyId() > 0) {
            for (Iterator it = getRoles().iterator(); it.hasNext(); ) {
                Role role = (Role) it.next();
                switch(role.getId()) {
                    // break;
                    case 4:
                        roleMap.put(role.getId(), resterm.getString("site_investigator").trim());
                        break;
                    case 5:
                        roleMap.put(role.getId(), resterm.getString("site_Data_Entry_Person").trim());
                        break;
                    case 6:
                        roleMap.put(role.getId(), resterm.getString("site_monitor").trim());
                        break;
                    case 7:
                        roleMap.put(role.getId(), resterm.getString("site_Data_Entry_Person2").trim());
                        break;
                    default:
                }
            }
        } else {
            for (Iterator it = getRoles().iterator(); it.hasNext(); ) {
                Role role = (Role) it.next();
                switch(role.getId()) {
                    case 2:
                        roleMap.put(role.getId(), resterm.getString("Study_Coordinator").trim());
                        break;
                    case 3:
                        roleMap.put(role.getId(), resterm.getString("Study_Director").trim());
                        break;
                    case 4:
                        roleMap.put(role.getId(), resterm.getString("Investigator").trim());
                        break;
                    case 5:
                        roleMap.put(role.getId(), resterm.getString("Data_Entry_Person").trim());
                        break;
                    case 6:
                        roleMap.put(role.getId(), resterm.getString("Monitor").trim());
                        break;
                    default:
                }
            }
        }
        request.setAttribute("roles", roleMap);
    }
    request.setAttribute("ldapEnabled", isLdapEnabled());
    request.setAttribute("activeStudy", activeStudy);
    if (!fp.isSubmitted() || changeRoles) {
        String[] textFields = { INPUT_USER_SOURCE, INPUT_USERNAME, INPUT_FIRST_NAME, INPUT_LAST_NAME, INPUT_EMAIL, INPUT_INSTITUTION, INPUT_DISPLAY_PWD };
        fp.setCurrentStringValuesAsPreset(textFields);
        String[] ddlbFields = { INPUT_STUDY, INPUT_ROLE, INPUT_TYPE, INPUT_RUN_WEBSERVICES };
        fp.setCurrentIntValuesAsPreset(ddlbFields);
        HashMap presetValues = fp.getPresetValues();
        // Mantis Issue 6058.
        String sendPwd = SQLInitServlet.getField("user_account_notification");
        fp.addPresetValue(USER_ACCOUNT_NOTIFICATION, sendPwd);
        //
        setPresetValues(presetValues);
        forwardPage(Page.CREATE_ACCOUNT);
    } else {
        UserAccountDAO udao = new UserAccountDAO(sm.getDataSource());
        Validator v = new Validator(request);
        // username must not be blank,
        // must be in the format specified by Validator.USERNAME,
        // and must be unique
        v.addValidation(INPUT_USERNAME, Validator.NO_BLANKS);
        v.addValidation(INPUT_USERNAME, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 64);
        v.addValidation(INPUT_USERNAME, Validator.IS_A_USERNAME);
        v.addValidation(INPUT_USERNAME, Validator.USERNAME_UNIQUE, udao);
        v.addValidation(INPUT_FIRST_NAME, Validator.NO_BLANKS);
        v.addValidation(INPUT_LAST_NAME, Validator.NO_BLANKS);
        v.addValidation(INPUT_FIRST_NAME, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 50);
        v.addValidation(INPUT_LAST_NAME, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 50);
        v.addValidation(INPUT_EMAIL, Validator.NO_BLANKS);
        v.addValidation(INPUT_EMAIL, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 120);
        v.addValidation(INPUT_EMAIL, Validator.IS_A_EMAIL);
        v.addValidation(INPUT_INSTITUTION, Validator.NO_BLANKS);
        v.addValidation(INPUT_INSTITUTION, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 255);
        v.addValidation(INPUT_STUDY, Validator.ENTITY_EXISTS, sdao);
        v.addValidation(INPUT_ROLE, Validator.IS_VALID_TERM, TermType.ROLE);
        HashMap errors = v.validate();
        if (errors.isEmpty()) {
            UserAccountBean createdUserAccountBean = new UserAccountBean();
            createdUserAccountBean.setName(fp.getString(INPUT_USERNAME));
            createdUserAccountBean.setFirstName(fp.getString(INPUT_FIRST_NAME));
            createdUserAccountBean.setLastName(fp.getString(INPUT_LAST_NAME));
            createdUserAccountBean.setEmail(fp.getString(INPUT_EMAIL));
            createdUserAccountBean.setInstitutionalAffiliation(fp.getString(INPUT_INSTITUTION));
            boolean isLdap = fp.getString(INPUT_USER_SOURCE).equals("ldap");
            String password = null;
            String passwordHash = UserAccountBean.LDAP_PASSWORD;
            if (!isLdap) {
                SecurityManager secm = (SecurityManager) SpringServletAccess.getApplicationContext(context).getBean("securityManager");
                password = secm.genPassword();
                passwordHash = secm.encrytPassword(password, getUserDetails());
            }
            createdUserAccountBean.setPasswd(passwordHash);
            createdUserAccountBean.setPasswdTimestamp(null);
            createdUserAccountBean.setLastVisitDate(null);
            createdUserAccountBean.setStatus(Status.AVAILABLE);
            createdUserAccountBean.setPasswdChallengeQuestion("");
            createdUserAccountBean.setPasswdChallengeAnswer("");
            createdUserAccountBean.setPhone("");
            createdUserAccountBean.setOwner(ub);
            createdUserAccountBean.setRunWebservices(fp.getBoolean(INPUT_RUN_WEBSERVICES));
            createdUserAccountBean.setAccessCode("null");
            createdUserAccountBean.setEnableApiKey(true);
            String apiKey = null;
            do {
                apiKey = getRandom32ChApiKey();
            } while (isApiKeyExist(apiKey));
            createdUserAccountBean.setApiKey(apiKey);
            int studyId = fp.getInt(INPUT_STUDY);
            Role r = Role.get(fp.getInt(INPUT_ROLE));
            createdUserAccountBean = addActiveStudyRole(createdUserAccountBean, studyId, r);
            UserType type = UserType.get(fp.getInt("type"));
            logger.debug("*** found type: " + fp.getInt("type"));
            logger.debug("*** setting type: " + type.getDescription());
            createdUserAccountBean.addUserType(type);
            createdUserAccountBean = (UserAccountBean) udao.create(createdUserAccountBean);
            AuthoritiesDao authoritiesDao = (AuthoritiesDao) SpringServletAccess.getApplicationContext(context).getBean("authoritiesDao");
            authoritiesDao.saveOrUpdate(new AuthoritiesBean(createdUserAccountBean.getName()));
            String displayPwd = fp.getString(INPUT_DISPLAY_PWD);
            if (createdUserAccountBean.isActive()) {
                addPageMessage(respage.getString("the_user_account") + "\"" + createdUserAccountBean.getName() + "\"" + respage.getString("was_created_succesfully"));
                if (!isLdap) {
                    if ("no".equalsIgnoreCase(displayPwd)) {
                        try {
                            sendNewAccountEmail(createdUserAccountBean, password);
                        } catch (Exception e) {
                            addPageMessage(respage.getString("there_was_an_error_sending_account_creating_mail"));
                        }
                    } else {
                        addPageMessage(respage.getString("user_password") + ":<br/>" + password + "<br/> " + respage.getString("please_write_down_the_password_and_provide"));
                    }
                }
            } else {
                addPageMessage(respage.getString("the_user_account") + "\"" + createdUserAccountBean.getName() + "\"" + respage.getString("could_not_created_due_database_error"));
            }
            if (createdUserAccountBean.isActive()) {
                request.setAttribute(ViewUserAccountServlet.ARG_USER_ID, new Integer(createdUserAccountBean.getId()).toString());
                forwardPage(Page.VIEW_USER_ACCOUNT_SERVLET);
            } else {
                forwardPage(Page.LIST_USER_ACCOUNTS_SERVLET);
            }
        } else {
            String[] textFields = { INPUT_USERNAME, INPUT_FIRST_NAME, INPUT_LAST_NAME, INPUT_EMAIL, INPUT_INSTITUTION, INPUT_DISPLAY_PWD, INPUT_USER_SOURCE };
            fp.setCurrentStringValuesAsPreset(textFields);
            String[] ddlbFields = { INPUT_STUDY, INPUT_ROLE, INPUT_TYPE, INPUT_RUN_WEBSERVICES };
            fp.setCurrentIntValuesAsPreset(ddlbFields);
            HashMap presetValues = fp.getPresetValues();
            setPresetValues(presetValues);
            setInputMessages(errors);
            addPageMessage(respage.getString("there_were_some_errors_submission") + respage.getString("see_below_for_details"));
            forwardPage(Page.CREATE_ACCOUNT);
        }
    }
}
Also used : AuthoritiesBean(org.akaza.openclinica.domain.user.AuthoritiesBean) SecurityManager(org.akaza.openclinica.core.SecurityManager) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Iterator(java.util.Iterator) UserAccountBean(org.akaza.openclinica.bean.login.UserAccountBean) AuthoritiesDao(org.akaza.openclinica.dao.hibernate.AuthoritiesDao) StudyDAO(org.akaza.openclinica.dao.managestudy.StudyDAO) FormProcessor(org.akaza.openclinica.control.form.FormProcessor) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) UserAccountDAO(org.akaza.openclinica.dao.login.UserAccountDAO) InsufficientPermissionException(org.akaza.openclinica.web.InsufficientPermissionException) Role(org.akaza.openclinica.bean.core.Role) ResourceBundle(java.util.ResourceBundle) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) UserType(org.akaza.openclinica.bean.core.UserType) Validator(org.akaza.openclinica.control.form.Validator)

Example 2 with UserType

use of org.akaza.openclinica.bean.core.UserType in project OpenClinica by OpenClinica.

the class EditUserAccountServlet method processRequest.

@Override
protected void processRequest() throws Exception {
    FormProcessor fp = new FormProcessor(request);
    // because we need to use this in the confirmation and error parts too
    ArrayList studies = getAllStudies();
    request.setAttribute("studies", studies);
    int userId = fp.getInt(ARG_USERID);
    UserAccountDAO udao = new UserAccountDAO(sm.getDataSource());
    UserAccountBean user = (UserAccountBean) udao.findByPK(userId);
    int stepNum = fp.getInt(ARG_STEPNUM);
    if (!fp.isSubmitted()) {
        addEntityList("userTypes", getUserTypes(), respage.getString("the_user_could_not_be_edited_because_no_user_types"), Page.ADMIN_SYSTEM);
        loadPresetValuesFromBean(fp, user);
        fp.addPresetValue(ARG_STEPNUM, EDIT_STEP);
        setPresetValues(fp.getPresetValues());
        // addEntityList("userTypes", getUserTypes(),
        // "The user could not be edited because there are no user types
        // available.",
        // Page.ADMIN_SYSTEM);
        request.setAttribute("userName", user.getName());
        forwardPage(Page.EDIT_ACCOUNT);
    } else if (stepNum == EDIT_STEP) {
        Validator v = new Validator(request);
        v.addValidation(INPUT_FIRST_NAME, Validator.NO_BLANKS);
        v.addValidation(INPUT_LAST_NAME, Validator.NO_BLANKS);
        v.addValidation(INPUT_FIRST_NAME, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 50);
        v.addValidation(INPUT_LAST_NAME, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 50);
        v.addValidation(INPUT_EMAIL, Validator.NO_BLANKS);
        v.addValidation(INPUT_EMAIL, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 120);
        v.addValidation(INPUT_EMAIL, Validator.IS_A_EMAIL);
        v.addValidation(INPUT_INSTITUTION, Validator.NO_BLANKS);
        v.addValidation(INPUT_INSTITUTION, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 255);
        HashMap errors = v.validate();
        if (errors.isEmpty()) {
            loadPresetValuesFromForm(fp);
            fp.addPresetValue(ARG_STEPNUM, CONFIRM_STEP);
            setPresetValues(fp.getPresetValues());
            request.setAttribute("userName", user.getName());
            forwardPage(Page.EDIT_ACCOUNT_CONFIRM);
        } else {
            loadPresetValuesFromForm(fp);
            fp.addPresetValue(ARG_STEPNUM, EDIT_STEP);
            setInputMessages(errors);
            setPresetValues(fp.getPresetValues());
            addEntityList("userTypes", getUserTypes(), respage.getString("the_user_could_not_be_edited_because_no_user_types"), Page.ADMIN_SYSTEM);
            addPageMessage(respage.getString("there_were_some_errors_submission") + respage.getString("see_below_for_details"));
            forwardPage(Page.EDIT_ACCOUNT);
        }
    } else if (stepNum == CONFIRM_STEP) {
        String button = fp.getString(INPUT_CONFIRM_BUTTON);
        if (button.equals(resword.getString("back"))) {
            loadPresetValuesFromForm(fp);
            fp.addPresetValue(ARG_STEPNUM, EDIT_STEP);
            addEntityList("userTypes", getUserTypes(), respage.getString("the_user_could_not_be_edited_because_no_user_types"), Page.ADMIN_SYSTEM);
            setPresetValues(fp.getPresetValues());
            request.setAttribute("userName", user.getName());
            forwardPage(Page.EDIT_ACCOUNT);
        } else if (button.equals(resword.getString("confirm"))) {
            user.setFirstName(fp.getString(INPUT_FIRST_NAME));
            user.setLastName(fp.getString(INPUT_LAST_NAME));
            user.setEmail(fp.getString(INPUT_EMAIL));
            user.setInstitutionalAffiliation(fp.getString(INPUT_INSTITUTION));
            user.setUpdater(ub);
            user.setRunWebservices(fp.getBoolean(INPUT_RUN_WEBSERVICES));
            user.setEnableApiKey(true);
            String apiKey = null;
            do {
                apiKey = getRandom32ChApiKey();
            } while (isApiKeyExist(apiKey));
            user.setApiKey(apiKey);
            UserType ut = UserType.get(fp.getInt(INPUT_USER_TYPE));
            if (ut.equals(UserType.SYSADMIN)) {
                user.addUserType(ut);
            } else if (ut.equals(UserType.TECHADMIN)) {
                user.addUserType(ut);
            } else {
                user.addUserType(UserType.USER);
            }
            if (fp.getBoolean(INPUT_RESET_PASSWORD)) {
                SecurityManager sm = ((SecurityManager) SpringServletAccess.getApplicationContext(context).getBean("securityManager"));
                String password = sm.genPassword();
                String passwordHash = sm.encrytPassword(password, getUserDetails());
                user.setPasswd(passwordHash);
                user.setPasswdTimestamp(null);
                udao.update(user);
                if ("no".equalsIgnoreCase(fp.getString(INPUT_DISPLAY_PWD))) {
                    logger.info("displayPwd is no");
                    try {
                        sendResetPasswordEmail(user, password);
                    } catch (Exception e) {
                        addPageMessage(respage.getString("there_was_an_error_sending_reset_email_try_reset"));
                    }
                } else {
                    addPageMessage(respage.getString("new_user_password") + ":<br/> " + password + "<br/>" + respage.getString("please_write_down_the_password_and_provide"));
                }
            } else {
                udao.update(user);
            }
            addPageMessage(respage.getString("the_user_account") + " \"" + user.getName() + "\" " + respage.getString("was_updated_succesfully"));
            forwardPage(Page.LIST_USER_ACCOUNTS_SERVLET);
        } else {
            throw new InconsistentStateException(Page.ADMIN_SYSTEM, resexception.getString("an_invalid_submit_button_was_clicked"));
        }
    } else {
        throw new InconsistentStateException(Page.ADMIN_SYSTEM, resexception.getString("an_invalid_step_was_specified"));
    }
}
Also used : SecurityManager(org.akaza.openclinica.core.SecurityManager) HashMap(java.util.HashMap) FormProcessor(org.akaza.openclinica.control.form.FormProcessor) ArrayList(java.util.ArrayList) UserAccountBean(org.akaza.openclinica.bean.login.UserAccountBean) UserAccountDAO(org.akaza.openclinica.dao.login.UserAccountDAO) UserType(org.akaza.openclinica.bean.core.UserType) InconsistentStateException(org.akaza.openclinica.web.InconsistentStateException) Validator(org.akaza.openclinica.control.form.Validator) InsufficientPermissionException(org.akaza.openclinica.web.InsufficientPermissionException) InconsistentStateException(org.akaza.openclinica.web.InconsistentStateException)

Example 3 with UserType

use of org.akaza.openclinica.bean.core.UserType in project OpenClinica by OpenClinica.

the class UserAccountController method createOrUpdateAccount.

/**
	 * @api {post} /pages/auth/api/v1/createuseraccount Create a user account
	 * @apiName createOrUpdateAccount2
	 * @apiPermission admin
	 * @apiVersion 3.8.0
	 * @apiParam {String} username UserName
	 * @apiParam {String} fName First Name
	 * @apiParam {String} lName Last Name
	 * @apiParam {String} institution Institution
	 * @apiParam {String} email Email Address
	 * @apiParam {String} study_name Study Name
	 * @apiParam {String} role_name Role Name
	 * @apiParam {String} user_type User Type
	 * @apiParam {String} authorize_soap Authorize Soap
	 *
	 * @apiGroup User Account
	 * @apiDescription Creates a user account
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "username": "testingUser",
	 *                  "fName": "Jimmy",
	 *                  "lName": "Sander",
	 *                  "institution": "OC",
	 *                  "email": "abcde@yahoo.com",
	 *                  "study_name": "Baseline Study 101",
	 *                  "role_name": "Data Manager",
	 *                  "user_type": "user",
	 *                  "authorize_soap":"false"
	 *                  }
	 * @apiErrorExample {json} Error-Response:
	 *                  HTTP/1.1 400 Bad Request
	 *                  {
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    HTTP/1.1 200 OK
	 *                    {
	 *                    "lastName": "Sander",
	 *                    "username": "testingUser",
	 *                    "firstName": "Jimmy",
	 *                    "password": "rgluVsO0",
	 *                    "apiKey": "5f462a16b3b04b1b9747262968bd5d2f"
	 *                    }
	 */
@RequestMapping(value = "/createuseraccount", method = RequestMethod.POST)
public ResponseEntity<HashMap> createOrUpdateAccount(HttpServletRequest request, @RequestBody HashMap<String, String> map) throws Exception {
    logger.info("I'm in createUserAccount");
    System.out.println("I'm in createUserAccount");
    uBean = null;
    String username = map.get("username");
    String fName = map.get("fName");
    String lName = map.get("lName");
    String institution = map.get("institution");
    String email = map.get("email");
    String studyName = map.get("study_name");
    String roleName = map.get("role_name");
    String userType = map.get("user_type");
    // true or false
    String authorizeSoap = map.get("authorize_soap");
    request.setAttribute("username", username);
    request.setAttribute("fName", fName);
    request.setAttribute("lName", lName);
    request.setAttribute("institution", institution);
    request.setAttribute("email", email);
    request.setAttribute("study_name", studyName);
    request.setAttribute("role_name", roleName);
    // UserAccountBean ownerUserAccount = getUserAccountByApiKey(apiKey);
    UserAccountBean ownerUserAccount = (UserAccountBean) request.getSession().getAttribute("userBean");
    if (!ownerUserAccount.isActive() && (!ownerUserAccount.isTechAdmin() || !ownerUserAccount.isSysAdmin())) {
        logger.info("The Owner User Account is not Valid Account or Does not have Admin user type");
        System.out.println("The Owner User Account is not Valid Account or Does not have Admin user type");
        return new ResponseEntity<HashMap>(new HashMap(), org.springframework.http.HttpStatus.BAD_REQUEST);
    }
    // generate password
    // generate
    String password = "";
    String passwordHash = UserAccountBean.LDAP_PASSWORD;
    SecurityManager secm = (SecurityManager) SpringServletAccess.getApplicationContext(context).getBean("securityManager");
    password = secm.genPassword();
    passwordHash = secm.encrytPassword(password, null);
    // Validate Entry Fields
    request.getSession().setAttribute(LocaleResolver.getLocaleSessionAttributeName(), new Locale("en_US"));
    Validator v = new Validator(request);
    addValidationToFields(v, username);
    HashMap errors = v.validate();
    if (!errors.isEmpty()) {
        logger.info("Validation Error: " + errors.toString());
        System.out.println("Validation Error: " + errors.toString());
        return new ResponseEntity<HashMap>(new HashMap(), org.springframework.http.HttpStatus.BAD_REQUEST);
    }
    StudyBean study = getStudyByName(studyName);
    if (!study.isActive()) {
        logger.info("The Study Name is not Valid");
        System.out.println("The Study Name is not Valid");
        return new ResponseEntity<HashMap>(new HashMap(), org.springframework.http.HttpStatus.BAD_REQUEST);
    }
    // Role
    ResourceBundle resterm = org.akaza.openclinica.i18n.util.ResourceBundleProvider.getTermsBundle();
    Map<Integer, String> roleMap = buildRoleMap(study, resterm);
    boolean found = false;
    Role role = null;
    for (Map.Entry<Integer, String> entry : roleMap.entrySet()) {
        if (roleName.equalsIgnoreCase(entry.getValue())) {
            Integer key = entry.getKey();
            role = Role.get(key);
            found = true;
            break;
        }
    }
    if (!found) {
        logger.info("The Role is not a Valid Role for the Study or Site");
        System.out.println("The Role is not a Valid Role for the Study or Site");
        return new ResponseEntity<HashMap>(new HashMap(), org.springframework.http.HttpStatus.BAD_REQUEST);
    }
    // User Types
    found = false;
    UserType uType = null;
    ArrayList<UserType> types = UserType.toArrayList();
    types.remove(UserType.INVALID);
    for (UserType type : types) {
        if (userType.equalsIgnoreCase(type.getName())) {
            uType = UserType.get(type.getId());
            found = true;
            break;
        }
    }
    if (!found) {
        logger.info("The Type is not a Valid User Type");
        System.out.println("The Type is not a Valid User Type");
        return new ResponseEntity<HashMap>(new HashMap(), org.springframework.http.HttpStatus.BAD_REQUEST);
    }
    // build UserName
    uBean = buildUserAccount(username, fName, lName, password, institution, study, ownerUserAccount, email, passwordHash, Boolean.valueOf(authorizeSoap), role, uType);
    HashMap<String, Object> userDTO = null;
    UserAccountBean uaBean = getUserAccount(uBean.getName());
    if (!uaBean.isActive()) {
        createUserAccount(uBean);
        uBean.setUpdater(uBean.getOwner());
        updateUserAccount(uBean);
        logger.info("***New User Account is created***");
        System.out.println("***New User Account is created***");
        uBean.setPasswd(password);
        userDTO = new HashMap<String, Object>();
        userDTO.put("username", uBean.getName());
        userDTO.put("password", uBean.getPasswd());
        userDTO.put("firstName", uBean.getFirstName());
        userDTO.put("lastName", uBean.getLastName());
        userDTO.put("apiKey", uBean.getApiKey());
    }
    return new ResponseEntity<HashMap>(userDTO, org.springframework.http.HttpStatus.OK);
}
Also used : Locale(java.util.Locale) SecurityManager(org.akaza.openclinica.core.SecurityManager) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) Role(org.akaza.openclinica.bean.core.Role) ResponseEntity(org.springframework.http.ResponseEntity) UserAccountBean(org.akaza.openclinica.bean.login.UserAccountBean) ResourceBundle(java.util.ResourceBundle) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UserType(org.akaza.openclinica.bean.core.UserType) Validator(org.akaza.openclinica.control.form.Validator) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with UserType

use of org.akaza.openclinica.bean.core.UserType in project OpenClinica by OpenClinica.

the class AccountController method buildUserAccount.

private UserAccountBean buildUserAccount(String studyOid, String studySubjectOid, String fName, String lName, String mobile, String accessCode, UserAccountBean ownerUserAccount, String pUserName, String email) throws Exception {
    UserAccountBean createdUserAccountBean = new UserAccountBean();
    createdUserAccountBean.setName(pUserName);
    createdUserAccountBean.setFirstName(fName);
    createdUserAccountBean.setLastName(lName);
    createdUserAccountBean.setEmail(INPUT_EMAIL);
    createdUserAccountBean.setInstitutionalAffiliation(INPUT_INSTITUTION);
    createdUserAccountBean.setLastVisitDate(null);
    createdUserAccountBean.setActiveStudyId(getStudy(studyOid).getId());
    createdUserAccountBean.setPasswdTimestamp(null);
    createdUserAccountBean.setPasswdChallengeQuestion("");
    createdUserAccountBean.setPasswdChallengeAnswer("");
    createdUserAccountBean.setOwner(ownerUserAccount);
    createdUserAccountBean.setRunWebservices(false);
    createdUserAccountBean.setPhone(mobile);
    createdUserAccountBean.setAccessCode(accessCode);
    createdUserAccountBean.setPasswd("5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8");
    createdUserAccountBean.setEmail(email);
    // Since 3.8, openclinica participate needs to be able to use api from openclinica using api_key
    // Copied from UserAccountController.java
    String apiKey = null;
    do {
        apiKey = getRandom32ChApiKey();
    } while (isApiKeyExist(apiKey));
    createdUserAccountBean.setEnableApiKey(true);
    createdUserAccountBean.setApiKey(apiKey);
    Role r = Role.RESEARCHASSISTANT2;
    createdUserAccountBean = addActiveStudyRole(createdUserAccountBean, getStudy(studyOid).getId(), r, ownerUserAccount);
    UserType type = UserType.get(2);
    createdUserAccountBean.addUserType(type);
    authoritiesDao = (AuthoritiesDao) SpringServletAccess.getApplicationContext(context).getBean("authoritiesDao");
    authoritiesDao.saveOrUpdate(new AuthoritiesBean(createdUserAccountBean.getName()));
    return createdUserAccountBean;
}
Also used : Role(org.akaza.openclinica.bean.core.Role) AuthoritiesBean(org.akaza.openclinica.domain.user.AuthoritiesBean) UserAccountBean(org.akaza.openclinica.bean.login.UserAccountBean) UserType(org.akaza.openclinica.bean.core.UserType)

Aggregations

UserType (org.akaza.openclinica.bean.core.UserType)4 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)4 HashMap (java.util.HashMap)3 Role (org.akaza.openclinica.bean.core.Role)3 Validator (org.akaza.openclinica.control.form.Validator)3 SecurityManager (org.akaza.openclinica.core.SecurityManager)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ResourceBundle (java.util.ResourceBundle)2 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)2 FormProcessor (org.akaza.openclinica.control.form.FormProcessor)2 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)2 AuthoritiesBean (org.akaza.openclinica.domain.user.AuthoritiesBean)2 InsufficientPermissionException (org.akaza.openclinica.web.InsufficientPermissionException)2 Iterator (java.util.Iterator)1 Locale (java.util.Locale)1 AuthoritiesDao (org.akaza.openclinica.dao.hibernate.AuthoritiesDao)1 StudyDAO (org.akaza.openclinica.dao.managestudy.StudyDAO)1 InconsistentStateException (org.akaza.openclinica.web.InconsistentStateException)1