001/* 002 * Copyright 2020 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.linkdirectory.right; 017 018import java.util.Map; 019import java.util.Set; 020 021import org.apache.avalon.framework.context.Context; 022import org.apache.avalon.framework.context.ContextException; 023import org.apache.avalon.framework.context.Contextualizable; 024import org.apache.cocoon.components.ContextHelper; 025import org.apache.commons.lang3.StringUtils; 026 027import org.ametys.core.right.AccessController; 028import org.ametys.core.right.RightsException; 029import org.ametys.plugins.core.impl.right.AbstractProfileStorageBasedAccessController; 030import org.ametys.plugins.linkdirectory.Link; 031import org.ametys.plugins.linkdirectory.repository.DefaultLink; 032import org.ametys.runtime.i18n.I18nizableText; 033import org.ametys.web.WebHelper; 034 035/** 036 * {@link AccessController} for a {@link Link} 037 */ 038public class LinkDirectoryAccessController extends AbstractProfileStorageBasedAccessController implements Contextualizable 039{ 040 /** the link directory context category */ 041 public static final I18nizableText LINK_DIRECTORY_CONTEXT_CATEGORY = new I18nizableText("plugin.link-directory", "PLUGINS_LINKDIRECTORY_LINKDIRECTORY_RIGHT_ASSIGNMENT_CONTEXT_LABEL"); 042 private Context _context; 043 044 public void contextualize(Context context) throws ContextException 045 { 046 _context = context; 047 } 048 049 @Override 050 public boolean isSupported(Object object) 051 { 052 return object instanceof Link; 053 } 054 055 @Override 056 protected boolean _isSupportedStoredContext(Object storedObject) 057 { 058 String siteName = WebHelper.getSiteName(ContextHelper.getRequest(_context)); 059 return storedObject instanceof DefaultLink link 060 && (siteName == null || StringUtils.equals(link.getSiteName(), siteName)); 061 } 062 063 @Override 064 protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts) 065 { 066 return null; 067 } 068 069 @Override 070 protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException 071 { 072 return new I18nizableText("plugin.link-directory", "PLUGINS_LINKDIRECTORY_LINK_ACCESS_CONTROLLER_LINK_CONTEXT_LABEL", Map.of("title", getObjectLabel(object))); 073 } 074 075 public I18nizableText getObjectLabel(Object object) throws RightsException 076 { 077 if (object instanceof DefaultLink link) 078 { 079 String title = link.getTitle(); 080 if (StringUtils.isNotBlank(title)) 081 { 082 return new I18nizableText(link.getLanguage().toUpperCase() + " > " + title + " (" + link.getUrl() + ")"); 083 } 084 else 085 { 086 return new I18nizableText(link.getLanguage().toUpperCase() + " > " + link.getUrl()); 087 } 088 } 089 throw new RightsException("Unsupported object " + object.toString()); 090 } 091 092 @Override 093 public I18nizableText getObjectCategory(Object object) 094 { 095 return LINK_DIRECTORY_CONTEXT_CATEGORY; 096 } 097}