bugfix in pureIdentifier + findUuidByIdName and CustomerEntity+Repository

This commit is contained in:
Michael Hoennig 2022-07-29 15:53:20 +02:00
parent 5126514877
commit 46957dc590
6 changed files with 84 additions and 41 deletions

View File

@ -0,0 +1,16 @@
package net.hostsharing.hsadminng;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@ResponseBody
@RequestMapping(value = "/api/ping", method = RequestMethod.GET)
public String ping() {
return "pong\n";
}
}

View File

@ -1,38 +0,0 @@
package net.hostsharing.hsadminng.controller;
import net.hostsharing.hsadminng.context.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
@Controller
public class TestController {
@PersistenceContext
private EntityManager em;
@Autowired
private Context context;
@ResponseBody
@RequestMapping(value = "/api/ping", method = RequestMethod.GET)
public String ping() {
return "pong\n";
}
@Transactional
@ResponseBody
@RequestMapping(value = "/api/currentUser", method = RequestMethod.GET)
public String currentUser() {
context.setCurrentUser("mike@hostsharing.net");
final var query = em.createNativeQuery("select currentUser()");
return query.getSingleResult() + "\n";
}
}

View File

@ -0,0 +1,37 @@
package net.hostsharing.hsadminng.customer;
import net.hostsharing.hsadminng.context.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.transaction.Transactional;
import java.util.List;
@Controller
public class CustomerController {
@Autowired
private Context context;
@Autowired
private CustomerRepository customerRepository;
@ResponseBody
@RequestMapping(value = "/api/customer", method = RequestMethod.GET)
@Transactional
public List<CustomerEntity> listCustomers(
@RequestHeader(value = "current-user") String userName,
@RequestHeader(value="assumed-roles", required=false) String assumedRoles
) {
context.setCurrentUser(userName);
if ( assumedRoles != null && !assumedRoles.isBlank() ) {
context.assumeRoles(assumedRoles);
}
return customerRepository.findAll();
}
}

View File

@ -0,0 +1,16 @@
package net.hostsharing.hsadminng.customer;
import lombok.Getter;
import javax.persistence.*;
import java.util.UUID;
@Entity
@Table(name = "customer_rv")
@Getter
public class CustomerEntity {
private @Id UUID uuid;
private String prefix;
private int reference;
private @Column(name="adminusername")String adminUserName;
}

View File

@ -0,0 +1,9 @@
package net.hostsharing.hsadminng.customer;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.UUID;
public interface CustomerRepository extends JpaRepository<CustomerEntity, UUID> {
}

View File

@ -583,7 +583,7 @@ begin
end; $$;
create or replace function pureIdentifier(rawIdentifier varchar)
returns uuid
returns varchar
returns null on null input
language plpgsql as $$
begin
@ -596,11 +596,14 @@ create or replace function findUuidByIdName(objectTable varchar, objectIdName va
language plpgsql as $$
declare
sql varchar;
uuid uuid;
begin
objectTable := pureIdentifier(objectTable);
objectIdName := pureIdentifier(objectIdName);
sql := objectTable || 'UuidByIdName(' || objectIdName || ');';
execute sql;
sql := format('select * from %sUuidByIdName(%L);', objectTable, objectIdName);
raise notice 'sql: %', sql;
execute sql into uuid;
return uuid;
end; $$;
create or replace function currentSubjectIds()