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