View Javadoc
1   package fr.ifremer.quadrige3.synchro.server.security;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Synchro server
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import org.springframework.security.core.Authentication;
27  import org.springframework.security.core.context.SecurityContextHolder;
28  
29  public class SecurityContextHelper {
30  
31  	public static final String USER_ANONYMOUS = "anonymousUser";
32  
33  	public static final int USER_ID_ANONYMOUS = -1;
34  
35  	public static String getPrincipalUsername() {
36  		QuadrigeUserDetails userDetails = getPrincipal();
37  		if (userDetails == null) {
38  			return null;
39  		}
40  		String username = userDetails.getUsername();
41  		if (USER_ANONYMOUS.equals(username)) {
42  			return null;
43  		}
44  		return username;
45  	}
46  
47  	public static QuadrigeUserDetails getPrincipal() {
48  		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
49  		if (authentication == null || authentication.getPrincipal() == null) {
50  			return null;
51  		}
52  		Object principal = authentication.getPrincipal();
53  		if (principal instanceof QuadrigeUserDetails) {
54  			return (QuadrigeUserDetails) principal;
55  		} else {
56  			return null;
57  		}
58  	}
59  
60  	public static int getPrincipalUserId() {
61  		QuadrigeUserDetails userDetails = getPrincipal();
62  		if (userDetails != null) {
63  			if (USER_ANONYMOUS.equals(userDetails.getUsername())) {
64  				return USER_ID_ANONYMOUS;
65  			}
66  			return userDetails.getUserId();
67  		}
68  		return USER_ID_ANONYMOUS;
69  	}
70  
71  	/**
72  	 * @return true if user is authenticated not anonymously.
73  	 */
74  	public static boolean isAuthenticateNotAnonymous() {
75  		return getPrincipalUsername() != null;
76  	}
77  
78  	/**
79  	 * @return true if user is authenticated.
80  	 */
81  	public static boolean isAuthenticate() {
82  		return getPrincipal() != null;
83  	}
84  
85  
86  	/**
87  	 * @return true if user is authenticated anonymously.
88  	 */
89  	public static boolean isAuthenticateAnonymous() {
90  		return getPrincipalUserId() == USER_ID_ANONYMOUS;
91  	}
92  
93  	public static boolean isAuthenticateAsAdmin() {
94  		return isAuthenticate() && getPrincipal().isAdmin();
95  	}
96  }