Commit d158105f authored by matt@zope.com's avatar matt@zope.com

Accelerated to go with Acquisition C api

parent b65b818e
...@@ -85,123 +85,14 @@ ...@@ -85,123 +85,14 @@
__doc__='''Objects that implement Permission-based roles. __doc__='''Objects that implement Permission-based roles.
$Id: PermissionRole.py,v 1.9 2000/12/05 18:49:42 shane Exp $''' $Id: PermissionRole.py,v 1.10 2001/07/03 19:37:52 matt Exp $'''
__version__='$Revision: 1.9 $'[11:-2] __version__='$Revision: 1.10 $'[11:-2]
import sys import cAccessControl
rolesForPermissionOn=cAccessControl.rolesForPermissionOn
from ExtensionClass import Base PermissionRole=cAccessControl.PermissionRole
imPermisionRole=cAccessControl.imPermissionRole
import string _what_not_even_god_should_do= cAccessControl._what_not_even_god_should_do
name_trans=filter(lambda c, an=string.letters+string.digits+'_': c not in an,
map(chr,range(256)))
name_trans=string.maketrans(string.join(name_trans,''), '_'*len(name_trans))
def rolesForPermissionOn(perm, object, default=('Manager',)):
"""Return the roles that have the given permission on the given object
"""
im=imPermissionRole()
im._p='_'+string.translate(perm, name_trans)+"_Permission"
im._d=default
return im.__of__(object)
class PermissionRole(Base):
"""Implement permission-based roles.
Under normal circumstances, our __of__ method will be
called with an unwrapped object. The result will then be called
with a wrapped object, if the original object was wrapped.
To deal with this, we have to create an intermediate object.
"""
def __init__(self, name, default=('Manager',)):
self.__name__=name
self._p='_'+string.translate(name,name_trans)+"_Permission"
self._d=default
def __of__(self, parent, None=None, getattr=getattr):
r=imPermissionRole()
r._p=self._p
r._pa=parent
r._d=self._d
p=getattr(parent, 'aq_inner', None)
if p is not None:
return r.__of__(p)
else:
return r
# This is used when a permission maps explicitly to no permission.
_what_not_even_god_should_do=[]
class imPermissionRole(Base):
"""Implement permission-based roles
"""
def __of__(self, parent,tt=type(()),st=type(''),getattr=getattr,None=None):
obj=parent
n=self._p
r=None
while 1:
if hasattr(obj,n):
roles=getattr(obj, n)
if roles is None: return 'Anonymous',
t=type(roles)
if t is tt:
# If we get a tuple, then we don't acquire
if r is None: return roles
return r+list(roles)
if t is st:
# We found roles set to a name. Start over
# with the new permission name. If the permission
# name is '', then treat as private!
if roles:
if roles != n:
n=roles
# If we find a name that is the same as the
# current name, we just ignore it.
roles=None
else:
return _what_not_even_god_should_do
elif roles:
if r is None: r=list(roles)
else: r=r+list(roles)
obj=getattr(obj, 'aq_inner', None)
if obj is None: break
obj=obj.aq_parent
if r is None: r=self._d
return r
# The following methods are needed in the unlikely case that an unwrapped
# object is accessed:
def __getitem__(self, i):
try:
v=self._v
except:
v=self._v=self.__of__(self._pa)
del self._pa
return v[i]
def __len__(self):
try:
v=self._v
except:
v=self._v=self.__of__(self._pa)
del self._pa
return len(v)
############################################################################## ##############################################################################
# Test functions: # Test functions:
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE. DAMAGE.
$Id: cAccessControl.c,v 1.7 2001/07/03 12:52:56 matt Exp $ $Id: cAccessControl.c,v 1.8 2001/07/03 19:37:52 matt Exp $
If you have questions regarding this software, If you have questions regarding this software,
contact: contact:
...@@ -51,6 +51,7 @@ ...@@ -51,6 +51,7 @@
#include <stdio.h> #include <stdio.h>
#include "ExtensionClass.h" #include "ExtensionClass.h"
#include "Acquisition.h"
#define OBJECT(o) ((PyObject *) (o)) #define OBJECT(o) ((PyObject *) (o))
...@@ -339,7 +340,6 @@ static PyExtensionClass imPermissionRoleType = { ...@@ -339,7 +340,6 @@ static PyExtensionClass imPermissionRoleType = {
*/ */
static PyObject *Containers = NULL; static PyObject *Containers = NULL;
static PyObject *aq_base = NULL;
static PyObject *_noroles = NULL; static PyObject *_noroles = NULL;
static PyObject *Unauthorized = NULL; static PyObject *Unauthorized = NULL;
static PyObject *LOG = NULL; static PyObject *LOG = NULL;
...@@ -348,6 +348,7 @@ static PyObject *_what_not_even_god_should_do = NULL; ...@@ -348,6 +348,7 @@ static PyObject *_what_not_even_god_should_do = NULL;
static PyObject *Anonymous = NULL; static PyObject *Anonymous = NULL;
static PyObject *imPermissionRoleObj = NULL; static PyObject *imPermissionRoleObj = NULL;
static PyObject *defaultPermission = NULL; static PyObject *defaultPermission = NULL;
static PyObject *__roles__ = NULL;
/* -------------------------------------------------------------- /* --------------------------------------------------------------
** ZopeSecurityPolicy Methods ** ZopeSecurityPolicy Methods
...@@ -392,16 +393,9 @@ static int ZopeSecurityPolicy_setup(void) { ...@@ -392,16 +393,9 @@ static int ZopeSecurityPolicy_setup(void) {
Py_DECREF(module); Py_DECREF(module);
module = NULL; module = NULL;
/*| from Acquisition import aq_base
*/
IMPORT(module,"Acquisition");
GETATTR(module, aq_base);
Py_DECREF(module);
module = NULL;
defaultPermission = Py_BuildValue("(s)", "Manager"); defaultPermission = Py_BuildValue("(s)", "Manager");
_what_not_even_god_should_do = Py_BuildValue("[]"); _what_not_even_god_should_do = Py_BuildValue("[]");
__roles__ = PyString_FromString("__roles__");
return 1; return 1;
...@@ -662,12 +656,12 @@ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args) { ...@@ -662,12 +656,12 @@ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args) {
**| accessedbase = getattr(accessed, 'aq_base', container) **| accessedbase = getattr(accessed, 'aq_base', container)
*/ */
containerbase = PyObject_CallFunction(aq_base, "O", container); containerbase = aq_base(container);
if (containerbase == NULL) goto err; if (containerbase == NULL) goto err;
accessedbase = PyObject_GetAttrString(accessed, "aq_base"); if (aq_isWrapper(accessed))
if (accessedbase == NULL) { accessedbase = aq_base(accessed);
PyErr_Clear(); else {
Py_INCREF(container); Py_INCREF(container);
accessedbase = container; accessedbase = container;
} }
...@@ -695,7 +689,7 @@ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args) { ...@@ -695,7 +689,7 @@ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args) {
if (roles == NULL || roles == _noroles) { if (roles == NULL || roles == _noroles) {
Py_XDECREF(roles); Py_XDECREF(roles);
roles = PyObject_GetAttrString(value, "__roles__"); roles = PyObject_GetAttr(value, __roles__);
if (roles == NULL) { if (roles == NULL) {
PyErr_Clear(); PyErr_Clear();
Py_INCREF(_noroles); Py_INCREF(_noroles);
...@@ -739,7 +733,7 @@ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args) { ...@@ -739,7 +733,7 @@ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args) {
Py_XDECREF(roles); Py_XDECREF(roles);
roles = PyObject_GetAttrString(container, "__roles__"); roles = PyObject_GetAttr(container, __roles__);
if (roles == NULL) { if (roles == NULL) {
PyErr_Clear(); PyErr_Clear();
Py_INCREF(_noroles); Py_INCREF(_noroles);
...@@ -747,11 +741,7 @@ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args) { ...@@ -747,11 +741,7 @@ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args) {
} }
if (roles == _noroles) { if (roles == _noroles) {
PyObject *aq; if (aq_isWrapper(container) != 1) {
aq = PyObject_GetAttrString(container, "aq_acquire");
if (aq == NULL) {
PyErr_Clear();
Py_DECREF(roles); Py_DECREF(roles);
if (containerbase != accessedbase) { if (containerbase != accessedbase) {
rval = PyInt_FromLong(0); rval = PyInt_FromLong(0);
...@@ -762,10 +752,7 @@ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args) { ...@@ -762,10 +752,7 @@ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args) {
roles = _noroles; roles = _noroles;
} else { } else {
Py_DECREF(roles); Py_DECREF(roles);
roles = PyObject_CallFunction(aq, "s", roles = aq_acquire(container, __roles__);
"__roles__");
Py_DECREF(aq);
aq = NULL;
if (roles == NULL) { if (roles == NULL) {
/* XXX not JUST AttributeError*/ /* XXX not JUST AttributeError*/
/* XXX should we clear the error? */ /* XXX should we clear the error? */
...@@ -1289,9 +1276,9 @@ static PyObject *PermissionRole_of(PermissionRole *self, PyObject *args) { ...@@ -1289,9 +1276,9 @@ static PyObject *PermissionRole_of(PermissionRole *self, PyObject *args) {
**| return r **| return r
*/ */
_p = PyObject_GetAttrString(parent, "aq_inner");
if (_p) { if (aq_isWrapper(parent)) {
_p = aq_inner(parent);
result = PyObject_CallMethod(OBJECT(r),"__of__","O", _p); result = PyObject_CallMethod(OBJECT(r),"__of__","O", _p);
Py_DECREF(_p); Py_DECREF(_p);
/* Dont need goto */ /* Dont need goto */
...@@ -1503,13 +1490,15 @@ static PyObject *imPermissionRole_of(imPermissionRole *self, PyObject *args) { ...@@ -1503,13 +1490,15 @@ static PyObject *imPermissionRole_of(imPermissionRole *self, PyObject *args) {
**| obj = obj.aq_parent **| obj = obj.aq_parent
*/ */
tobj = PyObject_GetAttrString(obj, "aq_inner"); if (aq_isWrapper(obj) <= 0) break;
tobj = aq_inner(obj);
if (tobj == NULL) break; if (tobj == NULL) break;
Py_DECREF(obj); Py_DECREF(obj);
obj = tobj; obj = tobj;
if (obj == Py_None) break; if (obj == Py_None) break;
tobj = PyObject_GetAttrString(obj, "aq_parent"); if (aq_isWrapper(obj) <= 0) break;
tobj = aq_parent(obj);
if (tobj == NULL) goto err; if (tobj == NULL) goto err;
Py_DECREF(obj); Py_DECREF(obj);
obj = tobj; obj = tobj;
...@@ -1774,10 +1763,12 @@ static PyObject *permissionName(PyObject *name) { ...@@ -1774,10 +1763,12 @@ static PyObject *permissionName(PyObject *name) {
PUBLIC void initcAccessControl(void) { PUBLIC void initcAccessControl(void) {
PyObject *module; PyObject *module;
PyObject *dict; PyObject *dict;
char *rev = "$Revision: 1.7 $"; char *rev = "$Revision: 1.8 $";
if (!ExtensionClassImported) return; if (!ExtensionClassImported) return;
aq_init();
ZopeSecurityPolicyType.tp_getattro = ZopeSecurityPolicyType.tp_getattro =
(getattrofunc) PyExtensionClassCAPI->getattro; (getattrofunc) PyExtensionClassCAPI->getattro;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment