001/* 002 * Copyright 2022 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.forms.rights; 017 018import java.util.Collections; 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.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.components.ContextHelper; 027import org.apache.cocoon.environment.Request; 028import org.apache.commons.lang3.StringUtils; 029 030import org.ametys.core.right.AccessController; 031import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController; 032import org.ametys.plugins.forms.dao.FormDirectoryDAO; 033import org.ametys.plugins.forms.repository.Form; 034import org.ametys.plugins.forms.repository.FormDirectory; 035import org.ametys.plugins.repository.AmetysObject; 036import org.ametys.web.WebHelper; 037 038/** 039 * {@link AccessController} for a {@link Form} 040 */ 041public class FormAccessController extends AbstractHierarchicalAccessController<AmetysObject> implements Contextualizable 042{ 043 /** The form directory DAO */ 044 protected FormDirectoryDAO _formDirectoryDAO; 045 046 /** The context */ 047 protected Context _context; 048 049 @Override 050 public void service(ServiceManager manager) throws ServiceException 051 { 052 super.service(manager); 053 _formDirectoryDAO = (FormDirectoryDAO) manager.lookup(FormDirectoryDAO.ROLE); 054 } 055 056 public void contextualize(Context context) throws ContextException 057 { 058 _context = context; 059 } 060 061 @Override 062 public boolean isSupported(Object object) 063 { 064 return object instanceof Form || object instanceof FormDirectory; 065 } 066 067 @Override 068 protected Set<AmetysObject> _getParents(AmetysObject object) 069 { 070 AmetysObject parent = object.getParent(); 071 if (isSupported(parent)) 072 { 073 return Collections.singleton(parent); 074 } 075 else 076 { 077 return null; 078 } 079 } 080 081 @Override 082 protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts) 083 { 084 Request request = ContextHelper.getRequest(_context); 085 String siteName = WebHelper.getSiteName(request); 086 if (StringUtils.isNotBlank(siteName) && workspacesContexts.contains("/cms")) 087 { 088 return Collections.singleton(_formDirectoryDAO.getFormDirectoriesRootNode(siteName)); 089 } 090 return null; 091 } 092}