replace keykloakservice with constructor
This commit is contained in:
@ -4,7 +4,6 @@ import mops.gruppen2.domain.Account;
|
|||||||
import mops.gruppen2.service.ControllerService;
|
import mops.gruppen2.service.ControllerService;
|
||||||
import mops.gruppen2.service.GroupService;
|
import mops.gruppen2.service.GroupService;
|
||||||
import mops.gruppen2.service.IdService;
|
import mops.gruppen2.service.IdService;
|
||||||
import mops.gruppen2.service.KeyCloakService;
|
|
||||||
import mops.gruppen2.service.ProjectionService;
|
import mops.gruppen2.service.ProjectionService;
|
||||||
import mops.gruppen2.service.ValidationService;
|
import mops.gruppen2.service.ValidationService;
|
||||||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||||
@ -43,7 +42,7 @@ public class GroupCreationController {
|
|||||||
public String createGroupAsOrga(KeycloakAuthenticationToken token,
|
public String createGroupAsOrga(KeycloakAuthenticationToken token,
|
||||||
Model model) {
|
Model model) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
|
|
||||||
model.addAttribute("account", account);
|
model.addAttribute("account", account);
|
||||||
model.addAttribute("lectures", projectionService.projectLectures());
|
model.addAttribute("lectures", projectionService.projectLectures());
|
||||||
@ -64,7 +63,7 @@ public class GroupCreationController {
|
|||||||
@RequestParam(value = "parent", required = false) String parent,
|
@RequestParam(value = "parent", required = false) String parent,
|
||||||
@RequestParam(value = "file", required = false) MultipartFile file) {
|
@RequestParam(value = "file", required = false) MultipartFile file) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
UUID parentUUID = IdService.stringToUUID(parent);
|
UUID parentUUID = IdService.stringToUUID(parent);
|
||||||
|
|
||||||
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
|
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
|
||||||
@ -86,7 +85,7 @@ public class GroupCreationController {
|
|||||||
public String createGroupAsStudent(KeycloakAuthenticationToken token,
|
public String createGroupAsStudent(KeycloakAuthenticationToken token,
|
||||||
Model model) {
|
Model model) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
|
|
||||||
model.addAttribute("account", account);
|
model.addAttribute("account", account);
|
||||||
model.addAttribute("lectures", projectionService.projectLectures());
|
model.addAttribute("lectures", projectionService.projectLectures());
|
||||||
@ -105,7 +104,7 @@ public class GroupCreationController {
|
|||||||
@RequestParam(value = "maxInfiniteUsers", required = false) Boolean maxInfiniteUsers,
|
@RequestParam(value = "maxInfiniteUsers", required = false) Boolean maxInfiniteUsers,
|
||||||
@RequestParam(value = "parent", required = false) String parent) {
|
@RequestParam(value = "parent", required = false) String parent) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
UUID parentUUID = IdService.stringToUUID(parent);
|
UUID parentUUID = IdService.stringToUUID(parent);
|
||||||
|
|
||||||
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
|
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
|
||||||
|
|||||||
@ -7,7 +7,6 @@ import mops.gruppen2.domain.User;
|
|||||||
import mops.gruppen2.domain.Visibility;
|
import mops.gruppen2.domain.Visibility;
|
||||||
import mops.gruppen2.service.GroupService;
|
import mops.gruppen2.service.GroupService;
|
||||||
import mops.gruppen2.service.InviteService;
|
import mops.gruppen2.service.InviteService;
|
||||||
import mops.gruppen2.service.KeyCloakService;
|
|
||||||
import mops.gruppen2.service.ProjectionService;
|
import mops.gruppen2.service.ProjectionService;
|
||||||
import mops.gruppen2.service.ValidationService;
|
import mops.gruppen2.service.ValidationService;
|
||||||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||||
@ -51,7 +50,7 @@ public class GroupDetailsController {
|
|||||||
@PathVariable("id") String groupId) {
|
@PathVariable("id") String groupId) {
|
||||||
|
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
User user = new User(account);
|
User user = new User(account);
|
||||||
UUID parentId = group.getParent();
|
UUID parentId = group.getParent();
|
||||||
String actualURL = request.getRequestURL().toString();
|
String actualURL = request.getRequestURL().toString();
|
||||||
@ -91,7 +90,7 @@ public class GroupDetailsController {
|
|||||||
Model model,
|
Model model,
|
||||||
@PathVariable("id") String groupId) {
|
@PathVariable("id") String groupId) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
User user = new User(account);
|
User user = new User(account);
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
|
|
||||||
@ -116,7 +115,7 @@ public class GroupDetailsController {
|
|||||||
@RequestParam("description") String description,
|
@RequestParam("description") String description,
|
||||||
@RequestParam("groupId") String groupId) {
|
@RequestParam("groupId") String groupId) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
User user = new User(account);
|
User user = new User(account);
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
|
|
||||||
@ -134,7 +133,7 @@ public class GroupDetailsController {
|
|||||||
Model model,
|
Model model,
|
||||||
@PathVariable("id") String groupId) {
|
@PathVariable("id") String groupId) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
User user = new User(account);
|
User user = new User(account);
|
||||||
|
|
||||||
@ -155,7 +154,7 @@ public class GroupDetailsController {
|
|||||||
@RequestParam("group_id") String groupId,
|
@RequestParam("group_id") String groupId,
|
||||||
@RequestParam("user_id") String userId) {
|
@RequestParam("user_id") String userId) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
User principle = new User(account);
|
User principle = new User(account);
|
||||||
User user = new User(userId, "", "", "");
|
User user = new User(userId, "", "", "");
|
||||||
@ -180,7 +179,7 @@ public class GroupDetailsController {
|
|||||||
@RequestParam("maximum") Long maximum,
|
@RequestParam("maximum") Long maximum,
|
||||||
@RequestParam("group_id") String groupId) {
|
@RequestParam("group_id") String groupId) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
|
|
||||||
validationService.throwIfNewMaximumIsValid(maximum, group);
|
validationService.throwIfNewMaximumIsValid(maximum, group);
|
||||||
@ -197,7 +196,7 @@ public class GroupDetailsController {
|
|||||||
@RequestParam("group_id") String groupId,
|
@RequestParam("group_id") String groupId,
|
||||||
@RequestParam("user_id") String userId) {
|
@RequestParam("user_id") String userId) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
User principle = new User(account);
|
User principle = new User(account);
|
||||||
User user = new User(userId, "", "", "");
|
User user = new User(userId, "", "", "");
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
@ -220,7 +219,7 @@ public class GroupDetailsController {
|
|||||||
Model model,
|
Model model,
|
||||||
@RequestParam("id") String groupId) {
|
@RequestParam("id") String groupId) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
User user = new User(account);
|
User user = new User(account);
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
|
|
||||||
@ -240,7 +239,7 @@ public class GroupDetailsController {
|
|||||||
public String leaveGroup(KeycloakAuthenticationToken token,
|
public String leaveGroup(KeycloakAuthenticationToken token,
|
||||||
@RequestParam("group_id") String groupId) {
|
@RequestParam("group_id") String groupId) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
User user = new User(account);
|
User user = new User(account);
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
|
|
||||||
@ -255,7 +254,7 @@ public class GroupDetailsController {
|
|||||||
public String deleteGroup(KeycloakAuthenticationToken token,
|
public String deleteGroup(KeycloakAuthenticationToken token,
|
||||||
@RequestParam("group_id") String groupId) {
|
@RequestParam("group_id") String groupId) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
User user = new User(account);
|
User user = new User(account);
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
|
|
||||||
@ -273,7 +272,7 @@ public class GroupDetailsController {
|
|||||||
@RequestParam("group_id") String groupId,
|
@RequestParam("group_id") String groupId,
|
||||||
@RequestParam(value = "file", required = false) MultipartFile file) {
|
@RequestParam(value = "file", required = false) MultipartFile file) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
groupService.addUsersFromCsv(account, file, groupId);
|
groupService.addUsersFromCsv(account, file, groupId);
|
||||||
|
|
||||||
return "redirect:/gruppen2/details/members/" + groupId;
|
return "redirect:/gruppen2/details/members/" + groupId;
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package mops.gruppen2.controller;
|
|||||||
import mops.gruppen2.domain.Account;
|
import mops.gruppen2.domain.Account;
|
||||||
import mops.gruppen2.domain.User;
|
import mops.gruppen2.domain.User;
|
||||||
import mops.gruppen2.domain.exception.PageNotFoundException;
|
import mops.gruppen2.domain.exception.PageNotFoundException;
|
||||||
import mops.gruppen2.service.KeyCloakService;
|
|
||||||
import mops.gruppen2.service.ProjectionService;
|
import mops.gruppen2.service.ProjectionService;
|
||||||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
@ -33,7 +32,7 @@ public class GruppenfindungController {
|
|||||||
public String index(KeycloakAuthenticationToken token,
|
public String index(KeycloakAuthenticationToken token,
|
||||||
Model model) {
|
Model model) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
User user = new User(account);
|
User user = new User(account);
|
||||||
|
|
||||||
model.addAttribute("account", account);
|
model.addAttribute("account", account);
|
||||||
|
|||||||
@ -6,7 +6,6 @@ import mops.gruppen2.domain.User;
|
|||||||
import mops.gruppen2.domain.Visibility;
|
import mops.gruppen2.domain.Visibility;
|
||||||
import mops.gruppen2.service.GroupService;
|
import mops.gruppen2.service.GroupService;
|
||||||
import mops.gruppen2.service.InviteService;
|
import mops.gruppen2.service.InviteService;
|
||||||
import mops.gruppen2.service.KeyCloakService;
|
|
||||||
import mops.gruppen2.service.ProjectionService;
|
import mops.gruppen2.service.ProjectionService;
|
||||||
import mops.gruppen2.service.ValidationService;
|
import mops.gruppen2.service.ValidationService;
|
||||||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||||
@ -48,7 +47,7 @@ public class SearchAndInviteController {
|
|||||||
Model model,
|
Model model,
|
||||||
@RequestParam(value = "suchbegriff", required = false) String search) {
|
@RequestParam(value = "suchbegriff", required = false) String search) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
List<Group> groups = new ArrayList<>();
|
List<Group> groups = new ArrayList<>();
|
||||||
groups = validationService.checkSearch(search, groups, account);
|
groups = validationService.checkSearch(search, groups, account);
|
||||||
|
|
||||||
@ -65,7 +64,7 @@ public class SearchAndInviteController {
|
|||||||
Model model,
|
Model model,
|
||||||
@RequestParam("id") String groupId) {
|
@RequestParam("id") String groupId) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
UUID parentId = group.getParent();
|
UUID parentId = group.getParent();
|
||||||
Group parent = groupService.getParent(parentId);
|
Group parent = groupService.getParent(parentId);
|
||||||
@ -93,7 +92,7 @@ public class SearchAndInviteController {
|
|||||||
|
|
||||||
validationService.throwIfGroupNotExisting(group.getTitle());
|
validationService.throwIfGroupNotExisting(group.getTitle());
|
||||||
|
|
||||||
model.addAttribute("account", KeyCloakService.createAccountFromPrincipal(token));
|
model.addAttribute("account", new Account(token));
|
||||||
model.addAttribute("group", group);
|
model.addAttribute("group", group);
|
||||||
|
|
||||||
if (group.getVisibility() == Visibility.PUBLIC) {
|
if (group.getVisibility() == Visibility.PUBLIC) {
|
||||||
@ -109,7 +108,7 @@ public class SearchAndInviteController {
|
|||||||
public String postAcceptInvite(KeycloakAuthenticationToken token,
|
public String postAcceptInvite(KeycloakAuthenticationToken token,
|
||||||
@RequestParam("id") String groupId) {
|
@RequestParam("id") String groupId) {
|
||||||
|
|
||||||
Account account = KeyCloakService.createAccountFromPrincipal(token);
|
Account account = new Account(token);
|
||||||
User user = new User(account);
|
User user = new User(account);
|
||||||
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,14 @@
|
|||||||
package mops.gruppen2.domain;
|
package mops.gruppen2.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Value;
|
import lombok.Value;
|
||||||
|
import org.keycloak.KeycloakPrincipal;
|
||||||
|
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@Value
|
@Value
|
||||||
|
@AllArgsConstructor
|
||||||
public class Account {
|
public class Account {
|
||||||
|
|
||||||
String name; //user_id
|
String name; //user_id
|
||||||
@ -13,4 +17,14 @@ public class Account {
|
|||||||
String givenname;
|
String givenname;
|
||||||
String familyname;
|
String familyname;
|
||||||
Set<String> roles;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user