Permalink
Browse files

Initial release.

  • Loading branch information...
0 parents commit 931b4086773e35b93409d01c1eb4b7bcfb33cb0a @mitar mitar committed Jul 22, 2011
Showing with 83 additions and 0 deletions.
  1. +8 −0 .hgignore
  2. +1 −0 componentpermissions/__init__.py
  3. +52 −0 componentpermissions/api.py
  4. +22 −0 setup.py
@@ -0,0 +1,8 @@
+syntax: regexp
+/dist$
+/build$
+^dist$
+^build$
+syntax: glob
+*.egg-info
+*.pyc
@@ -0,0 +1 @@
+from api import *
@@ -0,0 +1,52 @@
+import re
+
+from trac.core import *
+from trac.config import Option
+from trac.perm import IPermissionRequestor, IPermissionPolicy
+from trac.ticket import model
+from trac.resource import ResourceNotFound
+
+class ComponentPermissionsPolicy(Component):
+ """
+ This component provides permissions based on ticket components for Trac.
+ """
+
+ implements(IPermissionRequestor, IPermissionPolicy)
+
+ ticket_field_name = Option('component-permissions', 'ticket_field_name', 'component_permissions_field',
+ """The name of the field which should be checked to see if the component permission is required.""")
+
+ # IPermissionRequestor methods
+
+ def _get_permission_name(self, component):
+ return 'COMPONENT_%s_VIEW' % (re.sub('[^a-zA-Z]+', '_', component).strip('_').upper(),)
+
+ def get_permission_actions(self):
+ for component in model.Component.select(self.env):
+ yield self._get_permission_name(component.name)
+
+ # IPermissionPolicy methods
+
+ def check_permission(self, action, username, resource, perm):
+ # To prevent recursion
+ if action in self.get_permission_actions():
+ return
+
+ # Check whether we're dealing with a ticket resource
+ while resource:
+ if resource.realm == 'ticket':
+ break
+ resource = resource.parent
+
+ if resource and resource.realm == 'ticket' and resource.id is not None:
+ try:
+ ticket = model.Ticket(self.env, int(resource.id))
+ should_check_permissions = ticket.values.get(self.ticket_field_name, 0)
+ except ResourceNotFound:
+ # There is a short race condition here but we cannot do much
+ return
+
+ if should_check_permissions and int(should_check_permissions) and 'component' in ticket.values:
+ permission = self._get_permission_name(ticket['component'])
+ if permission not in perm and 'TICKET_ADMIN' not in perm:
+ return False
@@ -0,0 +1,22 @@
+from setuptools import setup
+
+VERSION = '0.1'
+PACKAGE = 'componentpermissions'
+
+setup(
+ name = 'ComponentPermissionsPlugin',
+ version = VERSION,
+ description = "Provides permissions based on ticket components for Trac.",
+ author = 'Mitar',
+ author_email = 'mitar.trac@tnode.com',
+ url = 'http://mitar.tnode.com/',
+ keywords = 'trac plugin',
+ license = "GPLv3",
+ packages = [PACKAGE],
+ include_package_data = True,
+ install_requires = [],
+ zip_safe = False,
+ entry_points = {
+ 'trac.plugins': '%s = %s' % (PACKAGE, PACKAGE),
+ },
+)

0 comments on commit 931b408

Please sign in to comment.