2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://www.gnu.org/licenses/.
18 package cz.xelfi.quoridor.webidor.resources;
20 import com.sun.jersey.api.json.JSONWithPadding;
21 import cz.xelfi.quoridor.webidor.*;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.Properties;
28 import java.util.logging.Logger;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.QueryParam;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
40 * @author Jaroslav Tulach <jtulach@netbeans.org>
42 public final class Users {
43 private final Quoridor quoridor;
44 private final File dir;
45 private static final Logger LOG = Logger.getLogger(Users.class.getName());
47 Users(File dir, Quoridor quoridor) {
49 this.quoridor = quoridor;
54 @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
55 public JSONWithPadding getOwnInfo(
56 @QueryParam("callback") String callback,
57 @QueryParam("loginID") String loginId
58 ) throws IOException {
59 return getUserInfo(callback, loginId, null);
63 @QueryParam("loginID") String loginId,
64 @PathParam("id") String id
65 ) throws IOException {
66 String myid = quoridor.isLoggedIn(loginId);
70 Properties myp = getProp(myid);
71 Properties p = getProp(id);
72 User user = new User(id);
73 for (String n : p.stringPropertyNames()) {
74 final String prefix = "permission.";
75 if (n.startsWith(prefix)) {
76 if ("true".equals(p.getProperty(n))) {
77 user.addPermission(n.substring(prefix.length()));
81 if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
84 user.addProperty(n, p.getProperty(n));
91 @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
92 public JSONWithPadding getUserInfo(
93 @QueryParam("callback") String callback,
94 @QueryParam("loginID") String loginId,
95 @PathParam("id") String id
96 ) throws IOException {
97 return new JSONWithPadding(getUserInfo(loginId, id), callback);
102 @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
103 public synchronized Response changeProperty(
104 @QueryParam("loginID") String loginId,
105 @PathParam("id") String id,
106 @QueryParam("name") String name,
107 @QueryParam("value") String value
108 ) throws IOException {
109 String myid = quoridor.isLoggedIn(loginId);
110 if (!id.equals(myid) || name.startsWith("permission.")) {
111 return Response.status(Response.Status.UNAUTHORIZED).build();
114 Properties p = getProp(myid);
115 p.setProperty(name, value);
117 File f = new File(dir, id);
118 FileOutputStream os = new FileOutputStream(f);
122 return Response.ok().entity(getUserInfo(null, loginId, id).getJsonSource()).build();
125 final boolean verifyPassword(String id, String passwd) throws IOException {
126 Properties p = getProp(id);
128 return passwd.equals(p.getProperty("passwd"));
133 private synchronized Properties getProp(String id) throws FileNotFoundException, IOException {
134 Properties p = new Properties();
135 if (id != null && id.length() > 0) {
136 File f = new File(dir, id);
138 FileInputStream is = new FileInputStream(f);