View Javadoc
1   /*
2    * Copyright 2014 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.web.impl.ui.application.util;
20  
21  import java.util.Collection;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.springframework.security.core.Authentication;
26  import org.springframework.security.core.GrantedAuthority;
27  import org.springframework.security.core.context.SecurityContext;
28  import org.springframework.security.core.context.SecurityContextHolder;
29  import org.springframework.web.context.request.RequestContextHolder;
30  import org.springframework.web.context.request.ServletRequestAttributes;
31  
32  import com.hack23.cia.model.internal.application.user.impl.UserAccount;
33  import com.vaadin.server.Page;
34  
35  /**
36   * The Class UserContextUtil.
37   */
38  public final class UserContextUtil {
39  
40  	/**
41  	 * Instantiates a new user context util.
42  	 */
43  	private UserContextUtil() {
44  		super();
45  	}
46  
47  	/**
48  	 * Gets the user name from security context.
49  	 *
50  	 * @return the user name from security context
51  	 */
52  	public static String getUserNameFromSecurityContext() {
53  
54  		String result=null;
55  
56  		final SecurityContext context = SecurityContextHolder.getContext();
57  		if (context != null) {
58  			final Authentication authentication = context.getAuthentication();
59  			if (authentication != null) {
60  				final Object principal = authentication.getPrincipal();
61  
62  				if (principal instanceof UserAccount) {
63  					final UserAccount userAccount = (UserAccount) principal;
64  					result = userAccount.getUsername();
65  				}
66  			}
67  		}
68  
69  		return result;
70  	}
71  
72  
73  	/**
74  	 * Gets the user id from security context.
75  	 *
76  	 * @return the user id from security context
77  	 */
78  	public static String getUserIdFromSecurityContext() {
79  
80  		String result=null;
81  
82  		final SecurityContext context = SecurityContextHolder.getContext();
83  		if (context != null) {
84  			final Authentication authentication = context.getAuthentication();
85  			if (authentication != null) {
86  				final Object principal = authentication.getPrincipal();
87  
88  				if (principal instanceof UserAccount) {
89  					final UserAccount userAccount = (UserAccount) principal;
90  					result = userAccount.getUserId();
91  				}
92  			}
93  		}
94  
95  		return result;
96  	}
97  
98  
99  	/**
100 	 * Gets the user internal id from security context.
101 	 *
102 	 * @return the user internal id from security context
103 	 */
104 	public static Long getUserInternalIdFromSecurityContext() {
105 
106 		Long result=null;
107 
108 		final SecurityContext context = SecurityContextHolder.getContext();
109 		if (context != null) {
110 			final Authentication authentication = context.getAuthentication();
111 			if (authentication != null) {
112 				final Object principal = authentication.getPrincipal();
113 
114 				if (principal instanceof UserAccount) {
115 					final UserAccount userAccount = (UserAccount) principal;
116 					result = userAccount.getHjid();
117 				}
118 			}
119 		}
120 
121 		return result;
122 	}
123 
124 
125 	/**
126 	 * Gets the request url.
127 	 *
128 	 * @param current
129 	 *            the current
130 	 * @return the request url
131 	 */
132 	public static String getRequestUrl(final Page current) {
133 		if (current != null) {
134 			return current.getLocation().toString();
135 
136 		} else {
137 			final HttpServletRequest httpRequest=((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
138 			return httpRequest.getRequestURL().toString();
139 		}
140 	}
141 
142 	/**
143 	 * Allow role in security context.
144 	 *
145 	 * @param role
146 	 *            the role
147 	 * @return true, if successful
148 	 */
149 	public static boolean allowRoleInSecurityContext(final String role) {
150 
151 		boolean result = false;
152 
153 		final SecurityContext context = SecurityContextHolder.getContext();
154 		if (context != null && context.getAuthentication() != null) {
155 			final Collection<? extends GrantedAuthority> authorities = context.getAuthentication().getAuthorities();
156 
157 			for (final GrantedAuthority grantedAuthority : authorities) {
158 				if (role.equalsIgnoreCase(grantedAuthority.getAuthority())) {
159 					result = true;
160 				}
161 			}
162 		}
163 		return result;
164 	}
165 
166 
167 }