Skip to content

The assurances of the voter can be obtained from a cast vote if canup… #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.rulez.demokracia.pdengine;

import java.util.List;

public class ADAAssuranceManager implements AssuranceManager{

@Override
public List<String> getAssurances(final String proxyId) {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.rulez.demokracia.pdengine;

import java.util.List;

public interface AssuranceManager {
List<String> getAssurances(String proxyId);
}
22 changes: 15 additions & 7 deletions src/main/java/org/rulez/demokracia/pdengine/CastVote.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
public class CastVote extends CastVoteEntity implements CastVoteInterface {

private static final long serialVersionUID = 1L;
private static final String DELIMITER = "|";

public CastVote(final String proxyId, final List<RankedChoice> preferences) {
super();
Expand All @@ -17,25 +16,34 @@ public CastVote(final String proxyId, final List<RankedChoice> preferences) {
secretId = RandomUtils.createRandomKey();
}

public CastVote(
final String proxyId, final List<RankedChoice> preferences,
final List<String> assurances
) {
this(proxyId, preferences);
this.assurances = assurances;
}

@Override
public List<RankedChoice> getPreferences() {
return preferences;
}

@Override
public List<String> getAssurances() {
throw new UnsupportedOperationException();
return assurances;
}

public String contentToBeSigned() {
final StringBuilder str = new StringBuilder();
final String delimiter = "|";

str.append(proxyId).append(DELIMITER)
.append(secretId).append(DELIMITER);
str.append(proxyId).append(delimiter)
.append(secretId).append(delimiter);
for (final RankedChoice rc : preferences)
str.append(rc.id).append(DELIMITER)
.append(rc.choiceId).append(DELIMITER)
.append(rc.rank).append(DELIMITER);
str.append(rc.id).append(delimiter)
.append(rc.choiceId).append(delimiter)
.append(rc.rank).append(delimiter);
return str.toString();
}

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/rulez/demokracia/pdengine/IVoteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

public interface IVoteManager {

static IVoteManager getVoteManager(final WebServiceContext wsContext) {
return VoteManagerUtils.getVoteManager(wsContext);
static IVoteManager getVoteManager(
final WebServiceContext wsContext, final AssuranceManager assuranceManager
) {
return VoteManagerUtils.getVoteManager(wsContext, assuranceManager);
}

WebServiceContext getWsContext();
Expand Down Expand Up @@ -66,4 +68,10 @@ void setVoteParameters(
final VoteAdminInfo adminInfo, final VoteParameters voteParameters
);

List<String> getAssurances();

static AssuranceManager getAssuranceManager() {
return new ADAAssuranceManager();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ final public class VoteManagerUtils {
private static Map<WebServiceContext, IVoteManager> registry =
new HashMap<>();

private VoteManagerUtils() {
}

public static IVoteManager getVoteManager(final WebServiceContext wsContext) {
if (!registry.containsKey(wsContext)) {
registry.put(wsContext, new VoteRegistry(wsContext));
}
public static IVoteManager getVoteManager(
final WebServiceContext wsContext, final AssuranceManager assuranceManager
) {
if (!registry.containsKey(wsContext))
registry.put(wsContext, new VoteRegistry(wsContext, assuranceManager));
return registry.get(wsContext);
}
}
117 changes: 54 additions & 63 deletions src/main/java/org/rulez/demokracia/pdengine/VoteRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,54 @@

public class VoteRegistry extends ChoiceManager implements IVoteManager {

public VoteRegistry(final WebServiceContext wsContext) {
private final AssuranceManager assuranceManager;

public VoteRegistry(
final WebServiceContext wsContext, final AssuranceManager assuranceManager
) {
super(wsContext);
this.assuranceManager = assuranceManager;
}

@Override
public String obtainBallot(final String identifier, final String adminKey) {
Vote vote = getVote(identifier);
final Vote vote = getVote(identifier);
vote.checkAdminKey(adminKey);

if (adminKey.equals(vote.getAdminKey())) {
if (adminKey.equals(vote.getAdminKey()))
vote.increaseRecordedBallots("admin");
} else {
if (getWsContext().getUserPrincipal() == null) {
else {
if (getWsContext().getUserPrincipal() == null)
throw new IllegalArgumentException(
"Simple user is not authenticated, cannot issue any ballot."
);
}
if (!userHasAllAssurance(vote.getNeededAssurances())) {
if (!userHasAllAssurance(vote.getNeededAssurances()))
throw new IllegalArgumentException(
"The user does not have all of the needed assurances."
);
}
if (vote.getRecordedBallotsCount(getWsUserName()).intValue() > 0) {
if (vote.getRecordedBallotsCount(getWsUserName()).intValue() > 0)
throw new IllegalArgumentException("This user already have a ballot.");
}

vote.increaseRecordedBallots(getWsUserName());
}

String ballot = RandomUtils.createRandomKey();
final String ballot = RandomUtils.createRandomKey();
vote.addBallot(ballot);
return ballot;
}

public boolean userHasAllAssurance(final List<String> neededAssuranceList) {
for (String neededAssurance : neededAssuranceList) {
if (!hasAssurance(neededAssurance)) {
for (final String neededAssurance : neededAssuranceList)
if (!hasAssurance(neededAssurance))
return false;
}
}
return true;
}

@Override
public CastVote castVote(
final String voteId, final String ballot, final List<RankedChoice> theVote
) {
Vote vote = getVote(voteId);
final Vote vote = getVote(voteId);

checkIfVotingEnabled(vote);
checkIfUpdateConditionsAreConsistent(vote);
Expand All @@ -69,130 +69,117 @@ public CastVote castVote(
CastVote receipt;

if (vote.getParameters().canUpdate) {
receipt = vote.addCastVote(getWsUserName(), theVote);
} else {
final String proxyId = getWsUserName();
receipt = vote.addCastVote(proxyId, theVote, getAssurances());
} else
receipt = vote.addCastVote(null, theVote);
}

vote.removeBallot(ballot);
return receipt;
}

private void
validatePreferences(final List<RankedChoice> theVote, final Vote vote) {
for (RankedChoice choice : theVote) {
for (final RankedChoice choice : theVote)
validateOnePreference(vote, choice);
}
}

private void
validateOnePreference(final Vote vote, final RankedChoice choice) {
if (!vote.getChoices().containsKey(choice.choiceId)) {
if (!vote.getChoices().containsKey(choice.choiceId))
throw new ReportedException("Invalid choiceId");
}
if (choice.rank < 0) {
if (choice.rank < 0)
throw new ReportedException("Invalid rank");
}
}

private void validateBallot(final String ballot, final Vote vote) {
if (!vote.getBallots().contains(ballot)) {
if (!vote.getBallots().contains(ballot))
throw new ReportedException("Illegal ballot");
}
}

private void checkIfUpdateConditionsAreConsistent(final Vote vote) {
if (
vote.getParameters().canUpdate &&
getWsContext().getUserPrincipal() == null
) {
)
throw new ReportedException(
"canUpdate is true but the user is not authenticated"
);
}
}

private void checkIfVotingEnabled(final Vote vote) {
if (!vote.parameters.canVote) {
if (!vote.parameters.canVote)
throw new ReportedException("This issue cannot be voted on yet");
}
}

@Override
public void
modifyVote(final VoteAdminInfo voteAdminInfo, final String voteName) {
ValidationUtil.checkVoteName(voteName);
Vote vote = checkIfVoteCanBeModified(voteAdminInfo);
final Vote vote = checkIfVoteCanBeModified(voteAdminInfo);

vote.name = voteName;
}

@Override
public void deleteVote(final VoteAdminInfo adminInfo) {
Vote vote = checkIfVoteCanBeModified(adminInfo);
final Vote vote = checkIfVoteCanBeModified(adminInfo);

session.remove(vote);
}

private Vote checkIfVoteCanBeModified(final VoteAdminInfo adminInfo) {
Vote vote = checkAdminInfo(adminInfo);
final Vote vote = checkAdminInfo(adminInfo);

if (vote.hasIssuedBallots()) {
if (vote.hasIssuedBallots())
throw new IllegalArgumentException(
"This vote cannot be modified it has issued ballots."
);
}
return vote;
}

private Vote checkAdminInfo(final VoteAdminInfo adminInfo) {
Vote vote = getVote(adminInfo.voteId);
final Vote vote = getVote(adminInfo.voteId);
vote.checkAdminKey(adminInfo.adminKey);
return vote;
}

@Override
public JsonObject showVote(final VoteAdminInfo adminInfo) {
Vote vote = checkAdminInfo(adminInfo);
if (!adminInfo.adminKey.equals(vote.adminKey)) {
final Vote vote = checkAdminInfo(adminInfo);
if (!adminInfo.adminKey.equals(vote.adminKey))
checkAssurances(vote);
}

return vote.toJson();
}

private void checkAssurances(final Vote vote) {
for (String assurance : vote.countedAssurances) {
if (!this.hasAssurance(assurance)) {
for (final String assurance : vote.countedAssurances)
if (!hasAssurance(assurance))
throw new ReportedException("missing assurances", assurance);
}
}
}

@Override
public String
deleteChoice(final VoteAdminInfo voteAdminInfo, final String choiceId) {
Vote vote =
final Vote vote =
getVoteIfModifiable(voteAdminInfo.voteId, voteAdminInfo.adminKey);

Choice votesChoice = vote.getChoice(choiceId);
final Choice votesChoice = vote.getChoice(choiceId);
if ("user".equals(voteAdminInfo.adminKey)) {
if (votesChoice.userName.equals(getWsUserName())) {
if (vote.parameters.canAddin) {
if (vote.parameters.canAddin)
vote.choices.remove(votesChoice.id);
} else {
else
throw new IllegalArgumentException(
"The adminKey is \"user\" but canAddin is false."
);
}
} else {
} else
throw new IllegalArgumentException(
"The adminKey is \"user\" but the user is not same with that user who added the choice."
);
}
} else {
} else
vote.choices.remove(votesChoice.id);
}
return "OK";
}

Expand All @@ -201,23 +188,21 @@ public void modifyChoice(
final VoteAdminInfo adminInfo, final String choiceId,
final String choiceName
) {
Vote vote = getVoteIfModifiable(adminInfo.voteId, adminInfo.adminKey);
final Vote vote = getVoteIfModifiable(adminInfo.voteId, adminInfo.adminKey);

Choice votesChoice = vote.getChoice(choiceId);
final Choice votesChoice = vote.getChoice(choiceId);
if ("user".equals(adminInfo.adminKey)) {
if (!vote.parameters.canAddin) {
if (!vote.parameters.canAddin)
throw new ReportedException(
"Choice modification disallowed: adminKey is user, but canAddin is false"
);
}

if (!votesChoice.userName.equals(getWsUserName())) {
if (!votesChoice.userName.equals(getWsUserName()))
throw new ReportedException(
"Choice modification disallowed: adminKey is user, " +
"and the choice was added by a different user",
votesChoice.userName
);
}
}

votesChoice.name = choiceName;
Expand All @@ -227,19 +212,25 @@ public void modifyChoice(
public void setVoteParameters(
final VoteAdminInfo adminInfo, final VoteParameters voteParameters
) {
Vote vote = checkAdminInfo(adminInfo);
final Vote vote = checkAdminInfo(adminInfo);

if (voteParameters.minEndorsements >= 0) {
if (voteParameters.minEndorsements >= 0)
vote.setParameters(
voteParameters.minEndorsements, voteParameters.canAddin,
voteParameters.canEndorse,
voteParameters.canVote, voteParameters.canView
);
} else {
else
throw new ReportedException(
"Illegal minEndorsements",
Integer.toString(voteParameters.minEndorsements)
);
}
}

@Override
public List<String> getAssurances() {
final String wsUserName = getWsUserName();
return assuranceManager.getAssurances(wsUserName);
}

}
Loading