comparison sat_pubsub/pgsql_storage.py @ 250:eb14b8d30cba

fine tuning per-item permissions
author Goffi <goffi@goffi.org>
date Sun, 24 Jun 2012 19:35:49 +0200
parents aaf5e34ff765
children 25a1dc7181cc
comparison
equal deleted inserted replaced
249:aaf5e34ff765 250:eb14b8d30cba
59 from twisted.internet import defer 59 from twisted.internet import defer
60 from twisted.words.protocols.jabber import jid 60 from twisted.words.protocols.jabber import jid
61 61
62 from wokkel.generic import parseXml, stripNamespace 62 from wokkel.generic import parseXml, stripNamespace
63 from wokkel.pubsub import Subscription 63 from wokkel.pubsub import Subscription
64 from wokkel import data_form 64
65 65 from sat_pubsub import error, iidavoll, const
66 from sat_pubsub import error, iidavoll
67
68 NS_ITEM_CONFIG = "http://jabber.org/protocol/pubsub#item-config"
69 OPT_ACCESS_MODEL = 'pubsub#access_model'
70 OPT_ROSTER_GROUPS_ALLOWED = 'pubsub#roster_groups_allowed'
71 VAL_OPEN = 'open'
72 VAL_ROSTER = 'roster'
73 VAL_DEFAULT = VAL_OPEN
74 66
75 class Storage: 67 class Storage:
76 68
77 implements(iidavoll.IStorage) 69 implements(iidavoll.IStorage)
78 70
505 497
506 implements(iidavoll.ILeafNode) 498 implements(iidavoll.ILeafNode)
507 499
508 nodeType = 'leaf' 500 nodeType = 'leaf'
509 501
510 def storeItems(self, items, publisher): 502 def storeItems(self, item_data, publisher):
511 return self.dbpool.runInteraction(self._storeItems, items, publisher) 503 return self.dbpool.runInteraction(self._storeItems, item_data, publisher)
512 504
513 505
514 def _storeItems(self, cursor, items, publisher): 506 def _storeItems(self, cursor, item_data, publisher):
515 self._checkNodeExists(cursor) 507 self._checkNodeExists(cursor)
516 for item in items: 508 for item_datum in item_data:
517 self._storeItem(cursor, item, publisher) 509 self._storeItem(cursor, item_datum, publisher)
518 510
519 511
520 def _storeItem(self, cursor, item, publisher): 512 def _storeItem(self, cursor, item_datum, publisher):
521 item_config = None 513 access_model, item_config, item = item_datum
522 access_model = VAL_DEFAULT
523 for i in range(len(item.children)):
524 elt = item.children[i]
525 if not (elt.uri,elt.name)==(data_form.NS_X_DATA,'x'):
526 continue
527 form = data_form.Form.fromElement(elt)
528 if (form.formNamespace == NS_ITEM_CONFIG):
529 item_config = form
530 del item.children[i] #we need to remove the config from item
531 break
532
533 if item_config:
534 access_model = item_config.get(OPT_ACCESS_MODEL, VAL_DEFAULT)
535
536 data = item.toXml() 514 data = item.toXml()
537 515
538 cursor.execute("""UPDATE items SET date=now(), publisher=%s, data=%s 516 cursor.execute("""UPDATE items SET date=now(), publisher=%s, data=%s
539 FROM nodes 517 FROM nodes
540 WHERE nodes.node_id = items.node_id AND 518 WHERE nodes.node_id = items.node_id AND
554 publisher.full(), 532 publisher.full(),
555 data, 533 data,
556 access_model, 534 access_model,
557 self.nodeIdentifier)) 535 self.nodeIdentifier))
558 536
559 if access_model == VAL_ROSTER: 537 if access_model == const.VAL_ROSTER:
560 item_id = cursor.fetchone()[0]; 538 item_id = cursor.fetchone()[0];
561 if OPT_ROSTER_GROUPS_ALLOWED in item_config: 539 if const.OPT_ROSTER_GROUPS_ALLOWED in item_config:
562 item_config.fields[OPT_ROSTER_GROUPS_ALLOWED].fieldType='list-multi' #XXX: needed to have a list if there is only one value 540 item_config.fields[const.OPT_ROSTER_GROUPS_ALLOWED].fieldType='list-multi' #XXX: needed to force list if there is only one value
563 allowed_groups = item_config[OPT_ROSTER_GROUPS_ALLOWED] 541 allowed_groups = item_config[const.OPT_ROSTER_GROUPS_ALLOWED]
564 else: 542 else:
565 allowed_groups = [] 543 allowed_groups = []
566 for group in allowed_groups: 544 for group in allowed_groups:
567 #TODO: check that group are actually in roster 545 #TODO: check that group are actually in roster
568 cursor.execute("""INSERT INTO item_groups_authorized (item_id, groupname) 546 cursor.execute("""INSERT INTO item_groups_authorized (item_id, groupname)