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.cocoon.components.ContextHelper; 026import org.apache.cocoon.environment.Request; 027import org.apache.commons.lang3.StringUtils; 028 029import org.ametys.cms.rights.ContentTypeRightAssignmentContext; 030 031/** 032 * Like {@link ContentTypeRightAssignmentContext} for web 033 */ 034public class WebContentTypeRightAssignmentContext extends ContentTypeRightAssignmentContext 035{ 036 @Override 037 public Object convertJSContext(Object context) 038 { 039 if (context instanceof String) 040 { 041 List<String> contexts = new ArrayList<>(Arrays.asList(StringUtils.split((String) context, '/'))); 042 contexts.add(1, _getSiteName()); 043 return "/" + StringUtils.join(contexts, "/"); 044 } 045 return null; 046 } 047 048 @Override 049 public List<Object> getRootContexts(Map<String, Object> contextParameters) 050 { 051 return super.getRootContexts(contextParameters).stream().map(context -> context + "/" + _getSiteName()).collect(Collectors.toList()); 052 } 053 054 private String _getSiteName() 055 { 056 Request request = ContextHelper.getRequest(_context); 057 String siteName = request.getParameter("siteName"); 058 059 if (siteName == null) 060 { 061 siteName = (String) request.getAttribute("siteName"); 062 } 063 return siteName; 064 } 065 066 @Override 067 public Set<Object> getParentContexts(Object context) 068 { 069 if (context instanceof String) 070 { 071 List<String> contexts = new ArrayList<>(Arrays.asList(StringUtils.split((String) context, '/'))); 072 contexts.remove(1); 073 074 Set<Object> parents = super.getParentContexts("/" + StringUtils.join(contexts, "/")); 075 if (parents != null) 076 { 077 parents = parents.stream().map(c -> convertJSContext(c)).collect(Collectors.toSet()); 078 } 079 return parents; 080 } 081 else 082 { 083 return null; 084 } 085 } 086}