001/* 002 * Copyright 2017 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.web.rights; 017 018import java.util.ArrayList; 019import java.util.Arrays; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023import java.util.stream.Collectors; 024 025import org.apache.avalon.framework.context.Context; 026import org.apache.avalon.framework.context.ContextException; 027import org.apache.avalon.framework.context.Contextualizable; 028import org.apache.cocoon.components.ContextHelper; 029import org.apache.cocoon.environment.Request; 030import org.apache.commons.lang3.StringUtils; 031 032import org.ametys.core.right.RightsException; 033import org.ametys.plugins.core.impl.right.StringHierarchicalAccessController; 034import org.ametys.plugins.core.impl.right.WorkspaceAccessController; 035import org.ametys.runtime.i18n.I18nizableText; 036import org.ametys.web.WebHelper; 037 038/** 039 * This impl of {@link WorkspaceAccessController} will add the current sitemap to the cms workspace 040 */ 041public class WebWorkspaceAccessController extends StringHierarchicalAccessController implements Contextualizable 042{ 043 private Context _context; 044 045 public void contextualize(Context context) throws ContextException 046 { 047 _context = context; 048 } 049 050 @Override 051 protected Set<String> getSupportedPrefixes() 052 { 053 String sitename = _getSiteName(); 054 if (StringUtils.isBlank(sitename)) 055 { 056 return null; 057 } 058 059 return _prefixes; 060 } 061 062 @Override 063 protected Object _convertContext(Object initialContext) 064 { 065 String supportedContext = (String) initialContext; 066 List<String> contexts = new ArrayList<>(Arrays.asList(StringUtils.split(supportedContext, '/'))); 067 contexts.add(1, _getSiteName()); 068 return "/" + StringUtils.join(contexts, "/"); 069 } 070 071 @Override 072 protected Object _unconvertContext(Object storedObject) 073 { 074 List<String> contexts = new ArrayList<>(Arrays.asList(StringUtils.split((String) storedObject, '/'))); 075 contexts.remove(1); 076 return "/" + StringUtils.join(contexts, "/"); 077 } 078 079 @Override 080 protected Set<String> getRootPrefixes() 081 { 082 String sitename = "/" + _getSiteName(); 083 return _prefixes.stream().map(p -> p + sitename).collect(Collectors.toSet()); 084 } 085 086 /** 087 * Get the current site name 088 * @return the current site name 089 */ 090 protected String _getSiteName() 091 { 092 Request request = ContextHelper.getRequest(_context); 093 return WebHelper.getSiteName(request); 094 } 095 096 @Override 097 protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException 098 { 099 return new I18nizableText("plugin.web", "PLUGINS_WEB_WORKSPACE_ACCESS_CONTROLLER_CONTEXT_LABEL", Map.of("object", getObjectLabel(object))); 100 } 101 102 @Override 103 public I18nizableText getObjectLabel(Object object) 104 { 105 return WorkspaceAccessController.GENERAL_CONTEXT_CATEGORY; 106 } 107 108 @Override 109 public I18nizableText getObjectCategory(Object object) 110 { 111 return WorkspaceAccessController.GENERAL_CONTEXT_CATEGORY; 112 } 113}