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 loops.browser.common import BaseView
|
||||
from loops.knowledge.interfaces import IPerson, ITask
|
||||
from loops.organize.browser import getPersonForLoggedInUser
|
||||
from loops.organize.browser import getPersonForUser
|
||||
|
||||
_ = MessageFactory('zope')
|
||||
|
||||
|
@ -48,7 +48,7 @@ class MyKnowledge(BaseView):
|
|||
|
||||
@Lazy
|
||||
def person(self):
|
||||
person = getPersonForLoggedInUser(self.request)
|
||||
person = getPersonForUser(self.context, self.request)
|
||||
if person is not None:
|
||||
person = IPerson(person)
|
||||
return person
|
||||
|
|
|
@ -37,6 +37,7 @@ ZCML setup):
|
|||
|
||||
>>> from loops import Loops
|
||||
>>> loopsRoot = site['loops'] = Loops()
|
||||
>>> loopsId = relations.getUniqueIdForObject(loopsRoot)
|
||||
|
||||
>>> from loops.setup import SetupManager
|
||||
>>> setup = SetupManager(loopsRoot)
|
||||
|
@ -104,7 +105,7 @@ For testing, we first have to provide the needed utilities and settings
|
|||
|
||||
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
||||
>>> from loops.organize.party import ANNOTATION_KEY
|
||||
>>> annotations.get(ANNOTATION_KEY) == johnC
|
||||
>>> annotations[ANNOTATION_KEY][loopsId] == johnC
|
||||
True
|
||||
|
||||
Change a userId assignment:
|
||||
|
@ -113,15 +114,15 @@ Change a userId assignment:
|
|||
>>> john.userId = 'users.johnny'
|
||||
|
||||
>>> annotations = principalAnnotations.getAnnotationsById('users.johnny')
|
||||
>>> annotations.get(ANNOTATION_KEY) == johnC
|
||||
>>> annotations[ANNOTATION_KEY][loopsId] == johnC
|
||||
True
|
||||
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
||||
>>> annotations.get(ANNOTATION_KEY) is None
|
||||
>>> annotations[ANNOTATION_KEY][loopsId] is None
|
||||
True
|
||||
|
||||
>>> john.userId = None
|
||||
>>> annotations = principalAnnotations.getAnnotationsById('users.johnny')
|
||||
>>> annotations.get(ANNOTATION_KEY) is None
|
||||
>>> annotations[ANNOTATION_KEY][loopsId] is None
|
||||
True
|
||||
|
||||
Deleting a person with a userId assigned removes the corresponding
|
||||
|
@ -138,17 +139,20 @@ principal annotation:
|
|||
|
||||
>>> john.userId = 'users.john'
|
||||
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
||||
>>> annotations.get(ANNOTATION_KEY) == johnC
|
||||
>>> annotations[ANNOTATION_KEY][loopsId] == johnC
|
||||
True
|
||||
|
||||
>>> del concepts['john']
|
||||
>>> annotations = principalAnnotations.getAnnotationsById('users.john')
|
||||
>>> annotations.get(ANNOTATION_KEY) is None
|
||||
>>> annotations[ANNOTATION_KEY][loopsId] is None
|
||||
True
|
||||
|
||||
If we try to assign a userId of a principal that already has a person
|
||||
concept assigned we should get an error:
|
||||
|
||||
>>> johnC = concepts['john'] = Concept(u'John')
|
||||
>>> johnC.conceptType = person
|
||||
>>> john = IPerson(johnC)
|
||||
>>> john.userId = 'users.john'
|
||||
|
||||
>>> marthaC = concepts['martha'] = Concept(u'Martha')
|
||||
|
|
|
@ -40,21 +40,17 @@ from loops.browser.node import NodeView
|
|||
from loops.browser.concept import ConceptRelationView
|
||||
from loops.organize.interfaces import ANNOTATION_KEY, IMemberRegistrationManager
|
||||
from loops.organize.interfaces import IMemberRegistration
|
||||
from loops.organize.party import getPersonForUser
|
||||
|
||||
_ = MessageFactory('zope')
|
||||
|
||||
|
||||
def getPersonForLoggedInUser(request):
|
||||
pa = annotations(request.principal)
|
||||
return pa.get(ANNOTATION_KEY, None)
|
||||
|
||||
|
||||
class MyStuff(ConceptView):
|
||||
|
||||
def __init__(self, context, request):
|
||||
self.context = context
|
||||
self.request = request
|
||||
self.person = getPersonForLoggedInUser(request)
|
||||
self.person = getPersonForUser(context, request)
|
||||
if self.person is not None:
|
||||
self.context = self.person
|
||||
|
||||
|
|
|
@ -51,6 +51,7 @@ def raiseValidationError(info):
|
|||
class UserId(schema.TextLine):
|
||||
|
||||
def _validate(self, userId):
|
||||
from loops.organize.party import getPersonForUser
|
||||
if not userId:
|
||||
return
|
||||
context = removeSecurityProxy(self.context).context
|
||||
|
@ -60,8 +61,7 @@ class UserId(schema.TextLine):
|
|||
except PrincipalLookupError:
|
||||
raiseValidationError(_(u'User $userId does not exist',
|
||||
mapping={'userId': userId}))
|
||||
pa = annotations(principal)
|
||||
person = pa.get(ANNOTATION_KEY, None)
|
||||
person = getPersonForUser(context, principal=principal)
|
||||
if person is not None and person != context:
|
||||
raiseValidationError(
|
||||
_(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$
|
||||
"""
|
||||
|
||||
from persistent.mapping import PersistentMapping
|
||||
from zope import interface, component
|
||||
from zope.app import zapi
|
||||
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.party import Person as BasePerson
|
||||
from cybertools.relation.interfaces import IRelationRegistry
|
||||
from cybertools.typology.interfaces import IType
|
||||
from loops.concept import Concept
|
||||
from loops.interfaces import IConcept
|
||||
from loops.organize.interfaces import IPerson, ANNOTATION_KEY
|
||||
from loops.type import TypeInterfaceSourceList, AdapterBase
|
||||
|
@ -46,6 +49,22 @@ from loops.type import TypeInterfaceSourceList, AdapterBase
|
|||
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):
|
||||
""" typeInterface adapter for concepts of type 'person'.
|
||||
"""
|
||||
|
@ -58,16 +77,22 @@ class Person(AdapterBase, BasePerson):
|
|||
def getUserId(self):
|
||||
return getattr(self.context, '_userId', None)
|
||||
def setUserId(self, userId):
|
||||
auth = self.authentication
|
||||
#auth = self.authentication
|
||||
if userId:
|
||||
principal = auth.getPrincipal(userId)
|
||||
pa = annotations(principal)
|
||||
person = pa.get(ANNOTATION_KEY, None)
|
||||
principal = self.getPrincipalForUserId(userId)
|
||||
person = getPersonForUser(self.context, principal=principal)
|
||||
if person is not None and person != self.context:
|
||||
raise ValueError(
|
||||
'There is alread a person (%s) assigned to user %s.'
|
||||
% (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
|
||||
if oldUserId and oldUserId != userId:
|
||||
self.removeReferenceFromPrincipal(oldUserId)
|
||||
|
@ -78,7 +103,14 @@ class Person(AdapterBase, BasePerson):
|
|||
principal = self.getPrincipalForUserId(userId)
|
||||
if principal is not None:
|
||||
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):
|
||||
return getattr(self.context, '_phoneNumbers', [])
|
||||
|
@ -104,16 +136,6 @@ class Person(AdapterBase, BasePerson):
|
|||
except PrincipalLookupError:
|
||||
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):
|
||||
return component.getUtility(IAuthentication, context=context)
|
||||
|
|
Loading…
Add table
Reference in a new issue