Commits

Sebastian Sdorra committed 3d1615a

fix permission caching for logged in users

  • Participants
  • Parent commits 4f614d0

Comments (0)

Files changed (2)

scm-core/src/main/java/sonia/scm/group/GroupNames.java

 
 //~--- non-JDK imports --------------------------------------------------------
 
+import com.google.common.base.Objects;
 import com.google.common.collect.Lists;
 
 //~--- JDK imports ------------------------------------------------------------
   }
 
   /**
+   * {@inheritDoc}
+   */
+  @Override
+  public boolean equals(Object obj)
+  {
+    if (obj == null)
+    {
+      return false;
+    }
+
+    if (getClass() != obj.getClass())
+    {
+      return false;
+    }
+
+    final GroupNames other = (GroupNames) obj;
+
+    return Objects.equal(collection, other.collection);
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public int hashCode()
+  {
+    return Objects.hashCode(collection);
+  }
+
+  /**
    * Method description
    *
    *

scm-webapp/src/main/java/sonia/scm/security/AuthorizationCollector.java

 
 //~--- non-JDK imports --------------------------------------------------------
 
+import com.google.common.base.Objects;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableSet;
     RepositoryDAO repositoryDAO, SecuritySystem securitySystem,
     PermissionResolver resolver)
   {
-    this.cache = cacheManager.getCache(String.class, AuthorizationInfo.class,
+    this.cache = cacheManager.getCache(CacheKey.class, AuthorizationInfo.class,
       CACHE_NAME);
     this.repositoryDAO = repositoryDAO;
     this.securitySystem = securitySystem;
           user.getName());
       }
 
-      cache.remove(user.getId());
+      // check if this is neccessary
+      cache.clear();
     }
   }
 
 
     Preconditions.checkNotNull(user, "no user found in principal collection");
 
-    AuthorizationInfo info = cache.get(user.getId());
+    GroupNames groupNames = principals.oneByType(GroupNames.class);
+
+    CacheKey cacheKey = new CacheKey(user.getId(), groupNames);
+
+    AuthorizationInfo info = cache.get(cacheKey);
 
     if (info == null)
     {
         logger.trace("collect AuthorizationInfo for user {}", user.getName());
       }
 
-      GroupNames groupNames = principals.oneByType(GroupNames.class);
-
       info = createAuthorizationInfo(user, groupNames);
-      cache.put(user.getId(), info);
+      cache.put(cacheKey, info);
     }
     else if (logger.isTraceEnabled())
     {
     //J+
   }
 
+  //~--- inner classes --------------------------------------------------------
+
+  /**
+   * Class description
+   *
+   *
+   * @version        Enter version here..., 13/08/28
+   * @author         Enter your name here...
+   */
+  private static class CacheKey
+  {
+
+    /**
+     * Constructs ...
+     *
+     *
+     * @param username
+     * @param groupnames
+     */
+    private CacheKey(String username, GroupNames groupnames)
+    {
+      this.username = username;
+      this.groupnames = groupnames;
+    }
+
+    //~--- methods ------------------------------------------------------------
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object obj)
+    {
+      if (obj == null)
+      {
+        return false;
+      }
+
+      if (getClass() != obj.getClass())
+      {
+        return false;
+      }
+
+      final CacheKey other = (CacheKey) obj;
+
+      return Objects.equal(username, other.username)
+        && Objects.equal(groupnames, other.groupnames);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode()
+    {
+      return Objects.hashCode(username, groupnames);
+    }
+
+    //~--- fields -------------------------------------------------------------
+
+    /** Field description */
+    private final GroupNames groupnames;
+
+    /** Field description */
+    private final String username;
+  }
+
+
   //~--- fields ---------------------------------------------------------------
 
   /** Field description */
-  private Cache<String, AuthorizationInfo> cache;
+  private Cache<CacheKey, AuthorizationInfo> cache;
 
   /** Field description */
   private RepositoryDAO repositoryDAO;