Cleaned up the fine-grained permissions system a bit so that Yohoho can make
use of it in good conscience. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5117 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1358,7 +1358,7 @@ public class ChatDirector extends BasicDirector
|
||||
|
||||
public boolean checkAccess (BodyObject user)
|
||||
{
|
||||
return user.checkAccess(ChatCodes.BROADCAST_ACCESS, null) == null;
|
||||
return user.checkAccess(ChatCodes.BROADCAST_ACCESS) == null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
package com.threerings.crowd.chat.data;
|
||||
|
||||
import com.threerings.presents.data.InvocationCodes;
|
||||
import com.threerings.presents.data.Permission;
|
||||
|
||||
import com.threerings.crowd.data.BodyObject;
|
||||
|
||||
@@ -40,13 +41,11 @@ public interface ChatCodes extends InvocationCodes
|
||||
/** The message identifier for a chat notification message. */
|
||||
public static final String CHAT_NOTIFICATION = "chat";
|
||||
|
||||
/** The access control identifier for normal chat privileges. See {@link
|
||||
* BodyObject#checkAccess}. */
|
||||
public static final String CHAT_ACCESS = "crowd.chat.chat";
|
||||
/** The access control identifier for normal chat privileges. */
|
||||
public static final Permission CHAT_ACCESS = new Permission();
|
||||
|
||||
/** The access control identifier for broadcast chat privileges. See {@link
|
||||
* BodyObject#checkAccess}. */
|
||||
public static final String BROADCAST_ACCESS = "crowd.chat.broadcast";
|
||||
/** The access control identifier for broadcast chat privileges. */
|
||||
public static final Permission BROADCAST_ACCESS = new Permission();
|
||||
|
||||
/** The configuration key for idle time. */
|
||||
public static final String IDLE_TIME_KEY = "narya.chat.idle_time";
|
||||
|
||||
@@ -52,7 +52,7 @@ import com.threerings.crowd.chat.data.UserMessage;
|
||||
* The chat provider handles the server side of the chat-related invocation services.
|
||||
*/
|
||||
public class ChatProvider
|
||||
implements ChatCodes, InvocationProvider
|
||||
implements InvocationProvider
|
||||
{
|
||||
/** Interface to allow an auto response to a tell message. */
|
||||
public static interface TellAutoResponder
|
||||
@@ -128,13 +128,10 @@ public class ChatProvider
|
||||
throws InvocationException
|
||||
{
|
||||
// ensure that the caller has normal chat privileges
|
||||
BodyObject source = (BodyObject)caller;
|
||||
String errmsg = source.checkAccess(CHAT_ACCESS, null);
|
||||
if (errmsg != null) {
|
||||
throw new InvocationException(errmsg);
|
||||
}
|
||||
InvocationException.requireAccess(caller, ChatCodes.CHAT_ACCESS);
|
||||
|
||||
// deliver the tell message to the target
|
||||
BodyObject source = (BodyObject)caller;
|
||||
deliverTell(createTellMessage(source, message), target, listener);
|
||||
|
||||
// inform the auto-responder if needed
|
||||
@@ -151,11 +148,8 @@ public class ChatProvider
|
||||
throws InvocationException
|
||||
{
|
||||
// make sure the requesting user has broadcast privileges
|
||||
InvocationException.requireAccess(caller, ChatCodes.BROADCAST_ACCESS);
|
||||
BodyObject body = (BodyObject)caller;
|
||||
String errmsg = body.checkAccess(BROADCAST_ACCESS, null);
|
||||
if (errmsg != null) {
|
||||
throw new InvocationException(errmsg);
|
||||
}
|
||||
broadcast(body.getVisibleName(), null, message, false, true);
|
||||
}
|
||||
|
||||
@@ -215,12 +209,12 @@ public class ChatProvider
|
||||
if (_chatForwarder != null && _chatForwarder.forwardTell(message, target, listener)) {
|
||||
return;
|
||||
}
|
||||
throw new InvocationException(USER_NOT_ONLINE);
|
||||
throw new InvocationException(ChatCodes.USER_NOT_ONLINE);
|
||||
}
|
||||
|
||||
if (tobj.status == OccupantInfo.DISCONNECTED) {
|
||||
String errmsg = MessageBundle.compose(
|
||||
USER_DISCONNECTED, TimeUtil.getTimeOrderString(
|
||||
ChatCodes.USER_DISCONNECTED, TimeUtil.getTimeOrderString(
|
||||
System.currentTimeMillis() - tobj.statusTime, TimeUtil.SECOND));
|
||||
throw new InvocationException(errmsg);
|
||||
}
|
||||
@@ -275,7 +269,7 @@ public class ChatProvider
|
||||
}
|
||||
|
||||
} else {
|
||||
SpeakUtil.sendSpeak(object, from, bundle, msg, BROADCAST_MODE);
|
||||
SpeakUtil.sendSpeak(object, from, bundle, msg, ChatCodes.BROADCAST_MODE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.threerings.util.Name;
|
||||
|
||||
import com.threerings.presents.data.ClientObject;
|
||||
import com.threerings.presents.data.InvocationCodes;
|
||||
import com.threerings.presents.data.Permission;
|
||||
|
||||
import com.threerings.crowd.chat.data.ChatCodes;
|
||||
import com.threerings.crowd.chat.data.SpeakObject;
|
||||
@@ -87,27 +88,6 @@ public class BodyObject extends ClientObject
|
||||
return (location == null) ? -1 : location.placeOid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether or not this user has access to the specified feature. Currently used by the
|
||||
* chat system to regulate access to chat broadcasts but also forms the basis of an extensible
|
||||
* fine-grained permissions system.
|
||||
*
|
||||
* @return null if the user has access, a fully-qualified translatable message string
|
||||
* indicating the reason for denial of access (or just {@link InvocationCodes#ACCESS_DENIED} if
|
||||
* you don't want to be specific).
|
||||
*/
|
||||
public String checkAccess (String feature, Object context)
|
||||
{
|
||||
// our default access control policy; how quaint
|
||||
if (ChatCodes.BROADCAST_ACCESS.equals(feature)) {
|
||||
return getTokens().isAdmin() ? null : ChatCodes.ACCESS_DENIED;
|
||||
} else if (ChatCodes.CHAT_ACCESS.equals(feature)) {
|
||||
return null;
|
||||
} else {
|
||||
return InvocationCodes.ACCESS_DENIED;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this user's access control tokens.
|
||||
*/
|
||||
@@ -157,6 +137,18 @@ public class BodyObject extends ClientObject
|
||||
setLocation(null);
|
||||
}
|
||||
|
||||
@Override // from ClientObject
|
||||
public String checkAccess (Permission perm, Object context)
|
||||
{
|
||||
if (perm == ChatCodes.BROADCAST_ACCESS) {
|
||||
return getTokens().isAdmin() ? null : ChatCodes.ACCESS_DENIED;
|
||||
} else if (perm == ChatCodes.CHAT_ACCESS) {
|
||||
return null;
|
||||
} else {
|
||||
return super.checkAccess(perm, context);
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void applyToListeners (ListenerOp op)
|
||||
{
|
||||
|
||||
@@ -24,8 +24,9 @@ package com.threerings.crowd.data;
|
||||
import com.threerings.io.SimpleStreamableObject;
|
||||
|
||||
/**
|
||||
* Defines access control tokens that convey certain privileges to users (see {@link
|
||||
* BodyObject#checkAccess}).
|
||||
* Defines access control tokens that convey certain privileges to users.
|
||||
*
|
||||
* @see BodyObject#checkAccess
|
||||
*/
|
||||
public class TokenRing extends SimpleStreamableObject
|
||||
{
|
||||
|
||||
@@ -27,10 +27,8 @@ import com.threerings.presents.dobj.DObject;
|
||||
import com.threerings.presents.dobj.DSet;
|
||||
|
||||
/**
|
||||
* Every client in the system has an associated client object to which
|
||||
* only they subscribe. The client object can be used to deliver messages
|
||||
* solely to a particular client as well as to publish client-specific
|
||||
* data.
|
||||
* A distributed object to which only the client subscribes. Used to deliver messages solely to a
|
||||
* particular client as well as to publish client-specific data.
|
||||
*/
|
||||
public class ClientObject extends DObject
|
||||
{
|
||||
@@ -39,14 +37,12 @@ public class ClientObject extends DObject
|
||||
public static final String RECEIVERS = "receivers";
|
||||
// AUTO-GENERATED: FIELDS END
|
||||
|
||||
/** The name of a message event delivered to the client when they
|
||||
* switch usernames (and therefore user objects). */
|
||||
/** The name of a message event delivered to the client when they switch usernames (and
|
||||
* therefore user objects). */
|
||||
public static final String CLOBJ_CHANGED = "!clobj_changed!";
|
||||
|
||||
/** Used to publish all invocation service receivers registered on
|
||||
* this client. */
|
||||
public DSet<InvocationReceiver.Registration> receivers =
|
||||
new DSet<InvocationReceiver.Registration>();
|
||||
/** Used to publish all invocation service receivers registered on this client. */
|
||||
public DSet<InvocationReceiver.Registration> receivers = DSet.newDSet();
|
||||
|
||||
/**
|
||||
* Returns a short string identifying this client.
|
||||
@@ -56,6 +52,27 @@ public class ClientObject extends DObject
|
||||
return "(" + getOid() + ")";
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether or not this client has access to the specified feature. Forms the basis of an
|
||||
* extensible fine-grained permissions system.
|
||||
*
|
||||
* @return null if the user has access, a fully-qualified translatable message string
|
||||
* indicating the reason for denial of access (or just {@link InvocationCodes#ACCESS_DENIED} if
|
||||
* you don't want to be specific).
|
||||
*/
|
||||
public String checkAccess (Permission feature, Object context)
|
||||
{
|
||||
return InvocationCodes.ACCESS_DENIED;
|
||||
}
|
||||
|
||||
/**
|
||||
* A version of {@link #checkAccess(Permission,Object} that provides no context.
|
||||
*/
|
||||
public String checkAccess (Permission feature)
|
||||
{
|
||||
return checkAccess(feature, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for reference counting client objects, adds a reference to
|
||||
* this object.
|
||||
@@ -81,9 +98,6 @@ public class ClientObject extends DObject
|
||||
return (--_references > 0);
|
||||
}
|
||||
|
||||
/** Used to reference count resolved client objects. */
|
||||
protected transient int _references;
|
||||
|
||||
// AUTO-GENERATED: METHODS START
|
||||
/**
|
||||
* Requests that the specified entry be added to the
|
||||
@@ -133,4 +147,7 @@ public class ClientObject extends DObject
|
||||
this.receivers = clone;
|
||||
}
|
||||
// AUTO-GENERATED: METHODS END
|
||||
|
||||
/** Used to reference count resolved client objects. */
|
||||
protected transient int _references;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://www.threerings.net/code/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.presents.data;
|
||||
|
||||
/**
|
||||
* A value class used by {@link ClientObject#checkAccess} to do fine-grained access control.
|
||||
*/
|
||||
public class Permission
|
||||
{
|
||||
}
|
||||
@@ -65,6 +65,22 @@ public class DSet<E extends DSet.Entry>
|
||||
public Comparable getKey ();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new DSet of the appropriate generic type.
|
||||
*/
|
||||
public static <E extends DSet.Entry> DSet<E> newDSet ()
|
||||
{
|
||||
return new DSet<E>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new DSet of the appropriate generic type.
|
||||
*/
|
||||
public static <E extends DSet.Entry> DSet<E> newDSet (Iterable<E> source)
|
||||
{
|
||||
return new DSet<E>(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a distributed set and populates it with values from the supplied iterator. This
|
||||
* should be done before the set is unleashed into the wild distributed object world because no
|
||||
|
||||
@@ -23,11 +23,37 @@ package com.threerings.presents.server;
|
||||
|
||||
import com.threerings.util.MessageBundle;
|
||||
|
||||
import com.threerings.presents.data.ClientObject;
|
||||
import com.threerings.presents.data.Permission;
|
||||
|
||||
/**
|
||||
* Used to report failures when executing service requests.
|
||||
*/
|
||||
public class InvocationException extends Exception
|
||||
{
|
||||
/**
|
||||
* Requires that the specified client have the specified permissions.
|
||||
*
|
||||
* @throws InvocationException if they do not.
|
||||
*/
|
||||
public static void requireAccess (ClientObject clobj, Permission perm, Object context)
|
||||
throws InvocationException
|
||||
{
|
||||
String errmsg = clobj.checkAccess(perm, context);
|
||||
if (errmsg != null) {
|
||||
throw new InvocationException(errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A version of {@link #requireAccess} that takes no context.
|
||||
*/
|
||||
public static void requireAccess (ClientObject clobj, Permission perm)
|
||||
throws InvocationException
|
||||
{
|
||||
requireAccess(clobj, perm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an invocation exception with the supplied cause code
|
||||
* string.
|
||||
|
||||
Reference in New Issue
Block a user