Skip to content

Commit

Permalink
move some 'security' tests from grok to grokcore.view
Browse files Browse the repository at this point in the history
  • Loading branch information
gotcha committed Jul 18, 2008
1 parent af44efc commit 23cebbe
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 60 deletions.
28 changes: 28 additions & 0 deletions devel/grokcore.view/src/grokcore/view/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from zope import component, interface
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
from zope.security.interfaces import IPermission

import martian
from martian import util
Expand All @@ -13,6 +14,7 @@
from grokcore.view import formlib
from grokcore.view import templatereg
from grokcore.view.util import default_view_name
from grokcore.view.util import default_fallback_to_name


class ViewGrokkerBase(martian.ClassGrokker):
Expand Down Expand Up @@ -88,6 +90,32 @@ def protectName(self, config, factory, permission):
raise NotImplementedError


class PermissionGrokker(martian.ClassGrokker):
martian.component(grokcore.view.Permission)
martian.priority(1500)
martian.directive(grokcore.component.name)
martian.directive(grokcore.component.title,
get_default=default_fallback_to_name)
martian.directive(grokcore.component.description)

def execute(self, factory, config, name, title, description, **kw):
if not name:
raise GrokError(
"A permission needs to have a dotted name for its id. Use "
"grok.name to specify one.", factory)
# We can safely convert to unicode, since the directives make sure
# it is either unicode already or ASCII.
permission = factory(unicode(name), unicode(title),
unicode(description))

config.action(
discriminator=('utility', IPermission, name),
callable=component.provideUtility,
args=(permission, IPermission, name),
order=-1) # need to do this early in the process
return True


class TemplateGrokker(martian.GlobalGrokker):
# this needs to happen before any other grokkers execute that use
# the template registry
Expand Down
2 changes: 1 addition & 1 deletion devel/grokcore.view/src/grokcore/view/tests/grok.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from grokcore.view.tests.components import Model, View
from grokcore.view import testing
from grokcore.component import name
from grokcore.component import name, context
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
>>> grok.testing.grok(__name__)
Traceback (most recent call last):
...
ConfigurationExecutionError: martian.error.GrokError: Undefined permission 'doesnt.exist' in <class 'grok.tests.security.missing_permission.MissingPermission'>. Use grok.Permission first.
ConfigurationExecutionError: martian.error.GrokError: Undefined permission 'doesnt.exist' in <class 'grokcore.view.tests.security.missing_permission.MissingPermission'>. Use grok.Permission first.
...
"""

import grok
import zope.interface

from grokcore.view.tests import grok
import grokcore.view


class MissingPermission(grok.View):
grok.context(zope.interface.Interface)
grok.require('doesnt.exist')
grokcore.view.require('doesnt.exist')

def render(self):
pass

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
GrokError: A permission needs to have a dotted name for its id.
Use grok.name to specify one.
"""
from grokcore.view.tests import grok
import grokcore.view

import grok
import zope.interface

class MissingName(grok.Permission):
class MissingName(grokcore.view.Permission):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
>>> grok.testing.grok(__name__)
Traceback (most recent call last):
...
GrokError: grok.require was called multiple times in <class 'grok.tests.security.multiple_require.MultipleView'>. It may only be set once for a class.
GrokError: grok.require was called multiple times in <class 'grokcore.view.tests.security.multiple_require.MultipleView'>. It may only be set once for a class.
"""
import grok
import zope.interface

class One(grok.Permission):
from grokcore.view.tests import grok
import grokcore.view


class One(grokcore.view.Permission):
grok.name('permission.1')

class Two(grok.Permission):

class Two(grokcore.view.Permission):
grok.name('permission.2')


class MultipleView(grok.View):
grok.context(zope.interface.Interface)
grok.require(One)
grok.require(Two)
grokcore.view.require(One)
grokcore.view.require(Two)

def render(self):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
... pass
>>>
>>> class NoPermission(grok.View):
... grok.context(zope.interface.Interface)
... grok.require(NotAProperPermission)
... grok.context(Interface)
... grokcore.view.require(NotAProperPermission)
...
... def render(self):
... pass
Expand All @@ -18,6 +18,5 @@
grok.Permission to the 'require' directive.
"""

