Skip to content

Commit

Permalink
Migrate user endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DomWestAnd committed Aug 3, 2023
1 parent 09e7f34 commit 739f918
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
package gov.cabinetoffice.gap.adminbackend.controllers;

import gov.cabinetoffice.gap.adminbackend.dtos.MigrateUserDto;
import gov.cabinetoffice.gap.adminbackend.dtos.UserDTO;
import gov.cabinetoffice.gap.adminbackend.exceptions.UnauthorizedException;
import gov.cabinetoffice.gap.adminbackend.mappers.UserMapper;
import gov.cabinetoffice.gap.adminbackend.models.AdminSession;
import gov.cabinetoffice.gap.adminbackend.security.AuthManager;
import gov.cabinetoffice.gap.adminbackend.services.JwtService;
import gov.cabinetoffice.gap.adminbackend.services.UserService;
import gov.cabinetoffice.gap.adminbackend.utils.HelperUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
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 org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.*;

import static org.springframework.util.ObjectUtils.isEmpty;

@RequiredArgsConstructor
@RestController
@RequestMapping("/users")
@Log4j2
public class UserController {

private final UserMapper userMapper;
private final JwtService jwtService;
private final UserService userService;

@GetMapping("/loggedInUser")
public ResponseEntity<UserDTO> getLoggedInUserDetails() {
AdminSession session = HelperUtils.getAdminSessionForAuthenticatedUser();
return ResponseEntity.ok(userMapper.adminSessionToUserDTO(session));
}

@PatchMapping("/migrate")
public ResponseEntity<String> migrateUser(@RequestBody MigrateUserDto migrateUserDto, @RequestHeader("Authorization") String token) {
// Authing here rather than middleware as we do not have an admin session at this state in the journey
if (isEmpty(token) || !token.startsWith("Bearer "))
throw new UnauthorizedException("Expected Authorization header not provided");
jwtService.verifyToken(token.split(" ")[1]);

userService.migrateUser(migrateUserDto.getOneLoginSub(), migrateUserDto.getColaSub());
return ResponseEntity.ok("User migrated successfully");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gov.cabinetoffice.gap.adminbackend.dtos;

import lombok.Builder;
import lombok.Data;

import java.util.UUID;

@Data
@Builder
public class MigrateUserDto {

private String oneLoginSub;

private UUID colaSub;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package gov.cabinetoffice.gap.adminbackend.exceptions;

import org.springframework.web.bind.annotation.ResponseStatus;

import static org.springframework.http.HttpStatus.UNAUTHORIZED;

@ResponseStatus(UNAUTHORIZED)
public class UnauthorizedException extends RuntimeException {

public UnauthorizedException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

import gov.cabinetoffice.gap.adminbackend.entities.GapUser;

import java.util.Optional;

public interface GapUserRepository extends JpaRepository<GapUser, Integer> {

Optional<GapUser> findByUserSub(String userSub);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gov.cabinetoffice.gap.adminbackend.repositories;

import gov.cabinetoffice.gap.adminbackend.dtos.submission.GrantApplicant;
import gov.cabinetoffice.gap.adminbackend.entities.GapUser;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface GrantApplicantRepository extends JpaRepository<GrantApplicant, Long> {
Optional<GrantApplicant> findByUserId(String userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
+ "}/export-batch/{batchExportId:" + UUID_REGEX_STRING + "}/signedUrl",
"/export-batch/{exportId:" + UUID_REGEX_STRING + "}/outstandingCount",
"/grant-advert/lambda/{grantAdvertId:" + UUID_REGEX_STRING + "}/publish",
"/grant-advert/lambda/{grantAdvertId:" + UUID_REGEX_STRING + "}/unpublish")
"/grant-advert/lambda/{grantAdvertId:" + UUID_REGEX_STRING + "}/unpublish",
"/users/migrate")
.permitAll()
.antMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-resources/**",
"/swagger-ui.html", "/webjars/**")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gov.cabinetoffice.gap.adminbackend.services;

import gov.cabinetoffice.gap.adminbackend.repositories.GapUserRepository;
import gov.cabinetoffice.gap.adminbackend.repositories.GrantApplicantRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@RequiredArgsConstructor
public class UserService {

private final GapUserRepository gapUserRepository;
private final GrantApplicantRepository grantApplicantRepository;

public void migrateUser(final String oneLoginSub, final UUID colaSub) {
gapUserRepository.findByUserSub(colaSub.toString()).ifPresent(gapUser -> {
gapUser.setUserSub(oneLoginSub);
gapUserRepository.save(gapUser);
});

grantApplicantRepository.findByUserId(colaSub.toString()).ifPresent(grantApplicant -> {
grantApplicant.setUserId(oneLoginSub);
grantApplicantRepository.save(grantApplicant);
});
}
}

0 comments on commit 739f918

Please sign in to comment.