001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nelson Silva <nelson.silva@inevo.pt> 016 */ 017package org.nuxeo.ecm.automation.io.services.enricher; 018 019import java.io.IOException; 020import java.security.Principal; 021import java.util.ArrayList; 022import java.util.Arrays; 023import java.util.List; 024import java.util.Map; 025 026import org.codehaus.jackson.JsonGenerator; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.io.marshallers.json.enrichers.BasePermissionsJsonEnricher; 030 031import com.google.common.base.Predicate; 032import com.google.common.collect.Iterables; 033 034/** 035 * This enricher adds a list of the permissions granted to the user on the document 036 * 037 * @since 6.0 038 * @deprecated This enricher was migrated to {@link BasePermissionsJsonEnricher}. The content enricher service doesn't 039 * work anymore. 040 */ 041@Deprecated 042public class UserPermissionsContentEnricher extends AbstractContentEnricher { 043 044 private static final String PERMISSIONS_PARAMETER = "permissions"; 045 046 public static final String PERMISSIONS_CONTENT_ID = "permissions"; 047 048 private List<String> availablePermissions = new ArrayList<>(); 049 050 @Override 051 public void enrich(JsonGenerator jg, RestEvaluationContext ec) throws IOException { 052 final DocumentModel doc = ec.getDocumentModel(); 053 jg.writeStartArray(); 054 for (String permission : getPermissions(doc)) { 055 jg.writeString(permission); 056 } 057 jg.writeEndArray(); 058 jg.flush(); 059 } 060 061 @Override 062 public void setParameters(Map<String, String> parameters) { 063 String permissionsList = parameters.get(PERMISSIONS_PARAMETER); 064 if (permissionsList != null) { 065 availablePermissions.addAll(Arrays.asList(permissionsList.split(","))); 066 } 067 } 068 069 private Iterable<String> getPermissions(final DocumentModel doc) { 070 final CoreSession session = doc.getCoreSession(); 071 final Principal principal = session.getPrincipal(); 072 073 return Iterables.filter(availablePermissions, new Predicate<String>() { 074 @Override 075 public boolean apply(String permission) { 076 return session.hasPermission(principal, doc.getRef(), permission); 077 } 078 }); 079 } 080}