Author: ngn
Date: Wed Sep  9 20:55:14 2009
New Revision: 813114

URL: http://svn.apache.org/viewvc?rev=813114&view=rev
Log:
Implement affiliation support in room includinbg handling of banned users 
trying to enter room

Added:
    
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/Affiliations.java
    
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/AffiliationsTestCase.java
Modified:
    
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/handler/MUCPresenceHandler.java
    
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/Room.java
    
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/handler/MUCPresenceHandlerEnterRoomTestCase.java

Modified: 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/handler/MUCPresenceHandler.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/handler/MUCPresenceHandler.java?rev=813114&r1=813113&r2=813114&view=diff
==============================================================================
--- 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/handler/MUCPresenceHandler.java
 (original)
+++ 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/handler/MUCPresenceHandler.java
 Wed Sep  9 20:55:14 2009
@@ -185,6 +185,10 @@
             }
             
             Occupant newOccupant = room.addOccupant(newOccupantJid, nick);
+            if(newOccupant == null) {
+                // outcast
+                return createPresenceErrorStanza(roomJid, newOccupantJid, 
stanza.getID(), "auth", "forbidden");
+            }
             
             // relay presence of all existing room occupants to the now joined 
occupant
             for(Occupant occupant : room.getOccupants()) {

Added: 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/Affiliations.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/Affiliations.java?rev=813114&view=auto
==============================================================================
--- 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/Affiliations.java
 (added)
+++ 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/Affiliations.java
 Wed Sep  9 20:55:14 2009
@@ -0,0 +1,50 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.vysper.xmpp.modules.extension.xep0045_muc.model;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.vysper.xmpp.addressing.Entity;
+
+
+/**
+ * Describes the persistent affiliations for a room
+ *
+ * @author The Apache MINA Project ([email protected])
+ */
+public class Affiliations {
+
+    // TODO should be loaded from the storage provider
+    // keyed by bare JIDs
+    private Map<Entity, Affiliation> affiliations = new HashMap<Entity, 
Affiliation>();
+    
+    public void add(Entity user, Affiliation affiliation) {
+        affiliations.put(user.getBareJID(), affiliation);
+    }
+    
+    public void remove(Entity user) {
+        affiliations.remove(user.getBareJID());
+    }
+    
+    public Affiliation getAffiliation(Entity user) {
+        return affiliations.get(user.getBareJID());
+    }
+}

Modified: 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/Room.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/Room.java?rev=813114&r1=813113&r2=813114&view=diff
==============================================================================
--- 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/Room.java
 (original)
+++ 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/Room.java
 Wed Sep  9 20:55:14 2009
@@ -55,6 +55,7 @@
     private String name;
     private String password;
     private DiscussionHistory history = new DiscussionHistory();
+    private Affiliations affiliations = new Affiliations();
     
     // keep in a map to allow for quick access
     private Map<Entity, Occupant> occupants = new ConcurrentHashMap<Entity, 
Occupant>();
@@ -103,8 +104,14 @@
     }
 
     public Occupant addOccupant(Entity occupantJid, String name) {
-        // TODO uses a default Affiliation.None, later to be looked up based 
on the user
-        Affiliation affiliation = Affiliation.None;
+        Affiliation affiliation = affiliations.getAffiliation(occupantJid);
+        
+        if(affiliation == Affiliation.Outcast) {
+            return null;
+        }
+        
+        // default to none
+        if(affiliation == null) affiliation = Affiliation.None;
         Role role = Role.getRole(affiliation, roomTypes);
         Occupant occupant = new Occupant(occupantJid, name, affiliation, 
role); 
         occupants.put(occupantJid, occupant);
@@ -192,5 +199,9 @@
     public DiscussionHistory getHistory() {
         return history;
     }
-    
+
+    public Affiliations getAffiliations() {
+        return affiliations;
+    }
+
 }

