Skip to content

Commit

Permalink
Merge pull request #508 from COS301-SE-2023/backend/improvements
Browse files Browse the repository at this point in the history
Backend/improvements
  • Loading branch information
AlistairMRoss authored Oct 16, 2023
2 parents 1a8498c + 6390bed commit 8c56f23
Show file tree
Hide file tree
Showing 117 changed files with 1,433 additions and 916 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Users will be assigned certain roles when their profile has been created on the

### Supporting Documentation:
- :cd: [Installation Guide](https://drive.google.com/file/d/125hVZvrzfT5g-fSAJtC2lvuoXEt2QXdl/view?usp=drive_link)
- :scroll: [User Manual](https://drive.google.com/file/d/1I3YdViR5jbR4gqQQjGEcjIQmcQFEhaRv/view?usp=drive_link)
- :scroll: [User Manual](documentation/document_files/Infosafe_User_Manual_V4.pdf)
- :triangular_ruler: [System Architecture](https://drive.google.com/file/d/1imZXfUnqDkVMFoVG3KLTdezh4MdND6yX/view?usp=drive_link)
- :test_tube: [Testing Policy](https://drive.google.com/file/d/1OaNACeGeoWsiuj7rx3519Z2q8MjpRZz5/view?usp=drive_link)
- :floppy_disk: [Database Design](https://drive.google.com/file/d/1l1JDXrADDrWAG31QOSZ7N1B5cr5oR-vK/view?usp=drive_link)
Expand Down
4 changes: 0 additions & 4 deletions backend/infosafe_backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="deleted_ risks")
@Table(name="deleted_risks")
public class DeletedRisk {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class PersistencePrimaryConfiguration {
@PreDestroy
@Bean
public LocalContainerEntityManagerFactoryBean primaryEntityManager() {
//System.out.println("This happened1");
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(primaryDataSource());
em.setPackagesToScan("com.fragile.infosafe.primary");
Expand All @@ -52,7 +51,6 @@ public LocalContainerEntityManagerFactoryBean primaryEntityManager() {
@Bean
public DataSource primaryDataSource () {
RDSLogin login = awsSecretService.getRDSLogin();
//System.out.println("This happened2");
DataSource dataSource = DataSourceBuilder
.create()
.driverClassName("com.mysql.cj.jdbc.Driver")
Expand All @@ -73,7 +71,6 @@ public DataSource primaryDataSource () {
@PreDestroy
@Bean
public PlatformTransactionManager primaryTransactionManager () {
//System.out.println("This happened3");
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(primaryEntityManager().getObject());
return transactionManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public DataSource secondaryDataSource() {
DataSource dataSource = DataSourceBuilder
.create()
.driverClassName("com.mysql.cj.jdbc.Driver")
.url("jdbc:" + login.getEngine() + "://" + login.getHost() + ":" + login.getPort() + "/secondary_database") //+ login.getDbname())//"jdbc:" + login.getEngine() + "://" + login.getHost() + ":" + login.getPort() + "/secondary_database") //+ login.getDbname())
.username(login.getUsername()) //login.getUsername())
.password(login.getPassword()) //login.getPassword())
.url("jdbc:" + login.getEngine() + "://" + login.getHost() + ":" + login.getPort() + "/secondary_database")
.username(login.getUsername())
.password(login.getPassword())
.build();
if (dataSource instanceof HikariDataSource hikariDataSource) {
hikariDataSource.setMaximumPoolSize(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("https://ec2-52-91-180-105.compute-1.amazonaws.com", "https://infosafe.live", "http://localhost:3000")); //""
configuration.setAllowedOrigins(Arrays.asList("https://ec2-52-91-180-105.compute-1.amazonaws.com", "http://localhost:8080", "http://localhost:3000")); //""
configuration.setAllowedMethods(Arrays.asList("*"));
configuration.setAllowedHeaders(Arrays.asList("*"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.fragile.infosafe.primary.config;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StorageConfig {

@Value("${AWS_ACCESS_KEY_ID}")
private String accessKey;

@Value("${AWS_SECRET_ACCESS_KEY}")
private String secretKey;

@Bean
public AmazonS3 generateS3Client(){
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
return AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion("us-east-1").build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fragile.infosafe.primary.controller;

import com.fragile.infosafe.primary.service.StorageService;
import com.sun.mail.iap.ByteArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Slf4j
@RequestMapping("api/storage")
public class StorageController {

private StorageService service;

@PostMapping("/upload")
public ResponseEntity<String> uploadFile(@RequestParam(value = "file") MultipartFile file){
return new ResponseEntity<>(service.uploadFile(file), HttpStatus.OK);
}

@GetMapping("/download/{fileName}")
public ResponseEntity<ByteArrayResource> downloadFile(@PathVariable String fileName){
byte[] data = service.dowloadFile(fileName);
ByteArrayResource resource = new ByteArrayResource(data);
return ResponseEntity
.ok()
.contentLength(data.length)
.header("Content-type", "application/octet-stream")
.header("Content-disposition", "attachment; filename=\"" + fileName + "\"")
.body(resource);
}

@DeleteMapping("/delete/{fileName}")
public ResponseEntity<String> deleteFile(@PathVariable String fileName){
return new ResponseEntity<>(service.deleteFile(fileName), HttpStatus.OK);
}
}
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 Down Expand Up @@ -29,7 +32,8 @@ public class Asset {
@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 +43,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,31 @@ 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;

@Override
public String toString() {
return "DataScope{" +
"data_scope_id=" + data_scope_id +
", ds_name='" + ds_name + '\'' +
", ds_description='" + ds_description + '\'' +
", date_captured=" + date_captured +
", dataCustodian=" + dataCustodian +
", ds_status='" + ds_status + '\'' +
", users=" + users +
'}';
}
}
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,7 @@ public class Risk {
private String risk_description;
private String suggested_mitigation;
private String risk_status;

@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 Expand Up @@ -39,4 +40,19 @@ public class Task {
private Set<User> users = new HashSet<>();
@Column(nullable = true)
private int daysUntilDue;

@Override
public String toString() {
return "Task{" +
"task_id=" + task_id +
", task_name='" + task_name + '\'' +
", task_description='" + task_description + '\'' +
", task_status='" + task_status + '\'' +
", due_date='" + due_date + '\'' +
", date_created='" + date_created + '\'' +
", data_scope_id=" + data_scope_id +
", users=" + users +
", daysUntilDue=" + daysUntilDue +
'}';
}
}
Loading

0 comments on commit 8c56f23

Please sign in to comment.