// Path: src/main/java/org/esupportail/papercut/web/WebUtils.java
// public class WebUtils {
// 
// 	public static String getContext(HttpServletRequest request) {
// 		String path = request.getServletPath();
// 		if("/error".equals(path)) {
// 			path = (String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
// 		}
// 		String papercutContext = path.replaceFirst("/([^/]*).*", "$1");
// 		return papercutContext;
// 	}
// 	
// 	public static List<String> availableContexts() {
// 		 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
// 		 if(auth != null && auth.getPrincipal() instanceof ContextUserDetails) {
// 			 ContextUserDetails userDetails = (ContextUserDetails)auth.getPrincipal();
// 			 return userDetails.getAvailableContexts();
// 		 } else {
// 			 return new ArrayList<String>();
// 		 }
// 	}
// 
// 	public static boolean isUser() {
// 		return hasRole("ROLE_USER");
// 	}
// 	
// 	public static boolean isManager() {
// 		return hasRole("ROLE_MANAGER");
// 	}
// 	
// 	public static boolean isAdmin() {
// 		 return hasRole("ROLE_ADMIN");
// 	}
// 	
// 	private static boolean hasRole(String roleName) {
// 		 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
// 		 return auth!=null && auth.getAuthorities() != null && auth.getAuthorities().contains(new SimpleGrantedAuthority(roleName));
// 	}
// }


import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.esupportail.papercut.web.WebUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
/**
 * Licensed to EsupPortail under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 *
 * EsupPortail licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.esupportail.papercut.security;




public class ContextUserDetails implements UserDetails {

	private static final long serialVersionUID = 1L;
	
	String username;
	
	Map<String, Set<GrantedAuthority>> contextAuthorities = new HashMap<String, Set<GrantedAuthority>>();
	Map<String, String> contextUids = new HashMap<String, String>(); 
	Map<String, String> contextEmails = new HashMap<String, String>(); 
	
	final List<GrantedAuthority> defaultAuthorities = Arrays.asList(new GrantedAuthority[] {new SimpleGrantedAuthority("ROLE_NONE")});
	
	List<String> availableContexts;

	public ContextUserDetails(String username, Map<String, Set<GrantedAuthority>> contextAuthorities, Map<String, String> contextUids, Map<String, String> contextEmails, List<String> availableContexts) {
		this.username = username;
		this.contextAuthorities = contextAuthorities;
		this.contextUids = contextUids;
		this.contextEmails = contextEmails;
		this.availableContexts = availableContexts;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
	    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
	    if (requestAttributes != null && requestAttributes instanceof ServletRequestAttributes) {
	        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
// Path: src/main/java/org/esupportail/papercut/web/WebUtils.java
// public class WebUtils {
// 
// 	public static String getContext(HttpServletRequest request) {
// 		String path = request.getServletPath();
// 		if("/error".equals(path)) {
// 			path = (String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
// 		}
// 		String papercutContext = path.replaceFirst("/([^/]*).*", "$1");
// 		return papercutContext;
// 	}
// 	
// 	public static List<String> availableContexts() {
// 		 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
// 		 if(auth != null && auth.getPrincipal() instanceof ContextUserDetails) {
// 			 ContextUserDetails userDetails = (ContextUserDetails)auth.getPrincipal();
// 			 return userDetails.getAvailableContexts();
// 		 } else {
// 			 return new ArrayList<String>();
// 		 }
// 	}
// 
// 	public static boolean isUser() {
// 		return hasRole("ROLE_USER");
// 	}
// 	
// 	public static boolean isManager() {
// 		return hasRole("ROLE_MANAGER");
// 	}
// 	
// 	public static boolean isAdmin() {
// 		 return hasRole("ROLE_ADMIN");
// 	}
// 	
// 	private static boolean hasRole(String roleName) {
// 		 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
// 		 return auth!=null && auth.getAuthorities() != null && auth.getAuthorities().contains(new SimpleGrantedAuthority(roleName));
// 	}
// }


// Path: src/main/java/org/esupportail/papercut/security/ContextUserDetails.java
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.esupportail.papercut.web.WebUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

/**
 * Licensed to EsupPortail under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 *
 * EsupPortail licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.esupportail.papercut.security;




public class ContextUserDetails implements UserDetails {

	private static final long serialVersionUID = 1L;
	
	String username;
	
	Map<String, Set<GrantedAuthority>> contextAuthorities = new HashMap<String, Set<GrantedAuthority>>();
	Map<String, String> contextUids = new HashMap<String, String>(); 
	Map<String, String> contextEmails = new HashMap<String, String>(); 
	
	final List<GrantedAuthority> defaultAuthorities = Arrays.asList(new GrantedAuthority[] {new SimpleGrantedAuthority("ROLE_NONE")});
	
	List<String> availableContexts;

	public ContextUserDetails(String username, Map<String, Set<GrantedAuthority>> contextAuthorities, Map<String, String> contextUids, Map<String, String> contextEmails, List<String> availableContexts) {
		this.username = username;
		this.contextAuthorities = contextAuthorities;
		this.contextUids = contextUids;
		this.contextEmails = contextEmails;
		this.availableContexts = availableContexts;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
	    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
	    if (requestAttributes != null && requestAttributes instanceof ServletRequestAttributes) {
	        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
	        String papercutContext = WebUtils.getContext(request);