Skip to content

Commit

Permalink
Refactor service/authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomTannenbaum committed Nov 27, 2024
1 parent 3be0d39 commit 914ba2f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ public AuthorizationRegistrationService(UserBusinessService userBusinessService,

@Cacheable(value = AUTHORIZATION_USER_CACHE, key = "T(ch.puzzle.okr.SpringCachingConfig).cacheKey(#userFromToken)")
public AuthorizationUser updateOrAddAuthorizationUser(User userFromToken) {
var userFromDB = userBusinessService.getOrCreateUser(userFromToken);
var userFromDBWithTokenData = setFirstLastNameFromToken(userFromDB, userFromToken);
var userFromDBWithTokenAndPropertiesData = setOkrChampionFromProperties(userFromDBWithTokenData);
userBusinessService.saveUser(userFromDBWithTokenAndPropertiesData);
return new AuthorizationUser(userFromDBWithTokenAndPropertiesData);
User userFromDb = userBusinessService.getOrCreateUser(userFromToken);
User userFromDbWithTokenData = setFirstLastNameFromToken(userFromDb, userFromToken);
User userFromDbWithTokenAndPropertiesData = setOkrChampionFromProperties(userFromDbWithTokenData);
userBusinessService.saveUser(userFromDbWithTokenAndPropertiesData);
return new AuthorizationUser(userFromDbWithTokenAndPropertiesData);
}

// firstname and lastname comes from JWT token
private User setFirstLastNameFromToken(User userFromDB, User userFromToken) {
return helper.setFirstLastNameFromToken(userFromDB, userFromToken);
private User setFirstLastNameFromToken(User userFromDb, User userFromToken) {
return helper.setFirstLastNameFromToken(userFromDb, userFromToken);
}

// okr champion is set in application properties
Expand All @@ -59,10 +59,10 @@ public User setOkrChampionFromProperties(User user, TenantConfigProvider.TenantC
return user;
}

public User setFirstLastNameFromToken(User userFromDB, User userFromToken) {
userFromDB.setFirstname(userFromToken.getFirstname());
userFromDB.setLastname(userFromToken.getLastname());
return userFromDB;
public User setFirstLastNameFromToken(User userFromDb, User userFromToken) {
userFromDb.setFirstname(userFromToken.getFirstname());
userFromDb.setLastname(userFromToken.getLastname());
return userFromDb;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ public static boolean hasRoleWriteForTeam(AuthorizationUser authorizationUser, L
return authorizationUser.isUserMemberInTeam(teamId);
}

public static void checkRoleWriteAndReadAll(AuthorizationUser user,
public static void checkRoleWriteAndReadAll(AuthorizationUser authorizationUser,
OkrResponseStatusException notAuthorizedException) {
if (hasRoleWriteAndReadAll(user)) {
if (hasRoleWriteAndReadAll(authorizationUser)) {
return;
}
throw notAuthorizedException;
}

public static boolean hasRoleWriteAndReadAll(AuthorizationUser user) {
return user.user().isOkrChampion();
public static boolean hasRoleWriteAndReadAll(AuthorizationUser authorizationUser) {
return authorizationUser.user().isOkrChampion();
}

public AuthorizationUser updateOrAddAuthorizationUser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ch.puzzle.okr.dto.userokrdata.UserOkrDataDto;
import ch.puzzle.okr.exception.OkrResponseStatusException;
import ch.puzzle.okr.mapper.UserOkrDataMapper;
import ch.puzzle.okr.models.Team;
import ch.puzzle.okr.models.User;
import ch.puzzle.okr.models.UserTeam;
import ch.puzzle.okr.models.keyresult.KeyResult;
Expand Down Expand Up @@ -32,7 +33,7 @@ public UserAuthorizationService(UserBusinessService userBusinessService, Authori
}

public List<User> getAllUsers() {
var allUsers = userBusinessService.getAllUsers();
List<User> allUsers = userBusinessService.getAllUsers();
allUsers.forEach(this::setTeamWritableForUser);
return allUsers;
}
Expand All @@ -42,18 +43,18 @@ private void setTeamWritableForUser(User user) {
}

private void setTeamWritableForUserTeam(UserTeam userTeam) {
var team = userTeam.getTeam();
Team team = userTeam.getTeam();
team.setWriteable(teamAuthorizationService.isUserWriteAllowed(team.getId()));
}

public User getById(long id) {
var user = userBusinessService.getUserById(id);
User user = userBusinessService.getUserById(id);
setTeamWritableForUser(user);
return user;
}

public User setIsOkrChampion(long id, boolean isOkrChampion) {
var user = userBusinessService.getUserById(id);
User user = userBusinessService.getUserById(id);
AuthorizationService.checkRoleWriteAndReadAll(authorizationService.updateOrAddAuthorizationUser(),
OkrResponseStatusException.of(ErrorKey.NOT_AUTHORIZED_TO_WRITE, USER));
return userBusinessService.setIsOkrChampion(user, isOkrChampion);
Expand Down

0 comments on commit 914ba2f

Please sign in to comment.