Skip to content

Commit

Permalink
[kbss-cvut/record-manager-ui#202] Fix RoleGroup persistence context
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Dec 11, 2024
1 parent c4ee42a commit 8a3c607
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cz.cvut.kbss.study.rest.dto.RawJson;
import cz.cvut.kbss.study.rest.util.RestUtils;
import cz.cvut.kbss.study.persistence.data.DataLoader;
import cz.cvut.kbss.study.service.UserService;
import cz.cvut.kbss.study.service.security.SecurityUtils;
import cz.cvut.kbss.study.util.ConfigParam;
import cz.cvut.kbss.study.util.Utils;
Expand Down Expand Up @@ -44,16 +45,17 @@ public class FormGenService {

private final Environment environment;

private final SecurityUtils securityUtils;
private final UserService userService;

public FormGenService(FormGenDao formGenDao,
DataLoader dataLoader,
Environment environment,
SecurityUtils securityUtils) {
SecurityUtils securityUtils,
UserService userService) {
this.formGenDao = formGenDao;
this.dataLoader = dataLoader;
this.environment = environment;
this.securityUtils = securityUtils;
this.userService = userService;
}

@PostConstruct
Expand All @@ -72,7 +74,7 @@ private void loadConfiguration() {
*/
public RawJson generateForm(PatientRecord record) {
Objects.requireNonNull(record);
final User author = securityUtils.getCurrentUser();
final User author = userService.findCurrentUser();
if (author.getInstitution() != null) {
author.getInstitution().setMembers(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import cz.cvut.kbss.study.exception.ValidationException;
import cz.cvut.kbss.study.model.Institution;
import cz.cvut.kbss.study.model.User;
import cz.cvut.kbss.study.model.Vocabulary;
import cz.cvut.kbss.study.persistence.dao.GenericDao;
import cz.cvut.kbss.study.persistence.dao.PatientRecordDao;
import cz.cvut.kbss.study.persistence.dao.UserDao;
Expand Down Expand Up @@ -79,7 +78,8 @@ public User getCurrentUser() {
@Transactional(readOnly = true)
@Override
public User findCurrentUser() {
return userDao.findByUsername(securityUtils.getCurrentUser().getUsername());
String currentUserName = securityUtils.getCurrentUserUsername();
return userDao.findByUsername(currentUserName);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ private User resolveAccountFromOAuthPrincipal(Jwt principal) {
return user;
}

/**
* Gets the currently authenticated user username.
*
* @return Current user username
*/
public String getCurrentUserUsername() {
final SecurityContext context = SecurityContextHolder.getContext();
assert context != null;
final Object principal = context.getAuthentication().getPrincipal();
if (principal instanceof Jwt) {
return resolveAccountUsernameFromOAuthPrincipal((Jwt) principal);
} else {
return context.getAuthentication().getName();
}
}

private String resolveAccountUsernameFromOAuthPrincipal(Jwt principal) {
final OidcUserInfo userInfo = new OidcUserInfo(principal.getClaims());
return userDao.findByUsername(userInfo.getPreferredUsername()).getUsername();
}

/**
* Checks whether the current user is a member of a institution with the specified key.
*
Expand Down

0 comments on commit 8a3c607

Please sign in to comment.