Skip to content

Commit

Permalink
Miscellaneous cleanups (#43)
Browse files Browse the repository at this point in the history
* [clean] Cleanup

* [cln] Update Name

* [cln] Remove Lombok

* [ref] Loop in InitializerService

* [ref] Cleanup

* [sec] Permit All

* [cln] Format

* [cln] remove autowired
  • Loading branch information
chrisvrose authored May 25, 2024
1 parent 773e8df commit 34393b0
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 250 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.modelmapper:modelmapper:3.2.0'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class GateKeeperApplication {

@GetMapping("/")
String index(){
return "works";
return "GateKeeper";
}

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,28 @@ public class SecurityConfiguration {
public UserDetailsService userDetailsService() {
return new UserInfoUserDetailsService();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.formLogin(Customizer.withDefaults()).httpBasic(Customizer.withDefaults()).authorizeHttpRequests(SecurityConfiguration::getCustomizedHttpAuthorization).csrf(AbstractHttpConfigurer::disable)

.cors(customizer->customizer.configurationSource(corsConfigurationSource()));
return http.build();
}

private static void getCustomizedHttpAuthorization(AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry customizer) {
customizer

.requestMatchers(HttpMethod.POST,"/user").hasRole("Admin")
.requestMatchers(HttpMethod.PUT,"/user").hasRole("Admin")
.requestMatchers(HttpMethod.DELETE,"/user").hasRole("Admin")
.requestMatchers(HttpMethod.POST, "/user").hasRole("Admin")
.requestMatchers(HttpMethod.PUT, "/user").hasRole("Admin")
.requestMatchers(HttpMethod.DELETE, "/user").hasRole("Admin")
.requestMatchers("/role").hasRole("Admin")
.requestMatchers("/society").hasRole("Admin")
.anyRequest().authenticated();
.anyRequest().permitAll();

}

Expand All @@ -66,9 +68,8 @@ CorsConfigurationSource corsConfigurationSource() {
}


public static String getRequesterDetails()
{
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication.getName();
}
public static String getRequesterDetails() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication.getName();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.ieeervce.gatekeeper.controller;

import jakarta.mail.Multipart;
import org.ieeervce.gatekeeper.InvalidDataException;
import org.ieeervce.gatekeeper.ItemNotFoundException;
import org.ieeervce.gatekeeper.PDFNotConversionException;
import org.ieeervce.gatekeeper.exception.InvalidDataException;
import org.ieeervce.gatekeeper.exception.ItemNotFoundException;
import org.ieeervce.gatekeeper.exception.PDFNotConversionException;
import org.ieeervce.gatekeeper.dto.RequestDTO;
import org.ieeervce.gatekeeper.dto.RequestFormPdfDTO;
import org.ieeervce.gatekeeper.dto.ResponseRequestFormDTO;
import org.ieeervce.gatekeeper.dto.UserDTO;
import org.ieeervce.gatekeeper.entity.*;

import org.ieeervce.gatekeeper.service.RequestFormService;
Expand All @@ -17,17 +15,15 @@
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.modelmapper.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Type;
import java.util.Optional;

import java.awt.*;
import java.security.Principal;
import java.util.List;
import java.util.Optional;

import static org.ieeervce.gatekeeper.config.SecurityConfiguration.getRequesterDetails;

Expand All @@ -36,6 +32,7 @@


public class RequestFormController {
private static final Logger LOGGER = LoggerFactory.getLogger(RequestFormController.class);

private final ModelMapper modelMapper;

Expand All @@ -50,145 +47,145 @@ protected void configure() {
skip().setFormPDF(null);
}
};
RequestFormController(RequestFormService requestFormService, ModelMapper modelMapper, UserService userService, RoleService roleService, ReviewLogService reviewLogService){

RequestFormController(RequestFormService requestFormService, ModelMapper modelMapper, UserService userService, RoleService roleService, ReviewLogService reviewLogService) {
this.requestFormService = requestFormService;
this.modelMapper= modelMapper;
this.modelMapper = modelMapper;
this.userService = userService;
this.roleService=roleService;
this.reviewLogService=reviewLogService;
this.roleService = roleService;
this.reviewLogService = reviewLogService;
this.modelMapper.addMappings(skipReferencedFieldsMap);
this.modelMapper.getConfiguration().setAmbiguityIgnored(true);


}

@GetMapping
public List<RequestForm> getAll(){
public List<RequestForm> getAll() {
return requestFormService.list();
}

@GetMapping("/byRequester")
public List<RequestFormPdfDTO> getByUser()
{
List<RequestForm> requestFormList=requestFormService.getRequestFormByRequester(userService.getUserByEmail(getRequesterDetails()).get());
Type listType = new TypeToken<List<ResponseRequestFormDTO>>(){}.getType();
return modelMapper.map(requestFormList,listType);
public List<RequestFormPdfDTO> getByUser() {
List<RequestForm> requestFormList = requestFormService.getRequestFormByRequester(userService.getUserByEmail(getRequesterDetails()).get());
Type listType = new TypeToken<List<ResponseRequestFormDTO>>() {
}.getType();
return modelMapper.map(requestFormList, listType);
}

@GetMapping("/{requestFormId}")
public ResponseRequestFormDTO getOne(@PathVariable Long requestFormId) throws ItemNotFoundException {

return modelMapper.map(requestFormService.findOne(requestFormId),ResponseRequestFormDTO.class);
return modelMapper.map(requestFormService.findOne(requestFormId), ResponseRequestFormDTO.class);
}

@DeleteMapping("/{requestFormID}")
public void deleteRequestForm(@PathVariable Long requestFormId) throws ItemNotFoundException{
public void deleteRequestForm(@PathVariable Long requestFormId) throws ItemNotFoundException {
requestFormService.delete(requestFormId);
}

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseRequestFormDTO postRequestForm(@RequestParam("eventTitle") String eventTitle, @RequestParam("isFinance") boolean isFinance , @RequestParam("formPDF") MultipartFile formPDF ) throws InvalidDataException,PDFNotConversionException{
System.out.println("in");
public ResponseRequestFormDTO postRequestForm(@RequestParam("eventTitle") String eventTitle, @RequestParam("isFinance") boolean isFinance, @RequestParam("formPDF") MultipartFile formPDF) throws InvalidDataException, PDFNotConversionException {
LOGGER.info("in: post request form");
RequestForm requestForm = new RequestForm();
requestForm.setEventTitle(eventTitle);
requestForm.setFinance(isFinance);
requestForm.setStatus(FinalStatus.PENDING);


System.out.println(getRequesterDetails());
LOGGER.debug("Requester Details: {}", getRequesterDetails());
try {
User optionalUser = userService.getUserByEmail(getRequesterDetails()).get();
requestForm.setRequester(optionalUser);


requestForm.setRequestHierarchy(roleService.generateHierarchy(optionalUser,isFinance));
requestForm.setRequestHierarchy(roleService.generateHierarchy(optionalUser, isFinance));

userService.setPendingRequests(requestForm,requestForm.getRequestHierarchy(),requestForm.getRequestIndex(),optionalUser);
userService.setPendingRequests(requestForm, requestForm.getRequestHierarchy(), requestForm.getRequestIndex(), optionalUser);
} catch (Exception e) {

System.out.println("here");
LOGGER.warn("Exception getting user and hierarchy", e);
}

try {
requestForm.setFormPDF(formPDF.getBytes());

} catch (java.io.IOException e){
requestForm.setFormPDF(formPDF.getBytes());
} catch (java.io.IOException e) {
throw new PDFNotConversionException("Could not store pdf");
}


RequestForm savedRequestForm =requestFormService.save(requestForm);
return modelMapper.map(savedRequestForm,ResponseRequestFormDTO.class);//truncated
RequestForm savedRequestForm = requestFormService.save(requestForm);
return modelMapper.map(savedRequestForm, ResponseRequestFormDTO.class);//truncated
}

@PutMapping("/{requestFormId}")
public ResponseRequestFormDTO editRequestForm(@RequestBody RequestDTO requestDTO,@PathVariable Long requestFormId) throws ItemNotFoundException{
public ResponseRequestFormDTO editRequestForm(@RequestBody RequestDTO requestDTO, @PathVariable Long requestFormId) throws ItemNotFoundException {
RequestForm editedRequestForm = modelMapper.map(requestDTO, RequestForm.class);

return modelMapper.map(requestFormService.edit(requestFormId, editedRequestForm),ResponseRequestFormDTO.class); //truncated
return modelMapper.map(requestFormService.edit(requestFormId, editedRequestForm), ResponseRequestFormDTO.class); //truncated
}

@PostMapping("/{requestFormId}/approve")
public ResponseRequestFormDTO approveRequest(@PathVariable Long requestFormId,String comment) throws ItemNotFoundException {
public ResponseRequestFormDTO approveRequest(@PathVariable Long requestFormId, String comment) throws ItemNotFoundException {
User optionalUser = userService.getUserByEmail(getRequesterDetails()).get();
RequestForm requestForm = requestFormService.findOne(requestFormId);
if(requestForm.getStatus()!=FinalStatus.PENDING)
return modelMapper.map(requestForm,ResponseRequestFormDTO.class);
int index=requestForm.getRequestIndex();
userService.removePendingRequests(requestForm,requestForm.getRequestHierarchy(),index,optionalUser,StatusEnum.ACCEPTED);
ReviewLog reviewLog=new ReviewLog();
if (requestForm.getStatus() != FinalStatus.PENDING)
return modelMapper.map(requestForm, ResponseRequestFormDTO.class);
int index = requestForm.getRequestIndex();
userService.removePendingRequests(requestForm, requestForm.getRequestHierarchy(), index, optionalUser, StatusEnum.ACCEPTED);
ReviewLog reviewLog = new ReviewLog();
reviewLog.setComments(comment);
reviewLog.setStatus(StatusEnum.ACCEPTED);
reviewLog.setUserId(optionalUser);

reviewLog.setRequestFormId(requestForm);

reviewLogService.addReview(reviewLog);
reviewLogService.addReview(reviewLog);
requestForm.getReviewLogs().add(reviewLog);
requestForm.setRequestIndex(index+1);
requestForm.setRequestIndex(index + 1);
index++;
if(index<requestForm.getRequestHierarchy().size())
userService.setPendingRequests(requestForm,requestForm.getRequestHierarchy(),index,requestForm.getRequester());
else
{
if (index < requestForm.getRequestHierarchy().size())
userService.setPendingRequests(requestForm, requestForm.getRequestHierarchy(), index, requestForm.getRequester());
else {
requestForm.setStatus(FinalStatus.ACCEPTED);
}
//TODO send mails to requester at every step and send mail to the next set of users assigned(update setPendingRequests() method to add this)
return modelMapper.map(requestFormService.save(requestForm),ResponseRequestFormDTO.class);//truncated
return modelMapper.map(requestFormService.save(requestForm), ResponseRequestFormDTO.class);//truncated
}

@PostMapping("/{requestFormId}/reject")
public ResponseRequestFormDTO rejectRequest(@PathVariable Long requestFormId,String comment) throws ItemNotFoundException {
public ResponseRequestFormDTO rejectRequest(@PathVariable Long requestFormId, String comment) throws ItemNotFoundException {

RequestForm requestForm = requestFormService.findOne(requestFormId);
if(requestForm.getStatus()!=FinalStatus.PENDING)
return modelMapper.map(requestForm,ResponseRequestFormDTO.class);
if (requestForm.getStatus() != FinalStatus.PENDING)
return modelMapper.map(requestForm, ResponseRequestFormDTO.class);
User optionalUser = userService.getUserByEmail(getRequesterDetails()).get();
int index=requestForm.getRequestIndex();
userService.removePendingRequests(requestForm,requestForm.getRequestHierarchy(),index,optionalUser,StatusEnum.REJECTED);
requestForm.setRequestIndex((index+1));
ReviewLog reviewLog=new ReviewLog();
int index = requestForm.getRequestIndex();
userService.removePendingRequests(requestForm, requestForm.getRequestHierarchy(), index, optionalUser, StatusEnum.REJECTED);
requestForm.setRequestIndex((index + 1));
ReviewLog reviewLog = new ReviewLog();
reviewLog.setComments(comment);
reviewLog.setStatus(StatusEnum.REJECTED);
reviewLog.setUserId(optionalUser);
reviewLog.setRequestFormId(requestForm);
reviewLogService.addReview(reviewLog);
requestForm.setStatus(FinalStatus.REJECTED);
requestForm.getReviewLogs().add(reviewLog);
return modelMapper.map(requestFormService.save(requestForm),ResponseRequestFormDTO.class);//truncated
return modelMapper.map(requestFormService.save(requestForm), ResponseRequestFormDTO.class);//truncated
//TODO update requester with email
}

@GetMapping("/pdf/{requestFormId}")
private RequestFormPdfDTO formPdf(@PathVariable Long requestFormId) throws ItemNotFoundException {
RequestForm requestForm=requestFormService.findOne(requestFormId);
return modelMapper.map(requestForm,RequestFormPdfDTO.class);
RequestForm requestForm = requestFormService.findOne(requestFormId);
return modelMapper.map(requestForm, RequestFormPdfDTO.class);

}

@GetMapping("/")
private List<ResponseRequestFormDTO> getAllRequests()
{
List<RequestForm> requestFormList=requestFormService.getAllRequests();
Type listType = new TypeToken<List<ResponseRequestFormDTO>>(){}.getType();
return modelMapper.map(requestFormList,listType);
private List<ResponseRequestFormDTO> getAllRequests() {
List<RequestForm> requestFormList = requestFormService.getAllRequests();
Type listType = new TypeToken<List<ResponseRequestFormDTO>>() {
}.getType();
return modelMapper.map(requestFormList, listType);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.ieeervce.gatekeeper.controller;

import org.ieeervce.gatekeeper.ItemNotFoundException;
import org.ieeervce.gatekeeper.exception.ItemNotFoundException;
import org.ieeervce.gatekeeper.dto.RoleDTO;
import org.ieeervce.gatekeeper.dto.SocietyDTO;
import org.ieeervce.gatekeeper.entity.Role;
import org.ieeervce.gatekeeper.service.RoleService;
import org.modelmapper.ModelMapper;
Expand All @@ -17,7 +16,6 @@
public class RoleController {
private final RoleService roleService;
private final ModelMapper modelMapper;
@Autowired
public RoleController(RoleService roleService, ModelMapper modelMapper){
this.roleService= roleService;
this.modelMapper=modelMapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.ieeervce.gatekeeper.controller;

import org.ieeervce.gatekeeper.ItemNotFoundException;
import org.ieeervce.gatekeeper.exception.ItemNotFoundException;
import org.ieeervce.gatekeeper.dto.SocietyDTO;
import org.ieeervce.gatekeeper.entity.Society;
import org.ieeervce.gatekeeper.service.SocietyService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.ieeervce.gatekeeper.controller;

import org.ieeervce.gatekeeper.InvalidDataException;
import org.ieeervce.gatekeeper.ItemNotFoundException;
import org.ieeervce.gatekeeper.exception.InvalidDataException;
import org.ieeervce.gatekeeper.exception.ItemNotFoundException;
import org.ieeervce.gatekeeper.dto.UserDTO;
import org.ieeervce.gatekeeper.entity.RequestForm;
import org.ieeervce.gatekeeper.entity.Role;
import org.ieeervce.gatekeeper.entity.Society;
import org.ieeervce.gatekeeper.entity.User;
Expand All @@ -16,7 +15,6 @@
import org.springframework.web.bind.annotation.*;
import org.ieeervce.gatekeeper.service.UserService;

import java.util.List;
import java.util.Optional;

@RestController
Expand All @@ -26,14 +24,14 @@ public class UserController {
private final ModelMapper modelMapper;
private final SocietyService societyService;
private final RoleService roleService;
private final PasswordEncoder passwordEncoder;
PropertyMap<UserDTO,User> skipReferencedFieldsMap = new PropertyMap<UserDTO, User>() {
@Override
protected void configure() {
skip().setUserId(null);
}
};

private PasswordEncoder passwordEncoder;
@Autowired
public UserController(UserService userService, ModelMapper modelMapper,SocietyService societyService,RoleService roleService, PasswordEncoder passwordEncoder) {
this.userService = userService;
Expand Down Expand Up @@ -69,14 +67,9 @@ User addUser(@RequestBody UserDTO userDTO) throws InvalidDataException {
@GetMapping("/{email}")
public UserDTO getUserByEmail(@PathVariable String email) throws ItemNotFoundException{
Optional<User> userOptional = userService.getUserByEmail(email);
if(userOptional.isPresent()){
User user = userOptional.get();
return modelMapper.map(user, UserDTO.class);
}
else{
throw new ItemNotFoundException("User Not Found");
}

return userOptional
.map(user->modelMapper.map(user, UserDTO.class))
.orElseThrow(()->new ItemNotFoundException("User Not Found"));
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.ieeervce.gatekeeper;
package org.ieeervce.gatekeeper.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand Down
Loading

0 comments on commit 34393b0

Please sign in to comment.