1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38 public final class UserContextUtil {
39
40
41
42
43 private UserContextUtil() {
44 super();
45 }
46
47
48
49
50
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
75
76
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
101
102
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
127
128
129
130
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
144
145
146
147
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 }