Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance User Authentication, Communication, and Frontend Integration #6

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@
<artifactId>jjwt</artifactId>
<version>0.12.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/lk/ijse/fieldguardianbackend/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lk.ijse.fieldguardianbackend.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* WebConfig class (Configuration)
* This class configures the CORS policy for the application.
*/
@Configuration
public class WebConfig {
@Value("${cors.allowed.origins}")
private String allowedOrigins;

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(allowedOrigins)
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import static org.springframework.security.config.Customizer.withDefaults;
/**
* -----------------------
* This class configures the security settings for the application.
Expand Down Expand Up @@ -45,6 +46,7 @@ public class WebSecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.cors(withDefaults())
.authorizeRequests(req ->
req.requestMatchers(
"api/v1/auth/**",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package lk.ijse.fieldguardianbackend.controller;

import lk.ijse.fieldguardianbackend.customObj.OtpResponse;
import lk.ijse.fieldguardianbackend.jwtModels.JwtAuthResponse;
import lk.ijse.fieldguardianbackend.jwtModels.UserRequestDTO;
import lk.ijse.fieldguardianbackend.service.AuthService;
import lk.ijse.fieldguardianbackend.service.JwtService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -17,10 +19,10 @@
@RestController
@RequestMapping("/api/v1/auth")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class AuthController {
private final AuthService authService;
private final PasswordEncoder passwordEncoder;
private final JwtService jwtService;
/**
* Endpoint for user registration.
*
Expand All @@ -42,6 +44,29 @@ public ResponseEntity<JwtAuthResponse> signUp(@RequestBody UserRequestDTO userRe
public ResponseEntity<JwtAuthResponse> signIn(@RequestBody UserRequestDTO userRequestDTO) {
return ResponseEntity.status(HttpStatus.OK).body(authService.signIn(userRequestDTO));
}
/**
* Endpoint for validating a user token.
* @param token the JWT token
* @return ResponseEntity with the validation status
*/
@PostMapping("/validate")
public ResponseEntity<Boolean> validateUser(@RequestHeader("Authorization") String token) {
jwtService.extractUsername(token.substring(7));
return ResponseEntity.status(HttpStatus.OK).body(true);
}
/**
* Endpoint for requesting an OTP.
*
* @param option the option to verify the user
* @param email the email of the user
* @return ResponseEntity with the OTP response and OK status
*/
@PostMapping("/otp")
public ResponseEntity<OtpResponse> requestOtp(
@RequestParam("option") String option, @RequestParam("email") String email) {
authService.verifyUserEmail(option, email);
return ResponseEntity.status(HttpStatus.OK).build();
}
/**
* Endpoint for refreshing the JWT token.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
@RestController
@RequestMapping("/api/v1/crop")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class CropController {
private final CropService cropService;
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package lk.ijse.fieldguardianbackend.controller;

import lk.ijse.fieldguardianbackend.dto.impl.FieldMonitoringCountDTO;
import lk.ijse.fieldguardianbackend.service.DashboardService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* REST controller for managing dashboard.
*/
@RestController
@RequestMapping("/api/v1/dashboard")
@RequiredArgsConstructor
public class DashboardController {
private final DashboardService dashboardService;
/**
* {@code GET /user-count} : Get the total number of users.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the total number of users in the body.
*/
@GetMapping("/user-count")
public ResponseEntity<Long> getTotalUsers() {
return ResponseEntity.ok(dashboardService.getTotalUsers());
}
/**
* {@code GET /staff-count} : Get the total number of active staff.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the total number of active staff in the body.
*/
@GetMapping("/staff-count")
public ResponseEntity<Long> getTotalActiveStaff() {
return ResponseEntity.ok(dashboardService.getTotalActiveStaff());
}
/**
* {@code GET /crop-count} : Get the total number of active crops.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the total number of active crops in the body.
*/
@GetMapping("/crop-count")
public ResponseEntity<Long> getTotalActiveCrops() {
return ResponseEntity.ok(dashboardService.getTotalActiveCrops());
}
/**
* {@code GET /vehicle-count} : Get the total number of active vehicles.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the total number of active vehicles in the body.
*/
@GetMapping("/vehicle-count")
public ResponseEntity<Long> getTotalActiveVehicles() {
return ResponseEntity.ok(dashboardService.getTotalActiveVehicles());
}
/**
* {@code GET /equipment-count} : Get the total number of active equipment.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the total number of active equipment in the body.
*/
@GetMapping("/equipment-count")
public ResponseEntity<Long> getTotalActiveEquipment() {
return ResponseEntity.ok(dashboardService.getTotalActiveEquipment());
}
/**
* {@code GET /top-fields} : Get the top five monitored fields.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the top five monitored fields in the body.
*/
@GetMapping("/top-fields")
public List<FieldMonitoringCountDTO> getTopMonitoredFields() {
return dashboardService.getTopFiveMonitoredFields();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
@RestController
@RequestMapping("/api/v1/equipment")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class EquipmentController {
private final EquipmentService equipmentService;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
@RestController
@RequestMapping("/api/v1/field")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class FieldController {
private final FieldService fieldService;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
@RestController
@RequestMapping("/api/v1/monitoring-log")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class MonitoringLogController {
private final MonitoringLogService monitoringLogService;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lk.ijse.fieldguardianbackend.dto.impl.StaffDTO;
import lk.ijse.fieldguardianbackend.dto.impl.StaffFieldDTO;
import lk.ijse.fieldguardianbackend.dto.impl.VehicleDTO;
import lk.ijse.fieldguardianbackend.entity.enums.Designation;
import lk.ijse.fieldguardianbackend.service.StaffService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -19,7 +20,6 @@
@RestController
@RequestMapping("/api/v1/staff")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class StaffController {
private final StaffService staffService;
/**
Expand Down Expand Up @@ -107,4 +107,12 @@ public ResponseEntity<List<StaffFieldDTO>> getStaffFields(@PathVariable("id") St
public ResponseEntity<List<StaffDTO>> getStaffWithoutEquipment() {
return ResponseEntity.status(HttpStatus.OK).body(staffService.getStaffWithoutEquipment());
}
/**
* {@code GET /designations} : Get all staff designations.
* @return the {@link List} of staff designations.
*/
@GetMapping("/designations")
public List<Designation> getAllStaffDesignations() {
return staffService.getAllStaffDesignations();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
@RestController
@RequestMapping("/api/v1/user")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class UserController {
private final UserService userService;
private final PasswordEncoder passwordEncoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
@RestController
@RequestMapping("/api/v1/vehicle")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class VehicleController {
private final VehicleService vehicleService;
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lk.ijse.fieldguardianbackend.customObj;

import java.io.Serializable;

public interface MailResponse extends Serializable {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lk.ijse.fieldguardianbackend.customObj;

import java.io.Serializable;

public interface OtpResponse extends Serializable {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lk.ijse.fieldguardianbackend.customObj.impl;

import lk.ijse.fieldguardianbackend.customObj.MailResponse;
import lombok.Builder;

import java.util.Map;

@Builder
public record MailBody(String to, String subject, String templateName, Map<String, Object> replacements) implements MailResponse {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package lk.ijse.fieldguardianbackend.customObj.impl;

import lk.ijse.fieldguardianbackend.customObj.OtpResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class OtpErrorResponse implements OtpResponse {
private int errorCode;
private String errorMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package lk.ijse.fieldguardianbackend.customObj.impl;

import lk.ijse.fieldguardianbackend.customObj.OtpResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class OtpValueResponse implements OtpResponse {
private String otp;
private String username;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class CropSaveDTO implements SuperDTO, CropResponse {
@Size(min = 3, max = 20, message = "Category must be between 3 and 20 characters")
private String category;
@NotBlank(message = "Season is mandatory")
@Size(min = 3, max = 10, message = "Season must be between 3 and 10 characters")
@Size(min = 3, max = 30, message = "Season must be between 3 and 30 characters")
private String season;
@NotBlank(message = "Status is mandatory")
@NotBlank(message = "FieldCode is mandatory")
private String fieldCode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package lk.ijse.fieldguardianbackend.dto.impl;

import lk.ijse.fieldguardianbackend.dto.SuperDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class FieldMonitoringCountDTO implements SuperDTO {
private String fieldName;
private long monitoringCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.time.LocalDate;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class MonitoringLogResponseDTO implements SuperDTO, MonitoringLogResponse {
private String code;
private Date date;
private LocalDate date;
private String details;
private String observedImage;
private String fieldCode;
private int staffCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
import java.time.LocalDate;

@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -19,7 +19,7 @@ public class MonitoringLogSaveDTO implements SuperDTO, MonitoringLogResponse {
private String code;
@NotNull(message = "Date is mandatory")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date date;
private LocalDate date;
@NotBlank(message = "Details is mandatory")
@Size(min = 3, max = 400, message = "Details must be between 3 and 400 characters")
private String details;
Expand Down
Loading