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.plugins.site.population; 017 018import java.util.List; 019 020import org.apache.avalon.framework.service.ServiceException; 021 022import org.ametys.core.user.population.PopulationContextHelper; 023import org.ametys.core.user.population.UserPopulation; 024import org.ametys.plugins.site.Site; 025import org.ametys.plugins.site.SiteInformationCache; 026 027/** 028 * Helper for associating {@link UserPopulation}s to contexts for FO application. 029 * Overrides {@link PopulationContextHelper#getUserPopulationsOnContext(String, boolean)} in order to authenticate through Front-Office URLs 030 */ 031public class SitePopulationContextHelper extends PopulationContextHelper 032{ 033 private static final String __SITES_PREFIX_CONTEXT = "/sites/"; 034 private static final String __SITES_FO_PREFIX_CONTEXT = "/sites-fo/"; 035 036 private SiteInformationCache _siteInformationCache; 037 038 private void _delayedService() 039 { 040 if (_siteInformationCache == null) 041 { 042 try 043 { 044 _siteInformationCache = (SiteInformationCache) _manager.lookup(SiteInformationCache.ROLE); 045 } 046 catch (ServiceException e) 047 { 048 // do nothing, we are in safe mode 049 } 050 } 051 } 052 053 @Override 054 public List<String> getUserPopulationsOnContext(String context, boolean withDisabled) 055 { 056 if (context.startsWith(__SITES_PREFIX_CONTEXT)) 057 { 058 String siteName = context.substring(__SITES_PREFIX_CONTEXT.length()); 059 Site site = _findSite(siteName); 060 if (site != null) 061 { 062 return site.getPopulationIds(); 063 } 064 } 065 else if (context.startsWith(__SITES_FO_PREFIX_CONTEXT)) 066 { 067 String siteName = context.substring(__SITES_FO_PREFIX_CONTEXT.length()); 068 Site site = _findSite(siteName); 069 if (site != null) 070 { 071 return site.getPopulationIds(); 072 } 073 } 074 075 return super.getUserPopulationsOnContext(context, withDisabled); 076 } 077 078 private Site _findSite(String siteName) 079 { 080 _delayedService(); 081 082 if (_siteInformationCache != null) 083 { 084 for (Site site : _siteInformationCache.getSites().values()) 085 { 086 if (site.getName().equals(siteName)) 087 { 088 return site; 089 } 090 } 091 } 092 093 return null; 094 } 095}