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.ArrayList; 019import java.util.Collections; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.commons.lang.StringUtils; 027 028import org.ametys.core.right.AbstractStaticRightAssignmentContext; 029import org.ametys.core.right.RightAssignmentContext; 030import org.ametys.plugins.repository.AmetysObject; 031import org.ametys.plugins.repository.AmetysObjectResolver; 032import org.ametys.web.repository.page.Page; 033import org.ametys.web.repository.site.Site; 034import org.ametys.web.repository.site.SiteManager; 035import org.ametys.web.repository.sitemap.Sitemap; 036 037/** 038 * {@link RightAssignmentContext} for assign rights to a {@link Page} or a {@link Sitemap} 039 */ 040public class PageRightAssignmentContext extends AbstractStaticRightAssignmentContext 041{ 042 /** The id if this right context */ 043 public static final String ID = "right.assignment.context.pageaccess"; 044 045 /** The Ametys object resolver */ 046 protected AmetysObjectResolver _resolver; 047 /** The site manager */ 048 protected SiteManager _siteManager; 049 050 @Override 051 public void service(ServiceManager smanager) throws ServiceException 052 { 053 super.service(smanager); 054 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 055 _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE); 056 } 057 058 @Override 059 public Object convertJSContext(Object context) 060 { 061 if (context instanceof String) 062 { 063 return _resolver.resolveById((String) context); 064 } 065 return null; 066 } 067 068 @Override 069 public String getContextIdentifier(Object context) 070 { 071 if (context instanceof Page || context instanceof Sitemap) 072 { 073 return ((AmetysObject) context).getId(); 074 } 075 return null; 076 } 077 078 @Override 079 public Set<Object> getParentContexts(Object context) 080 { 081 if (context instanceof Page) 082 { 083 return Collections.singleton(((Page) context).getParent()); 084 } 085 return null; 086 } 087 088 @Override 089 public List<Object> getRootContexts(Map<String, Object> contextParameters) 090 { 091 List<Object> rootContexts = new ArrayList<>(); 092 093 if (matchWorkspace(contextParameters)) 094 { 095 String siteName = (String) contextParameters.get("siteName"); 096 if (StringUtils.isNotEmpty(siteName)) 097 { 098 Site site = _siteManager.getSite(siteName); 099 for (Sitemap sitemap : site.getSitemaps()) 100 { 101 rootContexts.add(sitemap); 102 } 103 } 104 } 105 106 return rootContexts; 107 } 108}