Skip to content

Commit

Permalink
Added commands addAcl and delAcl
Browse files Browse the repository at this point in the history
Fix #6
  • Loading branch information
gturri committed Dec 21, 2014
1 parent 60a99ba commit d2a39ad
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

* Added methods addAcl and delAcl
* Added methods and commands addAcl and delAcl
* putPage and appendPage can read from stdin
* Added method logoff

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dw/cli/CommandFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;
import java.util.Set;

import dw.cli.commands.AclChanger;
import dw.cli.commands.AclChecker;
import dw.cli.commands.AllPageGetter;
import dw.cli.commands.AttachmentDeleter;
Expand Down Expand Up @@ -81,6 +82,8 @@ private static Map<String, Command> buildMapping(){
mapping.put("search", new Searcher());
mapping.put("unlock", new LocksSetter(true));
mapping.put("version", new CliVersion());
mapping.put("addAcl", new AclChanger(AclChanger.aclAction.add));
mapping.put("delAcl", new AclChanger(AclChanger.aclAction.delete));

return mapping;
}
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/dw/cli/commands/AclChanger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dw.cli.commands;

import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPException;

import dw.cli.Command;
import dw.cli.Output;
import dw.xmlrpc.DokuJClient;
import dw.xmlrpc.exception.DokuException;

public class AclChanger extends Command {
public enum aclAction {
add,
delete
}

private final aclAction _action;

public AclChanger(aclAction action){
_action = action;
}

@Override
protected void registerParameters(JSAP jsap) throws JSAPException {
jsap.registerParameter(new FlaggedOption("scope")
.setStringParser(JSAP.STRING_PARSER)
.setRequired(true)
.setLongFlag("scope"));

jsap.registerParameter(new FlaggedOption("username")
.setStringParser(JSAP.STRING_PARSER)
.setRequired(true)
.setLongFlag("username"));

if ( _action == aclAction.add ){
jsap.registerParameter(new FlaggedOption("permission")
.setStringParser(JSAP.INTEGER_PARSER)
.setRequired(true)
.setLongFlag("permission"));
}
}

@Override
protected Output run(DokuJClient dokuClient) throws DokuException {
String scope = _config.getString("scope");
String username = _config.getString("username");
boolean success;

if ( _action == aclAction.add ){
int permission = _config.getInt("permission");
success = dokuClient.addAcl(scope, username, permission);
} else {
success = dokuClient.delAcl(scope, username);
}

if ( success ){
return new Output();
} else {
return new Output("Acl change returned false.", 1);
}
}
}
16 changes: 16 additions & 0 deletions src/test/java/dw/cli/commands/itest/Test_AclChanger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dw.cli.commands.itest;

import dw.cli.itest.TestHelper;

public class Test_AclChanger extends TestHelper {
@org.junit.Test
public void canAddAndRemoveAcl() throws Exception {
assertSuccess("8", runWithArgumentAsWriterUser("aclCheck", "ns1:start"));

assertSuccess("", runWithArguments("delAcl", "--scope", "*", "--username", "@user"));
assertSuccess("0", runWithArgumentAsWriterUser("aclCheck", "ns1:start"));

assertSuccess("", runWithArguments("addAcl", "--scope", "*", "--username", "@user", "--permission", "8"));
assertSuccess("8", runWithArgumentAsWriterUser("aclCheck", "ns1:start"));
}
}

0 comments on commit d2a39ad

Please sign in to comment.