# HG changeset patch # User Jaroslav Tulach # Date 1261551556 -3600 # Node ID 524c7f359c4e483f427e5a9ee973906d69d25d75 # Parent 7d29ebd9c208797758e2d168c3c3b35535838ca7 Adding support for 'permission.games' to allow special roles to enlist all the available games diff -r 7d29ebd9c208 -r 524c7f359c4e webidor/pom.xml --- a/webidor/pom.xml Thu Dec 10 08:47:44 2009 +0100 +++ b/webidor/pom.xml Wed Dec 23 07:59:16 2009 +0100 @@ -9,7 +9,7 @@ org.apidesign webidor jar - 1.10 + 1.11 webidor server http://maven.apache.org diff -r 7d29ebd9c208 -r 524c7f359c4e webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java Thu Dec 10 08:47:44 2009 +0100 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java Wed Dec 23 07:59:16 2009 +0100 @@ -28,6 +28,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.TreeSet; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; @@ -46,6 +48,7 @@ private String id; @XmlElement(name="property") private List properties; + private transient Set permissions; User() { } @@ -61,6 +64,13 @@ properties.add(new Property(name, value)); } + public void addPermission(String permission) { + if (permissions == null) { + permissions = new TreeSet(); + } + permissions.add(permission); + } + public String getProperty(String name) { if (properties == null) { return null; @@ -73,6 +83,13 @@ return null; } + public boolean hasPermission(String permission) { + if (permissions == null) { + return false; + } + return permissions.contains(permission); + } + public String getId() { return id; } diff -r 7d29ebd9c208 -r 524c7f359c4e webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Thu Dec 10 08:47:44 2009 +0100 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Wed Dec 23 07:59:16 2009 +0100 @@ -134,7 +134,7 @@ @QueryParam("loginID") @DefaultValue("") String loginId, @PathParam("id") String id, @QueryParam("move") @DefaultValue("-1") int move - ) { + ) throws IOException { Game g = findGame(id, move); if (canSee(g.getId(), loginId)) { return g; @@ -142,7 +142,7 @@ throw new WebApplicationException(Status.UNAUTHORIZED); } - private boolean canSee(GameId id, String loginId) { + private boolean canSee(GameId id, String loginId) throws IOException { if (!id.isFinished()) { return true; } @@ -156,6 +156,10 @@ if (logUser.equals(id.getBlack())) { return true; } + User info = quoridor.getUsers().getUserInfo(loginId, logUser); + if (info != null && info.hasPermission("games")) { + return true; + } return false; } @@ -204,7 +208,7 @@ public List listGames( @DefaultValue("") @QueryParam("loginID") String loginId, @DefaultValue("") @QueryParam("status") String status - ) { + ) throws IOException { List arr = new ArrayList(games.size()); for (Game g : games) { if (!canSee(g.getId(), loginId)) { diff -r 7d29ebd9c208 -r 524c7f359c4e webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java Thu Dec 10 08:47:44 2009 +0100 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java Wed Dec 23 07:59:16 2009 +0100 @@ -80,7 +80,11 @@ Properties p = getProp(id); User user = new User(id); for (String n : p.stringPropertyNames()) { - if (n.startsWith("permission.")) { + final String prefix = "permission."; + if (n.startsWith(prefix)) { + if ("true".equals(p.getProperty(n))) { + user.addPermission(n.substring(prefix.length())); + } continue; } if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) { diff -r 7d29ebd9c208 -r 524c7f359c4e webidor/src/test/java/cz/xelfi/quoridor/webidor/AllGamesTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/AllGamesTest.java Wed Dec 23 07:59:16 2009 +0100 @@ -0,0 +1,146 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.webidor; + +import com.sun.jersey.api.client.GenericType; +import java.util.List; +import com.sun.jersey.api.client.UniformInterfaceException; +import com.sun.jersey.test.framework.JerseyTest; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Properties; +import javax.ws.rs.core.MediaType; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author Jaroslav Tulach + */ +public class AllGamesTest extends JerseyTest { + static { + System.setProperty("JERSEY_HTTP_PORT", "45420"); + } + + private File dir; + + public AllGamesTest() throws Exception { + super("cz.xelfi.quoridor.webidor.resources"); + } + + @Override + public void setUp() throws Exception { + dir = File.createTempFile("quoridor", ".dir"); + dir.delete(); + System.setProperty("quoridor.dir", dir.getPath()); + dir.mkdirs(); + File passwd = new File(dir, "passwd"); + FileOutputStream os = new FileOutputStream(passwd); + os.write("Jarda=heslo\nJirka=pesko\nJan=pan\n".getBytes("UTF-8")); + os.close(); + super.setUp(); + } + + @Override + public void tearDown() throws Exception { + deleteRec(dir); + } + + static void deleteRec(File dir) throws IOException { + if (dir == null) { + return; + } + File[] arr = dir.listFiles(); + if (arr != null) { + for (File f : arr) { + deleteRec(f); + } + } + dir.delete(); + } + @Test public void testListGames() throws Exception { + File usersDir = new File(dir, "users"); + usersDir.mkdirs(); + File fJarda = new File(usersDir, "Jarda"); + { + Properties p = new Properties(); + p.setProperty("email", "jar@da.cz"); + p.setProperty("permission.games", "true"); + p.store(new FileOutputStream(fJarda), ""); + } + File fJirka = new File(usersDir, "Jirka"); + { + Properties p = new Properties(); + p.setProperty("email", "jir@ka.cz"); + p.store(new FileOutputStream(fJirka), ""); + } + File fJan = new File(usersDir, "Jan"); + { + Properties p = new Properties(); + p.setProperty("email", "j@an.cz"); + p.store(new FileOutputStream(fJan), ""); + } + + String logRoot = webResource.path("login"). + queryParam("name", "Jarda"). + queryParam("password", "heslo"). + accept(MediaType.TEXT_PLAIN). + put(String.class); + String logJirka = webResource.path("login"). + queryParam("name", "Jirka"). + queryParam("password", "pesko"). + accept(MediaType.TEXT_PLAIN). + put(String.class); + String logJan = webResource.path("login"). + queryParam("name", "Jan"). + queryParam("password", "pan"). + accept(MediaType.TEXT_PLAIN). + put(String.class); + + GameId s = webResource.path("games").queryParam("white", "Jan") + .queryParam("loginID", logJan) + .queryParam("black", "Jirka").post(GameId.class); + webResource.path("games/" + s.getId()) + .queryParam("loginID", logJan) + .queryParam("player", "Jan").queryParam("move", "RESIGN").put(GameId.class); + + GenericType> gType = new GenericType>() {}; + List all = webResource.path("games/").queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(gType); + boolean found = false; + for (GameId id : all) { + if (id.getId().equals(s.getId())) { + found = true; + break; + } + } + assertTrue("List of games shall contai all games: " + all, found); + + Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(Game.class); + assertEquals("One can see status of games with priviledges", GameStatus.blackWon, end.getId().getStatus()); + } +}