From 914ba2fb1d576c20ac14e729e47d15e72a06e743 Mon Sep 17 00:00:00 2001 From: Jannik Pulfer Date: Wed, 27 Nov 2024 15:06:40 +0100 Subject: [PATCH] Refactor service/authorization --- .../AuthorizationRegistrationService.java | 22 +++++++++---------- .../authorization/AuthorizationService.java | 8 +++---- .../UserAuthorizationService.java | 9 ++++---- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/backend/src/main/java/ch/puzzle/okr/service/authorization/AuthorizationRegistrationService.java b/backend/src/main/java/ch/puzzle/okr/service/authorization/AuthorizationRegistrationService.java index fc1213b565..0ce7d9ed68 100644 --- a/backend/src/main/java/ch/puzzle/okr/service/authorization/AuthorizationRegistrationService.java +++ b/backend/src/main/java/ch/puzzle/okr/service/authorization/AuthorizationRegistrationService.java @@ -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 @@ -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; } } diff --git a/backend/src/main/java/ch/puzzle/okr/service/authorization/AuthorizationService.java b/backend/src/main/java/ch/puzzle/okr/service/authorization/AuthorizationService.java index 3ddde9734b..b316be08b5 100644 --- a/backend/src/main/java/ch/puzzle/okr/service/authorization/AuthorizationService.java +++ b/backend/src/main/java/ch/puzzle/okr/service/authorization/AuthorizationService.java @@ -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() { diff --git a/backend/src/main/java/ch/puzzle/okr/service/authorization/UserAuthorizationService.java b/backend/src/main/java/ch/puzzle/okr/service/authorization/UserAuthorizationService.java index 682a4bef94..25ff4aa3e6 100644 --- a/backend/src/main/java/ch/puzzle/okr/service/authorization/UserAuthorizationService.java +++ b/backend/src/main/java/ch/puzzle/okr/service/authorization/UserAuthorizationService.java @@ -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; @@ -32,7 +33,7 @@ public UserAuthorizationService(UserBusinessService userBusinessService, Authori } public List getAllUsers() { - var allUsers = userBusinessService.getAllUsers(); + List allUsers = userBusinessService.getAllUsers(); allUsers.forEach(this::setTeamWritableForUser); return allUsers; } @@ -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);