Skip to content

Commit

Permalink
Fix and client authorities through client api
Browse files Browse the repository at this point in the history
  • Loading branch information
Portals committed Feb 24, 2024
1 parent 05bb240 commit 69d1aad
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 127 deletions.
6 changes: 1 addition & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ java {
apply plugin: "org.springframework.boot"
apply plugin: "io.spring.dependency-management"
group = "it.chalmers"
version = "1.0.0-SNAPSHOT"

repositories {
mavenCentral()
}
version = "2.0.0-SNAPSHOT"

dependencies {
annotationProcessor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package it.chalmers.gamma.adapter.primary.api.client;

import it.chalmers.gamma.app.client.ClientAuthorityFacade;
import it.chalmers.gamma.app.group.GroupFacade;
import it.chalmers.gamma.app.supergroup.SuperGroupFacade;
import it.chalmers.gamma.app.user.MeFacade;
import it.chalmers.gamma.app.user.UserFacade;
import java.util.List;
import java.util.UUID;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -19,17 +21,17 @@ public class ClientApiV1Controller {
private final UserFacade userFacade;
private final GroupFacade groupFacade;
private final SuperGroupFacade superGroupFacade;
private final MeFacade meFacade;
private final ClientAuthorityFacade clientAuthorityFacade;

public ClientApiV1Controller(
UserFacade userFacade,
GroupFacade groupFacade,
SuperGroupFacade superGroupFacade,
MeFacade meFacade) {
ClientAuthorityFacade clientAuthorityFacade) {
this.userFacade = userFacade;
this.groupFacade = groupFacade;
this.superGroupFacade = superGroupFacade;
this.meFacade = meFacade;
this.clientAuthorityFacade = clientAuthorityFacade;
}

@GetMapping("/groups")
Expand All @@ -46,4 +48,14 @@ public List<SuperGroupFacade.SuperGroupDTO> getSuperGroups() {
public List<UserFacade.UserDTO> getUsersForClient() {
return this.userFacade.getAllByClientAccepting();
}

@GetMapping("/authorities")
public List<String> getClientAuthorities() {
return this.clientAuthorityFacade.getClientAuthorities();
}

@GetMapping("/authorities/for/{id}")
public List<String> getClientAuthoritiesForUser(@PathVariable("id") UUID userId) {
return this.clientAuthorityFacade.getUserAuthorities(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
Expand All @@ -24,12 +23,12 @@ public GoldappsV1ApiController(GoldappsFacade goldappsFacade) {
}

@GetMapping("/supergroups")
public List<GoldappsFacade.GoldappsSuperGroupDTO> getSuperGroups(@RequestParam String types) {
return this.goldappsFacade.getActiveSuperGroups(List.of(types.split(";")));
public List<GoldappsFacade.GoldappsSuperGroupDTO> getSuperGroups() {
return this.goldappsFacade.getActiveSuperGroups();
}

@GetMapping("/users")
public List<GoldappsFacade.GoldappsUserDTO> getUsers(@RequestParam String types) {
return this.goldappsFacade.getActiveUsers(List.of(types.split(";")));
public List<GoldappsFacade.GoldappsUserDTO> getUsers() {
return this.goldappsFacade.getActiveUsers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,32 @@
import java.util.List;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface ClientAuthorityJpaRepository
extends JpaRepository<ClientAuthorityEntity, ClientAuthorityEntityPK> {
List<ClientAuthorityEntity> findAllById_Client_Id(UUID clientUid);

@Query(
value =
"""
SELECT DISTINCT a.* FROM g_client_authority a
LEFT JOIN g_client_authority_user uau ON a.client_uid = uau.client_uid AND a.authority_name = uau.authority_name
WHERE uau.user_id = :userId AND uau.client_uid = :clientUid
UNION
SELECT DISTINCT au.* FROM g_client_authority au
INNER JOIN g_client_authority_super_group sga ON au.client_uid = sga.client_uid AND au.authority_name = sga.authority_name
INNER JOIN g_super_group sg ON sg.super_group_id = sga.super_group_id
INNER JOIN g_group g ON g.super_group_id = sg.super_group_id
INNER JOIN g_membership m ON m.group_id = g.group_id
WHERE m.user_id = :userId AND au.client_uid = :clientUid
""",
nativeQuery = true)
List<ClientAuthorityEntity> findAllClientAuthoritiesByUser(
@Param("clientUid") UUID clientUid, @Param("userId") UUID userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import it.chalmers.gamma.app.user.domain.GammaUser;
import it.chalmers.gamma.app.user.domain.UserId;
import jakarta.transaction.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.EmptyResultDataAccessException;
Expand All @@ -30,19 +29,19 @@ public class ClientAuthorityRepositoryAdapter implements ClientAuthorityReposito
LoggerFactory.getLogger(ClientAuthorityRepositoryAdapter.class);
private static final PersistenceErrorState notFoundError =
new PersistenceErrorState(null, PersistenceErrorState.Type.FOREIGN_KEY_VIOLATION);
private final ClientAuthorityJpaRepository repository;
private final ClientAuthorityJpaRepository clientAuthorityJpaRepository;
private final UserJpaRepository userJpaRepository;
private final SuperGroupJpaRepository superGroupJpaRepository;
private final ClientJpaRepository clientJpaRepository;
private final ClientAuthorityEntityConverter clientAuthorityEntityConverter;

public ClientAuthorityRepositoryAdapter(
ClientAuthorityJpaRepository repository,
ClientAuthorityJpaRepository clientAuthorityJpaRepository,
UserJpaRepository userJpaRepository,
SuperGroupJpaRepository superGroupJpaRepository,
ClientJpaRepository clientJpaRepository,
ClientAuthorityEntityConverter clientAuthorityEntityConverter) {
this.repository = repository;
this.clientAuthorityJpaRepository = clientAuthorityJpaRepository;
this.userJpaRepository = userJpaRepository;
this.superGroupJpaRepository = superGroupJpaRepository;
this.clientJpaRepository = clientJpaRepository;
Expand All @@ -52,11 +51,11 @@ public ClientAuthorityRepositoryAdapter(
@Override
public void create(ClientUid clientUid, AuthorityName authorityName)
throws ClientAuthorityAlreadyExistsException {
if (repository.existsById(toAuthorityEntityPK(clientUid, authorityName))) {
if (clientAuthorityJpaRepository.existsById(toAuthorityEntityPK(clientUid, authorityName))) {
throw new ClientAuthorityAlreadyExistsException(authorityName.value());
}

repository.saveAndFlush(
clientAuthorityJpaRepository.saveAndFlush(
new ClientAuthorityEntity(
this.clientJpaRepository.findById(clientUid.value()).orElseThrow(),
authorityName.getValue()));
Expand All @@ -66,7 +65,7 @@ public void create(ClientUid clientUid, AuthorityName authorityName)
public void delete(ClientUid clientUid, AuthorityName authorityName)
throws ClientAuthorityNotFoundException {
try {
repository.deleteById(toAuthorityEntityPK(clientUid, authorityName));
clientAuthorityJpaRepository.deleteById(toAuthorityEntityPK(clientUid, authorityName));
} catch (EmptyResultDataAccessException e) {
throw new ClientAuthorityNotFoundException();
}
Expand All @@ -77,7 +76,7 @@ public void save(Authority authority) throws ClientAuthorityNotFoundRuntimeExcep
ClientAuthorityEntity entity = toEntity(authority);

try {
this.repository.saveAndFlush(entity);
this.clientAuthorityJpaRepository.saveAndFlush(entity);
} catch (Exception e) {
PersistenceErrorState state = PersistenceErrorHelper.getState(e);

Expand All @@ -91,60 +90,33 @@ public void save(Authority authority) throws ClientAuthorityNotFoundRuntimeExcep

@Override
public List<Authority> getAllByClient(ClientUid clientUid) {
return this.repository.findAllById_Client_Id(clientUid.value()).stream()
return this.clientAuthorityJpaRepository.findAllById_Client_Id(clientUid.value()).stream()
.map(this.clientAuthorityEntityConverter::toDomain)
.toList();
}

@Override
public List<Authority> getAllByUser(ClientUid clientUid, UserId userId) {

throw new UnsupportedOperationException();
// Set<UserAuthority> names = new HashSet<>();
//
//
// this.authorityUserRepository.findAllById_UserEntity_Id(userId.value()).forEach(authorityUserEntity -> names.add(new UserAuthority(authorityUserEntity.getId().getValue().authorityName(), AuthorityType.AUTHORITY)));
//
// Set<SuperGroup> userSuperGroups = new HashSet<>();
//
//
// this.membershipJpaRepository.findAllById_User_Id(userId.value()).forEach(membershipEntity ->
// {
// names.add(new UserAuthority(new
// AuthorityName(membershipEntity.getId().getGroup().getName()), AuthorityType.GROUP));
//
// SuperGroupEntity superGroupEntity =
// membershipEntity.getId().getGroup().getSuperGroup();
// userSuperGroups.add(this.superGroupEntityConverter.toDomain(superGroupEntity));
//
// PostEntity postEntity = membershipEntity.getId().getPost();
//
//
// this.authorityPostRepository.findAllById_SuperGroupEntity_Id_AndId_PostEntity_Id(superGroupEntity.getId(), postEntity.getId()).forEach(authorityPostEntity -> names.add(new UserAuthority(authorityPostEntity.getId().getValue().authorityName(), AuthorityType.AUTHORITY)));
// });
//
// userSuperGroups.forEach(superGroup -> names.add(new UserAuthority(new
// AuthorityName(superGroup.name().value()), AuthorityType.SUPERGROUP)));
//
// userSuperGroups.forEach(superGroupId ->
// names.addAll(this.authoritySuperGroupRepository.findAllById_SuperGroupEntity_Id(superGroupId.id().value()).stream().map(AuthoritySuperGroupEntity::getId).map(AuthoritySuperGroupPK::getValue).map(AuthoritySuperGroupPK.AuthoritySuperGroupPKDTO::authorityName).map(clientAuthority -> new UserAuthority(clientAuthority, AuthorityType.AUTHORITY)).toList()));
//
// return new ArrayList<>(names);
public List<AuthorityName> getAllByUser(ClientUid clientUid, UserId userId) {
return this.clientAuthorityJpaRepository
.findAllClientAuthoritiesByUser(clientUid.value(), userId.value())
.stream()
.map(ClientAuthorityEntity::getId)
.map(ClientAuthorityEntityPK::getValue)
.map(ClientAuthorityEntityPK.AuthorityEntityPKRecord::authorityName)
.toList();
}

@Override
public Optional<Authority> get(ClientUid clientUid, AuthorityName authorityName) {
return this.repository
return this.clientAuthorityJpaRepository
.findById(toAuthorityEntityPK(clientUid, authorityName))
.map(this.clientAuthorityEntityConverter::toDomain);
}

private ClientAuthorityEntity toEntity(Authority authority)
throws ClientAuthorityNotFoundRuntimeException {
String name = authority.name().getValue();

ClientAuthorityEntity clientAuthorityEntity =
this.repository
this.clientAuthorityJpaRepository
.findById(
new ClientAuthorityEntityPK(
this.clientJpaRepository
Expand Down Expand Up @@ -175,11 +147,11 @@ private ClientAuthorityEntity toEntity(Authority authority)
}

private UserEntity toEntity(GammaUser user) {
return this.userJpaRepository.getById(user.id().getValue());
return this.userJpaRepository.getReferenceById(user.id().getValue());
}

private SuperGroupEntity toEntity(SuperGroup superGroup) {
return this.superGroupJpaRepository.getById(superGroup.id().getValue());
return this.superGroupJpaRepository.getReferenceById(superGroup.id().getValue());
}

private ClientAuthorityEntityPK toAuthorityEntityPK(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class GroupRepositoryAdapter implements GroupRepository {

private static final PersistenceErrorState SUPER_GROUP_NOT_FOUND =
new PersistenceErrorState(
"fkit_group_super_group_id_fkey", PersistenceErrorState.Type.FOREIGN_KEY_VIOLATION);
"g_group_super_group_id_fkey", PersistenceErrorState.Type.FOREIGN_KEY_VIOLATION);
private static final PersistenceErrorState GROUP_NAME_ALREADY_EXISTS =
new PersistenceErrorState("fkit_group_e_name_key", PersistenceErrorState.Type.NOT_UNIQUE);
new PersistenceErrorState("g_group_e_name_key", PersistenceErrorState.Type.NOT_UNIQUE);
private static final PersistenceErrorState USER_NOT_FOUND =
new PersistenceErrorState(
"membership_user_id_fkey", PersistenceErrorState.Type.FOREIGN_KEY_VIOLATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@ public interface MembershipJpaRepository extends JpaRepository<MembershipEntity,
List<MembershipEntity> findAllById_Post_Id(UUID postId);

List<MembershipEntity> findAllById_User_Id(UUID userId);

List<MembershipEntity> findAllById_Group_Id(UUID groupId);

List<MembershipEntity> findAllById_GroupIdAndId_PostId(UUID groupId, UUID postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

@Repository
public interface SuperGroupJpaRepository extends JpaRepository<SuperGroupEntity, UUID> {

List<SuperGroupEntity> findAllBySuperGroupType_Name(String type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ public class SuperGroupRepositoryAdapter implements SuperGroupRepository {

private static final PersistenceErrorState typeNotFound =
new PersistenceErrorState(
"fkit_super_group_super_group_type_name_fkey",
"g_super_group_super_group_type_name_fkey",
PersistenceErrorState.Type.FOREIGN_KEY_VIOLATION);
private static final PersistenceErrorState nameAlreadyExists =
new PersistenceErrorState(
"fkit_super_group_e_name_key", PersistenceErrorState.Type.NOT_UNIQUE);
new PersistenceErrorState("g_super_group_e_name_key", PersistenceErrorState.Type.NOT_UNIQUE);
private static final PersistenceErrorState superGroupIsUsed =
new PersistenceErrorState(
"fkit_group_super_group_id_fkey", PersistenceErrorState.Type.FOREIGN_KEY_VIOLATION);
"g_group_super_group_id_fkey", PersistenceErrorState.Type.FOREIGN_KEY_VIOLATION);
private final SuperGroupJpaRepository repository;
private final SuperGroupTypeJpaRepository superGroupTypeJpaRepository;
private final SuperGroupEntityConverter superGroupEntityConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import it.chalmers.gamma.app.Facade;
import it.chalmers.gamma.app.authentication.AccessGuard;
import it.chalmers.gamma.app.client.domain.Client;
import it.chalmers.gamma.app.client.domain.ClientUid;
import it.chalmers.gamma.app.client.domain.authority.Authority;
import it.chalmers.gamma.app.client.domain.authority.AuthorityName;
Expand All @@ -16,6 +17,8 @@
import it.chalmers.gamma.app.user.domain.GammaUser;
import it.chalmers.gamma.app.user.domain.UserId;
import it.chalmers.gamma.app.user.domain.UserRepository;
import it.chalmers.gamma.security.authentication.ApiAuthentication;
import it.chalmers.gamma.security.authentication.AuthenticationExtractor;
import jakarta.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -175,6 +178,39 @@ public void removeUserFromClientAuthority(UUID clientUid, String name, UUID user
this.clientAuthorityRepository.save(authority.withUsers(newUsers));
}

public List<String> getClientAuthorities() {
this.accessGuard.require(isClientApi());

if (AuthenticationExtractor.getAuthentication()
instanceof ApiAuthentication apiAuthentication) {
Client client = apiAuthentication.getClient().orElseThrow();

return this.clientAuthorityRepository.getAllByClient(client.clientUid()).stream()
.map(Authority::name)
.map(AuthorityName::value)
.toList();
}

throw new RuntimeException();
}

public List<String> getUserAuthorities(UUID userId) {
this.accessGuard.require(isClientApi());

if (AuthenticationExtractor.getAuthentication()
instanceof ApiAuthentication apiAuthentication) {
Client client = apiAuthentication.getClient().orElseThrow();

return this.clientAuthorityRepository
.getAllByUser(client.clientUid(), new UserId(userId))
.stream()
.map(AuthorityName::value)
.toList();
}

throw new RuntimeException();
}

public record ClientAuthorityDTO(
UUID clientUid,
String authorityName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void save(Authority authority)

List<Authority> getAllByClient(ClientUid clientUid);

List<Authority> getAllByUser(ClientUid clientUid, UserId userId);
List<AuthorityName> getAllByUser(ClientUid clientUid, UserId userId);

Optional<Authority> get(ClientUid clientUid, AuthorityName authorityName);

Expand Down
Loading

0 comments on commit 69d1aad

Please sign in to comment.