Search in sources :

Example 86 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.

the class LoginServices method checkNewPassword.

public static void checkNewPassword(GenericValue userLogin, String currentPassword, String newPassword, String newPasswordVerify, String passwordHint, List<String> errorMessageList, boolean ignoreCurrentPassword, Locale locale) {
    Delegator delegator = userLogin.getDelegator();
    boolean useEncryption = "true".equals(EntityUtilProperties.getPropertyValue("security", "password.encrypt", delegator));
    String errMsg = null;
    if (!ignoreCurrentPassword) {
        // if the password.accept.encrypted.and.plain property in security is set to true allow plain or encrypted passwords
        // if this is a system account don't bother checking the passwords
        boolean passwordMatches = checkPassword(userLogin.getString("currentPassword"), useEncryption, currentPassword);
        if ((currentPassword == null) || (!passwordMatches)) {
            errMsg = UtilProperties.getMessage(resource, "loginservices.old_password_not_correct_reenter", locale);
            errorMessageList.add(errMsg);
        }
        if (checkPassword(userLogin.getString("currentPassword"), useEncryption, newPassword)) {
            errMsg = UtilProperties.getMessage(resource, "loginservices.new_password_is_equal_to_old_password", locale);
            errorMessageList.add(errMsg);
        }
    }
    if (UtilValidate.isEmpty(newPassword) || UtilValidate.isEmpty(newPasswordVerify)) {
        errMsg = UtilProperties.getMessage(resource, "loginservices.password_or_verify_missing", locale);
        errorMessageList.add(errMsg);
    } else if (!newPassword.equals(newPasswordVerify)) {
        errMsg = UtilProperties.getMessage(resource, "loginservices.password_did_not_match_verify_password", locale);
        errorMessageList.add(errMsg);
    }
    int passwordChangeHistoryLimit = 0;
    try {
        passwordChangeHistoryLimit = EntityUtilProperties.getPropertyAsInteger("security", "password.change.history.limit", 0).intValue();
    } catch (NumberFormatException nfe) {
        // No valid value is found so don't bother to save any password history
        passwordChangeHistoryLimit = 0;
    }
    Debug.logInfo(" password.change.history.limit is set to " + passwordChangeHistoryLimit, module);
    if (passwordChangeHistoryLimit > 0) {
        Debug.logInfo(" checkNewPassword Checking if user is tyring to use old password " + passwordChangeHistoryLimit, module);
        try {
            List<GenericValue> pwdHistList = EntityQuery.use(delegator).from("UserLoginPasswordHistory").where("userLoginId", userLogin.getString("userLoginId")).orderBy("-fromDate").queryList();
            for (GenericValue pwdHistValue : pwdHistList) {
                if (checkPassword(pwdHistValue.getString("currentPassword"), useEncryption, newPassword)) {
                    Map<String, Integer> messageMap = UtilMisc.toMap("passwordChangeHistoryLimit", passwordChangeHistoryLimit);
                    errMsg = UtilProperties.getMessage(resource, "loginservices.password_must_be_different_from_last_passwords", messageMap, locale);
                    errorMessageList.add(errMsg);
                    break;
                }
            }
        } catch (GenericEntityException e) {
            Debug.logWarning(e, "", module);
            Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage());
            errMsg = UtilProperties.getMessage(resource, "loginevents.error_accessing_password_change_history", messageMap, locale);
        }
    }
    int minPasswordLength = 0;
    try {
        minPasswordLength = EntityUtilProperties.getPropertyAsInteger("security", "password.length.min", 0).intValue();
    } catch (NumberFormatException nfe) {
        minPasswordLength = 0;
    }
    if (newPassword != null) {
        // Matching password with pattern
        String passwordPattern = EntityUtilProperties.getPropertyValue("security", "security.login.password.pattern", "^.*(?=.{5,}).*$", delegator);
        boolean usePasswordPattern = UtilProperties.getPropertyAsBoolean("security", "security.login.password.pattern.enable", true);
        if (usePasswordPattern) {
            Pattern pattern = Pattern.compile(passwordPattern);
            Matcher matcher = pattern.matcher(newPassword);
            boolean matched = matcher.matches();
            if (!matched) {
                // This is a mix to handle the OOTB pattern which is only a fixed length
                Map<String, String> messageMap = UtilMisc.toMap("minPasswordLength", Integer.toString(minPasswordLength));
                String passwordPatternMessage = EntityUtilProperties.getPropertyValue("security", "security.login.password.pattern.description", "loginservices.password_must_be_least_characters_long", delegator);
                errMsg = UtilProperties.getMessage(resource, passwordPatternMessage, messageMap, locale);
                errorMessageList.add(errMsg);
            }
        } else {
            if (!(newPassword.length() >= minPasswordLength)) {
                Map<String, String> messageMap = UtilMisc.toMap("minPasswordLength", Integer.toString(minPasswordLength));
                errMsg = UtilProperties.getMessage(resource, "loginservices.password_must_be_least_characters_long", messageMap, locale);
                errorMessageList.add(errMsg);
            }
        }
        if (newPassword.equalsIgnoreCase(userLogin.getString("userLoginId"))) {
            errMsg = UtilProperties.getMessage(resource, "loginservices.password_may_not_equal_username", locale);
            errorMessageList.add(errMsg);
        }
        if (UtilValidate.isNotEmpty(passwordHint) && (passwordHint.toUpperCase(Locale.getDefault()).indexOf(newPassword.toUpperCase(Locale.getDefault())) >= 0)) {
            errMsg = UtilProperties.getMessage(resource, "loginservices.password_hint_may_not_contain_password", locale);
            errorMessageList.add(errMsg);
        }
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 87 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.

the class PreferenceServices method removeUserPreference.

public static Map<String, Object> removeUserPreference(DispatchContext ctx, Map<String, ?> context) {
    Delegator delegator = ctx.getDelegator();
    Locale locale = (Locale) context.get("locale");
    String userLoginId = PreferenceWorker.getUserLoginId(context, false);
    String userPrefTypeId = (String) context.get("userPrefTypeId");
    if (UtilValidate.isEmpty(userLoginId) || UtilValidate.isEmpty(userPrefTypeId)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "setPreference.invalidArgument", locale));
    }
    try {
        GenericValue rec = EntityQuery.use(delegator).from("UserPreference").where("userLoginId", userLoginId, "userPrefTypeId", userPrefTypeId).queryOne();
        if (rec != null) {
            rec.remove();
        }
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "setPreference.writeFailure", new Object[] { e.getMessage() }, locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 88 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.

the class PreferenceServices method copyUserPreferenceGroup.

/**
 * Copies a user preference group. Call with
 * fromUserLoginId, userPrefGroupTypeId and optional userPrefLoginId. If userPrefLoginId
 * isn't specified, then the currently logged-in user's userLoginId will be
 * used.
 * @param ctx The DispatchContext that this service is operating in.
 * @param context Map containing the input arguments.
 * @return Map with the result of the service, the output parameters.
 */
public static Map<String, Object> copyUserPreferenceGroup(DispatchContext ctx, Map<String, ?> context) {
    Delegator delegator = ctx.getDelegator();
    Locale locale = (Locale) context.get("locale");
    String userLoginId = PreferenceWorker.getUserLoginId(context, false);
    String fromUserLoginId = (String) context.get("fromUserLoginId");
    String userPrefGroupTypeId = (String) context.get("userPrefGroupTypeId");
    if (UtilValidate.isEmpty(userLoginId) || UtilValidate.isEmpty(userPrefGroupTypeId) || UtilValidate.isEmpty(fromUserLoginId)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "copyPreference.invalidArgument", locale));
    }
    try {
        List<GenericValue> resultList = EntityQuery.use(delegator).from("UserPreference").where("userLoginId", fromUserLoginId, "userPrefGroupTypeId", userPrefGroupTypeId).queryList();
        if (resultList != null) {
            for (GenericValue preference : resultList) {
                preference.set("userLoginId", userLoginId);
            }
            delegator.storeAll(resultList);
        }
    } catch (GenericEntityException e) {
        Debug.logWarning(e.getMessage(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "copyPreference.writeFailure", new Object[] { e.getMessage() }, locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 89 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.

the class StatusWorker method getStatusValidChangeToDetails.

public static void getStatusValidChangeToDetails(PageContext pageContext, String attributeName, String statusId) {
    Delegator delegator = (Delegator) pageContext.getRequest().getAttribute("delegator");
    List<GenericValue> statusValidChangeToDetails = null;
    try {
        statusValidChangeToDetails = EntityQuery.use(delegator).from("StatusValidChangeToDetail").where("statusId", statusId).orderBy("sequenceId").cache(true).queryList();
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    if (statusValidChangeToDetails != null)
        pageContext.setAttribute(attributeName, statusValidChangeToDetails);
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 90 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.

the class GenericDAO method select.

public void select(GenericEntity entity, SQLProcessor sqlP) throws GenericEntityException {
    ModelEntity modelEntity = entity.getModelEntity();
    if (modelEntity.getPksSize() <= 0) {
        throw new GenericEntityException("Entity has no primary keys, cannot select by primary key");
    }
    StringBuilder sqlBuffer = new StringBuilder("SELECT ");
    if (modelEntity.getNopksSize() > 0) {
        modelEntity.colNameString(modelEntity.getNopksCopy(), sqlBuffer, "", ", ", "", datasource.getAliasViewColumns());
    } else {
        sqlBuffer.append("*");
    }
    sqlBuffer.append(SqlJdbcUtil.makeFromClause(modelEntity, modelFieldTypeReader, datasource));
    sqlBuffer.append(SqlJdbcUtil.makeWhereClause(modelEntity, modelEntity.getPkFieldsUnmodifiable(), entity, "AND", datasource.getJoinStyle()));
    sqlP.prepareStatement(sqlBuffer.toString(), true, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader);
    sqlP.executeQuery();
    if (sqlP.next()) {
        int idx = 1;
        Iterator<ModelField> nopkIter = modelEntity.getNopksIterator();
        while (nopkIter.hasNext()) {
            ModelField curField = nopkIter.next();
            SqlJdbcUtil.getValue(sqlP.getResultSet(), idx, curField, entity, modelFieldTypeReader);
            idx++;
        }
        entity.synchronizedWithDatasource();
    } else {
        // Debug.logWarning("[GenericDAO.select]: select failed, result set was empty for entity: " + entity.toString(), module);
        throw new GenericEntityNotFoundException("Result set was empty for entity: " + entity.toString());
    }
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericEntityNotFoundException(org.apache.ofbiz.entity.GenericEntityNotFoundException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Aggregations

GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)913 GenericValue (org.apache.ofbiz.entity.GenericValue)847 Delegator (org.apache.ofbiz.entity.Delegator)599 Locale (java.util.Locale)384 HashMap (java.util.HashMap)336 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)270 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)259 LinkedList (java.util.LinkedList)231 BigDecimal (java.math.BigDecimal)213 Timestamp (java.sql.Timestamp)171 Map (java.util.Map)109 GeneralException (org.apache.ofbiz.base.util.GeneralException)95 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)78 IOException (java.io.IOException)75 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)57 Security (org.apache.ofbiz.security.Security)54 ArrayList (java.util.ArrayList)48 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)47 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)39 LinkedHashMap (java.util.LinkedHashMap)37