Skip to content

Commit

Permalink
Merge branch 'backend/improvements' of https://github.com/COS301-SE-2…
Browse files Browse the repository at this point in the history
…023/InfoSafe into backend/improvements
  • Loading branch information
ChrisMitt committed Oct 16, 2023
2 parents 6176b15 + 9449291 commit 9a94b67
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ public ResponseEntity<Boolean> deleteUser(@RequestBody DeleteRequest deleteReque
@PostMapping("/changePassword")
public ResponseEntity<Boolean> changePassword(@RequestBody ChangePasswordRequest changePasswordRequest) {
try {
Optional<User> user = userService.getUserByEmail(encryptionService.encryptString(changePasswordRequest.getUserEmail()));
if (user.isPresent()) {
userService.changePassword(user.get(), changePasswordRequest.getNewPassword());
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof User authenticatedUser) {
userService.changePassword(authenticatedUser, changePasswordRequest.getNewPassword());
return ResponseEntity.ok(true);
} else {
}else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(false);
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fragile.infosafe.primary.model;


import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -26,4 +27,15 @@ public class AccessRequest {
@ManyToOne
@JoinColumn(name = "data_scope_id")
private DataScope data_scope_id;

@Override
public String toString() {
return "AccessRequest{" +
"request_id=" + request_id +
", reason='" + reason + '\'' +
", status='" + status + '\'' +
", user_id=" + (user_id != null ? user_id.getUser_id() : null) +
", data_scope_id=" + (data_scope_id != null ? data_scope_id.getData_scope_id() : null) +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.fragile.infosafe.primary.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.*;

Expand All @@ -22,14 +25,16 @@ public class Asset {
private String used;
private String device_type;

@JsonBackReference
@ManyToOne
@JoinColumn(name = "current_assignee_id", referencedColumnName = "user_id")
private User current_assignee;
@ManyToOne
@JoinColumn(name = "previous_assignee_id", referencedColumnName = "user_id")
private User previous_assignee;

@OneToMany(mappedBy = "asset", cascade = CascadeType.REMOVE)
@JsonIgnore
@OneToMany(mappedBy = "asset")
private List<AssetRequests> assetRequestsList;

@PreUpdate
Expand All @@ -39,5 +44,21 @@ private void validateAssignees() {
throw new IllegalArgumentException("Current assignee cannot be the same as the previous assignee.");
}
}

@Override
public String toString() {
return "Asset{" +
"asset_id=" + asset_id +
", asset_name='" + asset_name + '\'' +
", asset_description='" + asset_description + '\'' +
", status='" + status + '\'' +
", availability='" + availability + '\'' +
", used='" + used + '\'' +
", device_type='" + device_type + '\'' +
", current_assignee=" + (current_assignee != null ? current_assignee.getUser_id() : "null") +
", previous_assignee=" + (previous_assignee != null ? previous_assignee.getUser_id() : "null") +
'}';
}

}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fragile.infosafe.primary.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -19,10 +20,24 @@ public class AssetRequests {
private String reason;
private String desired_date;
private String request_status;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

@ManyToOne
@JoinColumn(name = "asset_id")
private Asset asset;

@Override
public String toString() {
return "AssetRequest{" +
"asset_request_id=" + asset_request_id +
", reason='" + reason + '\'' +
", desired_date='" + desired_date + '\'' +
", request_status='" + request_status + '\'' +
", user=" + (user != null ? user.getUser_id() : null) +
", asset=" + (asset != null ? asset.getAsset_id() : null) +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fragile.infosafe.primary.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.*;

Expand Down Expand Up @@ -36,14 +37,19 @@ public class DataScope {
private Set<User> users = new HashSet<>();

@JsonIgnore
@OneToMany(mappedBy = "data_scope_id", cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "data_scope_id")
private List<Task> tasks;

@JsonIgnore
@OneToMany(mappedBy = "dataScope", cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "dataScope")
private List<Risk> risks;

@JsonIgnore
@OneToMany(mappedBy = "data_scope_id", cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "data_scope_id")
private List<AccessRequest> accessRequests;

@JsonIgnore
@OneToMany(mappedBy = "dataScope_id")
private List<SupportRequest> supportRequests;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fragile.infosafe.primary.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -21,6 +22,8 @@ public class Risk {
private String risk_description;
private String suggested_mitigation;
private String risk_status;

@JsonBackReference
@ManyToOne
@JoinColumn(name = "data_scope_id")
private DataScope dataScope;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fragile.infosafe.primary.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -13,14 +14,17 @@
@Entity
@Table(name="support_requests")
public class SupportRequest {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int support_id;
private String support_type;
private String support_description;
private String support_status;

@ManyToOne
@JoinColumn(name = "user_id")
private User user_id;

@ManyToOne
@JoinColumn(name = "data_scope_id")
private DataScope dataScope_id;
Expand All @@ -32,4 +36,18 @@ public class SupportRequest {
@ManyToOne
@JoinColumn(name = "asset_id")
private Asset asset_id;

@Override
public String toString() {
return "SupportRequest{" +
"support_id=" + support_id +
", support_type='" + support_type + '\'' +
", support_description='" + support_description + '\'' +
", support_status='" + support_status + '\'' +
", user_id=" + (user_id != null ? user_id.getUser_id() : null) +
", dataScope_id=" + (dataScope_id != null ? dataScope_id.getData_scope_id() : null) +
", task_id=" + (task_id != null ? task_id.getTask_id() : null) +
", asset_id=" + (asset_id != null ? asset_id.getAsset_id() : null) +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fragile.infosafe.primary.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fragile.infosafe.primary.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import jakarta.persistence.*;
Expand Down Expand Up @@ -42,18 +43,34 @@ public class User implements UserDetails {
@JoinColumn(name = "role_name")
private Role role;

@OneToMany(mappedBy = "current_assignee", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
@JsonManagedReference
@OneToMany(mappedBy = "current_assignee", fetch = FetchType.LAZY)
private List<Asset> assets;

@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
@JsonIgnore
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private List<AssetRequests> assetRequests;

@OneToMany(mappedBy = "user_id", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
@JsonIgnore
@OneToMany(mappedBy = "user_id", fetch = FetchType.LAZY)
private List<SupportRequest> supportRequests;

@OneToMany(mappedBy = "user_id", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
@JsonIgnore
@OneToMany(mappedBy = "user_id", fetch = FetchType.LAZY)
private List<AccessRequest> accessRequests;

@Override
public String toString() {
return "User{" +
"user_id=" + user_id +
", first_name='" + first_name + '\'' +
", last_name='" + last_name + '\'' +
", email='" + email + '\'' +
", password='" + password + '\'' +
", otp='" + otp + '\'' +
", role=" + (role != null ? role.getRole_name() : "null") +
'}';
}

public int getUser_id() {
return user_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
@AllArgsConstructor
@NoArgsConstructor
public class ChangePasswordRequest {
private String userEmail;
private String newPassword;

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.*;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -49,7 +46,12 @@ public ResponseEntity<String> makeAR(AccessRequestRequest request, User authenti
}

public List<AccessRequest> getAllAccessRequests() {
return accessRequestRepository.findAll();
List<AccessRequest> ar = accessRequestRepository.findAll();
for(AccessRequest accessRequest : ar){
accessRequest.getUser_id().setFirst_name(encryptionService.decryptString(accessRequest.getUser_id().getFirst_name()));
accessRequest.getUser_id().setLast_name(encryptionService.decryptString(accessRequest.getUser_id().getLast_name()));
}
return ar;
}

public AccessRequest updateAccessRequest(AccessRequest accessRequest) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fragile.infosafe.primary.service;

import com.fragile.infosafe.primary.model.AccessRequest;
import com.fragile.infosafe.primary.model.Asset;
import com.fragile.infosafe.primary.model.AssetRequests;
import com.fragile.infosafe.primary.model.User;
Expand Down Expand Up @@ -32,6 +33,7 @@ public class AssetRequestService {
private final DeleteService deleteService;
private final EmailService emailService;
private final NotificationsService notificationsService;
private final EncryptionService encryptionService;

public ResponseEntity<String> makeAR(AssetRequestRequest request, User authenticatedUser) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Expand All @@ -43,21 +45,23 @@ public ResponseEntity<String> makeAR(AssetRequestRequest request, User authentic
.user(authenticatedUser)
.build();

if (!isNumeric(request.getAsset_id())) {
System.out.println("here");
Optional<Asset> asset = assetRepository.findByAssetId(request.getAsset_id());
if (asset.isPresent()) {
assetRequests.setAsset(asset.get());
} else {
log.error("Asset with " + request.getAsset_id() + " not found");
}
Optional<Asset> asset = assetRepository.findByAssetId(request.getAsset_id());
if (asset.isPresent()) {
assetRequests.setAsset(asset.get());
} else {
log.error("Asset with " + request.getAsset_id() + " not found");
}
assetRequestRepository.save(assetRequests);
return ResponseEntity.status(HttpStatus.OK).body("added");
}

public List<AssetRequests> getAllAssetRequests() {
return assetRequestRepository.findAll();
List<AssetRequests> ar = assetRequestRepository.findAll();
for (AssetRequests assetRequests : ar) {
assetRequests.getUser().setFirst_name(encryptionService.decryptString(assetRequests.getUser().getFirst_name()));
assetRequests.getUser().setLast_name(encryptionService.decryptString(assetRequests.getUser().getLast_name()));
}
return ar;
}

public AssetRequests updateAssetRequest(AssetRequests assetRequests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public void deleteUserAndSaveToSecondary(String email) {
deleteSupportRequestAndSaveToSecondary(support.getSupport_id());
}
for(Asset asset : entityToDelete.getAssets()){
deleteAssetAndSaveToSecondary(asset.getAsset_id());
asset.setPrevious_assignee(asset.getCurrent_assignee());
asset.setCurrent_assignee(null);
asset.setAvailability("Yes");
}
deletedUserRepository.save(de);
userRepository.delete(entityToDelete);
Expand Down Expand Up @@ -105,6 +107,9 @@ public void deleteAssetAndSaveToSecondary(int asset_id) {
de.setDevice_type(entityToDelete.getDevice_type());
//de.setCurrent_assignee(entityToDelete.getCurrent_assignee());
//de.setPrevious_assignee(entityToDelete.getPrevious_assignee());
for(AssetRequests assetRequests : entityToDelete.getAssetRequestsList()){
deleteAssetRequestAndSaveToSecondary(assetRequests.getAsset_request_id());
}
deletedAssetRepository.save(de);
assetRepository.delete(entityToDelete);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class NotificationsService {

private final NotificationsRepository notificationsRepository;

@Scheduled(cron = "0 0 */10 * * *")
@Scheduled(cron = "0 0 */5 * * *")
public void deleteOldRecords() {
LocalDateTime twelveHoursAgo = LocalDateTime.now().minusHours(12);
notificationsRepository.deleteByCreatedAtBefore(twelveHoursAgo);
Expand Down
Loading

0 comments on commit 9a94b67

Please sign in to comment.