Skip to content

Commit

Permalink
Refactoring the logic related to the button to update active user pro…
Browse files Browse the repository at this point in the history
…files in ootb (#946)
  • Loading branch information
gitCarrot authored Jun 29, 2024
1 parent 46c394a commit a219428
Show file tree
Hide file tree
Showing 21 changed files with 525 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

Expand All @@ -27,7 +25,6 @@
import edu.hawaii.its.groupings.access.UserContextService;
import edu.hawaii.its.groupings.configuration.OotbStaticUserAuthenticationFilter;
import edu.hawaii.its.groupings.service.OotbActiveUserProfileService;
import edu.hawaii.its.groupings.util.JsonUtil;

@RestController
@RequestMapping("/api/groupings/ootb")
Expand Down Expand Up @@ -60,6 +57,11 @@ public OotbRestController() {
* the overrides file. Gets the active profiles and only runs the tests the
* active profile relies on the API.
*/
@GetMapping("/availableProfiles")
public ResponseEntity<List<String>> getAvailableProfiles() {
List<String> profiles = ootbActiveUserProfileService.getAvailableProfiles();
return ResponseEntity.ok(profiles);
}

@PostMapping(value = "/{activeProfile}")
public ResponseEntity<String> updateActiveDefaultUser(@PathVariable String activeProfile) {
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/edu/hawaii/its/groupings/access/User.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package edu.hawaii.its.groupings.access;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand All @@ -13,6 +16,10 @@ public class User extends org.springframework.security.core.userdetails.User {
private String uhUuid;
private UhAttributes attributes;

public User(){
super("", "", null);
}

public User(String uid, String uhUuid, Collection<GrantedAuthority> authorities) {
super(uid, "", authorities);
setUhUuid(uhUuid);
Expand All @@ -22,7 +29,7 @@ public User(String uid, Collection<GrantedAuthority> authorities) {
super(uid, "", authorities);
}

private User(Builder builder) {
public User(Builder builder) {
super(builder.uid, "", builder.authorities);
this.uhUuid = builder.uhUuid;
this.attributes = new UhAttributes(builder.attributes);
Expand Down Expand Up @@ -74,7 +81,7 @@ public String toString() {
public static class Builder {
private final String uid;
private String uhUuid;
private Collection<GrantedAuthority> authorities;
private final Collection<GrantedAuthority> authorities = new ArrayList<>();
private final Map<String, Object> attributes = new HashMap<>();

public Builder(String uid) {
Expand All @@ -86,8 +93,16 @@ public Builder uhUuid(String uhUuid) {
return this;
}

public Builder authorities(Collection<GrantedAuthority> authorities) {
this.authorities = authorities;
public Builder addAuthorities(String authority) {
this.authorities.add(new SimpleGrantedAuthority(authority));
return this;
}

public Builder addAuthorities(List<String> authorities) {
List<GrantedAuthority> grantedAuthorities = authorities.stream()
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
this.authorities.addAll(grantedAuthorities);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
public class OotbSecurityConfig {

private static final Log logger = LogFactory.getLog(OotbSecurityConfig.class);

@Value("${ootb.active.user.profile}")
private String userProfile;

@Value("${ootb.profiles.filename}")
private String profilesFileName;

@Value("${url.base}")
private String appUrlBase;

Expand Down Expand Up @@ -108,6 +109,15 @@ public OotbStaticUserAuthenticationFilter ootbStaticUserAuthenticationFilter() {

@Bean
public UserDetailsService userDetailsService() {
return new OotbActiveUserProfileService();
// Make Profile Service with JSON file that user sets in the properties file
OotbActiveUserProfileService ootbActiveUserProfileService = new OotbActiveUserProfileService(profilesFileName);

// Set the default profile to admin
setUserProfile(ootbActiveUserProfileService.findGivenNameForAdminRole());
return ootbActiveUserProfileService;
}

public void setUserProfile(String userProfile) {
this.userProfile = userProfile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ public boolean isDefault() {
return isProfileActive("default");
}

public boolean isOotb() { return isProfileActive("ootb"); }

}
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
Expand All @@ -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;
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/edu/hawaii/its/groupings/type/OotbActiveProfile.java
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;
}
}
28 changes: 28 additions & 0 deletions src/main/java/edu/hawaii/its/groupings/util/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package edu.hawaii.its.groupings.util;

import java.nio.file.Files;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.fasterxml.jackson.databind.ObjectMapper;

Expand Down Expand Up @@ -35,6 +40,28 @@ public static <T> T asObject(final String json, Class<T> type) {
return result;
}

public static <T> List<T> asList(final String json, Class<T> type) {
List<T> result = null;
try {
ObjectMapper mapper = new ObjectMapper();
result = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, type));
} catch (Exception e) {
logger.error("Error: " + e);
}
return result;
}

public static String readJsonFileToString(String filePath) {
String result = null;
try {
Resource resource = new ClassPathResource(filePath);
result = Files.readString(resource.getFile().toPath());
} catch (Exception e) {
logger.error("Error: " + e);
}
return result;
}

public static void printJson(Object obj) {
ObjectMapper objectMapper = new ObjectMapper();
try {
Expand All @@ -44,6 +71,7 @@ public static void printJson(Object obj) {
logger.error("Error: " + e);
}
}

public static void prettyPrint(Object object) {
try {
String json = new ObjectMapper()
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application-ootb.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ app.api.handshake.enabled=false
# There are two options: GROUPER, OOTB : "GROUPER" is a real grouper api service while "OOTB" is a ootb grouper api service.
grouping.api.server.type=OOTB

# User Type : USER, OWNER, ADMIN
ootb.active.user.profile=ADMIN
# Profiles file
ootb.profiles.filename=ootb.active.user.profiles.json

# =========================================================================
app.environment=ootb
32 changes: 32 additions & 0 deletions src/main/resources/ootb.active.user.profiles.json
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"
}
}
]
Loading

0 comments on commit a219428

Please sign in to comment.