<commit_before>package net.whydah.crmservice.customer;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import net.whydah.crmservice.security.Authentication;
import net.whydah.sso.extensions.crmcustomer.types.Customer;
import net.whydah.sso.user.types.UserToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ratpack.exec.Blocking;
import ratpack.handling.Context;
import ratpack.handling.Handler;
import ratpack.handling.internal.UuidBasedRequestIdGenerator;

import java.sql.SQLIntegrityConstraintViolationException;

import static ratpack.jackson.Jackson.fromJson;

@Singleton
public class CreateCustomerHandler implements Handler {


    private static final Logger log = LoggerFactory.getLogger(CreateCustomerHandler.class);

    private final CustomerRepository customerRepository;

    @Inject
    public CreateCustomerHandler(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    @Override
    public void handle(Context ctx) throws Exception {

        boolean customerRefIsGenerated;
        String customerRef;

        if (ctx.getPathTokens().get("customerRef") != null) {
            customerRef = ctx.getPathTokens().get("customerRef");
            customerRefIsGenerated = false;
        } else {
            customerRef = UuidBasedRequestIdGenerator.INSTANCE.generate(ctx.getRequest()).toString();
            customerRefIsGenerated = true;
        }

        log.trace("Creating customer with ref={}", customerRef);

        ctx.parse(fromJson(Customer.class)).then(customer -> {
            Blocking.op(() -> {

                // TODO  fix this to verify against a sensible UserRole
                if ("useradmin".equalsIgnoreCase(Authentication.getAuthenticatedUser().getUid().toString())) {
                    customerRepository.createCustomer(customerRef, customer);
                } else {
                    if (customerRefIsGenerated) {
                        //Verify userId
                        if (customer.getId() == null || !customer.getId().equals(Authentication.getAuthenticatedUser().getUid())) {
                            log.debug("User {} not authorized to create data for uid {}", Authentication.getAuthenticatedUser().getUid(), customer.getId());
                            ctx.clientError(401);
                        }
                    } else {
                        //Verify customerRef
                        if (customerRef == null || !customerRef.equals(Authentication.getAuthenticatedUser().getPersonRef())) {
                            log.debug("User {} with personRef {} not authorized to create data for personRef {}", Authentication.getAuthenticatedUser().getUid(), Authentication.getAuthenticatedUser().getPersonRef(), customerRef);
                            ctx.clientError(401);
                        }
                    }

                    customerRepository.createCustomer(customerRef, customer);
                }
            }).onError(throwable -> {
                if (throwable instanceof SQLIntegrityConstraintViolationException) {
                    ctx.clientError(400); //Bad request
                } else {
                    throw new RuntimeException(throwable);
                }
            }).then(() -> {
                ctx.redirect(201, customerRef); //Created
            });
        });
    }
}<commit_msg>Hopefully this tick will fix the id/personRef chaos<commit_after>package net.whydah.crmservice.customer;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import net.whydah.crmservice.security.Authentication;
import net.whydah.sso.extensions.crmcustomer.types.Customer;
import net.whydah.sso.user.types.UserToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ratpack.exec.Blocking;
import ratpack.handling.Context;
import ratpack.handling.Handler;
import ratpack.handling.internal.UuidBasedRequestIdGenerator;

import java.sql.SQLIntegrityConstraintViolationException;

import static ratpack.jackson.Jackson.fromJson;

@Singleton
public class CreateCustomerHandler implements Handler {


    private static final Logger log = LoggerFactory.getLogger(CreateCustomerHandler.class);

    private final CustomerRepository customerRepository;

    @Inject
    public CreateCustomerHandler(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    @Override
    public void handle(Context ctx) throws Exception {

        boolean customerRefIsGenerated;
        String customerRef;

        if (ctx.getPathTokens().get("customerRef") != null) {
            customerRef = ctx.getPathTokens().get("customerRef");
            customerRefIsGenerated = false;
        } else {
            customerRef = UuidBasedRequestIdGenerator.INSTANCE.generate(ctx.getRequest()).toString();
            customerRefIsGenerated = true;
        }

        log.trace("Creating customer with ref={}", customerRef);

        ctx.parse(fromJson(Customer.class)).then(customer -> {
            Blocking.op(() -> {

                // TODO  fix this to verify against a sensible UserRole
                if ("useradmin".equalsIgnoreCase(Authentication.getAuthenticatedUser().getUid().toString())) {
                    customerRepository.createCustomer(getCorrectID(customerRef, customer), customer);
                } else {
                    if (customerRefIsGenerated) {
                        //Verify userId
                        if (customer.getId() == null || !customer.getId().equals(Authentication.getAuthenticatedUser().getUid())) {
                            log.debug("User {} not authorized to create data for uid {}", Authentication.getAuthenticatedUser().getUid(), customer.getId());
                            ctx.clientError(401);
                        }
                    } else {
                        //Verify customerRef
                        if (customerRef == null || !customerRef.equals(Authentication.getAuthenticatedUser().getPersonRef())) {
                            log.debug("User {} with personRef {} not authorized to create data for personRef {}", Authentication.getAuthenticatedUser().getUid(), Authentication.getAuthenticatedUser().getPersonRef(), customerRef);
                            ctx.clientError(401);
                        }
                    }

                    customerRepository.createCustomer(getCorrectID(customerRef, customer), customer);
                }
            }).onError(throwable -> {
                if (throwable instanceof SQLIntegrityConstraintViolationException) {
                    ctx.clientError(400); //Bad request
                } else {
                    throw new RuntimeException(throwable);
                }
            }).then(() -> {
                ctx.redirect(201, customerRef); //Created
            });
        });
    }

    private String getCorrectID(String customerRef, Customer customer) {
        if (customer.getId() == null || customer.getId().length() < 5) {
            return customerRef;
        }
        return customer.getId();

    }
}
{'fraction_non_alphanumeric': 0.09984552731357955, 'fraction_numerical': 0.004072461732902682, 'mean_word_length': 2.2744827586206897, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1920035', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1677, 'n_words': 330}