001/* 002 * Copyright 2016 Anyware Services 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 */ 016package org.ametys.cms.rights; 017 018import java.util.ArrayList; 019import java.util.Collections; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026 027import org.ametys.cms.content.RootContentHelper; 028import org.ametys.cms.repository.Content; 029import org.ametys.core.right.AbstractStaticRightAssignmentContext; 030import org.ametys.core.right.RightAssignmentContext; 031import org.ametys.core.ui.ClientSideElementHelper; 032import org.ametys.plugins.repository.AmetysObject; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034import org.ametys.plugins.repository.collection.AmetysObjectCollection; 035 036/** 037 * {@link RightAssignmentContext} for assign rights to a {@link Content} or a jcr node root holding the contents 038 */ 039public class ContentRightAssignmentContext extends AbstractStaticRightAssignmentContext 040{ 041 /** The id if this right context */ 042 public static final String ID = "right.assignment.context.content"; 043 044 /** The Ametys object resolver */ 045 protected AmetysObjectResolver _resolver; 046 /** The root content helper */ 047 protected RootContentHelper _rootContentHelper; 048 049 @Override 050 public void service(ServiceManager smanager) throws ServiceException 051 { 052 super.service(smanager); 053 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 054 _rootContentHelper = (RootContentHelper) smanager.lookup(RootContentHelper.ROLE); 055 } 056 057 @Override 058 public Object convertJSContext(Object context) 059 { 060 if (context instanceof String) 061 { 062 return _resolver.resolveById((String) context); 063 } 064 return null; 065 } 066 067 @Override 068 public String getContextIdentifier(Object context) 069 { 070 if (context instanceof Content || context instanceof AmetysObjectCollection) 071 { 072 return ((AmetysObject) context).getId(); 073 } 074 return null; 075 } 076 077 @Override 078 public Set<Object> getParentContexts(Object context) 079 { 080 if (context instanceof Content) 081 { 082 return Collections.singleton(((Content) context).getParent()); 083 } 084 return null; 085 } 086 087 @Override 088 public List<Object> getRootContexts(Map<String, Object> contextParameters) 089 { 090 List<Object> rootContexts = new ArrayList<>(); 091 if (matchWorkspace(contextParameters)) 092 { 093 rootContexts.add(getRootContent()); 094 } 095 return rootContexts; 096 } 097 098 @Override 099 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 100 { 101 List<Script> scripts = new ArrayList<>(); 102 103 List<Script> superScripts = super.getScripts(ignoreRights, contextParameters); 104 if (superScripts != null && superScripts.size() == 1) 105 { 106 // First duplicate the script 107 Script script = ClientSideElementHelper.cloneScript(superScripts.get(0)); 108 script.getParameters().put("root-context", ((AmetysObject) getRootContent()).getId()); 109 scripts.add(script); 110 } 111 112 return scripts; 113 } 114 115 /** 116 * Gets the root of contents 117 * @return the root of contents 118 */ 119 protected Object getRootContent() 120 { 121 return _rootContentHelper.getRootContent(); 122 } 123}