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.Set; 022import java.util.stream.Collectors; 023 024import org.apache.avalon.framework.context.Context; 025import org.apache.avalon.framework.context.ContextException; 026import org.apache.avalon.framework.context.Contextualizable; 027import org.apache.cocoon.components.ContextHelper; 028import org.apache.cocoon.environment.Request; 029import org.apache.commons.lang3.StringUtils; 030 031import org.ametys.plugins.core.impl.right.StringHierarchicalAccessController; 032import org.ametys.plugins.core.impl.right.WorkspaceAccessController; 033 034/** 035 * This impl of {@link WorkspaceAccessController} will add the current sitemap to the cms workspace 036 */ 037public class WebWorkspaceAccessController extends StringHierarchicalAccessController implements Contextualizable 038{ 039 private Context _context; 040 041 public void contextualize(Context context) throws ContextException 042 { 043 _context = context; 044 } 045 046 @Override 047 protected Set<String> getSupportedPrefixes() 048 { 049 String sitename = _getSiteName(); 050 if (StringUtils.isBlank(sitename)) 051 { 052 return null; 053 } 054 055 return _prefixes; 056 } 057 058 @Override 059 protected Object _convertContext(Object initialContext) 060 { 061 String supportedContext = (String) initialContext; 062 List<String> contexts = new ArrayList<>(Arrays.asList(StringUtils.split(supportedContext, '/'))); 063 contexts.add(1, _getSiteName()); 064 return "/" + StringUtils.join(contexts, "/"); 065 } 066 067 @Override 068 protected Set<String> getRootPrefixes() 069 { 070 String sitename = "/" + _getSiteName(); 071 return _prefixes.stream().map(p -> p + sitename).collect(Collectors.toSet()); 072 } 073 074 private String _getSiteName() 075 { 076 Request request = ContextHelper.getRequest(_context); 077 String siteName = request.getParameter("siteName"); 078 079 if (siteName == null) 080 { 081 siteName = (String) request.getAttribute("siteName"); 082 } 083 084 if (siteName == null) 085 { 086 siteName = (String) request.getAttribute("site"); 087 } 088 089 return siteName; 090 } 091}