loops/organize/party.py
helmutm 966a75f166 work in progress: user management via IPerson adapters for concepts
git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@1208 fd906abe-77d9-0310-91a1-e0d9ade77398
2006-05-12 19:44:29 +00:00

112 lines
3.7 KiB
Python

#
# Copyright (c) 2006 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Adapters for IConcept providing interfaces from the cybertools.organize package.
$Id$
"""
from zope import interface, component
from zope.app import zapi
from zope.app.principalannotation import annotations
from zope.app.security.interfaces import IAuthentication, PrincipalLookupError
from zope.component import adapts
from zope.interface import implements
from zope.cachedescriptors.property import Lazy
from zope.security.proxy import removeSecurityProxy
#from cybertools.organize.interfaces import IPerson
from cybertools.organize.party import Person as BasePerson
from cybertools.relation.interfaces import IRelationRegistry
from loops.interfaces import IConcept
from loops.type import TypeInterfaceSourceList
from loops.organize.interfaces import IPerson
# register IPerson as a type interface - (TODO: use a function for this)
TypeInterfaceSourceList.typeInterfaces += (IPerson,)
class Person(BasePerson):
""" typeInterface adapter for concepts of type 'person'.
"""
implements(IPerson)
adapts(IConcept)
__attributes = ('context', '__parent__', 'userId',)
def __init__(self, context):
self.context = context # to get the permission stuff right
self.__parent__ = context
def __getattr__(self, attr):
self.checkAttr(attr)
return getattr(self.context, '_' + attr, None)
def __setattr__(self, attr, value):
if attr in self.__attributes:
object.__setattr__(self, attr, value)
else:
self.checkAttr(attr)
setattr(self.context, '_' + attr, value)
def checkAttr(self, attr):
if attr not in list(IPerson) + list(IConcept):
raise AttributeError(attr)
def getUserId(self):
return getattr(self.context, '_userId', None)
def setUserId(self, userId):
auth = self.authentication
oldUserId = self.userId
if oldUserId and oldUserId != userId:
principal = self.getPrincipalForUserId(oldUserId)
if principal is not None:
pa = annotations(principal)
pa['loops.organize.person'] = None
if userId:
principal = auth.getPrincipal(userId)
pa = annotations(principal)
relations = component.getUtility(IRelationRegistry)
uid = relations.getUniqueIdForObject(self.context)
pa['loops.organize.person'] = uid
self.context._userId = userId
userId = property(getUserId, setUserId)
@Lazy
def authentication(self):
return component.getUtility(IAuthentication, context=self.context)
@Lazy
def principal(self):
return self.getPrincipalForUserId()
def getPrincipalForUserId(self, userId=None):
userId = userId or self.userId
if not userId:
return None
auth = self.authentication
try:
return auth.getPrincipal(userId)
except PrincipalLookupError:
return None