comparison sat_pubsub/backend.py @ 331:e93a9fd329d9

affiliations XMPP handling: /!\ PostGreSQL minimal version raised to 9.5 (for upsert support) - affiliations can now be handler after node creation using XMPP: affiliationsGet, affiliationsSet (both for node owner) and affiliations (for an entity to know with which nodes it is affiliated on the service) are implemented - pgsql: getOrCreateEntities helping method has been implemented, it get entity_id and create missing entities when needed.
author Goffi <goffi@goffi.org>
date Sun, 26 Mar 2017 20:58:48 +0200
parents 82d1259b3e36
children 0fcd0ea89c84
comparison
equal deleted inserted replaced
330:82d1259b3e36 331:e93a9fd329d9
553 raise error.Forbidden() 553 raise error.Forbidden()
554 554
555 return node.setConfiguration(options) 555 return node.setConfiguration(options)
556 556
557 557
558 def getAffiliations(self, entity): 558 def getAffiliations(self, entity, nodeIdentifier, pep, recipient):
559 return self.storage.getAffiliations(entity) 559 return self.storage.getAffiliations(entity, nodeIdentifier, pep, recipient)
560
561
562 def getAffiliationsOwner(self, nodeIdentifier, requestor, pep, recipient):
563 d = self.storage.getNode(nodeIdentifier, pep, recipient)
564 d.addCallback(_getAffiliation, requestor)
565 d.addCallback(self._doGetAffiliationsOwner)
566 return d
567
568
569 def _doGetAffiliationsOwner(self, result):
570 node, affiliation = result
571
572 if affiliation != 'owner':
573 raise error.Forbidden()
574 return node.getAffiliations()
575
576
577 def setAffiliationsOwner(self, nodeIdentifier, requestor, affiliations, pep, recipient):
578 d = self.storage.getNode(nodeIdentifier, pep, recipient)
579 d.addCallback(_getAffiliation, requestor)
580 d.addCallback(self._doSetAffiliationsOwner, requestor, affiliations)
581 return d
582
583
584 def _doSetAffiliationsOwner(self, result, requestor, affiliations):
585 # Check that requestor is allowed to set affiliations, and delete entities
586 # with "none" affiliation
587
588 # TODO: return error with failed affiliations in case of failure
589 node, requestor_affiliation = result
590
591 if requestor_affiliation != 'owner':
592 raise error.Forbidden()
593
594 # we don't allow requestor to change its own affiliation
595 requestor_bare = requestor.userhostJID()
596 if requestor_bare in affiliations and affiliations[requestor_bare] != 'owner':
597 # FIXME: it may be interesting to allow the owner to ask for ownership removal
598 # if at least one other entity is owner for this node
599 raise error.Forbidden("You can't change your own affiliation")
600
601 to_delete = [jid_ for jid_, affiliation in affiliations.iteritems() if affiliation == 'none']
602 for jid_ in to_delete:
603 del affiliations[jid_]
604
605 if to_delete:
606 d = node.deleteAffiliations(to_delete)
607 if affiliations:
608 d.addCallback(lambda dummy: node.setAffiliations(affiliations))
609 else:
610 d = node.setAffiliations(affiliations)
611
612 return d
560 613
561 614
562 def getItems(self, nodeIdentifier, requestor, recipient, maxItems=None, 615 def getItems(self, nodeIdentifier, requestor, recipient, maxItems=None,
563 itemIdentifiers=None, ext_data=None): 616 itemIdentifiers=None, ext_data=None):
564 d = self.getItemsData(nodeIdentifier, requestor, recipient, maxItems, itemIdentifiers, ext_data) 617 d = self.getItemsData(nodeIdentifier, requestor, recipient, maxItems, itemIdentifiers, ext_data)
1238 request.sender) 1291 request.sender)
1239 return d.addErrback(self._mapErrors) 1292 return d.addErrback(self._mapErrors)
1240 1293
1241 1294
1242 def affiliations(self, request): 1295 def affiliations(self, request):
1243 d = self.backend.getAffiliations(self._isPep(request), 1296 """Retrieve affiliation for normal entity (cf. XEP-0060 §5.7)
1244 request.sender) 1297
1298 retrieve all node where this jid is affiliated
1299 """
1300 d = self.backend.getAffiliations(request.sender,
1301 request.nodeIdentifier,
1302 self._isPep(request),
1303 request.recipient)
1245 return d.addErrback(self._mapErrors) 1304 return d.addErrback(self._mapErrors)
1246 1305
1247 1306
1248 def create(self, request): 1307 def create(self, request):
1249 d = self.backend.createNode(request.nodeIdentifier, 1308 d = self.backend.createNode(request.nodeIdentifier,
1269 1328
1270 def configureSet(self, request): 1329 def configureSet(self, request):
1271 d = self.backend.setNodeConfiguration(request.nodeIdentifier, 1330 d = self.backend.setNodeConfiguration(request.nodeIdentifier,
1272 request.options, 1331 request.options,
1273 request.sender, 1332 request.sender,
1333 self._isPep(request),
1334 request.recipient)
1335 return d.addErrback(self._mapErrors)
1336
1337
1338 def affiliationsGet(self, request):
1339 """Retrieve affiliations for owner (cf. XEP-0060 §8.9.1)
1340
1341 retrieve all affiliations for a node
1342 """
1343 d = self.backend.getAffiliationsOwner(request.nodeIdentifier,
1344 request.sender,
1345 self._isPep(request),
1346 request.recipient)
1347 return d.addErrback(self._mapErrors)
1348
1349 def affiliationsSet(self, request):
1350 d = self.backend.setAffiliationsOwner(request.nodeIdentifier,
1351 request.sender,
1352 request.affiliations,
1274 self._isPep(request), 1353 self._isPep(request),
1275 request.recipient) 1354 request.recipient)
1276 return d.addErrback(self._mapErrors) 1355 return d.addErrback(self._mapErrors)
1277 1356
1278 1357