Modified: 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/handler/MUCPresenceHandlerEnterRoomTestCase.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/handler/MUCPresenceHandlerEnterRoomTestCase.java?rev=813114&r1=813113&r2=813114&view=diff
==============================================================================
--- 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/handler/MUCPresenceHandlerEnterRoomTestCase.java
 (original)
+++ 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/handler/MUCPresenceHandlerEnterRoomTestCase.java
 Wed Sep  9 20:55:14 2009
@@ -105,6 +105,27 @@
         assertEquals("nick", occupant.getName());
     }
 
+    public void testEnterAsAdmin() throws Exception {
+        Room room = conference.findRoom(ROOM1_JID);
+        room.getAffiliations().add(OCCUPANT1_JID, Affiliation.Admin);
+
+        enterRoom(OCCUPANT1_JID, ROOM1_JID_WITH_NICK);
+
+        Occupant occupant = room.getOccupants().iterator().next();
+        
+        assertEquals(Affiliation.Admin, occupant.getAffiliation());
+    }
+
+    public void testEnterAsOutcast() throws Exception {
+        Room room = conference.findRoom(ROOM1_JID);
+        room.getAffiliations().add(OCCUPANT1_JID, Affiliation.Outcast);
+
+        Stanza error = enterRoom(OCCUPANT1_JID, ROOM1_JID_WITH_NICK);
+        assertPresenceErrorStanza(error, ROOM1_JID, OCCUPANT1_JID, "auth", 
"forbidden");
+
+        assertEquals(0, room.getOccupants().size());
+    }
+
     
     public void testEnterRoomWithDuplicateNick() throws Exception {
         assertNull(enterRoom(OCCUPANT1_JID, ROOM1_JID_WITH_NICK));

Added: 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/AffiliationsTestCase.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/AffiliationsTestCase.java?rev=813114&view=auto
==============================================================================
--- 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/AffiliationsTestCase.java
 (added)
+++ 
mina/sandbox/vysper/trunk/server/extensions/xep0045-muc/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/AffiliationsTestCase.java
 Wed Sep  9 20:55:14 2009
@@ -0,0 +1,69 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.vysper.xmpp.modules.extension.xep0045_muc.model;
+
+import junit.framework.TestCase;
+
+import org.apache.vysper.TestUtil;
+import org.apache.vysper.xmpp.addressing.Entity;
+
+/**
+ * 
+ * @author The Apache MINA Project ([email protected])
+ */
+public class AffiliationsTestCase extends TestCase {
+    
+    private static final Entity JID1 = 
TestUtil.parseUnchecked("[email protected]/res");
+    private static final Entity JID2 = 
TestUtil.parseUnchecked("[email protected]/other");
+    private static final Entity JID3 = 
TestUtil.parseUnchecked("[email protected]/res");
+
+    public void testAddAndGet() {
+        Affiliations affiliations = new Affiliations();
+        affiliations.add(JID1, Affiliation.Admin);
+        
+        assertEquals(Affiliation.Admin, affiliations.getAffiliation(JID1));
+        // get with different resource
+        assertEquals(Affiliation.Admin, affiliations.getAffiliation(JID2));
+
+        assertEquals(null, affiliations.getAffiliation(JID3));
+    }
+
+    public void testUpdate() {
+        Affiliations affiliations = new Affiliations();
+        affiliations.add(JID1, Affiliation.Admin);
+        
+        assertEquals(Affiliation.Admin, affiliations.getAffiliation(JID1));
+        // add with different resource and affiliation
+        affiliations.add(JID2, Affiliation.Member);
+        
+        assertEquals(Affiliation.Member, affiliations.getAffiliation(JID1));
+    }
+
+    public void testRemove() {
+        Affiliations affiliations = new Affiliations();
+        affiliations.add(JID1, Affiliation.Admin);
+        
+        assertEquals(Affiliation.Admin, affiliations.getAffiliation(JID1));
+        
+        affiliations.remove(JID2);
+
+        assertEquals(null, affiliations.getAffiliation(JID1));
+    }
+}


Reply via email to