1

replace keykloakservice with constructor

This commit is contained in:
Christoph
2020-04-07 01:42:30 +02:00
parent 9c6732d2d2
commit e13af57ab6
6 changed files with 34 additions and 56 deletions

View File

@ -4,7 +4,6 @@ import mops.gruppen2.domain.Account;
import mops.gruppen2.service.ControllerService;
import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.IdService;
import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.ProjectionService;
import mops.gruppen2.service.ValidationService;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
@ -43,7 +42,7 @@ public class GroupCreationController {
public String createGroupAsOrga(KeycloakAuthenticationToken token,
Model model) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
model.addAttribute("account", account);
model.addAttribute("lectures", projectionService.projectLectures());
@ -64,7 +63,7 @@ public class GroupCreationController {
@RequestParam(value = "parent", required = false) String parent,
@RequestParam(value = "file", required = false) MultipartFile file) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
UUID parentUUID = IdService.stringToUUID(parent);
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
@ -86,7 +85,7 @@ public class GroupCreationController {
public String createGroupAsStudent(KeycloakAuthenticationToken token,
Model model) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
model.addAttribute("account", account);
model.addAttribute("lectures", projectionService.projectLectures());
@ -105,7 +104,7 @@ public class GroupCreationController {
@RequestParam(value = "maxInfiniteUsers", required = false) Boolean maxInfiniteUsers,
@RequestParam(value = "parent", required = false) String parent) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
UUID parentUUID = IdService.stringToUUID(parent);
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);

View File

@ -7,7 +7,6 @@ import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.InviteService;
import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.ProjectionService;
import mops.gruppen2.service.ValidationService;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
@ -51,7 +50,7 @@ public class GroupDetailsController {
@PathVariable("id") String groupId) {
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
User user = new User(account);
UUID parentId = group.getParent();
String actualURL = request.getRequestURL().toString();
@ -91,7 +90,7 @@ public class GroupDetailsController {
Model model,
@PathVariable("id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
User user = new User(account);
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
@ -116,7 +115,7 @@ public class GroupDetailsController {
@RequestParam("description") String description,
@RequestParam("groupId") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
User user = new User(account);
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
@ -134,7 +133,7 @@ public class GroupDetailsController {
Model model,
@PathVariable("id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
User user = new User(account);
@ -155,7 +154,7 @@ public class GroupDetailsController {
@RequestParam("group_id") String groupId,
@RequestParam("user_id") String userId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
User principle = new User(account);
User user = new User(userId, "", "", "");
@ -180,7 +179,7 @@ public class GroupDetailsController {
@RequestParam("maximum") Long maximum,
@RequestParam("group_id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
validationService.throwIfNewMaximumIsValid(maximum, group);
@ -197,7 +196,7 @@ public class GroupDetailsController {
@RequestParam("group_id") String groupId,
@RequestParam("user_id") String userId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
User principle = new User(account);
User user = new User(userId, "", "", "");
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
@ -220,7 +219,7 @@ public class GroupDetailsController {
Model model,
@RequestParam("id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
User user = new User(account);
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
@ -240,7 +239,7 @@ public class GroupDetailsController {
public String leaveGroup(KeycloakAuthenticationToken token,
@RequestParam("group_id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
User user = new User(account);
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
@ -255,7 +254,7 @@ public class GroupDetailsController {
public String deleteGroup(KeycloakAuthenticationToken token,
@RequestParam("group_id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
User user = new User(account);
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
@ -273,7 +272,7 @@ public class GroupDetailsController {
@RequestParam("group_id") String groupId,
@RequestParam(value = "file", required = false) MultipartFile file) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
groupService.addUsersFromCsv(account, file, groupId);
return "redirect:/gruppen2/details/members/" + groupId;

View File

@ -3,7 +3,6 @@ package mops.gruppen2.controller;
import mops.gruppen2.domain.Account;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.exception.PageNotFoundException;
import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.ProjectionService;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.stereotype.Controller;
@ -33,7 +32,7 @@ public class GruppenfindungController {
public String index(KeycloakAuthenticationToken token,
Model model) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
User user = new User(account);
model.addAttribute("account", account);

View File

@ -6,7 +6,6 @@ import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.InviteService;
import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.ProjectionService;
import mops.gruppen2.service.ValidationService;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
@ -48,7 +47,7 @@ public class SearchAndInviteController {
Model model,
@RequestParam(value = "suchbegriff", required = false) String search) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
List<Group> groups = new ArrayList<>();
groups = validationService.checkSearch(search, groups, account);
@ -65,7 +64,7 @@ public class SearchAndInviteController {
Model model,
@RequestParam("id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
UUID parentId = group.getParent();
Group parent = groupService.getParent(parentId);
@ -93,7 +92,7 @@ public class SearchAndInviteController {
validationService.throwIfGroupNotExisting(group.getTitle());
model.addAttribute("account", KeyCloakService.createAccountFromPrincipal(token));
model.addAttribute("account", new Account(token));
model.addAttribute("group", group);
if (group.getVisibility() == Visibility.PUBLIC) {
@ -109,7 +108,7 @@ public class SearchAndInviteController {
public String postAcceptInvite(KeycloakAuthenticationToken token,
@RequestParam("id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Account account = new Account(token);
User user = new User(account);
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));

View File

@ -1,10 +1,14 @@
package mops.gruppen2.domain;
import lombok.AllArgsConstructor;
import lombok.Value;
import org.keycloak.KeycloakPrincipal;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import java.util.Set;
@Value
@AllArgsConstructor
public class Account {
String name; //user_id
@ -13,4 +17,14 @@ public class Account {
String givenname;
String familyname;
Set<String> roles;
public Account(KeycloakAuthenticationToken token) {
KeycloakPrincipal principal = (KeycloakPrincipal) token.getPrincipal();
name = principal.getName();
email = principal.getKeycloakSecurityContext().getIdToken().getEmail();
image = null;
givenname = principal.getKeycloakSecurityContext().getIdToken().getGivenName();
familyname = principal.getKeycloakSecurityContext().getIdToken().getFamilyName();
roles = token.getAccount().getRoles();
}
}

View File

@ -1,32 +0,0 @@
package mops.gruppen2.service;
import lombok.extern.log4j.Log4j2;
import mops.gruppen2.domain.Account;
import org.keycloak.KeycloakPrincipal;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.stereotype.Service;
@Service
@Log4j2
public final class KeyCloakService {
private KeyCloakService() {}
/**
* Creates an Account.
*
* @param token Ein toller token
*
* @return Account with current userdata
*/
public static Account createAccountFromPrincipal(KeycloakAuthenticationToken token) {
KeycloakPrincipal principal = (KeycloakPrincipal) token.getPrincipal();
return new Account(
principal.getName(),
principal.getKeycloakSecurityContext().getIdToken().getEmail(),
null,
principal.getKeycloakSecurityContext().getIdToken().getGivenName(),
principal.getKeycloakSecurityContext().getIdToken().getFamilyName(),
token.getAccount().getRoles());
}
}