Skip to content

Commit

Permalink
- An add_permission directive method was added to the Configurato…
Browse files Browse the repository at this point in the history
…r. This

  directive registers a free-standing permission introspectable into the
  Pyramid introspection system.  Frameworks built atop Pyramid can thus use
  the the ``permissions`` introspectable category data to build a
  comprehensive list of permissions supported by a running system.  Before
  this method was added, permissions were already registered in this
  introspectable category as a side effect of naming them in an ``add_view``
  call, this method just makes it possible to arrange for a permission to be
  put into the ``permissions`` introspectable category without naming it
  along with an associated view.  Here's an example of usage of
  ``add_permission``::

      config = Configurator()
      config.add_permission('view')
  • Loading branch information
mcdonc committed Aug 16, 2012
1 parent cdcea94 commit 6b180cb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
15 changes: 15 additions & 0 deletions CHANGES.txt
Expand Up @@ -85,3 +85,18 @@ Features
- When there is a predicate mismatch exception (seen when no view matches for
a given request due to predicates not working), the exception now contains
a textual description of the predicate which didn't match.

- An ``add_permission`` directive method was added to the Configurator. This
directive registers a free-standing permission introspectable into the
Pyramid introspection system. Frameworks built atop Pyramid can thus use
the the ``permissions`` introspectable category data to build a
comprehensive list of permissions supported by a running system. Before
this method was added, permissions were already registered in this
introspectable category as a side effect of naming them in an ``add_view``
call, this method just makes it possible to arrange for a permission to be
put into the ``permissions`` introspectable category without naming it
along with an associated view. Here's an example of usage of
``add_permission``::

config = Configurator()
config.add_permission('view')
1 change: 1 addition & 0 deletions docs/api/config.rst
Expand Up @@ -36,6 +36,7 @@
.. automethod:: set_authentication_policy
.. automethod:: set_authorization_policy
.. automethod:: set_default_permission
.. automethod:: add_permission

:methodcategory:`Setting Request Properties`

Expand Down
20 changes: 20 additions & 0 deletions pyramid/config/security.py
Expand Up @@ -137,3 +137,23 @@ def register():
introspectables=(intr, perm_intr,))


def add_permission(self, permission_name):
"""
A configurator directive which registers a free-standing
permission without associating it with a view callable. This can be
used so that the permission shows up in the introspectable data under
the ``permissions`` category (permissions mentioned via ``add_view``
already end up in there). For example::
config = Configurator()
config.add_permission('view')
"""
intr = self.introspectable(
'permissions',
permission_name,
permission_name,
'permission'
)
intr['value'] = permission_name
self.action(None, introspectables=(intr,))

9 changes: 9 additions & 0 deletions pyramid/tests/test_config/test_security.py
Expand Up @@ -89,3 +89,12 @@ def test_set_default_permission(self):
self.assertEqual(config.registry.getUtility(IDefaultPermission),
'view')

def test_add_permission(self):
config = self._makeOne(autocommit=True)
config.add_permission('perm')
cat = config.registry.introspector.get_category('permissions')
self.assertEqual(len(cat), 1)
D = cat[0]
intr = D['introspectable']
self.assertEqual(intr['value'], 'perm')

0 comments on commit 6b180cb

Please sign in to comment.