Skip to content

Commit

Permalink
Merge pull request #318 from KPMP/KPMP-5445_add-new-role
Browse files Browse the repository at this point in the history
add roles to userInformation
  • Loading branch information
zwright authored Nov 18, 2024
2 parents 4637258 + 72b514e commit 2c2fd64
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/main/java/org/kpmp/shibboleth/ShibbolethUserService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.kpmp.shibboleth;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.kpmp.users.User;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -58,6 +63,12 @@ public User getUserNoHeaders(HttpServletRequest request, JSONObject packageInfo)
}

public User getUser(HttpServletRequest request) {
List<String> roleList = new ArrayList<>();
HttpSession session = request.getSession(false);
if (session != null) {
JSONArray roles = (JSONArray)session.getAttribute("roles");
roleList = JSONArrayToList(roles);
}

String value = handleNull(request.getHeader("mail"));
String email = encoder.convertFromLatin1(value);
Expand All @@ -76,11 +87,25 @@ public User getUser(HttpServletRequest request) {
user.setFirstName(firstName);
user.setEmail(email);
user.setShibId(shibId);
user.setRoles(roleList);

return user;

}

private List<String> JSONArrayToList(JSONArray array) {
List<String> roles = new ArrayList<>();
if (array == null) {
return roles;
}
for (int i = 0; i < array.length(); i++) {
String role = array.optString(i);
roles.add(role);
System.err.println(role);
}
return roles;
}

private String handleNull(String value) {
if (value == null) {
return "";
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/org/kpmp/users/User.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.kpmp.users;

import java.util.List;

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@JsonPropertyOrder({ "id", "firstName", "lastName", "displayName", "email" })
@JsonPropertyOrder({ "id", "firstName", "lastName", "displayName", "email", "roles" })
@Document(collection = "users")
public class User {

Expand All @@ -18,6 +21,8 @@ public class User {
private String displayName;
private String email;
private String shibId;
@Transient
private List<String> roles;

public String getId() {
return id;
Expand Down Expand Up @@ -59,6 +64,15 @@ public void setEmail(String emailAddress) {
this.email = emailAddress;
}


public List<String> getRoles() {
return this.roles;
}

public void setRoles(List<String> roles) {
this.roles = roles;
}

@Override
public String toString() {
return "userId: " + id + ", firstName: " + firstName + ", lastName: " + lastName + ", displayName: "
Expand Down

0 comments on commit 2c2fd64

Please sign in to comment.