make assignment of user ids to persons work for multiple loops site for one loops principal folder
git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@1248 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
78310d3eee
commit
714e00bbfc
5 changed files with 54 additions and 32 deletions
|
@ -33,7 +33,7 @@ from zope.i18nmessageid import MessageFactory
|
||||||
from cybertools.typology.interfaces import IType
|
from cybertools.typology.interfaces import IType
|
||||||
from loops.browser.common import BaseView
|
from loops.browser.common import BaseView
|
||||||
from loops.knowledge.interfaces import IPerson, ITask
|
from loops.knowledge.interfaces import IPerson, ITask
|
||||||
from loops.organize.browser import getPersonForLoggedInUser
|
from loops.organize.browser import getPersonForUser
|
||||||
|
|
||||||
_ = MessageFactory('zope')
|
_ = MessageFactory('zope')
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ class MyKnowledge(BaseView):
|
||||||
|
|
||||||
@Lazy
|
@Lazy
|
||||||
def person(self):
|
def person(self):
|
||||||
person = getPersonForLoggedInUser(self.request)
|
person = getPersonForUser(self.context, self.request)
|
||||||
if person is not None:
|
if person is not None:
|
||||||
person = IPerson(person)
|
person = IPerson(person)
|
||||||
return person
|
return person
|
||||||
|
|
|
@ -37,6 +37,7 @@ ZCML setup):
|
||||||
|
|
||||||
>>> from loops import Loops
|
>>> from loops import Loops
|
||||||
>>> loopsRoot = site['loops'] = Loops()
|
>>> loopsRoot = site['loops'] = Loops()
|
||||||
|
>>> loopsId = relations.getUniqueIdForObject(loopsRoot)
|
||||||
|
|
||||||
>>> from loops.setup import SetupManager
|
>>> from loops.setup import SetupManager
|
||||||
>>> setup = SetupManager(loopsRoot)
|
>>> setup = SetupManager(loopsRoot)
|
||||||
|
@ -104,7 +105,7 @@ For testing, we first have to provide the needed utilities and settings
|
||||||
|
|
||||||
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
||||||
>>> from loops.organize.party import ANNOTATION_KEY
|
>>> from loops.organize.party import ANNOTATION_KEY
|
||||||
>>> annotations.get(ANNOTATION_KEY) == johnC
|
>>> annotations[ANNOTATION_KEY][loopsId] == johnC
|
||||||
True
|
True
|
||||||
|
|
||||||
Change a userId assignment:
|
Change a userId assignment:
|
||||||
|
@ -113,15 +114,15 @@ Change a userId assignment:
|
||||||
>>> john.userId = 'users.johnny'
|
>>> john.userId = 'users.johnny'
|
||||||
|
|
||||||
>>> annotations = principalAnnotations.getAnnotationsById('users.johnny')
|
>>> annotations = principalAnnotations.getAnnotationsById('users.johnny')
|
||||||
>>> annotations.get(ANNOTATION_KEY) == johnC
|
>>> annotations[ANNOTATION_KEY][loopsId] == johnC
|
||||||
True
|
True
|
||||||
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
||||||
>>> annotations.get(ANNOTATION_KEY) is None
|
>>> annotations[ANNOTATION_KEY][loopsId] is None
|
||||||
True
|
True
|
||||||
|
|
||||||
>>> john.userId = None
|
>>> john.userId = None
|
||||||
>>> annotations = principalAnnotations.getAnnotationsById('users.johnny')
|
>>> annotations = principalAnnotations.getAnnotationsById('users.johnny')
|
||||||
>>> annotations.get(ANNOTATION_KEY) is None
|
>>> annotations[ANNOTATION_KEY][loopsId] is None
|
||||||
True
|
True
|
||||||
|
|
||||||
Deleting a person with a userId assigned removes the corresponding
|
Deleting a person with a userId assigned removes the corresponding
|
||||||
|
@ -138,17 +139,20 @@ principal annotation:
|
||||||
|
|
||||||
>>> john.userId = 'users.john'
|
>>> john.userId = 'users.john'
|
||||||
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
||||||
>>> annotations.get(ANNOTATION_KEY) == johnC
|
>>> annotations[ANNOTATION_KEY][loopsId] == johnC
|
||||||
True
|
True
|
||||||
|
|
||||||
>>> del concepts['john']
|
>>> del concepts['john']
|
||||||
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
||||||
>>> annotations.get(ANNOTATION_KEY) is None
|
>>> annotations[ANNOTATION_KEY][loopsId] is None
|
||||||
True
|
True
|
||||||
|
|
||||||
If we try to assign a userId of a principal that already has a person
|
If we try to assign a userId of a principal that already has a person
|
||||||
concept assigned we should get an error:
|
concept assigned we should get an error:
|
||||||
|
|
||||||
|
>>> johnC = concepts['john'] = Concept(u'John')
|
||||||
|
>>> johnC.conceptType = person
|
||||||
|
>>> john = IPerson(johnC)
|
||||||
>>> john.userId = 'users.john'
|
>>> john.userId = 'users.john'
|
||||||
|
|
||||||
>>> marthaC = concepts['martha'] = Concept(u'Martha')
|
>>> marthaC = concepts['martha'] = Concept(u'Martha')
|
||||||
|
|
|
@ -40,21 +40,17 @@ from loops.browser.node import NodeView
|
||||||
from loops.browser.concept import ConceptRelationView
|
from loops.browser.concept import ConceptRelationView
|
||||||
from loops.organize.interfaces import ANNOTATION_KEY, IMemberRegistrationManager
|
from loops.organize.interfaces import ANNOTATION_KEY, IMemberRegistrationManager
|
||||||
from loops.organize.interfaces import IMemberRegistration
|
from loops.organize.interfaces import IMemberRegistration
|
||||||
|
from loops.organize.party import getPersonForUser
|
||||||
|
|
||||||
_ = MessageFactory('zope')
|
_ = MessageFactory('zope')
|
||||||
|
|
||||||
|
|
||||||
def getPersonForLoggedInUser(request):
|
|
||||||
pa = annotations(request.principal)
|
|
||||||
return pa.get(ANNOTATION_KEY, None)
|
|
||||||
|
|
||||||
|
|
||||||
class MyStuff(ConceptView):
|
class MyStuff(ConceptView):
|
||||||
|
|
||||||
def __init__(self, context, request):
|
def __init__(self, context, request):
|
||||||
self.context = context
|
self.context = context
|
||||||
self.request = request
|
self.request = request
|
||||||
self.person = getPersonForLoggedInUser(request)
|
self.person = getPersonForUser(context, request)
|
||||||
if self.person is not None:
|
if self.person is not None:
|
||||||
self.context = self.person
|
self.context = self.person
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,7 @@ def raiseValidationError(info):
|
||||||
class UserId(schema.TextLine):
|
class UserId(schema.TextLine):
|
||||||
|
|
||||||
def _validate(self, userId):
|
def _validate(self, userId):
|
||||||
|
from loops.organize.party import getPersonForUser
|
||||||
if not userId:
|
if not userId:
|
||||||
return
|
return
|
||||||
context = removeSecurityProxy(self.context).context
|
context = removeSecurityProxy(self.context).context
|
||||||
|
@ -60,8 +61,7 @@ class UserId(schema.TextLine):
|
||||||
except PrincipalLookupError:
|
except PrincipalLookupError:
|
||||||
raiseValidationError(_(u'User $userId does not exist',
|
raiseValidationError(_(u'User $userId does not exist',
|
||||||
mapping={'userId': userId}))
|
mapping={'userId': userId}))
|
||||||
pa = annotations(principal)
|
person = getPersonForUser(context, principal=principal)
|
||||||
person = pa.get(ANNOTATION_KEY, None)
|
|
||||||
if person is not None and person != context:
|
if person is not None and person != context:
|
||||||
raiseValidationError(
|
raiseValidationError(
|
||||||
_(u'There is alread a person ($person) assigned to user $userId.',
|
_(u'There is alread a person ($person) assigned to user $userId.',
|
||||||
|
|
|
@ -22,6 +22,7 @@ Adapters for IConcept providing interfaces from the cybertools.organize package.
|
||||||
$Id$
|
$Id$
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from persistent.mapping import PersistentMapping
|
||||||
from zope import interface, component
|
from zope import interface, component
|
||||||
from zope.app import zapi
|
from zope.app import zapi
|
||||||
from zope.app.principalannotation import annotations
|
from zope.app.principalannotation import annotations
|
||||||
|
@ -35,7 +36,9 @@ from zope.security.proxy import removeSecurityProxy
|
||||||
|
|
||||||
from cybertools.organize.interfaces import IAddress
|
from cybertools.organize.interfaces import IAddress
|
||||||
from cybertools.organize.party import Person as BasePerson
|
from cybertools.organize.party import Person as BasePerson
|
||||||
|
from cybertools.relation.interfaces import IRelationRegistry
|
||||||
from cybertools.typology.interfaces import IType
|
from cybertools.typology.interfaces import IType
|
||||||
|
from loops.concept import Concept
|
||||||
from loops.interfaces import IConcept
|
from loops.interfaces import IConcept
|
||||||
from loops.organize.interfaces import IPerson, ANNOTATION_KEY
|
from loops.organize.interfaces import IPerson, ANNOTATION_KEY
|
||||||
from loops.type import TypeInterfaceSourceList, AdapterBase
|
from loops.type import TypeInterfaceSourceList, AdapterBase
|
||||||
|
@ -46,6 +49,22 @@ from loops.type import TypeInterfaceSourceList, AdapterBase
|
||||||
TypeInterfaceSourceList.typeInterfaces += (IPerson, IAddress)
|
TypeInterfaceSourceList.typeInterfaces += (IPerson, IAddress)
|
||||||
|
|
||||||
|
|
||||||
|
def getPersonForUser(context, request=None, principal=None):
|
||||||
|
if principal is None:
|
||||||
|
principal = request.principal
|
||||||
|
loops = context.getLoopsRoot()
|
||||||
|
pa = annotations(principal).get(ANNOTATION_KEY, None)
|
||||||
|
if pa is None:
|
||||||
|
return None
|
||||||
|
if type(pa) == Concept: # backward compatibility
|
||||||
|
if pa.getLoopsRoot() == loops:
|
||||||
|
return pa
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
return pa.get(component.getUtility(
|
||||||
|
IRelationRegistry, context=context).getUniqueIdForObject(loops))
|
||||||
|
|
||||||
|
|
||||||
class Person(AdapterBase, BasePerson):
|
class Person(AdapterBase, BasePerson):
|
||||||
""" typeInterface adapter for concepts of type 'person'.
|
""" typeInterface adapter for concepts of type 'person'.
|
||||||
"""
|
"""
|
||||||
|
@ -58,16 +77,22 @@ class Person(AdapterBase, BasePerson):
|
||||||
def getUserId(self):
|
def getUserId(self):
|
||||||
return getattr(self.context, '_userId', None)
|
return getattr(self.context, '_userId', None)
|
||||||
def setUserId(self, userId):
|
def setUserId(self, userId):
|
||||||
auth = self.authentication
|
#auth = self.authentication
|
||||||
if userId:
|
if userId:
|
||||||
principal = auth.getPrincipal(userId)
|
principal = self.getPrincipalForUserId(userId)
|
||||||
pa = annotations(principal)
|
person = getPersonForUser(self.context, principal=principal)
|
||||||
person = pa.get(ANNOTATION_KEY, None)
|
|
||||||
if person is not None and person != self.context:
|
if person is not None and person != self.context:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'There is alread a person (%s) assigned to user %s.'
|
'There is alread a person (%s) assigned to user %s.'
|
||||||
% (zapi.getName(person), userId))
|
% (zapi.getName(person), userId))
|
||||||
pa[ANNOTATION_KEY] = self.context
|
pa = annotations(principal)
|
||||||
|
#pa[ANNOTATION_KEY] = self.context
|
||||||
|
intIds = component.getUtility(IRelationRegistry, context=self.context)
|
||||||
|
loopsId = intIds.getUniqueIdForObject(self.context.getLoopsRoot())
|
||||||
|
ann = pa.get(ANNOTATION_KEY)
|
||||||
|
if ann is None:
|
||||||
|
ann = pa[ANNOTATION_KEY] = PersistentMapping()
|
||||||
|
ann[loopsId] = self.context
|
||||||
oldUserId = self.userId
|
oldUserId = self.userId
|
||||||
if oldUserId and oldUserId != userId:
|
if oldUserId and oldUserId != userId:
|
||||||
self.removeReferenceFromPrincipal(oldUserId)
|
self.removeReferenceFromPrincipal(oldUserId)
|
||||||
|
@ -78,7 +103,14 @@ class Person(AdapterBase, BasePerson):
|
||||||
principal = self.getPrincipalForUserId(userId)
|
principal = self.getPrincipalForUserId(userId)
|
||||||
if principal is not None:
|
if principal is not None:
|
||||||
pa = annotations(principal)
|
pa = annotations(principal)
|
||||||
pa[ANNOTATION_KEY] = None
|
ann = pa.get(ANNOTATION_KEY)
|
||||||
|
if type(ann) == Concept: # backward compatibility
|
||||||
|
pa[ANNOTATION_KEY] = None
|
||||||
|
else:
|
||||||
|
if ann is not None:
|
||||||
|
intIds = component.getUtility(IRelationRegistry, context=self.context)
|
||||||
|
loopsId = intIds.getUniqueIdForObject(self.context.getLoopsRoot())
|
||||||
|
ann[loopsId] = None
|
||||||
|
|
||||||
def getPhoneNumbers(self):
|
def getPhoneNumbers(self):
|
||||||
return getattr(self.context, '_phoneNumbers', [])
|
return getattr(self.context, '_phoneNumbers', [])
|
||||||
|
@ -104,16 +136,6 @@ class Person(AdapterBase, BasePerson):
|
||||||
except PrincipalLookupError:
|
except PrincipalLookupError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def getAuthenticationUtility(context):
|
def getAuthenticationUtility(context):
|
||||||
return component.getUtility(IAuthentication, context=context)
|
return component.getUtility(IAuthentication, context=context)
|
||||||
|
|
Loading…
Add table
Reference in a new issue