Skip to content

Commit

Permalink
Validate tenant creation request data
Browse files Browse the repository at this point in the history
Fix tenant already exists exception not being thrown
  • Loading branch information
alexivanov committed Mar 28, 2017
1 parent fe1770e commit f527679
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 10 deletions.
Empty file modified gradlew
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private SecurityException(final String msg) {
super(msg);
}

public static SecurityException tenantAlreadyExisits(final String tenant) {
public static SecurityException tenantAlreadyExists(final String tenant) {
return new SecurityException("Tenant Already existing with "+tenant+" identifier");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public String generateApiKey(final SMSBridge smsBridge) {
public String generateApiKey(final String tenantId) {
Tenant tenant = this.tenantRepository.findByTenantId(tenantId) ;
if(tenant != null) {
org.fineract.messagegateway.exception.SecurityException.tenantAlreadyExisits(tenantId) ;
throw org.fineract.messagegateway.exception.SecurityException.tenantAlreadyExists(tenantId) ;
}

final String randomKey = UUID.randomUUID().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
*/
package org.fineract.messagegateway.tenants.api;

import org.fineract.messagegateway.tenants.domain.Tenant;
import org.fineract.messagegateway.exception.PlatformApiDataValidationException;
import org.fineract.messagegateway.exception.UnsupportedParameterException;
import org.fineract.messagegateway.helpers.ApiGlobalErrorResponse;
import org.fineract.messagegateway.helpers.PlatformApiDataValidationExceptionMapper;
import org.fineract.messagegateway.helpers.UnsupportedParameterExceptionMapper;
import org.fineract.messagegateway.tenants.service.TenantsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -40,8 +45,18 @@ public TenantsApiResource(final TenantsService tenantService) {
}

@RequestMapping(method = RequestMethod.POST, consumes = {"application/json"}, produces = {"application/json"})
public ResponseEntity<String> createSMSBridgeConfig(@RequestBody final Tenant tenant) {
String appKey = this.tenantService.createTenant(tenant) ;
return new ResponseEntity<>(appKey, HttpStatus.CREATED);
public ResponseEntity<String> createSMSBridgeConfig(@RequestBody final String requestJson) {
String appKey = this.tenantService.createTenant(requestJson) ;
return new ResponseEntity<>(appKey, HttpStatus.CREATED);
}

@ExceptionHandler({PlatformApiDataValidationException.class})
public ResponseEntity<ApiGlobalErrorResponse> handlePlatformApiDataValidationException(PlatformApiDataValidationException e) {
return PlatformApiDataValidationExceptionMapper.toResponse(e) ;
}

@ExceptionHandler({UnsupportedParameterException.class})
public ResponseEntity<ApiGlobalErrorResponse> handleUnsupportedParameterException(UnsupportedParameterException e) {
return UnsupportedParameterExceptionMapper.toResponse(e) ;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.fineract.messagegateway.tenants.constants;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.fineract.messagegateway.constants.MessageGatewayConstants;

public interface TenantConstants extends MessageGatewayConstants {

String TENANTS_RESOURCE_NAME = "tenants";

String TENANT_ID = "tenantId";
String TENANT_DESCRIPTION = "description";


// list of allowed parameters for tenant creation request
Set<String> CREATE_REQUEST_PARAMETERS = new HashSet<>(Arrays.asList(TENANT_ID, TENANT_DESCRIPTION));

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.fineract.messagegateway.tenants.serialization;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.fineract.messagegateway.exception.PlatformApiDataValidationException;
import org.fineract.messagegateway.helpers.ApiParameterError;
import org.fineract.messagegateway.helpers.DataValidatorBuilder;
import org.fineract.messagegateway.helpers.FromJsonHelper;
import org.fineract.messagegateway.tenants.constants.TenantConstants;
import org.fineract.messagegateway.tenants.domain.Tenant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

@Component
public class TenantSerializer {

private final FromJsonHelper fromJsonHelper;

@Autowired
public TenantSerializer(FromJsonHelper fromJsonHelper) {
this.fromJsonHelper = fromJsonHelper;
}

public Tenant validateCreateRequest(final String json) {
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
this.fromJsonHelper.checkForUnsupportedParameters(typeOfMap, json,
TenantConstants.CREATE_REQUEST_PARAMETERS);

final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
.resource(TenantConstants.TENANTS_RESOURCE_NAME);
final JsonElement element = this.fromJsonHelper.parse(json);


final String tenantId = this.fromJsonHelper.extractStringNamed(TenantConstants.TENANT_ID, element);
baseDataValidator.reset().parameter(TenantConstants.TENANT_ID)
.value(tenantId).notBlank().notExceedingLengthOf(32);

String tenantDescription = null;
if(this.fromJsonHelper.parameterExists(TenantConstants.TENANT_DESCRIPTION, element)) {
tenantDescription = this.fromJsonHelper.extractStringNamed(TenantConstants.TENANT_DESCRIPTION,
element);
baseDataValidator.reset().parameter(TenantConstants.TENANT_DESCRIPTION).value(tenantDescription)
.notBlank().notExceedingLengthOf(500);
}

if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors);
}

return new Tenant(tenantId, null, tenantDescription);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.fineract.messagegateway.tenants.domain.Tenant;
import org.fineract.messagegateway.tenants.exception.TenantNotFoundException;
import org.fineract.messagegateway.tenants.repository.TenantRepository;
import org.fineract.messagegateway.tenants.serialization.TenantSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -31,15 +32,19 @@ public class TenantsService {
private final TenantRepository tenantRepository ;

private final SecurityService securityService ;


private final TenantSerializer tenantSerializer;

@Autowired
public TenantsService(final TenantRepository tenantRepository,
final SecurityService securityService) {
final SecurityService securityService, TenantSerializer tenantSerializer) {
this.tenantRepository = tenantRepository ;
this.securityService = securityService ;
this.tenantSerializer = tenantSerializer;
}

public String createTenant(final Tenant tenant) {

public String createTenant(final String requestJson) {
Tenant tenant = tenantSerializer.validateCreateRequest(requestJson);
tenant.setTenantAppKey(this.securityService.generateApiKey(tenant.getTenantId()));
this.tenantRepository.save(tenant) ;
return tenant.getTenantAppKey() ;
Expand Down

0 comments on commit f527679

Please sign in to comment.