001/* 002 * Copyright 2016 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.Collections; 019import java.util.HashSet; 020import java.util.Map; 021import java.util.Set; 022 023import org.apache.avalon.framework.context.Context; 024import org.apache.avalon.framework.context.ContextException; 025import org.apache.avalon.framework.context.Contextualizable; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.cocoon.components.ContextHelper; 029import org.apache.cocoon.environment.Request; 030 031import org.ametys.core.right.AccessController; 032import org.ametys.core.right.RightsException; 033import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController; 034import org.ametys.plugins.repository.AmetysObject; 035import org.ametys.runtime.i18n.I18nizableText; 036import org.ametys.runtime.i18n.I18nizableTextParameter; 037import org.ametys.web.repository.page.Page; 038import org.ametys.web.repository.page.SitemapElement; 039import org.ametys.web.repository.site.Site; 040import org.ametys.web.repository.site.SiteManager; 041import org.ametys.web.repository.sitemap.Sitemap; 042 043/** 044 * {@link AccessController} for a {@link Page} 045 */ 046public class PageAccessController extends AbstractHierarchicalAccessController<AmetysObject> implements Contextualizable 047{ 048 /** the page context category */ 049 public static final I18nizableText PAGE_CONTEXT_CATEGORY = new I18nizableText("plugin.web", "PLUGINS_WEB_RIGHT_ASSIGNMENT_CONTEXT_PAGES_LABEL"); 050 /** The web site manager */ 051 protected SiteManager _siteManager; 052 private Context _context; 053 054 @Override 055 public void service(ServiceManager manager) throws ServiceException 056 { 057 super.service(manager); 058 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 059 } 060 061 public void contextualize(Context context) throws ContextException 062 { 063 _context = context; 064 } 065 066 public boolean supports(Object object) 067 { 068 return object instanceof SitemapElement; 069 } 070 071 @Override 072 protected Set<AmetysObject> _getParents(AmetysObject object) 073 { 074 AmetysObject parent = object.getParent(); 075 if (supports(parent)) 076 { 077 return Collections.singleton(parent); 078 } 079 else 080 { 081 return null; 082 } 083 } 084 085 @Override 086 protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts) 087 { 088 if (workspacesContexts.contains("/web")) 089 { 090 Request request = ContextHelper.getRequest(_context); 091 092 String siteName = request.getParameter("siteName"); 093 if (siteName == null) 094 { 095 siteName = (String) request.getAttribute("siteName"); 096 } 097 if (siteName == null) 098 { 099 siteName = (String) request.getAttribute("site"); 100 } 101 102 if (siteName != null) 103 { 104 Site site = _siteManager.getSite(siteName); 105 if (site != null) 106 { 107 Set<Sitemap> sitemaps = new HashSet<>(); 108 for (Sitemap sitemap : site.getSitemaps()) 109 { 110 sitemaps.add(sitemap); 111 } 112 return sitemaps; 113 } 114 } 115 116 getLogger().warn("Could not determine current site to obtain all permissions"); 117 } 118 119 return null; 120 } 121 122 @Override 123 protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException 124 { 125 if (object instanceof Page) 126 { 127 Map<String, I18nizableTextParameter> params = Map.of("title", getObjectLabel(object)); 128 return new I18nizableText("plugin.web", "PLUGINS_WEB_PAGE_ACCESS_CONTROLLER_PAGE_CONTEXT_EXPLANATION_LABEL", params); 129 } 130 else if (object instanceof Sitemap sitemap) 131 { 132 return new I18nizableText("plugin.web", "PLUGINS_WEB_PAGE_ACCESS_CONTROLLER_SITEMAP_CONTEXT_EXPLANATION_LABEL", Map.of("name", new I18nizableText(sitemap.getSitemapName().toUpperCase()))); 133 } 134 throw new RightsException("Unsupported object " + object.toString()); 135 } 136 137 @Override 138 public I18nizableText getObjectLabel(Object object) throws RightsException 139 { 140 if (object instanceof Sitemap sitemap) 141 { 142 return new I18nizableText("plugin.web", "PLUGINS_WEB_PAGE_ACCESS_CONTROLLER_SITEMAP_CONTEXT_LABEL", Map.of("name", new I18nizableText(sitemap.getSitemapName().toUpperCase()))); 143 } 144 else if (object instanceof Page page) 145 { 146 return new I18nizableText(getPageObjectLabel(page)); 147 } 148 throw new RightsException("Unsupported object " + object.toString()); 149 } 150 151 public I18nizableText getObjectCategory(Object object) 152 { 153 return PAGE_CONTEXT_CATEGORY; 154 } 155 156 public int getObjectPriority(Object object) 157 { 158 return object instanceof Sitemap ? 10 : super.getObjectPriority(object); 159 } 160 161 /** 162 * Get an object label for a page. 163 * 164 * This label will include the path to the page in the sitemap. 165 * @param page the page 166 * @return the label 167 */ 168 public static String getPageObjectLabel(Page page) 169 { 170 SitemapElement parent = page.getParent(); 171 if (parent instanceof Sitemap sitemap) 172 { 173 return sitemap.getSitemapName().toUpperCase() + " > " + page.getTitle(); 174 } 175 else 176 { 177 return getPageObjectLabel((Page) parent) + " > " + page.getTitle(); 178 } 179 } 180}