-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09e7f34
commit 739f918
Showing
7 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
27 changes: 24 additions & 3 deletions
27
src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/MigrateUserDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/gov/cabinetoffice/gap/adminbackend/exceptions/UnauthorizedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/GrantApplicantRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/gov/cabinetoffice/gap/adminbackend/services/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |