Index: src/main/java/com/xpn/xwiki/user/impl/xwiki/XWikiRightServiceImpl.java
===================================================================
--- src/main/java/com/xpn/xwiki/user/impl/xwiki/XWikiRightServiceImpl.java	(revision 24987)
+++ src/main/java/com/xpn/xwiki/user/impl/xwiki/XWikiRightServiceImpl.java	(working copy)
@@ -24,7 +24,9 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Vector;
@@ -35,6 +37,7 @@
 import org.apache.commons.logging.LogFactory;
 import org.xwiki.bridge.DocumentName;
 import org.xwiki.bridge.DocumentNameFactory;
+import org.xwiki.bridge.DocumentNameSerializer;
 
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
@@ -58,10 +61,20 @@
     private static final Log LOG = LogFactory.getLog(XWikiRightServiceImpl.class);
 
     private static final List<String> ALLLEVELS =
-        Arrays.asList("admin", "view", "edit", "comment", "delete", "undelete", "register", "programming");
+            Arrays.asList("admin", "view", "edit", "comment", "delete", "undelete", "register", "programming");
 
     private static Map<String, String> actionMap;
 
+    /**
+     * Used to convert a string into a proper Document Name.
+     */
+    private DocumentNameFactory documentNameFactory = Utils.getComponent(DocumentNameFactory.class);
+
+    /**
+     * Used to convert a proper Document Name to string.
+     */
+    private DocumentNameSerializer documentNameSerializer = Utils.getComponent(DocumentNameSerializer.class);
+
     protected void logAllow(String username, String page, String action, String info)
     {
         if (LOG.isDebugEnabled()) {
@@ -305,13 +318,14 @@
         boolean found = false;
 
         // Get the userdb and the shortname
-        String userdatabase = null;
+        DocumentName entityDocumentName = this.documentNameFactory.createDocumentName(name);
+
         String shortname = name;
         int i0 = name.indexOf(":");
         if (i0 != -1) {
-            userdatabase = name.substring(0, i0);
             shortname = name.substring(i0 + 1);
         }
+        String prefixedFullName = this.documentNameSerializer.serialize(entityDocumentName);
 
         if (LOG.isDebugEnabled()) {
             LOG.debug("Checking right: " + name + "," + doc.getFullName() + "," + accessLevel + "," + user + ","
@@ -375,7 +389,7 @@
                         // In the case where the document database and the user database is the same
                         // then we allow the usage of the short name, otherwise the fully qualified
                         // name is requested
-                        if (context.getDatabase().equals(userdatabase)) {
+                        if (context.getDatabase().equals(entityDocumentName.getWiki())) {
                             if (ArrayUtils.contains(userarray, shortname)) {
                                 if (LOG.isDebugEnabled()) {
                                     LOG.debug("Found matching right in " + users + " for " + shortname);
@@ -427,50 +441,57 @@
             context.put("grouplist", grouplistcache);
         }
 
-        Collection<String> grouplist = new ArrayList<String>();
+        Collection<String> grouplist = new HashSet<String>();
         XWikiGroupService groupService = context.getWiki().getGroupService(context);
-        String key = context.getDatabase() + ":" + name;
-        Collection<String> grouplist1 = grouplistcache.get(key);
 
-        if (grouplist1 == null) {
-            grouplist1 = new ArrayList<String>();
-            try {
-                Collection<String> glist = groupService.listGroupsForUser(name, context);
+        // FIXME: it looks like this code was supposed to get entity groups in current wikis but listGroupsForUser
+        // always return groups from entity wiki, maybe listGroupsForUser changed at some point
+        {
+            String key = context.getDatabase() + ":" + prefixedFullName;
+
+            Collection<String> tmpGroupList = grouplistcache.get(key);
+            if (tmpGroupList == null) {
+                try {
+                    Collection<String> glist = groupService.listGroupsForUser(name, context);
 
-                for (String groupName : glist) {
-                    grouplist1.add(context.getDatabase() + ":" + groupName);
+                    tmpGroupList = new ArrayList<String>(glist.size());
+                    for (String groupName : glist) {
+                        tmpGroupList.add(entityDocumentName.getWiki() + ":" + groupName);
+                    }
+                } catch (Exception e) {
+                    LOG.error("Failed to get groups for user or group [" + name + "]", e);
+
+                    tmpGroupList = Collections.emptyList();
                 }
-            } catch (Exception e) {
 
+                grouplistcache.put(key, tmpGroupList);
             }
 
-            grouplistcache.put(key, grouplist1);
+            grouplist.addAll(tmpGroupList);
         }
 
-        grouplist.addAll(grouplist1);
-
-        if (context.getWiki().isVirtualMode()) {
+        // Get entity groups in entity wiki
+        if (!context.isMainWiki(entityDocumentName.getWiki())) {
             String database = context.getDatabase();
             try {
-                // Utils.getName set context's database to name's wiki name (what is before ":")
-                shortname = Util.getName(name, context);
+                context.setDatabase(entityDocumentName.getWiki());
 
-                if (!database.equals(context.getDatabase())) {
-                    String key2 = context.getDatabase() + ":" + name;
-                    Collection<String> grouplist2 = grouplistcache.get(key2);
+                String key = context.getDatabase() + ":" + prefixedFullName;
+                Collection<String> tmpGroupList = grouplistcache.get(key);
 
-                    if (grouplist2 == null) {
-                        Collection<String> glist = groupService.listGroupsForUser(shortname, context);
-                        grouplist2 = new ArrayList<String>();
-                        for (String groupName : glist) {
-                            grouplist2.add(context.getDatabase() + ":" + groupName);
-                        }
-                        grouplistcache.put(key2, grouplist2);
-                    }
+                if (tmpGroupList == null) {
+                    Collection<String> glist = groupService.listGroupsForUser(shortname, context);
 
-                    grouplist.addAll(grouplist2);
+                    tmpGroupList = new ArrayList<String>(glist.size());
+                    for (String groupName : glist) {
+                        tmpGroupList.add(context.getDatabase() + ":" + groupName);
+                    }
+                    grouplistcache.put(key, tmpGroupList);
                 }
+
+                grouplist.addAll(tmpGroupList);
             } catch (Exception e) {
+                LOG.error("Failed to get groups for user or group [" + name + "]", e);
             } finally {
                 context.setDatabase(database);
             }