import grok
import zope.interface
from grokcore.view.tests import grok
import grokcore.view
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
>>> grok.testing.grok(__name__)
Traceback (most recent call last):
GrokError: The @grok.require decorator is used for method 'render' in view <class 'grok.tests.security.view_decorator.BogusView'>. It may only be used for XML-RPC methods.
GrokError: The @grok.require decorator is used for method 'render' in view <class 'grokcore.view.tests.security.view_decorator.BogusView'>. It may only be used for XML-RPC methods.
"""

import grok
import zope.interface

class Bogus(grok.Permission):
from grokcore.view.tests import grok
import grokcore.view


class Bogus(grokcore.view.Permission):
grok.name('bogus.perm')


class BogusView(grok.View):
grok.context(zope.interface.Interface)

@grok.require(Bogus)
@grokcore.view.require(Bogus)
def render(self):
pass
2 changes: 1 addition & 1 deletion devel/grokcore.view/src/grokcore/view/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def suiteFromPackage(name):

def test_suite():
suite = unittest.TestSuite()
for name in ['template', 'static', 'view']:
for name in ['template', 'static', 'view', 'security']:
suite.addTest(suiteFromPackage(name))
return suite

Expand Down
4 changes: 4 additions & 0 deletions devel/grokcore.view/src/grokcore/view/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ def url(request, obj, name=None, data={}):

def default_view_name(factory, module=None, **data):
return factory.__name__.lower()


def default_fallback_to_name(factory, module, name, **data):
return name
38 changes: 5 additions & 33 deletions src/grok/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

from zope.publisher.interfaces.xmlrpc import IXMLRPCRequest
from zope.viewlet.interfaces import IViewletManager, IViewlet
from zope.security.interfaces import IPermission
from zope.securitypolicy.interfaces import IRole
from zope.securitypolicy.rolepermission import rolePermissionManager

Expand All @@ -45,10 +44,6 @@
from martian.error import GrokError
from martian import util

import grokcore.view
from grokcore.view.meta import ViewGrokkerBase
from grokcore.view.util import default_view_name

import grok
from grok import components
from grok.util import make_checker
Expand All @@ -57,9 +52,12 @@

from grokcore.component.scan import determine_module_component

import grokcore.view
from grokcore.view.meta import ViewGrokkerBase
from grokcore.view.meta import PermissionGrokker
from grokcore.view.util import default_view_name
from grokcore.view.util import default_fallback_to_name

def default_fallback_to_name(factory, module, name, **data):
return name

def default_annotation_provides(factory, module, **data):
base_interfaces = interface.implementedBy(grok.Annotation)
Expand Down Expand Up @@ -283,32 +281,6 @@ def setupUtility(site, utility, provides, name=u'',
name=name)


class PermissionGrokker(martian.ClassGrokker):
martian.component(grok.Permission)
martian.priority(1500)
martian.directive(grok.name)
martian.directive(grok.title, get_default=default_fallback_to_name)
martian.directive(grok.description)

def execute(self, factory, config, name, title, description, **kw):
if not name:
raise GrokError(
"A permission needs to have a dotted name for its id. Use "
"grok.name to specify one.", factory)
# We can safely convert to unicode, since the directives make sure
# it is either unicode already or ASCII.
permission = factory(unicode(name), unicode(title),
unicode(description))

config.action(
discriminator=('utility', IPermission, name),
callable=component.provideUtility,
args=(permission, IPermission, name),
order=-1 # need to do this early in the process
)
return True


class RoleGrokker(martian.ClassGrokker):
martian.component(grok.Role)
martian.priority(martian.priority.bind().get(PermissionGrokker()) - 1)
Expand Down

0 comments on commit 23cebbe

Please sign in to comment.