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 public boolean supports(Object object) 050 { 051 return object instanceof Link; 052 } 053 054 @Override 055 protected boolean _isSupportedStoredContext(Object storedObject) 056 { 057 String siteName = WebHelper.getSiteName(ContextHelper.getRequest(_context)); 058 return storedObject instanceof DefaultLink link 059 && (siteName == null || StringUtils.equals(link.getSiteName(), siteName)); 060 } 061 062 @Override 063 protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts) 064 { 065 return null; 066 } 067 068 @Override 069 protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException 070 { 071 return new I18nizableText("plugin.link-directory", "PLUGINS_LINKDIRECTORY_LINK_ACCESS_CONTROLLER_LINK_CONTEXT_LABEL", Map.of("title", getObjectLabel(object))); 072 } 073 074 public I18nizableText getObjectLabel(Object object) throws RightsException 075 { 076 if (object instanceof DefaultLink link) 077 { 078 String title = link.getTitle(); 079 if (StringUtils.isNotBlank(title)) 080 { 081 return new I18nizableText(link.getLanguage().toUpperCase() + " > " + title + " (" + link.getUrl() + ")"); 082 } 083 else 084 { 085 return new I18nizableText(link.getLanguage().toUpperCase() + " > " + link.getUrl()); 086 } 087 } 088 throw new RightsException("Unsupported object " + object.toString()); 089 } 090 091 @Override 092 public I18nizableText getObjectCategory(Object object) 093 { 094 return LINK_DIRECTORY_CONTEXT_CATEGORY; 095 } 096}