Skip to content

Commit

Permalink
BXC-4486 add with-works and with-files to perms set cmd (#87)
Browse files Browse the repository at this point in the history
* add with-works and with-files to set cmd

* combine if statements, sort updatedRecords

* move default entry to top, fix test

* combine workflows

* removed unused code

* clean up code
  • Loading branch information
krwong authored Mar 15, 2024
1 parent a0483be commit 994261c
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -102,7 +103,7 @@ public void generatePermissions(PermissionMappingOptions options) throws Excepti
* @param options permission mapping options
*/
public void setPermissions(PermissionMappingOptions options) throws Exception {
if (!doesIdExistInIndex(options.getCdmId())) {
if (options.getCdmId() != null && !doesIdExistInIndex(options.getCdmId())) {
throw new IllegalArgumentException("Id " + options.getCdmId() + " does not exist in this project.");
}

Expand All @@ -111,7 +112,7 @@ public void setPermissions(PermissionMappingOptions options) throws Exception {
throw new InvalidProjectStateException("Permissions csv does not exist.");
}

// add or update permission for a specific cdmId
// add or update permission for a specific cdmId, works, and files
List<List<String>> records = updateCsvRecords(options);

try (
Expand Down Expand Up @@ -290,25 +291,48 @@ private String getObjectType(String id) {

private List<List<String>> updateCsvRecords(PermissionMappingOptions options) {
List<CSVRecord> previousRecords = getPermissions();
List<Map.Entry<String, String>> workAndFileRecords = new ArrayList<>();
List<List<String>> updatedRecords = new ArrayList<>();
Set<String> cdmIds = new HashSet<>();
Set<String> addedAndUpdatedIds = new HashSet<>();
String everyoneField = getAssignedRoleValue(options.isStaffOnly(), options.getEveryone());
String authenticatedField = getAssignedRoleValue(options.isStaffOnly(), options.getAuthenticated());

// update existing entry
// addedAndUpdatedIds: list of all ids that need to be added and updated
if (options.getCdmId() != null) {
workAndFileRecords.add(new AbstractMap.SimpleEntry<>(options.getCdmId(), getObjectType(options.getCdmId())));
addedAndUpdatedIds.add(options.getCdmId());
}
if (options.isWithWorks() || options.isWithFiles()) {
workAndFileRecords.addAll(queryForMappedIds(options));
Set<String> workFileIds = workAndFileRecords.stream().map(Map.Entry::getKey).collect(Collectors.toSet());
addedAndUpdatedIds.addAll(workFileIds);
}

// update existing entries and add unchanged entries to updatedRecords, then remove updated ids from updateIds
for (CSVRecord record : previousRecords) {
cdmIds.add(record.get(0));
if (record.get(0).equals(options.getCdmId())) {
if (addedAndUpdatedIds.contains(record.get(0))) {
updatedRecords.add(Arrays.asList(record.get(0), record.get(1), everyoneField, authenticatedField));
addedAndUpdatedIds.remove(record.get(0));
} else {
updatedRecords.add(Arrays.asList(record.get(0), record.get(1), record.get(2), record.get(3)));
addedAndUpdatedIds.remove(record.get(0));
}
}

// add new works or files entries
for (Map.Entry<String, String> workAndFileRecord : workAndFileRecords) {
if (addedAndUpdatedIds.contains(workAndFileRecord.getKey())) {
updatedRecords.add(Arrays.asList(workAndFileRecord.getKey(), workAndFileRecord.getValue(),
everyoneField, authenticatedField));
}
}

// add new entry
if (!cdmIds.contains(options.getCdmId())) {
String objectType = getObjectType(options.getCdmId());
updatedRecords.add(Arrays.asList(options.getCdmId(), objectType, everyoneField, authenticatedField));
updatedRecords.sort(Comparator.comparing(entry -> entry.get(0)));
// move default entry to top if it exists
List<String> updatedIds = updatedRecords.stream().map(entry -> entry.get(0)).collect(Collectors.toList());
if (updatedIds.contains(PermissionsInfo.DEFAULT_ID)) {
List<String> defaultEntry = updatedRecords.remove(updatedIds.indexOf(PermissionsInfo.DEFAULT_ID));
updatedRecords.add(0, defaultEntry);
}

return updatedRecords;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void generateDefaultPermissionsInvalid() throws Exception {
@Test
public void generateDefaultPermissionsWithoutForceFlag() throws Exception {
FileUtils.write(project.getPermissionsPath().toFile(),
"default,canViewMetadata,canViewMetadata", StandardCharsets.UTF_8, true);
"default,,canViewMetadata,canViewMetadata", StandardCharsets.UTF_8, true);

String[] args = new String[] {
"-w", project.getProjectPath().toString(),
Expand All @@ -94,7 +94,7 @@ public void generateDefaultPermissionsWithoutForceFlag() throws Exception {
@Test
public void generateDefaultPermissionsWithForceFlag() throws Exception {
FileUtils.write(project.getPermissionsPath().toFile(),
"default,canViewMetadata,canViewMetadata", StandardCharsets.UTF_8, true);
"default,,canViewMetadata,canViewMetadata", StandardCharsets.UTF_8, true);

String[] args = new String[] {
"-w", project.getProjectPath().toString(),
Expand Down Expand Up @@ -156,7 +156,7 @@ public void generateFilePermissionsWithDefault() throws Exception {
@Test
public void generateWorkAndFilePermissionsWithForce() throws Exception {
FileUtils.write(project.getPermissionsPath().toFile(),
"default,canViewOriginals,canViewOriginals", StandardCharsets.UTF_8, true);
"default,,canViewOriginals,canViewOriginals", StandardCharsets.UTF_8, true);

testHelper.indexExportData("mini_gilmer");
String[] args = new String[] {
Expand Down Expand Up @@ -194,7 +194,7 @@ public void generateWorkAndFilePermissionsWithDefault() throws Exception {
@Test
public void setPermissionExistingEntry() throws Exception {
FileUtils.write(project.getPermissionsPath().toFile(),
"25,canViewOriginals,canViewOriginals", StandardCharsets.UTF_8, true);
"25,,canViewOriginals,canViewOriginals", StandardCharsets.UTF_8, true);

testHelper.indexExportData("mini_gilmer");
String[] args = new String[] {
Expand Down Expand Up @@ -253,6 +253,71 @@ public void setPermissionNewGroupedWorkEntry() throws Exception {
assertMappingCount(2);
}

@Test
public void setPermissionsWithWorks() throws Exception {
FileUtils.write(project.getPermissionsPath().toFile(),
"25,,canViewOriginals,canViewOriginals", StandardCharsets.UTF_8, true);

testHelper.indexExportData("mini_gilmer");
String[] args = new String[] {
"-w", project.getProjectPath().toString(),
"permissions", "set",
"-ww",
"-e", "canViewMetadata",
"-a", "canViewMetadata"};
executeExpectSuccess(args);
assertMapping(0, "25", "canViewMetadata", "canViewMetadata");
assertMapping(1, "26", "canViewMetadata", "canViewMetadata");
assertMapping(2, "27", "canViewMetadata", "canViewMetadata");
}

@Test
public void setPermissionsWithFiles() throws Exception {
String[] args = new String[] {
"-w", project.getProjectPath().toString(),
"permissions", "generate",
"-wd",
"--everyone", "canViewOriginals",
"--authenticated", "canViewOriginals"};
executeExpectSuccess(args);

testHelper.indexExportData("grouped_gilmer");
setupGroupedIndex();
String[] args2 = new String[] {
"-w", project.getProjectPath().toString(),
"permissions", "set",
"-wf",
"-e", "canViewMetadata",
"-a", "canViewMetadata"};
executeExpectSuccess(args2);
assertMapping(0, "default", "canViewOriginals", "canViewOriginals");
assertMapping(1, "25", "canViewMetadata", "canViewMetadata");
assertMapping(2, "26", "canViewMetadata", "canViewMetadata");
}

@Test
public void setPermissionsWithWorksAndFiles() throws Exception {
FileUtils.write(project.getPermissionsPath().toFile(),
"603,file,canViewOriginals,canViewOriginals", StandardCharsets.UTF_8, true);

testHelper.indexExportData("mini_keepsakes");
String[] args = new String[] {
"-w", project.getProjectPath().toString(),
"permissions", "set",
"-ww",
"-wf",
"-e", "canViewMetadata",
"-a", "canViewMetadata"};
executeExpectSuccess(args);
assertMapping(0, "216", "canViewMetadata", "canViewMetadata");
assertMapping(1, "602", "canViewMetadata", "canViewMetadata");
assertMapping(2, "603", "canViewMetadata", "canViewMetadata");
assertMapping(3, "604", "canViewMetadata", "canViewMetadata");
assertMapping(4, "605", "canViewMetadata", "canViewMetadata");
assertMapping(5, "606", "canViewMetadata", "canViewMetadata");
assertMapping(6, "607", "canViewMetadata", "canViewMetadata");
}

@Test
public void validateValidDefaultPermissions() throws Exception {
String[] args = new String[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,115 @@ public void setPermissionNoCsvTest() throws Exception {
assertEquals(expectedMessage, actualMessage);
}

@Test
public void setPermissionWithWorksDefault() throws Exception {
writeCsv(mappingBody("default,,canViewMetadata,canViewMetadata", "26,work,none,none"));
testHelper.indexExportData("mini_gilmer");
Path permissionsMappingPath = project.getPermissionsPath();
var options = new PermissionMappingOptions();
options.setWithWorks(true);
options.setEveryone(UserRole.canViewMetadata);
options.setAuthenticated(UserRole.canViewMetadata);

service.setPermissions(options);
assertTrue(Files.exists(permissionsMappingPath));

List<CSVRecord> rows = listCsvRecords(permissionsMappingPath);
assertIterableEquals(Arrays.asList("default", "", "canViewMetadata", "canViewMetadata"), rows.get(0));
assertIterableEquals(Arrays.asList("25", "work", "canViewMetadata", "canViewMetadata"), rows.get(1));
assertIterableEquals(Arrays.asList("26", "work", "canViewMetadata", "canViewMetadata"), rows.get(2));
assertIterableEquals(Arrays.asList("27", "work", "canViewMetadata", "canViewMetadata"), rows.get(3));
}

@Test
public void setPermissionsWithFilesGroupedWork() throws Exception {
writeCsv(mappingBody("default,,canViewMetadata,canViewMetadata"));
testHelper.indexExportData("grouped_gilmer");
setupGroupedIndex();
Path permissionsMappingPath = project.getPermissionsPath();
var options = new PermissionMappingOptions();
options.setWithFiles(true);
options.setEveryone(UserRole.canViewMetadata);
options.setAuthenticated(UserRole.canViewMetadata);

service.setPermissions(options);
assertTrue(Files.exists(permissionsMappingPath));

List<CSVRecord> rows = listCsvRecords(permissionsMappingPath);
assertIterableEquals(Arrays.asList("default", "", "canViewMetadata", "canViewMetadata"), rows.get(0));
assertIterableEquals(Arrays.asList("25", "file", "canViewMetadata", "canViewMetadata"), rows.get(1));
assertIterableEquals(Arrays.asList("26", "file", "canViewMetadata", "canViewMetadata"), rows.get(2));
}

@Test
public void setPermissionsWithFilesCompoundObjects() throws Exception {
writeCsv(mappingBody("603,file,canViewMetadata,canViewMetadata"));
testHelper.indexExportData("mini_keepsakes");
Path permissionsMappingPath = project.getPermissionsPath();
var options = new PermissionMappingOptions();
options.setWithFiles(true);
options.setEveryone(UserRole.canViewMetadata);
options.setAuthenticated(UserRole.canViewMetadata);

service.setPermissions(options);
assertTrue(Files.exists(permissionsMappingPath));

List<CSVRecord> rows = listCsvRecords(permissionsMappingPath);
assertIterableEquals(Arrays.asList("602", "file", "canViewMetadata", "canViewMetadata"), rows.get(0));
assertIterableEquals(Arrays.asList("603", "file", "canViewMetadata", "canViewMetadata"), rows.get(1));
assertIterableEquals(Arrays.asList("605", "file", "canViewMetadata", "canViewMetadata"), rows.get(2));
assertIterableEquals(Arrays.asList("606", "file", "canViewMetadata", "canViewMetadata"), rows.get(3));
}

@Test
public void setPermissionsWithWorksAndFiles() throws Exception {
writeCsv(mappingBody("604,work,canViewMetadata,canViewMetadata"));
testHelper.indexExportData("mini_keepsakes");
Path permissionsMappingPath = project.getPermissionsPath();
var options = new PermissionMappingOptions();
options.setWithWorks(true);
options.setWithFiles(true);
options.setEveryone(UserRole.canViewMetadata);
options.setAuthenticated(UserRole.canViewMetadata);

service.setPermissions(options);
assertTrue(Files.exists(permissionsMappingPath));

List<CSVRecord> rows = listCsvRecords(permissionsMappingPath);
assertIterableEquals(Arrays.asList("216", "work", "canViewMetadata", "canViewMetadata"), rows.get(0));
assertIterableEquals(Arrays.asList("602", "file", "canViewMetadata", "canViewMetadata"), rows.get(1));
assertIterableEquals(Arrays.asList("603", "file", "canViewMetadata", "canViewMetadata"), rows.get(2));
assertIterableEquals(Arrays.asList("604", "work", "canViewMetadata", "canViewMetadata"), rows.get(3));
assertIterableEquals(Arrays.asList("605", "file", "canViewMetadata", "canViewMetadata"), rows.get(4));
assertIterableEquals(Arrays.asList("606", "file", "canViewMetadata", "canViewMetadata"), rows.get(5));
assertIterableEquals(Arrays.asList("607", "work", "canViewMetadata", "canViewMetadata"), rows.get(6));
}

@Test
public void setPermissionsExistingWorksNewFiles() throws Exception {
writeCsv(mappingBody("default,,none,none", "216,work,canViewMetadata,canViewMetadata",
"604,work,canViewMetadata,canViewMetadata", "607,work,canViewMetadata,canViewMetadata"));
testHelper.indexExportData("mini_keepsakes");
Path permissionsMappingPath = project.getPermissionsPath();
var options = new PermissionMappingOptions();
options.setWithFiles(true);
options.setEveryone(UserRole.canViewMetadata);
options.setAuthenticated(UserRole.canViewMetadata);

service.setPermissions(options);
assertTrue(Files.exists(permissionsMappingPath));

List<CSVRecord> rows = listCsvRecords(permissionsMappingPath);
assertIterableEquals(Arrays.asList("default", "", "none", "none"), rows.get(0));
assertIterableEquals(Arrays.asList("216", "work", "canViewMetadata", "canViewMetadata"), rows.get(1));
assertIterableEquals(Arrays.asList("602", "file", "canViewMetadata", "canViewMetadata"), rows.get(2));
assertIterableEquals(Arrays.asList("603", "file", "canViewMetadata", "canViewMetadata"), rows.get(3));
assertIterableEquals(Arrays.asList("604", "work", "canViewMetadata", "canViewMetadata"), rows.get(4));
assertIterableEquals(Arrays.asList("605", "file", "canViewMetadata", "canViewMetadata"), rows.get(5));
assertIterableEquals(Arrays.asList("606", "file", "canViewMetadata", "canViewMetadata"), rows.get(6));
assertIterableEquals(Arrays.asList("607", "work", "canViewMetadata", "canViewMetadata"), rows.get(7));
}

private String mappingBody(String... rows) {
return String.join(",", PermissionsInfo.CSV_HEADERS) + "\n"
+ String.join("\n", rows);
Expand Down

0 comments on commit 994261c

Please sign in to comment.