Skip to content

Commit

Permalink
0.13dev: Cache the permissions table.
Browse files Browse the repository at this point in the history
Related to #4245.

git-svn-id: http://svn.edgewall.org/repos/trac/trunk@11032 af82e41b-90c4-0310-8c96-b1721e28e2e2
  • Loading branch information
psuter committed Apr 13, 2012
1 parent 13412da commit b44d860
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions trac/perm.py
Expand Up @@ -23,6 +23,7 @@
from time import time

from trac.admin import AdminCommandError, IAdminCommandProvider, get_dir_list
from trac.cache import cached
from trac.config import ExtensionOption, OrderedExtensionsOption
from trac.core import *
from trac.resource import Resource, get_resource_name
Expand Down Expand Up @@ -182,11 +183,11 @@ def get_user_permissions(self, username):
subjects.update(provider.get_permission_groups(username) or [])

actions = set()
rows = self.env.db_query("SELECT username, action FROM permission")
perms = self._all_permissions
while True:
num_users = len(subjects)
num_actions = len(actions)
for user, action in rows:
for user, action in perms:
if user in subjects:
if action.isupper() and action not in actions:
actions.add(action)
Expand Down Expand Up @@ -220,6 +221,11 @@ def get_all_permissions(self):
The permissions are returned as a list of (subject, action)
formatted tuples."""
return self._all_permissions


@cached
def _all_permissions(self):
return [(username, action) for username, action in
self.env.db_query("SELECT username, action FROM permission")]

Expand All @@ -228,13 +234,19 @@ def grant_permission(self, username, action):
self.env.db_transaction("INSERT INTO permission VALUES (%s, %s)",
(username, action))
self.log.info("Granted permission for %s to %s", action, username)

# Invalidate cached property
del self._all_permissions

def revoke_permission(self, username, action):
"""Revokes a users' permission to perform the specified action."""
self.env.db_transaction(
"DELETE FROM permission WHERE username=%s AND action=%s",
(username, action))
self.log.info("Revoked permission for %s to %s", action, username)

# Invalidate cached property
del self._all_permissions


class DefaultPermissionGroupProvider(Component):
Expand Down

0 comments on commit b44d860

Please sign in to comment.