X-Git-Url: https://svn.cri.ensmp.fr/git/photoprint.git/blobdiff_plain/fbcbbb59f3bf16d329c9cf9b1295eaeb15ae040e..4bb6988027b9396a5f0ed10509f15dc2a230c61c:/utils.py diff --git a/utils.py b/utils.py index 68b8c46..9f4706f 100755 --- a/utils.py +++ b/utils.py @@ -11,28 +11,56 @@ """ Global utilities -$Id: utils.py 651 2009-02-04 15:38:20Z pin $ -$URL: http://svn.luxia.fr/svn/labo/projects/zope/photoprint/trunk/utils.py $ + + """ from AccessControl import ModuleSecurityInfo -# TODO: trouver une solution… from zope.i18n import translate as i18ntranslate from zope.i18nmessageid import MessageFactory +from zope.globalrequest import getRequest +from Products.CMFCore.utils import getUtilityByInterfaceName +from Products.Plinn.utils import _sudo +import transaction + security = ModuleSecurityInfo('Products.photoprint.utils') security.declarePublic('translate') -def translate(message, context): - """ Translate i18n message. - """ - # GTS = getGlobalTranslationService() - if isinstance(message, Exception): - try: - message = message[0] - except (TypeError, IndexError): - pass - return i18ntranslate(message, domain='photoprint', context=context.REQUEST) +def translate(msgid, mapping=None, default=None) : + """ traduction dans le domaine photoprint """ + return i18ntranslate(msgid, domain='photoprint', mapping=mapping, context=getRequest(), default=default) security.declarePublic('Message') Message = _ = MessageFactory('photoprint') + +security.declarePublic('grantAccess') +def grantAccess(collectionId, password, confirm, memberId) : + utool = getUtilityByInterfaceName('Products.CMFCore.interfaces.IURLTool') + mtool = getUtilityByInterfaceName('Products.CMFCore.interfaces.IMembershipTool') + portal = utool.getPortalObject() + + data = portal.private_collections.data + lines = filter(None, [l.strip() for l in data.split('\n')]) + assert len(lines) % 3 == 0 + collecInfos = {} + for i in xrange(0, len(lines), 3) : + collecInfos[lines[i]] = {'pw' : lines[i+1], + 'path' : lines[i+2]} + + if not collecInfos.has_key(collectionId) : + transaction.abort() + return _('Wrong private collection identifier.') + elif password != confirm : + transaction.abort() + return _("Collection's password does not match confirmation.") + else : + if collecInfos[collectionId]['pw'] != password : + transaction.abort() + return _("Wrong collection's password.") + else : + collec = portal.unrestrictedTraverse(collecInfos[collectionId]['path']) + def do() : + mtool.setLocalRoles(collec, [memberId], 'Reader') + + _sudo(do)