1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.security.spring; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
|
20 | |
import org.kuali.rice.core.config.Config; |
21 | |
import org.kuali.rice.core.config.ConfigContext; |
22 | |
import org.kuali.rice.kim.bo.entity.dto.KimPrincipalInfo; |
23 | |
import org.kuali.rice.kim.bo.role.dto.KimRoleInfo; |
24 | |
import org.kuali.rice.kim.service.IdentityService; |
25 | |
import org.kuali.rice.kim.service.RoleService; |
26 | |
import org.kuali.student.common.rice.StudentIdentityConstants; |
27 | |
import org.kuali.student.common.util.security.UserWithId; |
28 | |
import org.springframework.security.GrantedAuthority; |
29 | |
import org.springframework.security.userdetails.UserDetails; |
30 | |
import org.springframework.security.userdetails.UserDetailsService; |
31 | |
import org.springframework.security.userdetails.UsernameNotFoundException; |
32 | |
import org.springframework.security.util.AuthorityUtils; |
33 | |
import org.springframework.util.StringUtils; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class KSDefaultUserDetailsService implements UserDetailsService{ |
43 | |
|
44 | |
|
45 | 0 | protected boolean enabled = true; |
46 | 0 | protected boolean nonlocked = true; |
47 | |
|
48 | 0 | protected Config config = null; |
49 | |
|
50 | 0 | protected IdentityService identityService = null; |
51 | 0 | protected RoleService roleService = null; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { |
57 | |
String password; |
58 | |
|
59 | 0 | if(username==null || username.equals("")){ |
60 | 0 | throw new UsernameNotFoundException("Username cannot be null or empty"); |
61 | |
} |
62 | |
|
63 | |
|
64 | 0 | password = username; |
65 | |
|
66 | 0 | KimPrincipalInfo kimPrincipalInfo = null; |
67 | 0 | kimPrincipalInfo = identityService.getPrincipalByPrincipalName(username); |
68 | |
|
69 | |
String userId; |
70 | 0 | if (null != kimPrincipalInfo) { |
71 | 0 | username = kimPrincipalInfo.getPrincipalName(); |
72 | 0 | userId = kimPrincipalInfo.getPrincipalId(); |
73 | |
} else { |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | 0 | throw new KimUserNotFoundException("Invalid username or password"); |
79 | |
} |
80 | 0 | UserWithId ksuser = new UserWithId(username, password, enabled, true, true, nonlocked, getGrantedAuthority(userId)); |
81 | 0 | ksuser.setUserId(userId); |
82 | |
|
83 | 0 | return ksuser; |
84 | |
} |
85 | |
|
86 | |
protected GrantedAuthority[] getGrantedAuthority(String principalId){ |
87 | |
|
88 | 0 | String springRoles = ""; |
89 | |
|
90 | |
|
91 | 0 | ArrayList<String> adminRoleIdList = new ArrayList<String>(); |
92 | 0 | KimRoleInfo adminRole = roleService.getRoleByName(StudentIdentityConstants.KS_NAMESPACE_CD, StudentIdentityConstants.KSCM_ADMIN_ROLE_NAME); |
93 | 0 | if(adminRole != null) { |
94 | 0 | adminRoleIdList.add(adminRole.getRoleId()); |
95 | |
} |
96 | |
|
97 | |
|
98 | 0 | ArrayList<String> ksUserRoleIdList = new ArrayList<String>(); |
99 | 0 | KimRoleInfo ksUserRole = roleService.getRoleByName(StudentIdentityConstants.KS_NAMESPACE_CD, StudentIdentityConstants.KSCM_USER_ROLE_NAME); |
100 | 0 | if(ksUserRole != null) { |
101 | 0 | ksUserRoleIdList.add(ksUserRole.getRoleId()); |
102 | |
} |
103 | |
|
104 | 0 | ArrayList<String> ksSpringRolesList = new ArrayList<String>(); |
105 | |
|
106 | 0 | if(roleService.principalHasRole(principalId, adminRoleIdList, null)){ |
107 | 0 | ksSpringRolesList.add("ROLE_KS_ADMIN"); |
108 | |
} |
109 | 0 | if(roleService.principalHasRole(principalId, ksUserRoleIdList, null)){ |
110 | 0 | ksSpringRolesList.add("ROLE_KS_USER"); |
111 | |
} |
112 | |
|
113 | |
|
114 | 0 | if (enableBackdoorLogin()) { |
115 | 0 | ksSpringRolesList.add("ROLE_KS_BACKDOOR"); |
116 | |
} |
117 | |
|
118 | 0 | springRoles = StringUtils.collectionToCommaDelimitedString(ksSpringRolesList); |
119 | 0 | return AuthorityUtils.commaSeparatedStringToAuthorityArray(springRoles); |
120 | |
|
121 | |
} |
122 | |
|
123 | |
public Config getConfig() { |
124 | 0 | if(this.config == null){ |
125 | 0 | this.config = ConfigContext.getCurrentContextConfig(); |
126 | |
} |
127 | 0 | return config; |
128 | |
} |
129 | |
|
130 | |
public void setConfig(Config config) { |
131 | 0 | this.config = config; |
132 | 0 | } |
133 | |
|
134 | |
public void setIdentityService(IdentityService identityService) { |
135 | 0 | this.identityService = identityService; |
136 | 0 | } |
137 | |
|
138 | |
protected boolean enableBackdoorLogin() { |
139 | 0 | return "true".equalsIgnoreCase(getConfig().getProperty("enableKSBackdoorLogin")); |
140 | |
} |
141 | |
|
142 | |
public RoleService getRoleService() { |
143 | 0 | return roleService; |
144 | |
} |
145 | |
|
146 | |
public void setRoleService(RoleService roleService) { |
147 | 0 | this.roleService = roleService; |
148 | 0 | } |
149 | |
|
150 | |
public IdentityService getIdentityService() { |
151 | 0 | return identityService; |
152 | |
} |
153 | |
} |