comparison sat_pubsub/pgsql_storage.py @ 252:25a1dc7181cc

full items, with item-configuration, are returned if items are asked by the owner
author Goffi <goffi@goffi.org>
date Thu, 01 Nov 2012 19:28:43 +0100
parents eb14b8d30cba
children d55620ceafed
comparison
equal deleted inserted replaced
251:0a7d43b3dad6 252:25a1dc7181cc
569 569
570 return deleted 570 return deleted
571 571
572 572
573 def getItems(self, authorized_groups, unrestricted, maxItems=None): 573 def getItems(self, authorized_groups, unrestricted, maxItems=None):
574 """ Get all authorised items
575 @param authorized_groups: we want to get items that these groups can access
576 @param unrestricted: if true, don't check permissions (i.e.: get all items)
577 @param maxItems: nb of items we want to tget
578 @return: list of (item, access_model, access_model) if unrestricted is True, else list of items
579 """
574 return self.dbpool.runInteraction(self._getItems, authorized_groups, unrestricted, maxItems) 580 return self.dbpool.runInteraction(self._getItems, authorized_groups, unrestricted, maxItems)
575 581
576 582
577 def _getItems(self, cursor, authorized_groups, unrestricted, maxItems): 583 def _getItems(self, cursor, authorized_groups, unrestricted, maxItems):
578 self._checkNodeExists(cursor) 584 self._checkNodeExists(cursor)
579 if unrestricted: 585 if unrestricted:
580 query = ["""SELECT data FROM nodes 586 query = ["""SELECT data,items.access_model,item_id FROM nodes
581 INNER JOIN items USING (node_id) 587 INNER JOIN items USING (node_id)
582 WHERE node=%s ORDER BY date DESC"""] 588 WHERE node=%s ORDER BY date DESC"""]
583 args = [self.nodeIdentifier] 589 args = [self.nodeIdentifier]
584 else: 590 else:
585 query = ["""SELECT data FROM nodes 591 query = ["""SELECT data FROM nodes
586 INNER JOIN items USING (node_id) 592 INNER JOIN items USING (node_id)
599 args.append(maxItems) 605 args.append(maxItems)
600 606
601 cursor.execute(' '.join(query), args) 607 cursor.execute(' '.join(query), args)
602 608
603 result = cursor.fetchall() 609 result = cursor.fetchall()
610 if unrestricted:
611 ret = []
612 for data in result:
613 item = stripNamespace(parseXml(data[0]))
614 access_model = data[1]
615 item_id = data[2]
616 if access_model == 'roster': #TODO: jid access_model
617 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,))
618 access_list = [r[0] for r in cursor.fetchall()]
619 else:
620 access_list = None
621
622 ret.append((item, access_model, access_list))
623 return ret
604 items = [stripNamespace(parseXml(r[0])) for r in result] 624 items = [stripNamespace(parseXml(r[0])) for r in result]
605 return items 625 return items
606 626
607 627
608 def getItemsById(self, authorized_groups, unrestricted, itemIdentifiers): 628 def getItemsById(self, authorized_groups, unrestricted, itemIdentifiers):
609 return self.dbpool.runInteraction(self._getItemsById, itemIdentifiers) 629 """ Get items which are in the given list
630 @param authorized_groups: we want to get items that these groups can access
631 @param unrestricted: if true, don't check permissions
632 @param itemIdentifiers: list of ids of the items we want to get
633 @return: list of (item, access_model, access_model) if unrestricted is True, else list of items
634 """
635 return self.dbpool.runInteraction(self._getItemsById, authorized_groups, unrestricted, itemIdentifiers)
610 636
611 637
612 def _getItemsById(self, cursor, authorized_groups, unrestricted, itemIdentifiers): 638 def _getItemsById(self, cursor, authorized_groups, unrestricted, itemIdentifiers):
613 self._checkNodeExists(cursor) 639 self._checkNodeExists(cursor)
614 items = [] 640 ret = []
615 for itemIdentifier in itemIdentifiers: 641 if unrestricted: #we get everything without checking permissions
616 cursor.execute("""SELECT data FROM nodes 642 for itemIdentifier in itemIdentifiers:
617 INNER JOIN items USING (node_id) 643 cursor.execute("""SELECT data,items.access_model,item_id FROM nodes
618 WHERE node=%s AND item=%s""", 644 INNER JOIN items USING (node_id)
619 (self.nodeIdentifier, 645 WHERE node=%s AND item=%s""",
620 itemIdentifier)) 646 (self.nodeIdentifier,
621 result = cursor.fetchone() 647 itemIdentifier))
622 if result: 648 result = cursor.fetchone()
623 items.append(parseXml(result[0])) 649 if result:
624 return items 650 for data in result:
625 651 item = stripNamespace(parseXml(data[0]))
652 access_model = data[1]
653 item_id = data[2]
654 if access_model == 'roster': #TODO: jid access_model
655 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,))
656 access_list = [r[0] for r in cursor.fetchall()]
657 else:
658 access_list = None
659
660 ret.append((item, access_model, access_list))
661 else: #we check permission before returning items
662 for itemIdentifier in itemIdentifiers:
663 args = [self.nodeIdentifier, itemIdentifier]
664 if authorized_groups:
665 args.append(authorized_groups)
666 cursor.execute("""SELECT data FROM nodes
667 INNER JOIN items USING (node_id)
668 LEFT JOIN item_groups_authorized USING (item_id)
669 WHERE node=%s AND item=%s AND
670 (items.access_model='open' """ +
671 ("or (items.access_model='roster' and groupname in %s)" if authorized_groups else '') + ")",
672 args)
673
674 result = cursor.fetchone()
675 if result:
676 ret.append(parseXml(result[0]))
677
678 return ret
626 679
627 def purge(self): 680 def purge(self):
628 return self.dbpool.runInteraction(self._purge) 681 return self.dbpool.runInteraction(self._purge)
629 682
630 683