001/* 002 * (C) Copyright 2014 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 * Nelson Silva <nelson.silva@inevo.pt> 018 */ 019package org.nuxeo.ecm.automation.io.services.enricher; 020 021import java.io.IOException; 022import java.security.Principal; 023import java.util.ArrayList; 024import java.util.Arrays; 025import java.util.List; 026import java.util.Map; 027 028import org.codehaus.jackson.JsonGenerator; 029import org.nuxeo.ecm.core.api.CoreSession; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.io.marshallers.json.enrichers.BasePermissionsJsonEnricher; 032 033import com.google.common.base.Predicate; 034import com.google.common.collect.Iterables; 035 036/** 037 * This enricher adds a list of the permissions granted to the user on the document 038 * 039 * @since 6.0 040 * @deprecated This enricher was migrated to {@link BasePermissionsJsonEnricher}. The content enricher service doesn't 041 * work anymore. 042 */ 043@Deprecated 044public class UserPermissionsContentEnricher extends AbstractContentEnricher { 045 046 private static final String PERMISSIONS_PARAMETER = "permissions"; 047 048 public static final String PERMISSIONS_CONTENT_ID = "permissions"; 049 050 private List<String> availablePermissions = new ArrayList<>(); 051 052 @Override 053 public void enrich(JsonGenerator jg, RestEvaluationContext ec) throws IOException { 054 final DocumentModel doc = ec.getDocumentModel(); 055 jg.writeStartArray(); 056 for (String permission : getPermissions(doc)) { 057 jg.writeString(permission); 058 } 059 jg.writeEndArray(); 060 jg.flush(); 061 } 062 063 @Override 064 public void setParameters(Map<String, String> parameters) { 065 String permissionsList = parameters.get(PERMISSIONS_PARAMETER); 066 if (permissionsList != null) { 067 availablePermissions.addAll(Arrays.asList(permissionsList.split(","))); 068 } 069 } 070 071 private Iterable<String> getPermissions(final DocumentModel doc) { 072 final CoreSession session = doc.getCoreSession(); 073 final Principal principal = session.getPrincipal(); 074 075 return Iterables.filter(availablePermissions, new Predicate<String>() { 076 @Override 077 public boolean apply(String permission) { 078 return session.hasPermission(principal, doc.getRef(), permission); 079 } 080 }); 081 } 082}