2

我喜欢使用 IPython 的 zope 配置文件来检查我的 Plone 实例,但是与插入断点并使用管理员用户点击它相比,会出现一些令人讨厌的权限差异。

例如,我想遍历未发布的测试文件夹中的内容对象。此查询不会在 shell 中返回任何结果,但会从断点开始工作。

$ bin/instance shell
$ ipython --profile=zope

from Products.CMFPlone.utils import getToolByName
catalog = getToolByName(context, 'portal_catalog')
catalog({'path':'Plone/testing'})

我可以以管理员身份进行身份验证或以其他方式重新调整权限以从 ipython 完全操纵我的网站吗?

4

2 回答 2

2

这是我用来从调试外壳管理我的克隆应用程序的(非常脏的)代码。根据您的 Zope 和 Plone 版本,它可能需要一些更新。

from sys import stdin, stdout, exit
import base64
from thread import get_ident
from ZPublisher.HTTPRequest import HTTPRequest
from ZPublisher.HTTPResponse import HTTPResponse
from ZPublisher.BaseRequest import RequestContainer
from ZPublisher import Publish

from AccessControl import ClassSecurityInfo, getSecurityManager
from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.User import UnrestrictedUser

def loginAsUnrestrictedUser():
    """Exemple of use :
        old_user = loginAsUnrestrictedUser()
        # Manager stuff
        loginAsUser(old_user)
    """
    current_user = getSecurityManager().getUser()
    newSecurityManager(None, UnrestrictedUser('manager', '', ['Manager'], []))
    return current_user

def loginAsUser(user):
    newSecurityManager(None, user)

def makerequest(app, stdout=stdout, query_string=None, user_pass=None):
    """Make a request suitable for CMF sites & Plone
      - user_pass = "user:pass"
    """
    # copy from Testing.makerequest
    resp = HTTPResponse(stdout=stdout)
    env = {}
    env['SERVER_NAME'] = 'lxtools.makerequest.fr'
    env['SERVER_PORT'] = '80'
    env['REQUEST_METHOD'] = 'GET'
    env['REMOTE_HOST'] = 'a.distant.host'
    env['REMOTE_ADDR'] = '77.77.77.77'
    env['HTTP_HOST']   = '127.0.0.1'
    env['HTTP_USER_AGENT'] = 'LxToolsUserAgent/1.0'
    env['HTTP_ACCEPT']='image/gif, image/x-xbitmap, image/jpeg, */* '
    if user_pass:
        env['HTTP_AUTHORIZATION']="Basic %s" % base64.encodestring(user_pass)
    if query_string:
        p_q = query_string.split('?')
        if   len(p_q) == 1: 
            env['PATH_INFO'] = p_q[0]
        elif len(p_q) == 2: 
            (env['PATH_INFO'], env['QUERY_STRING'])=p_q
        else: 
            raise TypeError, ''
    req = HTTPRequest(stdin, env, resp)
    req['URL1']=req['URL'] # fix for CMFQuickInstaller
    #
    # copy/hacked from Localizer __init__ patches
    # first put the needed values in the request
    req['HTTP_ACCEPT_CHARSET'] = 'latin-9'
    #req.other['AcceptCharset'] = AcceptCharset(req['HTTP_ACCEPT_CHARSET'])
    #
    req['HTTP_ACCEPT_LANGUAGE'] = 'fr'
    #accept_language = AcceptLanguage(req['HTTP_ACCEPT_LANGUAGE'])
    #req.other['AcceptLanguage'] = accept_language 
    # XXX For backwards compatibility
    #req.other['USER_PREF_LANGUAGES'] = accept_language
    #req.other['AcceptLanguage'] = accept_language 
    #
    # Plone stuff
    #req['plone_skin'] = 'Plone Default'
    #
    # then store the request in Publish._requests
    # with the thread id
    id = get_ident()
    if hasattr(Publish, '_requests'):
        # we do not have _requests inside ZopeTestCase
        Publish._requests[id] = req
    # add a brainless session container
    req['SESSION'] = {}
    #
    # ok, let's wrap
    return app.__of__(RequestContainer(REQUEST = req))


def debug_init(app):
    loginAsUnrestrictedUser()
    app = makerequest(app)
    return app

这存在于 wshelpers Zope 产品中。一旦调试外壳启动,这只是一个问题;

>> from Products.wshelpers import wsdebug
>> app = wsdebug.debug_init(app)
>> # now you're logged in as admin
于 2009-01-09T12:37:52.897 回答
1

只需使用 catalog.search({'path':'Plone/testing'})。它执行与 catalog() 相同的查询,但不根据当前用户的权限过滤结果。

IPython 的 zope 配置文件确实提供了一个方法 utils.su('username') 来更改当前用户,但它不识别管理员用户(在 /acl_users 而不是 /Plone/acl_users 中定义)并且在调用它之后对目录的后续调用( ) 出现 AttributeError 失败:“模块”对象没有属性“checkPermission”。

于 2008-11-14T16:29:54.583 回答