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.plugins.contentio.synchronize.rights; 017 018import java.util.ArrayList; 019import java.util.HashMap; 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.repository.Content; 028import org.ametys.core.right.AbstractStaticRightAssignmentContext; 029import org.ametys.core.right.RightAssignmentContext; 030import org.ametys.core.ui.ClientSideElementHelper; 031import org.ametys.core.util.I18nUtils; 032import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection; 033import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO; 034import org.ametys.plugins.repository.AmetysObject; 035import org.ametys.plugins.repository.AmetysObjectResolver; 036import org.ametys.runtime.i18n.I18nizableText; 037import org.ametys.runtime.i18n.I18nizableTextParameter; 038 039/** 040 * {@link RightAssignmentContext} for assign rights to a {@link Content} or a jcr node root holding the contents 041 */ 042public class SynchronizeContentRightAssignmentContext extends AbstractStaticRightAssignmentContext 043{ 044 /** The prefix for rights on the root of a collection */ 045 public static final String ROOT_CONTEXT_PREFIX = "/synchronized-contents/"; 046 047 public static final String ID = "right.assignment.context.synchronized.content"; 048 049 /** The Ametys object resolver */ 050 protected AmetysObjectResolver _resolver; 051 /** The synchronize collection DAO */ 052 protected SynchronizableContentsCollectionDAO _collectionsDAO; 053 /** The i18n utils */ 054 protected I18nUtils _i18nUtils; 055 056 @Override 057 public void service(ServiceManager smanager) throws ServiceException 058 { 059 super.service(smanager); 060 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 061 _collectionsDAO = (SynchronizableContentsCollectionDAO) smanager.lookup(SynchronizableContentsCollectionDAO.ROLE); 062 _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE); 063 } 064 065 @Override 066 public Object convertJSContext(Object context) 067 { 068 if (context instanceof String) 069 { 070 String stringContext = (String) context; 071 if (stringContext.startsWith(ROOT_CONTEXT_PREFIX)) 072 { 073 return context; 074 } 075 else 076 { 077 return _resolver.resolveById((String) context); 078 } 079 } 080 081 return null; 082 } 083 084 @Override 085 public String getContextIdentifier(Object context) 086 { 087 if (context instanceof Content) 088 { 089 return ((AmetysObject) context).getId(); 090 } 091 else 092 { 093 return (String) context; 094 } 095 } 096 097 @Override 098 public Set<Object> getParentContexts(Object context) 099 { 100 if (context instanceof Content) 101 { 102 String[] collectionIds = ((Content) context).getInternalDataHolder().getValue(SynchronizableContentsCollection.COLLECTION_ID_DATA_NAME); 103 return Set.of(ROOT_CONTEXT_PREFIX + collectionIds[0]); 104 } 105 106 return null; 107 } 108 109 @Override 110 public List<Object> getRootContexts(Map<String, Object> contextParameters) 111 { 112 List<Object> rootContexts = new ArrayList<>(); 113 if (matchWorkspace(contextParameters)) 114 { 115 for (SynchronizableContentsCollection synchronizableContentsCollection : _collectionsDAO.getSynchronizableContentsCollections()) 116 { 117 if (synchronizableContentsCollection.handleRightAssignmentContext()) 118 { 119 rootContexts.add(ROOT_CONTEXT_PREFIX + synchronizableContentsCollection.getId()); 120 } 121 } 122 } 123 return rootContexts; 124 } 125 126 @Override 127 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 128 { 129 List<Script> scripts = new ArrayList<>(); 130 131 List<Script> superScripts = super.getScripts(ignoreRights, contextParameters); 132 if (superScripts != null && superScripts.size() == 1) 133 { 134 for (SynchronizableContentsCollection synchronizableContentsCollection : _collectionsDAO.getSynchronizableContentsCollections()) 135 { 136 if (synchronizableContentsCollection.handleRightAssignmentContext()) 137 { 138 // First duplicate the script 139 Script script = ClientSideElementHelper.cloneScript(superScripts.get(0)); 140 141 script.getParameters().put("root-context", ROOT_CONTEXT_PREFIX + synchronizableContentsCollection.getId()); 142 script.getParameters().put("scc", synchronizableContentsCollection.getId()); 143 144 _parametrizeValue(script, "label", synchronizableContentsCollection.getLabel()); 145 _parametrizeValue(script, "radio-option-all-label", synchronizableContentsCollection.getLabel()); 146 _parametrizeValue(script, "hint-all-contents", synchronizableContentsCollection.getLabel()); 147 _parametrizeValue(script, "result-grid-mask-message", synchronizableContentsCollection.getLabel()); 148 149 scripts.add(script); 150 } 151 } 152 } 153 154 return scripts; 155 } 156 157 private void _parametrizeValue(Script script, String key, I18nizableText i18nKey) 158 { 159 Object label = script.getParameters().get(key); 160 if (label == null) 161 { 162 script.getParameters().put(key, i18nKey); 163 } 164 else if (label instanceof String) 165 { 166 script.getParameters().put(key, label + " " + _i18nUtils.translate(i18nKey)); 167 } 168 else if (label instanceof I18nizableText) 169 { 170 I18nizableText i18nLabel = (I18nizableText) label; 171 if (i18nLabel.isI18n()) 172 { 173 if (i18nLabel.getParameters() != null) 174 { 175 List<String> parameters = new ArrayList<>(i18nLabel.getParameters()); 176 parameters.add(_i18nUtils.translate(i18nKey)); 177 script.getParameters().put(key, new I18nizableText(i18nLabel.getCatalogue(), i18nLabel.getKey(), parameters)); 178 } 179 else 180 { 181 Map<String, I18nizableTextParameter> parametersMap = i18nLabel.getParameterMap() != null ? new HashMap<>(i18nLabel.getParameterMap()) : new HashMap<>(); 182 parametersMap.put("collection", i18nKey); 183 script.getParameters().put(key, new I18nizableText(i18nLabel.getCatalogue(), i18nLabel.getKey(), parametersMap)); 184 } 185 } 186 else 187 { 188 script.getParameters().put(key, i18nLabel.getLabel() + " " + _i18nUtils.translate(i18nKey)); 189 } 190 } 191 } 192}