use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class PaymentMethodServices method updateEftAccount.
/**
* Updates EftAccount and PaymentMethod entities according to the parameters passed in the context
* <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_UPDATE permission
* @param ctx The DispatchContext that this service is operating in
* @param context Map containing the input parameters
* @return Map with the result of the service, the output parameters
*/
public static Map<String, Object> updateEftAccount(DispatchContext ctx, Map<String, ? extends Object> context) {
Map<String, Object> result = new HashMap<>();
Delegator delegator = ctx.getDelegator();
Security security = ctx.getSecurity();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
Timestamp now = UtilDateTime.nowTimestamp();
String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_UPDATE", "ACCOUNTING", "_UPDATE");
if (result.size() > 0) {
return result;
}
List<GenericValue> toBeStored = new LinkedList<>();
boolean isModified = false;
GenericValue paymentMethod = null;
GenericValue newPm = null;
GenericValue eftAccount = null;
GenericValue newEa = null;
String paymentMethodId = (String) context.get("paymentMethodId");
try {
eftAccount = EntityQuery.use(delegator).from("EftAccount").where("paymentMethodId", paymentMethodId).queryOne();
paymentMethod = EntityQuery.use(delegator).from("PaymentMethod").where("paymentMethodId", paymentMethodId).queryOne();
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingEftAccountCannotBeUpdatedReadFailure", UtilMisc.toMap("errorString", e.getMessage()), locale));
}
if (eftAccount == null || paymentMethod == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingEftAccountCannotBeUpdated", UtilMisc.toMap("errorString", paymentMethodId), locale));
}
if (!paymentMethod.getString("partyId").equals(partyId) && !security.hasEntityPermission("PAY_INFO", "_UPDATE", userLogin) && !security.hasEntityPermission("ACCOUNTING", "_UPDATE", userLogin)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingEftAccountCannotBeUpdated", UtilMisc.toMap("partyId", partyId, "paymentMethodId", paymentMethodId), locale));
}
newPm = GenericValue.create(paymentMethod);
toBeStored.add(newPm);
newEa = GenericValue.create(eftAccount);
toBeStored.add(newEa);
String newPmId = null;
try {
newPmId = delegator.getNextSeqId("PaymentMethod");
} catch (IllegalArgumentException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingEftAccountCannotBeCreated", locale));
}
newPm.set("partyId", partyId);
newPm.set("fromDate", context.get("fromDate"), false);
newPm.set("thruDate", context.get("thruDate"));
newPm.set("description", context.get("description"));
newEa.set("bankName", context.get("bankName"));
newEa.set("routingNumber", context.get("routingNumber"));
newEa.set("accountType", context.get("accountType"));
newEa.set("accountNumber", context.get("accountNumber"));
newEa.set("nameOnAccount", context.get("nameOnAccount"));
newEa.set("companyNameOnAccount", context.get("companyNameOnAccount"));
newEa.set("contactMechId", context.get("contactMechId"));
if (!newEa.equals(eftAccount) || !newPm.equals(paymentMethod)) {
newPm.set("paymentMethodId", newPmId);
newEa.set("paymentMethodId", newPmId);
newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now));
isModified = true;
}
GenericValue newPartyContactMechPurpose = null;
String contactMechId = (String) context.get("contactMechId");
if (UtilValidate.isNotEmpty(contactMechId)) {
// add a PartyContactMechPurpose of BILLING_LOCATION if necessary
String contactMechPurposeTypeId = "BILLING_LOCATION";
GenericValue tempVal = null;
try {
List<GenericValue> allPCWPs = EntityQuery.use(delegator).from("PartyContactWithPurpose").where("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId).queryList();
allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "contactFromDate", "contactThruDate", true);
allPCWPs = EntityUtil.filterByDate(allPCWPs, now, "purposeFromDate", "purposeThruDate", true);
tempVal = EntityUtil.getFirst(allPCWPs);
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
tempVal = null;
}
if (tempVal == null) {
// no value found, create a new one
newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now));
}
}
if (isModified) {
// Debug.logInfo("yes, is modified", module);
if (newPartyContactMechPurpose != null) {
toBeStored.add(newPartyContactMechPurpose);
}
// set thru date on old paymentMethod
paymentMethod.set("thruDate", now);
toBeStored.add(paymentMethod);
try {
delegator.storeAll(toBeStored);
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingEftAccountCannotBeUpdated", UtilMisc.toMap("errorString", e.getMessage()), locale));
}
} else {
result.put("paymentMethodId", paymentMethodId);
result.put("oldPaymentMethodId", paymentMethodId);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage(resource, "AccountingNoChangesMadeNotUpdatingEftAccount", locale));
return result;
}
result.put("paymentMethodId", newEa.getString("paymentMethodId"));
result.put("oldPaymentMethodId", paymentMethodId);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
return result;
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class PaymentMethodServices method createGiftCard.
public static Map<String, Object> createGiftCard(DispatchContext ctx, Map<String, ? extends Object> context) {
Map<String, Object> result = new HashMap<>();
Delegator delegator = ctx.getDelegator();
Security security = ctx.getSecurity();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
Timestamp now = UtilDateTime.nowTimestamp();
String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_CREATE", "ACCOUNTING", "_CREATE");
if (result.size() > 0) {
return result;
}
List<GenericValue> toBeStored = new LinkedList<>();
GenericValue newPm = delegator.makeValue("PaymentMethod");
toBeStored.add(newPm);
GenericValue newGc = delegator.makeValue("GiftCard");
toBeStored.add(newGc);
String newPmId = (String) context.get("paymentMethodId");
if (UtilValidate.isEmpty(newPmId)) {
try {
newPmId = delegator.getNextSeqId("PaymentMethod");
} catch (IllegalArgumentException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingGiftCardCannotBeCreated", locale));
}
}
newPm.set("partyId", partyId);
newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now));
newPm.set("thruDate", context.get("thruDate"));
newPm.set("description", context.get("description"));
newGc.set("cardNumber", context.get("cardNumber"));
newGc.set("pinNumber", context.get("pinNumber"));
newGc.set("expireDate", context.get("expireDate"));
newPm.set("paymentMethodId", newPmId);
newPm.set("paymentMethodTypeId", "GIFT_CARD");
newGc.set("paymentMethodId", newPmId);
try {
delegator.storeAll(toBeStored);
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingGiftCardCannotBeCreatedWriteFailure", UtilMisc.toMap("errorString", e.getMessage()), locale));
}
result.put("paymentMethodId", newGc.getString("paymentMethodId"));
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
return result;
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class ContentManagementServices method followNodeChildren.
public static Map<String, Object> followNodeChildren(DispatchContext dctx, Map<String, ? extends Object> context) throws GenericServiceException {
Map<String, Object> result = null;
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
Security security = dctx.getSecurity();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
if (!security.hasEntityPermission("CONTENTMGR", "_ADMIN", userLogin)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentPermissionNotGranted", locale));
}
String contentId = (String) context.get("contentId");
String serviceName = (String) context.get("serviceName");
String contentAssocTypeId = (String) context.get("contentAssocTypeId");
List<String> contentAssocTypeIdList = new LinkedList<String>();
if (UtilValidate.isNotEmpty(contentAssocTypeId)) {
contentAssocTypeIdList = StringUtil.split(contentAssocTypeId, "|");
}
if (contentAssocTypeIdList.size() == 0) {
contentAssocTypeIdList.add("SUB_CONTENT");
}
Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("userLogin", userLogin);
ctx.put("contentAssocTypeIdList", contentAssocTypeIdList);
try {
GenericValue content = EntityQuery.use(delegator).from("Content").where("contentId", contentId).queryOne();
result = followNodeChildrenMethod(content, dispatcher, serviceName, ctx);
} catch (GenericEntityException e) {
Debug.logError(e.toString(), module);
return ServiceUtil.returnError(e.toString());
}
return result;
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class CommonEvents method setFollowerPage.
public static String setFollowerPage(HttpServletRequest request, HttpServletResponse response) {
Security security = (Security) request.getAttribute("security");
GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
String visitId = request.getParameter("visitId");
if (visitId != null) {
request.setAttribute("visitId", visitId);
}
return "success";
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class UpgradeServices method generateMySqlFileWithAlterTableForTimestamps.
/**
* Generate sql file for data migration from mySql.5 and earlier version to mySql.6 to later version
* mySql added support in 5.6 to support microseconds for datetime field.
* https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html
* <ul>
* <li>Service will take [groupName] as in param,</li>
* <li>iterate all the entity and check for datetime and time field</li>
* <li>it will generate alter table sql statement to update the field data type</li>
* <li>datetime will be altered with DATETIME(3)</li>
* <li>time will be altered with TIME(3)</li>
* <li>sql fiel will be created at following location</li>
* <li>${ofbiz.home}/runtime/tempfiles/[groupName].sql</li>
* </ul>
* @param dctx
* @param context
* @return Map with the success result of the service,
*/
public static Map<String, Object> generateMySqlFileWithAlterTableForTimestamps(DispatchContext dctx, Map<String, Object> context) {
Delegator delegator = dctx.getDelegator();
Security security = dctx.getSecurity();
Locale locale = (Locale) context.get("locale");
// check permission
GenericValue userLogin = (GenericValue) context.get("userLogin");
if (!security.hasPermission("ENTITY_MAINT", userLogin)) {
Debug.logError(UtilProperties.getMessage(resource, "EntityExtServicePermissionNotGranted", locale), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtServicePermissionNotGranted", locale));
}
String groupName = (String) context.get("groupName");
Map<String, ModelEntity> modelEntities;
try (PrintWriter dataWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(System.getProperty("ofbiz.home") + "/runtime/tempfiles/" + groupName + ".sql")), "UTF-8")))) {
modelEntities = delegator.getModelEntityMapByGroup(groupName);
/* TODO:
1) fetch the meta data of the "date-time" field using the JDBC connection and JDBC meta data;
2) compare it to date-time and only generate the alter statement if they differs;
*/
dataWriter.println("SET FOREIGN_KEY_CHECKS=0;");
for (ModelEntity modelEntity : modelEntities.values()) {
List<ModelField> fields = modelEntity.getFieldsUnmodifiable();
for (ModelField field : fields) {
if (modelEntity.getPlainTableName() != null) {
if ("date-time".equals(field.getType())) {
dataWriter.println("ALTER TABLE " + modelEntity.getPlainTableName() + " MODIFY " + field.getColName() + " DATETIME(3);");
}
if ("time".equals(field.getType())) {
dataWriter.println("ALTER TABLE " + modelEntity.getPlainTableName() + " MODIFY " + field.getColName() + " TIME(3);");
}
}
}
}
dataWriter.println("SET FOREIGN_KEY_CHECKS=1;");
} catch (GenericEntityException e) {
Debug.logError(e, "Error getting list of entities in group: " + e.toString(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtErrorGettingListOfEntityInGroup", UtilMisc.toMap("errorString", e.toString()), locale));
} catch (FileNotFoundException | UnsupportedEncodingException e) {
Debug.logError(e, e.getMessage(), module);
return ServiceUtil.returnError(e.getMessage());
}
return ServiceUtil.returnSuccess();
}
Aggregations