This package provides basic elements for defining Zope permissions and
security checkers without ZCML.
.. contents::
Setting up ``grokcore.security``
================================
This package is essentially set up like the `grokcore.component`_
package, please refer to its documentation for details. The only
additional ZCML line you will need is::
<include package="grokcore.security" file="meta.zcml" />
Put this somewhere near the top of your root ZCML file but below the
line where you include ``grokcore.component``'s configuration.
Defining permissions
====================
In `grokcore.component`_, various components are defined (and
automatically registered) by subclassing from certain baseclasses.
The same applies to defining permissions with ``grokcore.security`` as
well::
import grokcore.security
class EditContent(grokcore.security.Permission):
grokcore.security.name('mypkg.EditContent')
This defines a permission with the ID ``mypkg.EditContent``. You must
always specify this ID explicitly. In addition, you can also give the
permission a human-readable title and description. This is useful
when your application provides lists of permissions somewhere and you
don't want to bother users with deciphering the dotted IDs::
import grokcore.security
class EditContent(grokcore.security.Permission):
grokcore.security.name('mypkg.EditContent')
grokcore.security.title('Edit content')
grokcore.security.description('Anyone who has this permission may '
'modify content in the application.')
Defining checkers for components
================================
``grokcore.security`` provides some means for defining checkers for
components:
* ``grokcore.security.require(permission)`` which can be used either
as a class-level directive to set a permission for a whole
component, or as a decorator to set a permission for a function or
method.
* ``protect_getattr`` and ``protect_setattr``, available from
``grokcore.security.util``, which take a class, an attribute name
and a permission as arguments and define Zope security checkers for
getting or setting a particular attribute on instance of said class.
With these, you can build grokkers for components that need security
declarations. For instance, the `grokcore.view`_ package uses them to
define a grokker that makes security declarations for views::
class ViewSecurityGrokker(martian.ClassGrokker):
martian.component(grokcore.view.View)
martian.directive(grokcore.security.require, name='permission')
def execute(self, factory, config, permission, **kw):
for method_name in zope.publisher.interfaces.browser.IBrowserPage:
config.action(
discriminator=('protectName', factory, method_name),
callable=grokcore.security.util.protect_getattr,
args=(factory, method_name, permission),
)
return True
With such a grokker, it is possible to protect views like so::
class Edit(grokcore.view.View):
grokcore.security.require(EditContent)
Note how we can simply pass a permission class to the ``require``
directive. Alternatively, you can pass the permission ID::
class Edit(grokcore.view.View):
grokcore.security.require('mypkg.EditContent')
If you wanted to be able to define permissions for individual class
methods rather than the whole class, you would simply base your
grokker on ``martian.MethodGrokker`` rather than ``ClassGrokker``.
The actual mechanics of defining a checker are the same.
Please note that ``grokcore.security`` does not yet provide directives
that allow you to specify permissions for simple attribute access
(read and write).
API overview
============
``Permission``
Base class for defining permissions. Use the ``name`` directive
to define the mandatory permission ID. Optionally use the
``title`` and ``description`` directives to give the permission
human-readable information.
``Public``
Special permission that can be referred to whenever a component
should not be protected by a permission at all (public access).
``require(permission_class_or_id)``
declares that the use of a particular component (when used as a
class-level directive) or a method (when used as a method
decorator) requires a certain permission. The argument can either
be a permission class (subclass of ``Permission``) or a permission
ID.
In addition, the ``grokcore.security`` package exposes the
`grokcore.component`_ API.
.. _grokcore.component: http://pypi.python.org/pypi/grokcore.component
.. _grokcore.view: http://pypi.python.org/pypi/grokcore.view
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共37个文件
py:20个
txt:10个
cfg:3个
资源分类:Python库 所属语言:Python 资源全名:grokcore.security-1.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源详情
资源评论
资源推荐
收起资源包目录
grokcore.security-1.2.tar.gz (37个子文件)
grokcore.security-1.2
PKG-INFO 7KB
README.txt 5KB
bootstrap
bootstrap.py 3KB
CHANGES.txt 999B
COPYRIGHT.txt 462B
src
grokcore.security.egg-info
PKG-INFO 7KB
requires.txt 134B
not-zip-safe 1B
SOURCES.txt 1KB
top_level.txt 9B
namespace_packages.txt 9B
dependency_links.txt 1B
grokcore
__init__.py 200B
security
testing.py 2KB
tests
test_all.py 1KB
__init__.py 32B
security
missing_permission_name.py 369B
protect_getattr.py 586B
multiple_require.py 546B
permission.py 935B
__init__.py 32B
not_a_permissionclass.py 591B
fallback.py 477B
missing_permission.py 515B
meta.py 2KB
util.py 2KB
meta.zcml 241B
interfaces.py 1KB
__init__.py 1KB
directive.py 2KB
components.py 777B
setup.cfg 59B
CREDITS.txt 145B
setup.py 1KB
versions.cfg 3KB
LICENSE.txt 2KB
buildout.cfg 320B
共 37 条
- 1
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0