Adding support for 'permission.games' to allow special roles to enlist all the available games
1.1 --- a/webidor/pom.xml Thu Dec 10 08:47:44 2009 +0100
1.2 +++ b/webidor/pom.xml Wed Dec 23 07:59:16 2009 +0100
1.3 @@ -9,7 +9,7 @@
1.4 <groupId>org.apidesign</groupId>
1.5 <artifactId>webidor</artifactId>
1.6 <packaging>jar</packaging>
1.7 - <version>1.10</version>
1.8 + <version>1.11</version>
1.9 <name>webidor server</name>
1.10 <url>http://maven.apache.org</url>
1.11 <repositories>
2.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java Thu Dec 10 08:47:44 2009 +0100
2.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java Wed Dec 23 07:59:16 2009 +0100
2.3 @@ -28,6 +28,8 @@
2.4
2.5 import java.util.ArrayList;
2.6 import java.util.List;
2.7 +import java.util.Set;
2.8 +import java.util.TreeSet;
2.9 import javax.xml.bind.annotation.XmlAccessType;
2.10 import javax.xml.bind.annotation.XmlAccessorType;
2.11 import javax.xml.bind.annotation.XmlAttribute;
2.12 @@ -46,6 +48,7 @@
2.13 private String id;
2.14 @XmlElement(name="property")
2.15 private List<Property> properties;
2.16 + private transient Set<String> permissions;
2.17
2.18 User() {
2.19 }
2.20 @@ -61,6 +64,13 @@
2.21 properties.add(new Property(name, value));
2.22 }
2.23
2.24 + public void addPermission(String permission) {
2.25 + if (permissions == null) {
2.26 + permissions = new TreeSet<String>();
2.27 + }
2.28 + permissions.add(permission);
2.29 + }
2.30 +
2.31 public String getProperty(String name) {
2.32 if (properties == null) {
2.33 return null;
2.34 @@ -73,6 +83,13 @@
2.35 return null;
2.36 }
2.37
2.38 + public boolean hasPermission(String permission) {
2.39 + if (permissions == null) {
2.40 + return false;
2.41 + }
2.42 + return permissions.contains(permission);
2.43 + }
2.44 +
2.45 public String getId() {
2.46 return id;
2.47 }
3.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Thu Dec 10 08:47:44 2009 +0100
3.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Wed Dec 23 07:59:16 2009 +0100
3.3 @@ -134,7 +134,7 @@
3.4 @QueryParam("loginID") @DefaultValue("") String loginId,
3.5 @PathParam("id") String id,
3.6 @QueryParam("move") @DefaultValue("-1") int move
3.7 - ) {
3.8 + ) throws IOException {
3.9 Game g = findGame(id, move);
3.10 if (canSee(g.getId(), loginId)) {
3.11 return g;
3.12 @@ -142,7 +142,7 @@
3.13 throw new WebApplicationException(Status.UNAUTHORIZED);
3.14 }
3.15
3.16 - private boolean canSee(GameId id, String loginId) {
3.17 + private boolean canSee(GameId id, String loginId) throws IOException {
3.18 if (!id.isFinished()) {
3.19 return true;
3.20 }
3.21 @@ -156,6 +156,10 @@
3.22 if (logUser.equals(id.getBlack())) {
3.23 return true;
3.24 }
3.25 + User info = quoridor.getUsers().getUserInfo(loginId, logUser);
3.26 + if (info != null && info.hasPermission("games")) {
3.27 + return true;
3.28 + }
3.29 return false;
3.30 }
3.31
3.32 @@ -204,7 +208,7 @@
3.33 public List<GameId> listGames(
3.34 @DefaultValue("") @QueryParam("loginID") String loginId,
3.35 @DefaultValue("") @QueryParam("status") String status
3.36 - ) {
3.37 + ) throws IOException {
3.38 List<GameId> arr = new ArrayList<GameId>(games.size());
3.39 for (Game g : games) {
3.40 if (!canSee(g.getId(), loginId)) {
4.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java Thu Dec 10 08:47:44 2009 +0100
4.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java Wed Dec 23 07:59:16 2009 +0100
4.3 @@ -80,7 +80,11 @@
4.4 Properties p = getProp(id);
4.5 User user = new User(id);
4.6 for (String n : p.stringPropertyNames()) {
4.7 - if (n.startsWith("permission.")) {
4.8 + final String prefix = "permission.";
4.9 + if (n.startsWith(prefix)) {
4.10 + if ("true".equals(p.getProperty(n))) {
4.11 + user.addPermission(n.substring(prefix.length()));
4.12 + }
4.13 continue;
4.14 }
4.15 if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/AllGamesTest.java Wed Dec 23 07:59:16 2009 +0100
5.3 @@ -0,0 +1,146 @@
5.4 +/*
5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5.6 + *
5.7 + * The contents of this file are subject to the terms of either the GNU
5.8 + * General Public License Version 2 only ("GPL") or the Common
5.9 + * Development and Distribution License("CDDL") (collectively, the
5.10 + * "License"). You may not use this file except in compliance with the
5.11 + * License. You can obtain a copy of the License at
5.12 + * http://www.netbeans.org/cddl-gplv2.html
5.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
5.14 + * specific language governing permissions and limitations under the
5.15 + * License. When distributing the software, include this License Header
5.16 + * Notice in each file and include the License file at
5.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
5.18 + * particular file as subject to the "Classpath" exception as provided
5.19 + * by Sun in the GPL Version 2 section of the License file that
5.20 + * accompanied this code. If applicable, add the following below the
5.21 + * License Header, with the fields enclosed by brackets [] replaced by
5.22 + * your own identifying information:
5.23 + * "Portions Copyrighted [year] [name of copyright owner]"
5.24 + *
5.25 + * Contributor(s):
5.26 + *
5.27 + * Portions Copyrighted 2009 Jaroslav Tulach
5.28 + */
5.29 +
5.30 +package cz.xelfi.quoridor.webidor;
5.31 +
5.32 +import com.sun.jersey.api.client.GenericType;
5.33 +import java.util.List;
5.34 +import com.sun.jersey.api.client.UniformInterfaceException;
5.35 +import com.sun.jersey.test.framework.JerseyTest;
5.36 +import java.io.File;
5.37 +import java.io.FileOutputStream;
5.38 +import java.io.IOException;
5.39 +import java.util.Properties;
5.40 +import javax.ws.rs.core.MediaType;
5.41 +import org.junit.Test;
5.42 +import static org.junit.Assert.*;
5.43 +
5.44 +/**
5.45 + *
5.46 + * @author Jaroslav Tulach <jtulach@netbeans.org>
5.47 + */
5.48 +public class AllGamesTest extends JerseyTest {
5.49 + static {
5.50 + System.setProperty("JERSEY_HTTP_PORT", "45420");
5.51 + }
5.52 +
5.53 + private File dir;
5.54 +
5.55 + public AllGamesTest() throws Exception {
5.56 + super("cz.xelfi.quoridor.webidor.resources");
5.57 + }
5.58 +
5.59 + @Override
5.60 + public void setUp() throws Exception {
5.61 + dir = File.createTempFile("quoridor", ".dir");
5.62 + dir.delete();
5.63 + System.setProperty("quoridor.dir", dir.getPath());
5.64 + dir.mkdirs();
5.65 + File passwd = new File(dir, "passwd");
5.66 + FileOutputStream os = new FileOutputStream(passwd);
5.67 + os.write("Jarda=heslo\nJirka=pesko\nJan=pan\n".getBytes("UTF-8"));
5.68 + os.close();
5.69 + super.setUp();
5.70 + }
5.71 +
5.72 + @Override
5.73 + public void tearDown() throws Exception {
5.74 + deleteRec(dir);
5.75 + }
5.76 +
5.77 + static void deleteRec(File dir) throws IOException {
5.78 + if (dir == null) {
5.79 + return;
5.80 + }
5.81 + File[] arr = dir.listFiles();
5.82 + if (arr != null) {
5.83 + for (File f : arr) {
5.84 + deleteRec(f);
5.85 + }
5.86 + }
5.87 + dir.delete();
5.88 + }
5.89 + @Test public void testListGames() throws Exception {
5.90 + File usersDir = new File(dir, "users");
5.91 + usersDir.mkdirs();
5.92 + File fJarda = new File(usersDir, "Jarda");
5.93 + {
5.94 + Properties p = new Properties();
5.95 + p.setProperty("email", "jar@da.cz");
5.96 + p.setProperty("permission.games", "true");
5.97 + p.store(new FileOutputStream(fJarda), "");
5.98 + }
5.99 + File fJirka = new File(usersDir, "Jirka");
5.100 + {
5.101 + Properties p = new Properties();
5.102 + p.setProperty("email", "jir@ka.cz");
5.103 + p.store(new FileOutputStream(fJirka), "");
5.104 + }
5.105 + File fJan = new File(usersDir, "Jan");
5.106 + {
5.107 + Properties p = new Properties();
5.108 + p.setProperty("email", "j@an.cz");
5.109 + p.store(new FileOutputStream(fJan), "");
5.110 + }
5.111 +
5.112 + String logRoot = webResource.path("login").
5.113 + queryParam("name", "Jarda").
5.114 + queryParam("password", "heslo").
5.115 + accept(MediaType.TEXT_PLAIN).
5.116 + put(String.class);
5.117 + String logJirka = webResource.path("login").
5.118 + queryParam("name", "Jirka").
5.119 + queryParam("password", "pesko").
5.120 + accept(MediaType.TEXT_PLAIN).
5.121 + put(String.class);
5.122 + String logJan = webResource.path("login").
5.123 + queryParam("name", "Jan").
5.124 + queryParam("password", "pan").
5.125 + accept(MediaType.TEXT_PLAIN).
5.126 + put(String.class);
5.127 +
5.128 + GameId s = webResource.path("games").queryParam("white", "Jan")
5.129 + .queryParam("loginID", logJan)
5.130 + .queryParam("black", "Jirka").post(GameId.class);
5.131 + webResource.path("games/" + s.getId())
5.132 + .queryParam("loginID", logJan)
5.133 + .queryParam("player", "Jan").queryParam("move", "RESIGN").put(GameId.class);
5.134 +
5.135 + GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
5.136 + List<GameId> all = webResource.path("games/").queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(gType);
5.137 + boolean found = false;
5.138 + for (GameId id : all) {
5.139 + if (id.getId().equals(s.getId())) {
5.140 + found = true;
5.141 + break;
5.142 + }
5.143 + }
5.144 + assertTrue("List of games shall contai all games: " + all, found);
5.145 +
5.146 + Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(Game.class);
5.147 + assertEquals("One can see status of games with priviledges", GameStatus.blackWon, end.getId().getStatus());
5.148 + }
5.149 +}