001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 *
019 */
020
021package org.nuxeo.ecm.platform.routing.core.audit.es;
022
023import java.util.ArrayList;
024import java.util.List;
025
026import org.json.JSONArray;
027import org.json.JSONException;
028import org.json.JSONObject;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.NuxeoPrincipal;
031import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
032import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
033import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
034import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
035import org.nuxeo.elasticsearch.ElasticSearchConstants;
036import org.nuxeo.elasticsearch.api.ElasticSearchAdmin;
037import org.nuxeo.elasticsearch.http.readonly.filter.AuditRequestFilter;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Define a elasticsearch passthrough filter for audit_wf index view. Restrict to 'Routing' event category and, if the
042 * user is not an administrator, to the list of workflow model on which the user has the 'Data Visualization'
043 * permission.
044 *
045 * @since 7.4
046 */
047public class RoutingAuditRequestFilter extends AuditRequestFilter {
048
049    private CoreSession session;
050
051    @Override
052    public void init(CoreSession session, String indices, String types, String rawQuery, String payload) {
053        this.session = session;
054        principal = (NuxeoPrincipal) session.getPrincipal();
055        ElasticSearchAdmin esa = Framework.getService(ElasticSearchAdmin.class);
056        this.indices = esa.getIndexNameForType(ElasticSearchConstants.ENTRY_TYPE);
057        this.types = ElasticSearchConstants.ENTRY_TYPE;
058        this.rawQuery = rawQuery;
059        this.payload = payload;
060        if (payload == null && !principal.isAdministrator()) {
061            // here we turn the UriSearch query_string into a body search
062            extractPayloadFromQuery();
063        }
064    }
065
066    @Override
067    public String getPayload() throws JSONException {
068        if (filteredPayload == null) {
069            if (payload.contains("\\")) {
070                // JSONObject removes backslash so we need to hide them
071                payload = payload.replaceAll("\\\\", BACKSLASH_MARKER);
072            }
073            JSONObject payloadJson = new JSONObject(payload);
074            JSONObject query;
075            if (payloadJson.has("query")) {
076                query = payloadJson.getJSONObject("query");
077
078                payloadJson.remove("query");
079            } else {
080                query = new JSONObject("{\"match_all\":{}}");
081            }
082            JSONObject categoryFilter = new JSONObject().put("term", new JSONObject().put(
083                    DocumentEventContext.CATEGORY_PROPERTY_KEY, DocumentRoutingConstants.ROUTING_CATEGORY));
084
085            JSONArray fs = new JSONArray().put(categoryFilter);
086
087            if (!principal.isAdministrator()) {
088                DocumentRoutingService documentRoutingService = Framework.getService(DocumentRoutingService.class);
089                List<DocumentRoute> wfModels = documentRoutingService.getAvailableDocumentRouteModel(session);
090                List<String> modelNames = new ArrayList<String>();
091                for (DocumentRoute model : wfModels) {
092                    if (session.hasPermission(model.getDocument().getRef(), DocumentRoutingConstants.CAN_DATA_VISU)) {
093                        modelNames.add(model.getModelName());
094                    }
095                }
096
097                JSONObject wfModelFilter = new JSONObject().put("terms",
098                        new JSONObject().put("extended.modelName", modelNames.toArray(new String[modelNames.size()])));
099
100                fs.put(wfModelFilter);
101            }
102
103            JSONObject filter = new JSONObject().put("bool", new JSONObject().put("must", fs));
104
105            JSONObject newQuery = new JSONObject().put("filtered",
106                    new JSONObject().put("query", query).put("filter", filter));
107            payloadJson.put("query", newQuery);
108            filteredPayload = payloadJson.toString();
109            if (filteredPayload.contains(BACKSLASH_MARKER)) {
110                filteredPayload = filteredPayload.replaceAll(BACKSLASH_MARKER, "\\\\");
111            }
112
113        }
114        return filteredPayload;
115    }
116
117}