|
20 | 20 |
|
21 | 21 | @Service |
22 | 22 | public class UserRoleService { |
23 | | - |
| 23 | + |
24 | 24 | @Autowired |
25 | 25 | UserRoleRepository userRoleRepository; |
26 | | - |
| 26 | + |
27 | 27 | @Secured("ROLE_VIEWER") |
28 | | - public String getUsername(){ |
| 28 | + public String getUsername() { |
29 | 29 | SecurityContext securityContext = SecurityContextHolder.getContext(); |
30 | 30 | return securityContext.getAuthentication().getName(); |
31 | 31 | } |
32 | | - |
33 | | - @Secured({"ROLE_VIEWER","ROLE_EDITOR"}) |
34 | | - public boolean isValidUsername(String username){ |
| 32 | + |
| 33 | + @Secured({ "ROLE_VIEWER", "ROLE_EDITOR" }) |
| 34 | + public boolean isValidUsername(String username) { |
35 | 35 | return userRoleRepository.isValidUsername(username); |
36 | 36 | } |
37 | | - |
| 37 | + |
38 | 38 | @RolesAllowed("ROLE_VIEWER") |
39 | | - public String getUsername2(){ |
| 39 | + public String getUsername2() { |
40 | 40 | SecurityContext securityContext = SecurityContextHolder.getContext(); |
41 | 41 | return securityContext.getAuthentication().getName(); |
42 | 42 | } |
43 | | - |
44 | | - @RolesAllowed({"ROLE_VIEWER","ROLE_EDITOR"}) |
45 | | - public boolean isValidUsername2(String username){ |
| 43 | + |
| 44 | + @RolesAllowed({ "ROLE_VIEWER", "ROLE_EDITOR" }) |
| 45 | + public boolean isValidUsername2(String username) { |
46 | 46 | return userRoleRepository.isValidUsername(username); |
47 | 47 | } |
48 | | - |
| 48 | + |
49 | 49 | @PreAuthorize("hasRole('ROLE_VIEWER')") |
50 | | - public String getUsernameInUpperCase(){ |
| 50 | + public String getUsernameInUpperCase() { |
51 | 51 | return getUsername().toUpperCase(); |
52 | 52 | } |
53 | | - |
| 53 | + |
54 | 54 | @PreAuthorize("hasAuthority('SYS_ADMIN')") |
55 | | - public String getUsernameInLowerCase(){ |
| 55 | + public String getUsernameLC() { |
56 | 56 | return getUsername().toLowerCase(); |
57 | 57 | } |
58 | | - |
| 58 | + |
59 | 59 | @PreAuthorize("hasRole('ROLE_VIEWER') or hasRole('ROLE_EDITOR')") |
60 | | - public boolean isValidUsername3(String username){ |
| 60 | + public boolean isValidUsername3(String username) { |
61 | 61 | return userRoleRepository.isValidUsername(username); |
62 | 62 | } |
63 | | - |
| 63 | + |
64 | 64 | @PreAuthorize("#username == authentication.principal.username") |
65 | | - public String getMyRoles(String username){ |
| 65 | + public String getMyRoles(String username) { |
66 | 66 | SecurityContext securityContext = SecurityContextHolder.getContext(); |
67 | 67 | return securityContext |
68 | 68 | .getAuthentication() |
69 | 69 | .getAuthorities() |
70 | | - .stream().map(auth -> auth.getAuthority()) |
71 | | - .collect(Collectors.joining(",")); |
| 70 | + .stream() |
| 71 | + .map(auth -> auth.getAuthority()).collect(Collectors.joining(",")); |
72 | 72 | } |
73 | | - |
| 73 | + |
74 | 74 | @PostAuthorize("returnObject.username == authentication.principal.nickName") |
75 | | - public CustomUser loadUserDetail(String username){ |
| 75 | + public CustomUser loadUserDetail(String username) { |
76 | 76 | return userRoleRepository.loadUserByUserName(username); |
77 | 77 | } |
78 | | - |
| 78 | + |
79 | 79 | @PreFilter("filterObject != authentication.principal.username") |
80 | | - public String joinUsernames(List<String> usernames){ |
| 80 | + public String joinUsernames(List<String> usernames) { |
81 | 81 | return usernames.stream().collect(Collectors.joining(";")); |
82 | 82 | } |
83 | | - |
84 | | - @PreFilter(value="filterObject != authentication.principal.username",filterTarget="usernames") |
85 | | - public String joinUsernamesAndRoles(List<String> usernames,List<String> roles){ |
86 | | - return usernames.stream().collect(Collectors.joining(";")) |
87 | | - +":"+roles.stream().collect(Collectors.joining(";")); |
| 83 | + |
| 84 | + @PreFilter(value = "filterObject != authentication.principal.username", filterTarget = "usernames") |
| 85 | + public String joinUsernamesAndRoles(List<String> usernames, List<String> roles) { |
| 86 | + return usernames.stream().collect(Collectors.joining(";")) + ":" + roles.stream().collect(Collectors.joining(";")); |
88 | 87 | } |
89 | | - |
| 88 | + |
90 | 89 | @PostFilter("filterObject != authentication.principal.username") |
91 | | - public List<String> getAllUsernamesExceptCurrent(){ |
| 90 | + public List<String> getAllUsernamesExceptCurrent() { |
92 | 91 | return userRoleRepository.getAllUsernames(); |
93 | 92 | } |
94 | | - |
| 93 | + |
95 | 94 | @IsViewer |
96 | | - public String getUsername4(){ |
| 95 | + public String getUsername4() { |
97 | 96 | SecurityContext securityContext = SecurityContextHolder.getContext(); |
98 | 97 | return securityContext.getAuthentication().getName(); |
99 | 98 | } |
100 | | - |
| 99 | + |
101 | 100 | @PreAuthorize("#username == authentication.principal.username") |
102 | 101 | @PostAuthorize("returnObject.username == authentication.principal.nickName") |
103 | | - public CustomUser securedLoadUserDetail(String username){ |
| 102 | + public CustomUser securedLoadUserDetail(String username) { |
104 | 103 | return userRoleRepository.loadUserByUserName(username); |
105 | 104 | } |
106 | | - |
| 105 | + |
107 | 106 | } |
0 commit comments