comparison idavoll/generic_backend.py @ 150:35977eb964e6

Use uuid.py module [1] for generating unique id's for nodes and items. Factor out authorization checking. [1] http://ofxsuite.berlios.de/uuid.py
author Ralph Meijer <ralphm@ik.nu>
date Tue, 04 Oct 2005 12:17:01 +0000
parents 5b0b3f013ccc
children ea8b4189ae3b
comparison
equal deleted inserted replaced
149:043f2e5ce8cd 150:35977eb964e6
1 import sha 1 import sha
2 import time 2 import time
3 import uuid
3 from twisted.words.protocols.jabber import jid 4 from twisted.words.protocols.jabber import jid
4 from twisted.application import service 5 from twisted.application import service
5 from twisted.xish import utility 6 from twisted.xish import utility
6 from twisted.internet import defer 7 from twisted.internet import defer
7 from zope.interface import implements 8 from zope.interface import implements
69 70
70 class PublishService(service.Service): 71 class PublishService(service.Service):
71 72
72 implements(backend.IPublishService) 73 implements(backend.IPublishService)
73 74
75 def _check_auth(self, node, requestor):
76 def check(affiliation, node):
77 if affiliation not in ['owner', 'publisher']:
78 raise backend.NotAuthorized
79 return node
80
81 d = node.get_affiliation(requestor)
82 d.addCallback(check, node)
83 return d
84
74 def publish(self, node_id, items, requestor): 85 def publish(self, node_id, items, requestor):
75 d = self.parent.storage.get_node(node_id) 86 d = self.parent.storage.get_node(node_id)
76 d.addCallback(_get_affiliation, requestor) 87 d.addCallback(self._check_auth, requestor)
77 d.addCallback(self._do_publish, items, requestor) 88 d.addCallback(self._do_publish, items, requestor)
78 return d 89 return d
79 90
80 def _do_publish(self, result, items, requestor): 91 def _do_publish(self, node, items, requestor):
81 node, affiliation = result
82 configuration = node.get_configuration() 92 configuration = node.get_configuration()
83 persist_items = configuration["pubsub#persist_items"] 93 persist_items = configuration["pubsub#persist_items"]
84 deliver_payloads = configuration["pubsub#deliver_payloads"] 94 deliver_payloads = configuration["pubsub#deliver_payloads"]
85 95
86 if affiliation not in ['owner', 'publisher']:
87 raise backend.NotAuthorized
88
89 if items and not persist_items and not deliver_payloads: 96 if items and not persist_items and not deliver_payloads:
90 raise backend.NoPayloadAllowed 97 raise backend.NoPayloadAllowed
91 elif not items and (persist_items or deliver_payloads): 98 elif not items and (persist_items or deliver_payloads):
92 raise backend.PayloadExpected 99 raise backend.PayloadExpected
93 100
94 if persist_items or deliver_payloads: 101 if persist_items or deliver_payloads:
95 for item in items: 102 for item in items:
96 if not item.getAttribute("id"): 103 if not item.getAttribute("id"):
97 item["id"] = sha.new(str(time.time()) + 104 item["id"] = uuid.generate()
98 requestor.full()).hexdigest()
99 105
100 if persist_items: 106 if persist_items:
101 d = node.store_items(items, requestor) 107 d = node.store_items(items, requestor)
102 else: 108 else:
103 d = defer.succeed(None) 109 d = defer.succeed(None)
138 implements(backend.ISubscriptionService) 144 implements(backend.ISubscriptionService)
139 145
140 def subscribe(self, node_id, subscriber, requestor): 146 def subscribe(self, node_id, subscriber, requestor):
141 subscriber_entity = subscriber.userhostJID() 147 subscriber_entity = subscriber.userhostJID()
142 if subscriber_entity != requestor: 148 if subscriber_entity != requestor:
143 return defer.fail(backend.NotAuthorized) 149 return defer.fail(backend.NotAuthorized())
144 150
145 d = self.parent.storage.get_node(node_id) 151 d = self.parent.storage.get_node(node_id)
146 d.addCallback(_get_affiliation, subscriber_entity) 152 d.addCallback(_get_affiliation, subscriber_entity)
147 d.addCallback(self._do_subscribe, subscriber) 153 d.addCallback(self._do_subscribe, subscriber)
148 return d 154 return d
183 def supports_instant_nodes(self): 189 def supports_instant_nodes(self):
184 return True 190 return True
185 191
186 def create_node(self, node_id, requestor): 192 def create_node(self, node_id, requestor):
187 if not node_id: 193 if not node_id:
188 node_id = 'generic/%s' % sha.new(str(time.time()) + 194 node_id = 'generic/%s' % uuid.generate()
189 requestor.full()).hexdigest()
190
191 d = self.parent.storage.create_node(node_id, requestor) 195 d = self.parent.storage.create_node(node_id, requestor)
192 d.addCallback(lambda _: node_id) 196 d.addCallback(lambda _: node_id)
193 return d 197 return d
194 198
195 def get_node_configuration(self, node_id): 199 def get_node_configuration(self, node_id):