9f4706f8deebb417831676dfc3c7b573597f4341
[photoprint.git] / utils.py
1 # -*- coding: utf-8 -*-
2 ############################################################
3 # Copyright © 2008 Benoît PIN <benoit.pin@ensmp.fr> #
4 # Plinn - http://plinn.org #
5 # #
6 # This program is free software; you can redistribute it #
7 # and/or modify it under the terms of the Creative Commons #
8 # "Attribution-Noncommercial 2.0 Generic" #
9 # http://creativecommons.org/licenses/by-nc/2.0/ #
10 ############################################################
11 """
12 Global utilities
13
14
15
16 """
17
18 from AccessControl import ModuleSecurityInfo
19 from zope.i18n import translate as i18ntranslate
20 from zope.i18nmessageid import MessageFactory
21 from zope.globalrequest import getRequest
22 from Products.CMFCore.utils import getUtilityByInterfaceName
23 from Products.Plinn.utils import _sudo
24 import transaction
25
26
27 security = ModuleSecurityInfo('Products.photoprint.utils')
28
29 security.declarePublic('translate')
30 def translate(msgid, mapping=None, default=None) :
31 """ traduction dans le domaine photoprint """
32 return i18ntranslate(msgid, domain='photoprint', mapping=mapping, context=getRequest(), default=default)
33
34 security.declarePublic('Message')
35 Message = _ = MessageFactory('photoprint')
36
37 security.declarePublic('grantAccess')
38 def grantAccess(collectionId, password, confirm, memberId) :
39 utool = getUtilityByInterfaceName('Products.CMFCore.interfaces.IURLTool')
40 mtool = getUtilityByInterfaceName('Products.CMFCore.interfaces.IMembershipTool')
41 portal = utool.getPortalObject()
42
43 data = portal.private_collections.data
44 lines = filter(None, [l.strip() for l in data.split('\n')])
45 assert len(lines) % 3 == 0
46 collecInfos = {}
47 for i in xrange(0, len(lines), 3) :
48 collecInfos[lines[i]] = {'pw' : lines[i+1],
49 'path' : lines[i+2]}
50
51 if not collecInfos.has_key(collectionId) :
52 transaction.abort()
53 return _('Wrong private collection identifier.')
54 elif password != confirm :
55 transaction.abort()
56 return _("Collection's password does not match confirmation.")
57 else :
58 if collecInfos[collectionId]['pw'] != password :
59 transaction.abort()
60 return _("Wrong collection's password.")
61 else :
62 collec = portal.unrestrictedTraverse(collecInfos[collectionId]['path'])
63 def do() :
64 mtool.setLocalRoles(collec, [memberId], 'Reader')
65
66 _sudo(do)