Skip to content

Commit

Permalink
Merge pull request #907 from dickschoeller/844-hide-confidential
Browse files Browse the repository at this point in the history
844 hide confidential
  • Loading branch information
dickschoeller committed May 10, 2020
2 parents 0a2d619 + def23e9 commit 3102342
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public LivingEstimator(final Person person,
* @return estimated living or dead
*/
public boolean estimate() {
final PersonVisitor visitor = new PersonVisitor();
person.accept(visitor);
if (visitor.hasDeathAttribute()) {
if (hasDeathAttribute()) {
// Death attribute found, we're out of here.
return false;
} else {
Expand All @@ -71,6 +69,18 @@ public boolean estimate() {
}
}

/**
* Just does the hasDeathAttribute check. This will allow us to compare dead
* with estimated dead.
*
* @return true if the person has the death attribute
*/
public boolean hasDeathAttribute() {
final PersonVisitor visitor = new PersonVisitor();
person.accept(visitor);
return visitor.hasDeathAttribute();
}

/**
* @param root data set to process
* @param living the list of people estimated to be living
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,24 @@ public Collection<String> getIndexNameHtmls(final String surname) {
}
final String html = createGedRenderer(person).getIndexName();
final String liHtml = "<li id=\"" + person.getString() + "\">"
+ html + "</li>";
+ html
+ indicateDeadWithoutRecord(person)
+ "</li>";
names.add(liHtml);
}
logger.info("Ending getIndexNameHtmls");
return names;
}

private String indicateDeadWithoutRecord(final Person person) {
final LivingEstimator le = new LivingEstimator(person,
getRenderingContext());
if (!le.estimate() && !le.hasDeathAttribute()) {
return " <b>no death record</b>";
}
return "";
}

/**
* @param person the person we are checking
* @return true if the person is confidential
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.schoellerfamily.gedbrowser.api.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -8,11 +9,15 @@
import org.apache.commons.logging.LogFactory;
import org.schoellerfamily.gedbrowser.analytics.LivingEstimator;
import org.schoellerfamily.gedbrowser.analytics.calendar.CalendarProvider;
import org.schoellerfamily.gedbrowser.api.controller.exception.ObjectNotFoundException;
import org.schoellerfamily.gedbrowser.api.crud.ObjectCrud;
import org.schoellerfamily.gedbrowser.api.crud.OperationsEnabler;
import org.schoellerfamily.gedbrowser.api.crud.PersonCrud;
import org.schoellerfamily.gedbrowser.api.datamodel.ApiPerson;
import org.schoellerfamily.gedbrowser.api.datamodel.ApiPerson.Builder;
import org.schoellerfamily.gedbrowser.datamodel.Person;
import org.schoellerfamily.gedbrowser.datamodel.visitor.PersonVisitor;
import org.schoellerfamily.gedbrowser.persistence.domain.PersonDocument;
import org.schoellerfamily.gedbrowser.security.service.UserService;
import org.schoellerfamily.gedbrowser.security.util.RequestUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -72,14 +77,38 @@ public ApiPerson create(
}

/**
* @param request the servlet request coming in
* @param db the name of the db to access
* @return the list of persons
*/
@GetMapping(value = "/v1/dbs/{db}/persons")
@ResponseBody
public List<ApiPerson> read(
public List<ApiPerson> read(final HttpServletRequest request,
@PathVariable final String db) {
return crud().readAll(db);
final List<PersonDocument> allPersons = ((PersonCrud) crud()).read(db);
return hide(request, allPersons);
}

private List<ApiPerson> hide(final HttpServletRequest request,
final List<PersonDocument> allPersons) {
logger.info("Start hiding list of persons");
final List<ApiPerson> list = new ArrayList<>();
final RequestUserUtil requestUserUtil = new RequestUserUtil(request, userService);
final boolean hasUser = requestUserUtil.hasUser();
final boolean hasAdmin = requestUserUtil.hasAdmin();
for (final PersonDocument doc : allPersons) {
final Person person = doc.getGedObject();
if (shouldHideConfidential(person, hasAdmin)) {
continue;
}
if (shouldHideLiving(person, hasUser)) {
continue;
}
final OperationsEnabler<?, ?> enabler = (OperationsEnabler<?, ?>) crud();
list.add((ApiPerson) enabler.getD2dm().convert(doc));
}
logger.info("Done hiding list of persons");
return list;
}

/**
Expand All @@ -95,23 +124,28 @@ public ApiPerson read(
@PathVariable final String db,
@PathVariable final String id) {
final Person person = ((PersonCrud) crud()).read(db, id).getGedObject();
if (shouldHideLiving(request, person)) {
final RequestUserUtil util = new RequestUserUtil(request, userService);
if (shouldHideConfidential(person, util.hasAdmin())) {
throw new ObjectNotFoundException("person not found", "ApiPerson", db, id);
}
if (shouldHideLiving(person, util.hasUser())) {
return createDummyLivingPerson(id);
}
logger.info("entering read person: " + id);
return crud().readOne(db, id);
}

/**
* Determine whether to return dummy "living" person.
*
* @param request the request, used to identify user authorization
* @param person the person, used to check if living
* @return true if the person is living and should be hidden
*/
private boolean shouldHideLiving(final HttpServletRequest request, final Person person) {
return !new RequestUserUtil(request, userService).hasUser()
&& new LivingEstimator(person, provider).estimate();
private boolean shouldHideConfidential(final Person person, final boolean hasAdmin) {
if (hasAdmin) {
return false;
}
final PersonVisitor visitor = new PersonVisitor();
person.accept(visitor);
return visitor.isConfidential();
}

private boolean shouldHideLiving(final Person person, final boolean hasUser) {
return !hasUser && new LivingEstimator(person, provider).estimate();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public List<ApiPerson> readAll(final String db) {
@Override
public ApiPerson readOne(final String db, final String id) {
logger.info("Entering read /dbs/" + db + "/persons/" + id);
return getD2dm().convert(read(db, id));
final PersonDocument read = read(db, id);
return getD2dm().convert(read);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,17 @@ default Y read(final RootDocument root, final String idString) {
* @return the list of objects of the requested type
*/
default List<Y> read(final RootDocument root) {
final List<Y> all = new ArrayList<>();
for (final Y document : getRepository().findAll(root)) {
all.add(document);
try {
final List<Y> all = new ArrayList<>();
final Iterable<Y> a = getRepository().findAll(root);
for (final Y document : a) {
all.add(document);
}
all.sort(new GetStringComparator());
return all;
} catch (RuntimeException e) {
throw e;
}
all.sort(new GetStringComparator());
return all;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public final class ControllerTestHelper {
builder = new ApiPerson.Builder().add(attribute).build();
}

private HttpHeaders adminLogin(final int port, final TestRestTemplate testRestTemplate) {
private HttpHeaders adminLogin(final int port, final TestRestTemplate template) {
try {
final LoginTestHelper helper = new LoginTestHelper(testRestTemplate, port);
final LoginTestHelper helper = new LoginTestHelper(template, port);
return helper.adminLogin();
} catch (URISyntaxException e) {
return new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@ public void before() {
helper = new LoginTestHelper(testRestTemplate, port);
}


/** */
/**
* @throws RestClientException should be never
* @throws URISyntaxException should be never
*/
@Test
public final void testGetPersonsGl120368() {
public final void testGetPersonsGl120368()
throws RestClientException, URISyntaxException {
final String url = "http://localhost:" + port
+ "/gedbrowserng/v1/dbs/gl120368/persons";
final ResponseEntity<String> entity =
testRestTemplate.getForEntity(url, String.class);
testRestTemplate.exchange(url, HttpMethod.GET, helper.adminEntity(), String.class);
final String bodyFragment =
"[ {\n"
+ " \"type\" : \"person\",\n"
Expand All @@ -90,13 +93,41 @@ public final void testGetPersonsGl120368() {
.startsWith(bodyFragment);
}

/** */
/**
* @throws RestClientException should be never
* @throws URISyntaxException should be never
*/
@Test
public final void testGetPersonsMiniSchoeller() {
public final void testGetPersonsGl120368NotLoggedIn()
throws RestClientException, URISyntaxException {
final String url = "http://localhost:" + port
+ "/gedbrowserng/v1/dbs/mini-schoeller/persons";
+ "/gedbrowserng/v1/dbs/gl120368/persons";
final ResponseEntity<String> entity =
testRestTemplate.getForEntity(url, String.class);
final String bodyFragment =
"[ {\n"
+ " \"type\" : \"person\",\n"
+ " \"string\" : \"I6\",\n"
+ " \"attributes\" : [ {\n"
+ " \"type\" : \"name\",\n"
+ " \"string\" : \"Reginald Amass /Williams/\",\n"
+ " \"attributes\" : [ ],\n"
+ " \"tail\" : \"\"\n";
then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
then(entity.getBody().substring(0, TRUNCATE_LENGTH))
.startsWith(bodyFragment);
}

/**
* @throws RestClientException should be never
* @throws URISyntaxException should be never
*/
@Test
public final void testGetPersonsMiniSchoeller() throws RestClientException, URISyntaxException {
final String url = "http://localhost:" + port
+ "/gedbrowserng/v1/dbs/mini-schoeller/persons";
final ResponseEntity<String> entity =
testRestTemplate.exchange(url, HttpMethod.GET, helper.adminEntity(), String.class);
final String bodyFragment =
"[ {\n"
+ " \"type\" : \"person\",\n"
Expand All @@ -109,9 +140,30 @@ public final void testGetPersonsMiniSchoeller() {
then(entity.getBody()).startsWith(bodyFragment);
}

/** */
@Test
public final void testGetPersonsMiniSchoellerNotLoggedIn() {
final String url = "http://localhost:" + port
+ "/gedbrowserng/v1/dbs/mini-schoeller/persons";
final ResponseEntity<String> entity =
testRestTemplate.getForEntity(url, String.class);
final String bodyFragment =
"[ {\n"
+ " \"type\" : \"person\",\n"
+ " \"string\" : \"I7\",\n"
+ " \"attributes\" : [ {\n"
+ " \"type\" : \"name\",\n"
+ " \"string\" : \"Arnold/Robinson/\",\n"
+ " \"attributes\" : [ ],\n"
+ " \"tail\" : \"\"\n"
+ " },";
then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
then(entity.getBody()).startsWith(bodyFragment);
}

/**
* @throws URISyntaxException should be never
* @throws RestClientException should be never
* @throws URISyntaxException should be never
*/
@Test
public final void testGetPersonsMiniSchoellerI2() throws RestClientException, URISyntaxException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* @author Dick Schoeller
*/
package org.schoellerfamily.gedbrowser.api.test;
package org.schoellerfamily.gedbrowser.api.test;
14 changes: 4 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,24 @@
<cglib.version>3.3.0</cglib.version>
<!-- might try 0.9.1 of google -->
<google-maps-services.version>0.2.11</google-maps-services.version>
<!-- jackson-core.version>2.9.6</jackson-core.version -->
<!-- don't upgrade -->
<!-- jackson-databind.version>2.9.10.1</jackson-databind.version -->
<jackson-core.version>2.10.0</jackson-core.version>
<jackson-databind.version>2.10.0</jackson-databind.version>
<!-- -->
<gedcom4j.version>4.0.1</gedcom4j.version>
<joda-time.version>2.10.5</joda-time.version>
<joda-time.version>2.10.6</joda-time.version>
<sauce_junit.version>2.1.25</sauce_junit.version>
<!-- -->
<geojson-jackson.version>1.14</geojson-jackson.version>
<!-- -->
<log4j-over-slf4j.version>1.7.30</log4j-over-slf4j.version>
<jcl-over-slf4j.version>1.7.30</jcl-over-slf4j.version>
<logback-classic.version>1.2.3</logback-classic.version>
<anselcharset.version>1.1</anselcharset.version>
<commons-io.version>2.6</commons-io.version>
<commons-logging.version>1.2</commons-logging.version>
<commons-lang3.version>3.9</commons-lang3.version>
<assertj.version>3.15.0</assertj.version>
<commons-lang3.version>3.10</commons-lang3.version>
<assertj.version>3.16.1</assertj.version>

<selenium.version>3.141.59</selenium.version>
<selenium-client-driver.version>1.0.2</selenium-client-driver.version>
<htmlunit-driver.version>2.37.0</htmlunit-driver.version>
<htmlunit-driver.version>2.40.0</htmlunit-driver.version>

<coveralls-plugin.version>4.3.0</coveralls-plugin.version>
<jacoco-plugin.version>0.8.5</jacoco-plugin.version>
Expand Down

0 comments on commit 3102342

Please sign in to comment.