API-first with openapiprocessor and modelmapper
This commit is contained in:
27
src/main/java/net/hostsharing/hsadminng/Mapper.java
Normal file
27
src/main/java/net/hostsharing/hsadminng/Mapper.java
Normal file
@ -0,0 +1,27 @@
|
||||
package net.hostsharing.hsadminng;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* A nicer API for ModelMapper.
|
||||
*
|
||||
* MOst
|
||||
*/
|
||||
public class Mapper {
|
||||
private final static ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
|
||||
public static <S, T> List<T> mapList(final List<S> source, final Class<T> targetClass) {
|
||||
return source
|
||||
.stream()
|
||||
.map(element -> modelMapper.map(element, targetClass))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static <S, T> T map(final S source, final Class<T> targetClass) {
|
||||
return modelMapper.map(source, targetClass);
|
||||
}
|
||||
}
|
@ -1,16 +1,22 @@
|
||||
package net.hostsharing.hsadminng.hs.hscustomer;
|
||||
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.generated.api.v1.api.CustomersApi;
|
||||
import net.hostsharing.hsadminng.generated.api.v1.model.CustomerResource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.hostsharing.hsadminng.Mapper.map;
|
||||
import static net.hostsharing.hsadminng.Mapper.mapList;
|
||||
|
||||
@RestController
|
||||
|
||||
public class CustomerController {
|
||||
public class CustomerController implements CustomersApi {
|
||||
|
||||
@Autowired
|
||||
private Context context;
|
||||
@ -18,34 +24,40 @@ public class CustomerController {
|
||||
@Autowired
|
||||
private CustomerRepository customerRepository;
|
||||
|
||||
@GetMapping(value = "/api/customers")
|
||||
@Override
|
||||
@Transactional
|
||||
public List<CustomerEntity> listCustomers(
|
||||
@RequestHeader(value = "current-user") String userName,
|
||||
@RequestHeader(value = "assumed-roles", required = false) String assumedRoles,
|
||||
@RequestParam(required = false) String prefix
|
||||
public ResponseEntity<List<CustomerResource>> listCustomers(
|
||||
String userName,
|
||||
String assumedRoles,
|
||||
String prefix
|
||||
) {
|
||||
context.setCurrentUser(userName);
|
||||
if (assumedRoles != null && !assumedRoles.isBlank()) {
|
||||
context.assumeRoles(assumedRoles);
|
||||
}
|
||||
return customerRepository.findCustomerByOptionalPrefixLike(prefix);
|
||||
return ResponseEntity.ok(
|
||||
mapList(
|
||||
customerRepository.findCustomerByOptionalPrefixLike(prefix),
|
||||
CustomerResource.class));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/api/customers")
|
||||
@Override
|
||||
@Transactional
|
||||
public CustomerEntity addCustomer(
|
||||
@RequestHeader(value = "current-user") String userName,
|
||||
@RequestHeader(value = "assumed-roles", required = false) String assumedRoles,
|
||||
@RequestBody CustomerEntity customer
|
||||
) {
|
||||
context.setCurrentUser(userName);
|
||||
public ResponseEntity<CustomerResource> addCustomer(
|
||||
final String currentUser,
|
||||
final String assumedRoles,
|
||||
final CustomerResource customer) {
|
||||
context.setCurrentUser(currentUser);
|
||||
if (assumedRoles != null && !assumedRoles.isBlank()) {
|
||||
context.assumeRoles(assumedRoles);
|
||||
}
|
||||
if (customer.getUuid() == null) {
|
||||
customer.setUuid(UUID.randomUUID());
|
||||
}
|
||||
return customerRepository.save(customer);
|
||||
return ResponseEntity.ok(
|
||||
map(
|
||||
customerRepository.save(map(customer, CustomerEntity.class)),
|
||||
CustomerResource.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,19 @@
|
||||
package net.hostsharing.hsadminng.hs.hspackage;
|
||||
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.generated.api.v1.api.PackagesApi;
|
||||
import net.hostsharing.hsadminng.generated.api.v1.model.PackageResource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
|
||||
import static net.hostsharing.hsadminng.Mapper.mapList;
|
||||
|
||||
@RestController
|
||||
public class PackageController {
|
||||
public class PackageController implements PackagesApi {
|
||||
|
||||
@Autowired
|
||||
private Context context;
|
||||
@ -16,18 +21,19 @@ public class PackageController {
|
||||
@Autowired
|
||||
private PackageRepository packageRepository;
|
||||
|
||||
@RequestMapping(value = "/api/packages", method = RequestMethod.GET)
|
||||
@Override
|
||||
@Transactional
|
||||
public List<PackageEntity> listPackages(
|
||||
@RequestHeader(value = "current-user") String userName,
|
||||
@RequestHeader(value = "assumed-roles", required = false) String assumedRoles,
|
||||
@RequestParam(required = false) String name
|
||||
public ResponseEntity<List<PackageResource>> listPackages(
|
||||
String userName,
|
||||
String assumedRoles,
|
||||
String name
|
||||
) {
|
||||
context.setCurrentUser(userName);
|
||||
if (assumedRoles != null && !assumedRoles.isBlank()) {
|
||||
context.assumeRoles(assumedRoles);
|
||||
}
|
||||
return packageRepository.findAllByOptionalNameLike(name);
|
||||
final var result = packageRepository.findAllByOptionalNameLike(name);
|
||||
return ResponseEntity.ok(mapList(result, PackageResource.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,16 +1,20 @@
|
||||
package net.hostsharing.hsadminng.rbac.rbacrole;
|
||||
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.generated.api.v1.api.RbacrolesApi;
|
||||
import net.hostsharing.hsadminng.generated.api.v1.model.RbacRoleResource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
|
||||
import static net.hostsharing.hsadminng.Mapper.mapList;
|
||||
|
||||
@RestController
|
||||
|
||||
public class RbacRoleController {
|
||||
public class RbacRoleController implements RbacrolesApi {
|
||||
|
||||
@Autowired
|
||||
private Context context;
|
||||
@ -18,17 +22,13 @@ public class RbacRoleController {
|
||||
@Autowired
|
||||
private RbacRoleRepository rbacRoleRepository;
|
||||
|
||||
@GetMapping(value = "/api/rbacroles")
|
||||
@Override
|
||||
@Transactional
|
||||
public Iterable<RbacRoleEntity> listCustomers(
|
||||
@RequestHeader(value = "current-user") String userName,
|
||||
@RequestHeader(value = "assumed-roles", required = false) String assumedRoles
|
||||
) {
|
||||
context.setCurrentUser(userName);
|
||||
public ResponseEntity<List<RbacRoleResource>> listRoles(final String currentUser, final String assumedRoles) {
|
||||
context.setCurrentUser(currentUser);
|
||||
if (assumedRoles != null && !assumedRoles.isBlank()) {
|
||||
context.assumeRoles(assumedRoles);
|
||||
}
|
||||
return rbacRoleRepository.findAll();
|
||||
return ResponseEntity.ok(mapList(rbacRoleRepository.findAll(), RbacRoleResource.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,14 +6,20 @@ import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.generated.api.v1.api.RbacusersApi;
|
||||
import net.hostsharing.hsadminng.generated.api.v1.model.RbacUserPermissionResource;
|
||||
import net.hostsharing.hsadminng.generated.api.v1.model.RbacUserResource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
|
||||
import static net.hostsharing.hsadminng.Mapper.mapList;
|
||||
|
||||
@RestController
|
||||
public class RbacUserController {
|
||||
public class RbacUserController implements RbacusersApi {
|
||||
|
||||
@Autowired
|
||||
private Context context;
|
||||
@ -21,18 +27,9 @@ public class RbacUserController {
|
||||
@Autowired
|
||||
private RbacUserRepository rbacUserRepository;
|
||||
|
||||
@GetMapping(value = "/api/rbacusers")
|
||||
@Operation(description = "List accessible RBAC users with optional filter by name.",
|
||||
responses = {
|
||||
@ApiResponse(responseCode = "200",
|
||||
content = @Content(array = @ArraySchema(
|
||||
schema = @Schema(implementation = RbacUserEntity.class)))),
|
||||
@ApiResponse(responseCode = "401",
|
||||
description = "if the 'current-user' cannot be identified"),
|
||||
@ApiResponse(responseCode = "403",
|
||||
description = "if the 'current-user' is not allowed to assume any of the roles from 'assumed-roles'") })
|
||||
@Override
|
||||
@Transactional
|
||||
public List<RbacUserEntity> listUsers(
|
||||
public ResponseEntity<List<RbacUserResource>> listUsers(
|
||||
@RequestHeader(name = "current-user") String currentUserName,
|
||||
@RequestHeader(name = "assumed-roles", required = false) String assumedRoles,
|
||||
@RequestParam(name="name", required = false) String userName
|
||||
@ -41,19 +38,12 @@ public class RbacUserController {
|
||||
if (assumedRoles != null && !assumedRoles.isBlank()) {
|
||||
context.assumeRoles(assumedRoles);
|
||||
}
|
||||
return rbacUserRepository.findByOptionalNameLike(userName);
|
||||
return ResponseEntity.ok(mapList(rbacUserRepository.findByOptionalNameLike(userName), RbacUserResource.class));
|
||||
}
|
||||
|
||||
@GetMapping(value = "/api/rbacuser/{userName}/permissions")
|
||||
@Operation(description = "List all visible permissions granted to the given user; reduced ", responses = {
|
||||
@ApiResponse(responseCode = "200",
|
||||
content = @Content(array = @ArraySchema( schema = @Schema(implementation = RbacUserPermission.class)))),
|
||||
@ApiResponse(responseCode = "401",
|
||||
description = "if the 'current-user' cannot be identified"),
|
||||
@ApiResponse(responseCode = "403",
|
||||
description = "if the 'current-user' is not allowed to view permissions of the given user") })
|
||||
@Override
|
||||
@Transactional
|
||||
public List<RbacUserPermission> listUserPermissions(
|
||||
public ResponseEntity<List<RbacUserPermissionResource>> listUserPermissions(
|
||||
@RequestHeader(name = "current-user") String currentUserName,
|
||||
@RequestHeader(name = "assumed-roles", required = false) String assumedRoles,
|
||||
@PathVariable(name= "userName") String userName
|
||||
@ -62,6 +52,6 @@ public class RbacUserController {
|
||||
if (assumedRoles != null && !assumedRoles.isBlank()) {
|
||||
context.assumeRoles(assumedRoles);
|
||||
}
|
||||
return rbacUserRepository.findPermissionsOfUser(userName);
|
||||
return ResponseEntity.ok(mapList(rbacUserRepository.findPermissionsOfUser(userName), RbacUserPermissionResource.class));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user