Skip to content

Commit

Permalink
Added org name to transaction record. (#165)
Browse files Browse the repository at this point in the history
* 8195 Add SPG to Audit Report

* Added missing sort mapping for spgRole.
Minor style tweaks to add spacing for multi checkboxes.

* addressed code review comments

* addressed code review comments - 2

* addressed review comments (3)

* Added org name to transaction record.
Updated audit screen for filter and results.
Updated view.

Co-authored-by: prit-cgi <prit.thakkar@cgi.com>
Co-authored-by: weskubo-cgi <Wesley.Kubo@gov.bc.ca>
  • Loading branch information
3 people authored Jan 24, 2023
1 parent 81198c4 commit 9b48ada
Show file tree
Hide file tree
Showing 16 changed files with 261 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nimbusds.oauth2.sdk.util.StringUtils;

import ca.bc.gov.hlth.hnweb.model.rest.StatusEnum;
import ca.bc.gov.hlth.hnweb.model.rest.auditreport.AuditOrganization;
import ca.bc.gov.hlth.hnweb.model.rest.auditreport.AuditRecord;
import ca.bc.gov.hlth.hnweb.model.rest.auditreport.AuditReportRequest;
import ca.bc.gov.hlth.hnweb.model.rest.auditreport.AuditReportResponse;
Expand Down Expand Up @@ -89,10 +88,13 @@ public ResponseEntity<AuditReportResponse> getAuditReport(@Valid @RequestBody Au
* @return list of organization
*/
@GetMapping("/organizations")
public ResponseEntity<List<String>> getOrganizations() {
List<String> organizations = convertOrganization(auditService.getOrganizations());
ResponseEntity<List<String>> responseEntity = ResponseEntity.ok(organizations);
return responseEntity;
public ResponseEntity<List<AuditOrganization>> getOrganizations() {
List<Organization> organizations = auditService.getOrganizations();
List<AuditOrganization> auditOrganizations = organizations.stream()
.map(o -> new AuditOrganization(o.getOrganization(), o.getOrganizationName()))
.collect(Collectors.toList());

return ResponseEntity.ok(auditOrganizations);
}

/**
Expand Down Expand Up @@ -130,6 +132,7 @@ private List<AuditRecord> convertReport(List<AffectedParty> affectedParties) {
affectedParties.forEach(affectedParty -> {
AuditRecord model = new AuditRecord();
model.setOrganization(affectedParty.getTransaction().getOrganization());
model.setOrganizationName(affectedParty.getTransaction().getOrganizationName());
model.setSpgRole(affectedParty.getTransaction().getSpgRole());
model.setTransactionId(affectedParty.getTransaction().getTransactionId().toString());
model.setType(affectedParty.getTransaction().getType());
Expand All @@ -144,11 +147,6 @@ private List<AuditRecord> convertReport(List<AffectedParty> affectedParties) {
return auditReportResponse;
}

private List<String> convertOrganization(List<Organization> organizations) {
return organizations.stream().filter(org -> StringUtils.isNotBlank(org.getOrganization()))
.map(org -> org.getOrganization()).collect(Collectors.toList());
}

private LocalDateTime convertDate(Date date) {
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ca.bc.gov.hlth.hnweb.model.rest.auditreport;

public class AuditOrganization {

private String id;

private String name;

public AuditOrganization(String id, String name) {
super();
this.id = id;
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class AuditRecord {

private String organization;

private String organizationName;

private String userId;

private LocalDateTime transactionStartTime;
Expand Down Expand Up @@ -44,6 +46,14 @@ public void setOrganization(String organization) {
this.organization = organization;
}

public String getOrganizationName() {
return organizationName;
}

public void setOrganizationName(String organizationName) {
this.organizationName = organizationName;
}

public String getUserId() {
return userId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package ca.bc.gov.hlth.hnweb.persistence.entity;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Organization {

@Id
@Column(name = "organization")
private String organization;

@Basic
@Column(name = "organization_name")
private String organizationName;

public String getOrganization() {
return organization;
}
Expand All @@ -17,11 +24,20 @@ public void setOrganization(String organizationId) {
this.organization = organizationId;
}

public String getOrganizationName() {
return organizationName;
}

public void setOrganizationName(String organizationName) {
this.organizationName = organizationName;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((organization == null) ? 0 : organization.hashCode());
result = prime * result + ((organizationName == null) ? 0 : organizationName.hashCode());
return result;
}

Expand All @@ -39,7 +55,11 @@ public boolean equals(Object obj) {
return false;
} else if (!organization.equals(other.organization))
return false;

if (organizationName == null) {
if (other.organizationName != null)
return false;
} else if (!organizationName.equals(other.organizationName))
return false;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,20 @@ public class Transaction {
@Column(name = "organization")
private String organization;

/**
* Organization Name of the user that initiated the transaction
*/
@Basic
@Column(name = "organization_name")
private String organizationName;

/**
* SPG of the user performing the transaction
*/
@Basic
@Column(name = "spg_role")
private String spgRole;

public String getSpgRole() {
return spgRole;
}

public void setSpgRole(String spgRole) {
this.spgRole = spgRole;
}

/**
* ID of the user that initiated the transaction
*/
Expand Down Expand Up @@ -139,6 +138,22 @@ public void setOrganization(String organization) {
this.organization = organization;
}

public String getOrganizationName() {
return organizationName;
}

public void setOrganizationName(String organizationName) {
this.organizationName = organizationName;
}

public String getSpgRole() {
return spgRole;
}

public void setSpgRole(String spgRole) {
this.spgRole = spgRole;
}

public String getUserId() {
return userId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ca.bc.gov.hlth.hnweb.persistence.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -11,5 +13,10 @@ public interface OrganizationRepository extends JpaRepository<Organization, Stri
@Modifying
@Query(value = "REFRESH MATERIALIZED VIEW mspdirect.organization", nativeQuery = true)
void refreshMaterializedView();

@Query(value = "select * from mspdirect.organization o where o.organization_name is not null "
+ " or (o.organization_name is null and not exists (select organization from mspdirect.organization o2 where o2.organization_name is not null and o.organization = o2.organization))"
+ "order by o.organization", nativeQuery = true)
List<Organization> findUnique();

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class SecurityUtil {

private static final String ORGANIZATION_ID = "id";

private static final String ORGANIZATION_NAME = "name";

private static final String USER_ROLES = "roles";

private static final String UNKNOWN_ROLE = "UNKNOWN";
Expand All @@ -54,7 +56,7 @@ public static UserInfo loadUserInfo() {
Jwt jwt = (Jwt) auth.getPrincipal();

UserInfo userInfo = new UserInfo();
userInfo.setOrganization(extractOrganization(jwt));
extractOrganization(jwt, userInfo);

List<String> roles = loadRoles(jwt);
userInfo.setRoles(roles);
Expand Down Expand Up @@ -111,15 +113,16 @@ public static String loadSPGBasedOnTransactionType(UserInfo userInfo, Transactio
}
return UNKNOWN_ROLE;
}

private static String extractOrganization(Jwt jwt) {


private static void extractOrganization(Jwt jwt, UserInfo userInfo) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree((String) jwt.getClaim(CLAIM_ORGANIZATION));
return node.get(ORGANIZATION_ID).asText();
JsonNode node = mapper.readTree((String)jwt.getClaim(CLAIM_ORGANIZATION));
userInfo.setOrganization(node.get(ORGANIZATION_ID).asText());
userInfo.setOrganizationName(node.get(ORGANIZATION_NAME).asText());
} catch (Exception e) {
logger.warn("User {} does not have claim {} set", jwt.getClaim(CLAIM_USERNAME), CLAIM_ORGANIZATION);
return null;
}
}

Expand Down
19 changes: 15 additions & 4 deletions backend/src/main/java/ca/bc/gov/hlth/hnweb/security/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class UserInfo {
private String username;
private String userId;
private String organization;
private String organizationName;
private String role;
private String sessionState;
private List<String> roles;
Expand All @@ -19,18 +20,20 @@ public UserInfo() {
super();
}

public UserInfo(String username, String organization, String role) {
public UserInfo(String username, String organization, String organizationName, String role) {
super();
this.username = username;
this.organization = organization;
this.organizationName = organizationName;
this.role = role;
}

public UserInfo(String username, String userId, String organization, String role, String sessionState) {
public UserInfo(String username, String userId, String organization, String organizationName, String role, String sessionState) {
super();
this.username = username;
this.userId = userId;
this.organization = organization;
this.organizationName = organizationName;
this.role = role;
this.sessionState = sessionState;
}
Expand Down Expand Up @@ -59,6 +62,14 @@ public void setOrganization(String organization) {
this.organization = organization;
}

public String getOrganizationName() {
return organizationName;
}

public void setOrganizationName(String organizationName) {
this.organizationName = organizationName;
}

public String getRole() {
return StringUtils.join(this.roles, " ");
}
Expand All @@ -81,8 +92,8 @@ public void setSessionState(String sessionState) {

@Override
public String toString() {
return "UserInfo [username=" + username + ", userId=" + userId + ", organization=" + organization + ", role="
+ role + ", sessionState=" + sessionState + "]";
return "UserInfo [username=" + username + ", userId=" + userId + ", organization=" + organization + ", organizationName="
+ organizationName + ", role=" + role + ", sessionState=" + sessionState + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class AuditService {

public static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

private static final String[] HEADERS = { "Type", "Organization", "SPG", "User ID", "Transaction Start Time",
private static final String[] HEADERS = { "Type", "Organization", "Organization Name", "SPG", "User ID", "Transaction Start Time",
"Affected Party ID", "Affected Party ID Type", "Transaction ID" };

private static final CSVFormat FORMAT = CSVFormat.DEFAULT.withHeader(HEADERS);
Expand Down Expand Up @@ -95,6 +95,7 @@ public class AuditService {
sortMap.put("affectedPartyId", "identifier");
sortMap.put("affectedPartyType", "identifierType");
sortMap.put("organization", "transaction.organization");
sortMap.put("organizationName", "transaction.organizationName");
sortMap.put("spgRole", "transaction.spgRole");
sortMap.put("transactionStartTime", "transaction.startTime");
sortMap.put("type", "transaction.type");
Expand All @@ -118,15 +119,16 @@ public Transaction createTransaction(String sourceIP, TransactionType type) {
} catch (Exception e) {
// Ignore
}
transaction.setOrganization(userInfo != null ? userInfo.getOrganization() : null);
transaction.setOrganization(userInfo != null ? userInfo.getOrganization(): null);
transaction.setOrganizationName(userInfo != null ? userInfo.getOrganizationName(): null);
transaction.setServer(getServer());
transaction.setSessionId(userInfo != null ? userInfo.getSessionState() : null);
transaction.setSourceIp(sourceIP);
transaction.setSpgRole(SecurityUtil.loadSPGBasedOnTransactionType(userInfo, type));
transaction.setStartTime(new Date());
transaction.setTransactionId(UUID.randomUUID());
transaction.setType(type.getValue());
transaction.setUserId(userInfo != null ? userInfo.getUsername() : null);
transaction.setSpgRole(SecurityUtil.loadSPGBasedOnTransactionType(userInfo, type));
return transactionRepository.save(transaction);
}

Expand Down Expand Up @@ -239,7 +241,7 @@ public AffectedParty createAffectedParty(Transaction transaction, IdentifierType
* @return list of organization.
*/
public List<Organization> getOrganizations() {
return organizationRepository.findAll(Sort.by("organization"));
return organizationRepository.findUnique();
}

/**
Expand Down Expand Up @@ -302,8 +304,8 @@ private ByteArrayInputStream writeDataToCsv(final List<AuditRecord> auditReports
try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
CSVPrinter printer = new CSVPrinter(new PrintWriter(stream), FORMAT)) {
for (AuditRecord auditRecord : auditReports) {
List<String> auditData = Arrays.asList(String.valueOf(auditRecord.getType()),
auditRecord.getOrganization(), auditRecord.getSpgRole(), auditRecord.getUserId(),
List<String> auditData = Arrays.asList(String.valueOf(auditRecord.getType()),
auditRecord.getOrganization(), auditRecord.getOrganizationName(), auditRecord.getSpgRole(), auditRecord.getUserId(),
convertLocalDateTime(auditRecord.getTransactionStartTime()), auditRecord.getAffectedPartyId(),
auditRecord.getAffectedPartyType(), auditRecord.getTransactionId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ DROP MATERIALIZED VIEW mspdirect.organization;
CREATE MATERIALIZED VIEW mspdirect.organization
AS
select DISTINCT
t.organization as organization
t.organization as organization,
t.organization_name as organization_name
from
mspdirect.transaction t
where organization is not null
Expand Down
Loading

0 comments on commit 9b48ada

Please sign in to comment.