Update list sharing

Fix quite a variety of bugs with list sharing, mostly relating to ListShareGET not conforming to its contract
This commit is contained in:
NMerz
2020-11-30 12:16:29 -05:00
parent d8f722c4ac
commit be80129de6
8 changed files with 125 additions and 58 deletions

View File

@@ -12,7 +12,7 @@ public class ListGetter implements CallHandler{
private final String cognitoID;
private final String GET_LIST = "SELECT * FROM List WHERE listID = ?;";
private final String GET_LISTS = "SELECT listID FROM ListSharee WHERE userID = ? ORDER BY uiPosition;";
private final String GET_LISTS = "SELECT listID, permissionLevel FROM ListSharee WHERE userID = ? ORDER BY uiPosition;";
private final String SHARE_CHECK = "SELECT * FROM ListSharee WHERE listID = ?;";
private final String GET_ENTRIES = "SELECT * FROM ListProduct WHERE listID = ?;";
@@ -32,7 +32,10 @@ public class ListGetter implements CallHandler{
System.out.println(getListsResults);
ArrayList<Integer> listIds = new ArrayList<>();
while (getListsResults.next()) {
listIds.add(getListsResults.getInt(1));
Integer permissionLevel = getListsResults.getInt("permissionLevel");
if (ListPermissions.hasPermission(permissionLevel, "Read")) {
listIds.add(getListsResults.getInt("listID"));
}
}
return listIds;
}

View File

@@ -24,7 +24,7 @@ public class ListPermissions {
}
public static boolean hasPermission(Integer level, String permission) {
return level % getKeyForPermission(permission) == 0;
return (level % getKeyForPermission(permission) == 0 && level != 0);
}
public static Integer getKeyForPermission(String permissionRaw) {

View File

@@ -9,9 +9,9 @@ public class ListShare {
Integer uiPosition;
ArrayList<ListShare> other;
public ListShare(ResultSet listRow) throws SQLException {
public ListShare(ResultSet listRow, String shareWithEmail) throws SQLException {
this.listID = listRow.getInt("listID");
this.shareWithEmail = listRow.getString("userID");
this.shareWithEmail = shareWithEmail;
this.permissionLevel = listRow.getInt("permissionLevel");
this.uiPosition = listRow.getInt("uiPosition");
other = new ArrayList<>();

View File

@@ -1,8 +1,14 @@
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.services.lambda.model.InvokeResult;
import com.google.gson.Gson;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
public class ListShareGetter implements CallHandler{
@@ -24,14 +30,63 @@ public class ListShareGetter implements CallHandler{
getList.setInt(1, listID);
ResultSet getListResults = getList.executeQuery();
getListResults.first();
System.out.println(getListResults);
ListShare first = null;
while (getListResults.next() && first == null) {
InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName("UserGET");
invokeRequest.setPayload("{" +
" \"body\": {" +
" }," +
" \"params\": {" +
" \"querystring\": {" +
" \"id\": \"" + getListResults.getString("userID") + "\"" +
" }" +
" }," +
" \"context\": {" +
" \"sub\": \"not used\"" +
" }" +
"}");
InvokeResult invokeResult = AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest);
if (invokeResult.getStatusCode() != 200) {
throw new InputMismatchException("Could not find specified user to share with");
}
String shareWithEmail = new Gson().fromJson(new String(invokeResult.getPayload().array()), User.class).email;
first = new ListShare(getListResults, shareWithEmail);
if (first.permissionLevel == 0 || first.permissionLevel == 1) {
first = null;
}
}
//ListShare object to hold the data values of the first row retrived
ListShare first = new ListShare(getListResults);
//Insert the ListShare objects to hold the data of the remaining rows into first's ListShare list
while (getListResults.next()) {
first.addtoList(new ListShare(getListResults));
InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName("UserGET");
invokeRequest.setPayload("{" +
" \"body\": {" +
" }," +
" \"params\": {" +
" \"querystring\": {" +
" \"id\": \"" + getListResults.getString("userID") + "\"" +
" }" +
" }," +
" \"context\": {" +
" \"sub\": \"not used\"" +
" }" +
"}");
InvokeResult invokeResult = AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest);
if (invokeResult.getStatusCode() != 200) {
throw new InputMismatchException("Could not find specified user to share with");
}
String shareWithEmail = new Gson().fromJson(new String(invokeResult.getPayload().array()), User.class).email;
ListShare newShare = new ListShare(getListResults, shareWithEmail);
System.out.println(newShare);
if (newShare.permissionLevel != 0 && newShare.permissionLevel != 1) {
first.addtoList(newShare);
}
}
return first;

View File

@@ -51,7 +51,7 @@ public class UserGetter implements CallHandler {
if (foundUsers.size() != 1) {
System.out.println(foundUsers);
if (foundUsers.size() == 0) {
throw new InputMismatchException("Not user with given attribute (" + attributeToGet + ")");
throw new InputMismatchException("No user with given attribute when searching for (" + attributeToGet + ")");
}
throw new InputMismatchException("Found more than one user with supposedly unique attribute (" + attributeToGet + ")");
}