Search in sources :

Example 1 with RequiresPermissions

use of com.manydesigns.portofino.security.RequiresPermissions in project Portofino by ManyDesigns.

the class ManyToManyAction method httpPostJson.

/**
 * Handles object creation via REST.
 * @param jsonObject the object (in serialized JSON form)
 * @since 4.2.1
 * @return the created object as JSON (in a JAX-RS Response).
 * @throws Exception only to make the compiler happy. Nothing should be thrown in normal operation. If this method throws, it is probably a bug.
 */
@POST
@RequiresPermissions(permissions = ManyToManyAction.PERMISSION_UPDATE)
@Produces(MimeTypes.APPLICATION_JSON_UTF8)
@Consumes(MimeTypes.APPLICATION_JSON_UTF8)
public Response httpPostJson(String jsonObject) throws Exception {
    JSONObject obj = new JSONObject(jsonObject);
    logger.debug(jsonObject);
    if (!correctlyConfigured) {
        return Response.serverError().entity(configurationForm).build();
    }
    for (Object key : obj.keySet()) {
        try {
            loadOnePk(key);
        } catch (Exception e) {
            logger.error("Cannot get key " + key, e);
        }
        JSONArray selectedKeysJson = obj.getJSONArray(onePk.toString());
        for (int i = 0; i < selectedKeysJson.length(); i++) {
            selectedPrimaryKeys.add(selectedKeysJson.get(i).toString());
        }
        try {
            loadAssociations();
        } catch (Exception e) {
            logger.error("Could not load associations", e);
            return Response.serverError().entity(e).build();
        }
        // TODO chiave multipla
        String onePropertyName = m2mConfiguration.getActualOnePropertyName();
        PropertyAccessor onePropertyAccessor = relationTableAccessor.getProperty(onePropertyName);
        // TODO chiave multipla
        String manyPropertyName = m2mConfiguration.getManySelectionProvider().getActualSelectionProvider().getReferences().get(0).getActualFromColumn().getActualPropertyName();
        PropertyAccessor manyPropertyAccessor = relationTableAccessor.getProperty(manyPropertyName);
        PropertyAccessor[] manyKeyProperties = manyTableAccessor.getKeyProperties();
        // TODO handle manyKeyProperties.length > 1
        PropertyAccessor manyPkAccessor = manyTableAccessor.getProperty(manyKeyProperties[0].getName());
        for (String pkString : selectedPrimaryKeys) {
            Object pkObject = manyTableAccessor.getIdStrategy().getPrimaryKey(pkString.split("/"));
            Object pk = manyPkAccessor.get(pkObject);
            if (!isExistingAssociation(manyPropertyAccessor, pk)) {
                Object newRelation = saveNewRelation(pk, onePropertyAccessor, manyPropertyAccessor);
                existingAssociations.add(newRelation);
            }
        }
        Iterator it = existingAssociations.iterator();
        while (it.hasNext()) {
            Object o = it.next();
            // TODO handle manyKeyProperties.length > 1
            Object pkObject = manyPropertyAccessor.get(o);
            String pkString = OgnlUtils.convertValue(pkObject, String.class);
            if (!selectedPrimaryKeys.contains(pkString)) {
                deleteRelation(o);
                it.remove();
            }
        }
    }
    session.getTransaction().commit();
    return objectCreated();
}
Also used : PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) RequiresPermissions(com.manydesigns.portofino.security.RequiresPermissions)

Example 2 with RequiresPermissions

use of com.manydesigns.portofino.security.RequiresPermissions in project Portofino by ManyDesigns.

the class ActionsAction method getResourceActionTypes.

@Path(":types")
@GET
@RequiresPermissions(level = AccessLevel.NONE)
@Produces(MediaType.APPLICATION_JSON)
public Map<String, ActionTypeInfo> getResourceActionTypes() {
    Map<String, ActionTypeInfo> result = new HashMap<>();
    TextProvider textProvider = ElementsThreadLocals.getTextProvider();
    actionRegistry.iterator().forEachRemaining(a -> {
        String className = a.actionClass.getName();
        result.put(a.description, new ActionTypeInfo(className, a.getActionName(textProvider), textProvider.getTextOrNull(className + ".description"), a.supportsDetail));
    });
    return result;
}
Also used : ActionTypeInfo(com.manydesigns.portofino.upstairs.actions.support.ActionTypeInfo) HashMap(java.util.HashMap) TextProvider(com.manydesigns.elements.i18n.TextProvider) RequiresPermissions(com.manydesigns.portofino.security.RequiresPermissions)

Aggregations

RequiresPermissions (com.manydesigns.portofino.security.RequiresPermissions)2 TextProvider (com.manydesigns.elements.i18n.TextProvider)1 PropertyAccessor (com.manydesigns.elements.reflection.PropertyAccessor)1 ActionTypeInfo (com.manydesigns.portofino.upstairs.actions.support.ActionTypeInfo)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1