-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring the logic related to the button to update active user pro…
…files in ootb (#946)
- Loading branch information
Showing
21 changed files
with
525 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,65 @@ | ||
package edu.hawaii.its.groupings.service; | ||
|
||
import java.util.HashMap; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.springframework.security.core.authority.AuthorityUtils; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
import edu.hawaii.its.groupings.access.Role; | ||
import edu.hawaii.its.groupings.access.User; | ||
import edu.hawaii.its.groupings.type.OotbActiveProfile; | ||
import edu.hawaii.its.groupings.util.JsonUtil; | ||
|
||
@Service | ||
public class OotbActiveUserProfileService implements UserDetailsService { | ||
private final Map<String, User> users = new HashMap<>(); | ||
private static final Log logger = LogFactory.getLog(OotbActiveUserProfileService.class); | ||
private final Map<String, User> users = new LinkedHashMap<>(); | ||
|
||
// Default JSON file for active profiles | ||
private String profilesFileName = "ootb.active.user.profiles.json"; | ||
|
||
public OotbActiveUserProfileService() { | ||
initUsers(); | ||
} | ||
|
||
public OotbActiveUserProfileService(String profilesFileName) { | ||
this.profilesFileName = profilesFileName; | ||
initUsers(); | ||
} | ||
|
||
private void initUsers() { | ||
// Initialize member with uid "member0123" | ||
users.put("MEMBER", new User.Builder("member0123") // UID is clearly the first parameter in the constructor | ||
.uhUuid("11111111") | ||
.authorities(AuthorityUtils.createAuthorityList("ROLE_UH", "ROLE_OOTB")) | ||
.addAttribute("cn", "MEMBER") | ||
.addAttribute("mail", "[email protected]") | ||
.addAttribute("givenName", "DefaultMember") | ||
.build()); | ||
|
||
// Initialize owner with uid "owner0123" | ||
users.put("OWNER", new User.Builder("owner0123") | ||
.uhUuid("22222222") | ||
.authorities(AuthorityUtils.createAuthorityList("ROLE_UH", "ROLE_OWNER", "ROLE_OOTB")) | ||
.addAttribute("cn", "OWNER") | ||
.addAttribute("mail", "[email protected]") | ||
.addAttribute("givenName", "OwnerUser") | ||
.build()); | ||
|
||
// Initialize admin with uid "admin0123" | ||
users.put("ADMIN", new User.Builder("admin0123") | ||
.uhUuid("33333333") | ||
.authorities(AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_UH", "ROLE_OWNER", "ROLE_OOTB")) | ||
.addAttribute("cn", "ADMIN") | ||
.addAttribute("mail", "[email protected]") | ||
.addAttribute("givenName", "AdminUser") | ||
.build()); | ||
List<OotbActiveProfile> ootbActiveProfiles = | ||
JsonUtil.asList(JsonUtil.readJsonFileToString(profilesFileName), OotbActiveProfile.class); | ||
ootbActiveProfiles.forEach(profile -> { | ||
users.put(profile.getAttributes().get("givenName"), createUserFromProfile(profile)); | ||
}); | ||
} | ||
|
||
private User createUserFromProfile(OotbActiveProfile profile) { | ||
User.Builder builder = new User.Builder(profile.getUid()) | ||
.uhUuid(profile.getUhUuid()) | ||
.addAuthorities(profile.getAuthorities()); | ||
|
||
profile.getAttributes().forEach(builder::addAttribute); | ||
|
||
return builder.build(); | ||
} | ||
|
||
public String findGivenNameForAdminRole() { | ||
Optional<String> givenName = users.values().stream() | ||
.filter(user -> user.hasRole(Role.ADMIN)) | ||
.map(User::getGivenName) | ||
.findFirst(); | ||
|
||
return givenName.orElse(null); | ||
} | ||
|
||
@Override | ||
|
@@ -56,6 +70,10 @@ public UserDetails loadUserByUsername(String userProfile) throws UsernameNotFoun | |
return users.get(userProfile); | ||
} | ||
|
||
public List<String> getAvailableProfiles() { | ||
return new ArrayList<>(getUsers().keySet()); | ||
} | ||
|
||
public Map<String, User> getUsers() { | ||
return users; | ||
} | ||
|
43 changes: 43 additions & 0 deletions
43
src/main/java/edu/hawaii/its/groupings/type/OotbActiveProfile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package edu.hawaii.its.groupings.type; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OotbActiveProfile { | ||
private String uid; | ||
private String uhUuid; | ||
private List<String> authorities; | ||
private Map<String, String> attributes; | ||
|
||
public String getUid() { | ||
return uid; | ||
} | ||
|
||
public void setUid(String uid) { | ||
this.uid = uid; | ||
} | ||
|
||
public String getUhUuid() { | ||
return uhUuid; | ||
} | ||
|
||
public void setUhUuid(String uhUuid) { | ||
this.uhUuid = uhUuid; | ||
} | ||
|
||
public List<String> getAuthorities() { | ||
return authorities; | ||
} | ||
|
||
public void setAuthorities(List<String> authorities) { | ||
this.authorities = authorities; | ||
} | ||
|
||
public Map<String, String> getAttributes() { | ||
return attributes; | ||
} | ||
|
||
public void setAttributes(Map<String, String> attributes) { | ||
this.attributes = attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[ | ||
{ | ||
"uid": "member0123", | ||
"uhUuid": "11111111", | ||
"authorities": ["ROLE_UH"], | ||
"attributes": { | ||
"cn": "MEMBER", | ||
"mail": "[email protected]", | ||
"givenName": "DefaultMember" | ||
} | ||
}, | ||
{ | ||
"uid": "owner0123", | ||
"uhUuid": "22222222", | ||
"authorities": ["ROLE_UH", "ROLE_OWNER"], | ||
"attributes": { | ||
"cn": "OWNER", | ||
"mail": "[email protected]", | ||
"givenName": "OwnerUser" | ||
} | ||
}, | ||
{ | ||
"uid": "admin0123", | ||
"uhUuid": "33333333", | ||
"authorities": ["ROLE_ADMIN", "ROLE_UH", "ROLE_OWNER"], | ||
"attributes": { | ||
"cn": "ADMIN", | ||
"mail": "[email protected]", | ||
"givenName": "AdminUser" | ||
} | ||
} | ||
] |
Oops, something went wrong.