-
Notifications
You must be signed in to change notification settings - Fork 56
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 Implement loadAvailableProfiles in general.controller.js and getAvailableOotbActiveProfiles in groupingsService to call api in ui and store the available data in .availableProfiles Add ootb.active.user.profiles.json that contains multiple profiles information Apply ng-repeat with the values from scope.availableProfiles that contains the profiles from api call Add isOotb() function to check current spring boot active profile and apply to the menubar.html Add tests for loadAvailableProfiles and getAvailableOotbActiveProfiles in general.controller.test.js and groupings.service.test.js Enhance the Builder class functions in the User class (access folder) Refactor the file inputstream function in OotbActiveUserProfileService Implement getAvailableProfiles function test in the controller test, add @Mockbean OotbActiveUserProfileService and mock the map that contains the ootb active profiles Complete adding multiple ootb active profiles into the users map dynamically from json file with null safe and wrong path of file error Fix cadacy code style Fix general controller test to check gs.getAvailableOotbActiveProfiles
- Loading branch information
Showing
12 changed files
with
224 additions
and
48 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
71 changes: 42 additions & 29 deletions
71
src/main/java/edu/hawaii/its/groupings/service/OotbActiveUserProfileService.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 |
---|---|---|
@@ -1,51 +1,64 @@ | ||
package edu.hawaii.its.groupings.service; | ||
|
||
import java.util.HashMap; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.security.core.authority.AuthorityUtils; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.core.io.ClassPathResource; | ||
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 com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import edu.hawaii.its.groupings.access.User; | ||
|
||
@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<>(); | ||
|
||
public OotbActiveUserProfileService() { | ||
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()); | ||
try { | ||
mapFromJsonFile("ootb.active.user.profiles.json").forEach((key, userData) -> { | ||
users.put(key, createUserFromMap(userData)); | ||
}); | ||
} catch (IOException e) { | ||
logger.error("Error while initiating active user profiles from a json file: ", e); | ||
} | ||
} | ||
|
||
private Map<String, Map<String, Object>> mapFromJsonFile(String filepath) throws IOException { | ||
File file = new ClassPathResource(filepath).getFile(); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
// Map<ootb active profile type, Map<profile property, property's value>> | ||
return objectMapper.readValue(file, new TypeReference<>() {}); | ||
} | ||
|
||
private User createUserFromMap(Map<String, Object> userData) { | ||
String uid = (String) userData.get("uid"); | ||
String uhUuid = (String) userData.get("uhUuid"); | ||
List<String> roles = (List<String>) userData.get("authorities"); | ||
Map<String, String> attributes = (Map<String, String>) userData.get("attributes"); | ||
|
||
User.Builder builder = new User.Builder(uid) | ||
.uhUuid(uhUuid) | ||
.addAuthorities(roles); | ||
|
||
attributes.forEach(builder::addAttribute); | ||
|
||
return builder.build(); | ||
} | ||
|
||
@Override | ||
|
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 @@ | ||
{ | ||
"MEMBER": { | ||
"uid": "member0123", | ||
"uhUuid": "11111111", | ||
"authorities": ["ROLE_UH"], | ||
"attributes": { | ||
"cn": "MEMBER", | ||
"mail": "[email protected]", | ||
"givenName": "DefaultMember" | ||
} | ||
}, | ||
"OWNER": { | ||
"uid": "owner0123", | ||
"uhUuid": "22222222", | ||
"authorities": ["ROLE_UH", "ROLE_OWNER"], | ||
"attributes": { | ||
"cn": "OWNER", | ||
"mail": "[email protected]", | ||
"givenName": "OwnerUser" | ||
} | ||
}, | ||
"ADMIN": { | ||
"uid": "admin0123", | ||
"uhUuid": "33333333", | ||
"authorities": ["ROLE_ADMIN", "ROLE_UH", "ROLE_OWNER"], | ||
"attributes": { | ||
"cn": "ADMIN", | ||
"mail": "[email protected]", | ||
"givenName": "AdminUser" | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -50,7 +50,7 @@ | |
<a class="btn btn-primary btn-v-align login" th:href="@{/login}">Login | ||
<i class="fas fa-sign-in-alt" aria-hidden="false"></i></a> | ||
</li> | ||
<li class="nav-item" sec:authorize="isAuthenticated() and !hasAuthority('ROLE_OOTB')"> | ||
<li class="nav-item" sec:authorize="isAuthenticated()" th:if="${[email protected]()}"> | ||
<form action="/logout" th:action="@{/logout}" method="post"> | ||
<div class="dropdown-divider d-block d-lg-none pb-3"></div> | ||
<button class="btn btn-outline-secondary btn-v-align text-nowrap" type="submit">Logout (<span | ||
|
@@ -59,15 +59,14 @@ | |
</button> | ||
</form> | ||
</li> | ||
<li class="nav-item dropdown" sec:authorize="isAuthenticated() and hasAuthority('ROLE_OOTB')"> | ||
<li class="nav-item dropdown" sec:authorize="isAuthenticated()" th:if="${@realm.isOotb()}" ng-click="loadAvailableProfiles()"> | ||
<a class="btn btn-outline-secondary btn-v-align text-nowrap dropdown-toggle" href="#" id="navbarDropdownMenuLink2" role="button" | ||
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> | ||
Logout (<span sec:authentication="name" id="name"></span>) | ||
</a> | ||
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> | ||
<a class="dropdown-item" href="login" ng-click="updateActiveUserProfile('MEMBER')">Member</a> | ||
<a class="dropdown-item" href="login" ng-click="updateActiveUserProfile('OWNER')">Owner</a> | ||
<a class="dropdown-item" href="login" ng-click="updateActiveUserProfile('ADMIN')">Admin</a> | ||
<a class="dropdown-item" href="login" ng-repeat="profile in availableProfiles" | ||
ng-click="updateActiveUserProfile(profile)">{{profile}}</a> | ||
</div> | ||
</li> | ||
</ul> | ||
|
Oops, something went wrong.