webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 25 Apr 2010 21:41:00 +0200
changeset 239 a47345ebbdd7
parent 189 6245e1b634aa
child 264 d60370059c3c
permissions -rw-r--r--
Password can be stored in user properties
jaroslav@143
     1
/*
jaroslav@143
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@143
     3
 *
jaroslav@143
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@143
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@143
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@143
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@143
     8
 * License. You can obtain a copy of the License at
jaroslav@143
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@143
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@143
    11
 * specific language governing permissions and limitations under the
jaroslav@143
    12
 * License.  When distributing the software, include this License Header
jaroslav@143
    13
 * Notice in each file and include the License file at
jaroslav@143
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@143
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@143
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@143
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@143
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@143
    19
 * your own identifying information:
jaroslav@143
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@143
    21
 *
jaroslav@143
    22
 * Contributor(s):
jaroslav@143
    23
 *
jaroslav@143
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@143
    25
 */
jaroslav@143
    26
jaroslav@143
    27
package cz.xelfi.quoridor.webidor.resources;
jaroslav@143
    28
jaroslav@189
    29
import com.sun.jersey.api.json.JSONWithPadding;
jaroslav@143
    30
import cz.xelfi.quoridor.webidor.*;
jaroslav@143
    31
import java.io.File;
jaroslav@143
    32
import java.io.FileInputStream;
jaroslav@143
    33
import java.io.FileNotFoundException;
jaroslav@144
    34
import java.io.FileOutputStream;
jaroslav@143
    35
import java.io.IOException;
jaroslav@143
    36
import java.util.Properties;
jaroslav@143
    37
import java.util.logging.Logger;
jaroslav@143
    38
import javax.ws.rs.GET;
jaroslav@144
    39
import javax.ws.rs.POST;
jaroslav@143
    40
import javax.ws.rs.Path;
jaroslav@143
    41
import javax.ws.rs.PathParam;
jaroslav@143
    42
import javax.ws.rs.Produces;
jaroslav@143
    43
import javax.ws.rs.QueryParam;
jaroslav@143
    44
import javax.ws.rs.core.MediaType;
jaroslav@144
    45
import javax.ws.rs.core.Response;
jaroslav@143
    46
jaroslav@143
    47
/**
jaroslav@143
    48
 *
jaroslav@143
    49
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@143
    50
 */
jaroslav@143
    51
public final class Users {
jaroslav@143
    52
    private final Quoridor quoridor;
jaroslav@143
    53
    private final File dir;
jaroslav@143
    54
    private static final Logger LOG = Logger.getLogger(Users.class.getName());
jaroslav@143
    55
jaroslav@143
    56
    Users(File dir, Quoridor quoridor) {
jaroslav@143
    57
        this.dir = dir;
jaroslav@143
    58
        this.quoridor = quoridor;
jaroslav@143
    59
        dir.mkdirs();
jaroslav@143
    60
    }
jaroslav@143
    61
jaroslav@143
    62
    @GET
jaroslav@189
    63
    @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@189
    64
    public JSONWithPadding getOwnInfo(
jaroslav@189
    65
        @QueryParam("callback") String callback,
jaroslav@189
    66
        @QueryParam("loginID") String loginId
jaroslav@145
    67
    ) throws IOException {
jaroslav@189
    68
        return getUserInfo(callback, loginId, null);
jaroslav@145
    69
    }
jaroslav@189
    70
jaroslav@189
    71
    User getUserInfo(
jaroslav@143
    72
        @QueryParam("loginID") String loginId,
jaroslav@143
    73
        @PathParam("id") String id
jaroslav@143
    74
    ) throws IOException {
jaroslav@143
    75
        String  myid = quoridor.isLoggedIn(loginId);
jaroslav@145
    76
        if (id == null) {
jaroslav@145
    77
            id = myid;
jaroslav@145
    78
        }
jaroslav@143
    79
        Properties myp = getProp(myid);
jaroslav@143
    80
        Properties p = getProp(id);
jaroslav@143
    81
        User user = new User(id);
jaroslav@143
    82
        for (String n : p.stringPropertyNames()) {
jaroslav@171
    83
            final String prefix = "permission.";
jaroslav@171
    84
            if (n.startsWith(prefix)) {
jaroslav@171
    85
                if ("true".equals(p.getProperty(n))) {
jaroslav@171
    86
                    user.addPermission(n.substring(prefix.length()));
jaroslav@171
    87
                }
jaroslav@143
    88
                continue;
jaroslav@143
    89
            }
jaroslav@143
    90
            if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
jaroslav@143
    91
                continue;
jaroslav@143
    92
            }
jaroslav@143
    93
            user.addProperty(n, p.getProperty(n));
jaroslav@143
    94
        }
jaroslav@143
    95
        return user;
jaroslav@143
    96
    }
jaroslav@143
    97
jaroslav@189
    98
    @GET
jaroslav@189
    99
    @Path("{id}")
jaroslav@189
   100
    @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@189
   101
    public JSONWithPadding getUserInfo(
jaroslav@189
   102
        @QueryParam("callback") String callback,
jaroslav@189
   103
        @QueryParam("loginID") String loginId,
jaroslav@189
   104
        @PathParam("id") String id
jaroslav@189
   105
    ) throws IOException {
jaroslav@189
   106
        return new JSONWithPadding(getUserInfo(loginId, id), callback);
jaroslav@189
   107
    }
jaroslav@189
   108
jaroslav@144
   109
    @POST
jaroslav@144
   110
    @Path("{id}")
jaroslav@144
   111
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@144
   112
    public synchronized Response changeProperty(
jaroslav@144
   113
        @QueryParam("loginID") String loginId,
jaroslav@144
   114
        @PathParam("id") String id,
jaroslav@144
   115
        @QueryParam("name") String name,
jaroslav@144
   116
        @QueryParam("value") String value
jaroslav@144
   117
    ) throws IOException {
jaroslav@144
   118
        String myid = quoridor.isLoggedIn(loginId);
jaroslav@144
   119
        if (!id.equals(myid) || name.startsWith("permission.")) {
jaroslav@144
   120
            return Response.status(Response.Status.UNAUTHORIZED).build();
jaroslav@144
   121
        }
jaroslav@144
   122
jaroslav@144
   123
        Properties p = getProp(myid);
jaroslav@144
   124
        p.setProperty(name, value);
jaroslav@144
   125
jaroslav@144
   126
        File f = new File(dir, id);
jaroslav@144
   127
        FileOutputStream os = new FileOutputStream(f);
jaroslav@144
   128
        p.store(os, "");
jaroslav@144
   129
        os.close();
jaroslav@144
   130
        
jaroslav@189
   131
        return Response.ok().entity(getUserInfo(null, loginId, id).getJsonSource()).build();
jaroslav@144
   132
    }
jaroslav@144
   133
jtulach@239
   134
    final boolean verifyPassword(String id, String passwd) throws IOException {
jtulach@239
   135
        Properties p = getProp(id);
jtulach@239
   136
        if (p != null) {
jtulach@239
   137
            return passwd.equals(p.getProperty("passwd"));
jtulach@239
   138
        }
jtulach@239
   139
        return false;
jtulach@239
   140
    }
jtulach@239
   141
jaroslav@144
   142
    private synchronized Properties getProp(String id) throws FileNotFoundException, IOException {
jaroslav@143
   143
        Properties p = new Properties();
jaroslav@143
   144
        if (id != null && id.length() > 0) {
jaroslav@143
   145
            File f = new File(dir, id);
jaroslav@145
   146
            if (f.exists()) {
jaroslav@145
   147
                FileInputStream is = new FileInputStream(f);
jaroslav@145
   148
                p.load(is);
jaroslav@145
   149
                is.close();
jaroslav@145
   150
            }
jaroslav@143
   151
        }
jaroslav@143
   152
        return p;
jaroslav@143
   153
    }
jaroslav@143
   154
}