comparison sat_pubsub/pgsql_storage.py @ 259:6fe7da6b4b32

node "roster" access model management
author Goffi <goffi@goffi.org>
date Mon, 06 May 2013 00:11:44 +0200
parents d55620ceafed
children f0cd02c032b3
comparison
equal deleted inserted replaced
258:e5b83fbb0219 259:6fe7da6b4b32
71 defaultConfig = { 71 defaultConfig = {
72 'leaf': { 72 'leaf': {
73 "pubsub#persist_items": True, 73 "pubsub#persist_items": True,
74 "pubsub#deliver_payloads": True, 74 "pubsub#deliver_payloads": True,
75 "pubsub#send_last_published_item": 'on_sub', 75 "pubsub#send_last_published_item": 'on_sub',
76 "pubsub#access_model": 'open', 76 const.OPT_ACCESS_MODEL: const.VAL_AMODEL_DEFAULT,
77 }, 77 },
78 'collection': { 78 'collection': {
79 "pubsub#deliver_payloads": True, 79 "pubsub#deliver_payloads": True,
80 "pubsub#send_last_published_item": 'on_sub', 80 "pubsub#send_last_published_item": 'on_sub',
81 "pubsub#access_model": 'open', 81 const.OPT_ACCESS_MODEL: const.VAL_AMODEL_DEFAULT,
82 } 82 }
83 } 83 }
84 84
85 def __init__(self, dbpool): 85 def __init__(self, dbpool):
86 self.dbpool = dbpool 86 self.dbpool = dbpool
93 configuration = {} 93 configuration = {}
94 cursor.execute("""SELECT node_type, 94 cursor.execute("""SELECT node_type,
95 persist_items, 95 persist_items,
96 deliver_payloads, 96 deliver_payloads,
97 send_last_published_item, 97 send_last_published_item,
98 access_model 98 access_model,
99 FROM nodes 99 FROM nodes
100 WHERE node=%s""", 100 WHERE node=%s""",
101 (nodeIdentifier,)) 101 (nodeIdentifier,))
102 row = cursor.fetchone() 102 row = cursor.fetchone()
103 103
106 106
107 if row[0] == 'leaf': 107 if row[0] == 'leaf':
108 configuration = { 108 configuration = {
109 'pubsub#persist_items': row[1], 109 'pubsub#persist_items': row[1],
110 'pubsub#deliver_payloads': row[2], 110 'pubsub#deliver_payloads': row[2],
111 'pubsub#send_last_published_item': 111 'pubsub#send_last_published_item': row[3],
112 row[3], 112 const.OPT_ACCESS_MODEL:row[4],
113 'pubsub#access_model':row[4]} 113 }
114 node = LeafNode(nodeIdentifier, configuration) 114 node = LeafNode(nodeIdentifier, configuration)
115 node.dbpool = self.dbpool 115 node.dbpool = self.dbpool
116 return node 116 return node
117 elif row[0] == 'collection': 117 elif row[0] == 'collection':
118 configuration = { 118 configuration = {
119 'pubsub#deliver_payloads': row[2], 119 'pubsub#deliver_payloads': row[2],
120 'pubsub#send_last_published_item': 120 'pubsub#send_last_published_item': row[3],
121 row[3], 121 const.OPT_ACCESS_MODEL: row[4],
122 'pubsub#access_model':row[4]} 122 }
123 node = CollectionNode(nodeIdentifier, configuration) 123 node = CollectionNode(nodeIdentifier, configuration)
124 node.dbpool = self.dbpool 124 node.dbpool = self.dbpool
125 return node 125 return node
126 126
127 127
150 (%s, 'leaf', %s, %s, %s, %s)""", 150 (%s, 'leaf', %s, %s, %s, %s)""",
151 (nodeIdentifier, 151 (nodeIdentifier,
152 config['pubsub#persist_items'], 152 config['pubsub#persist_items'],
153 config['pubsub#deliver_payloads'], 153 config['pubsub#deliver_payloads'],
154 config['pubsub#send_last_published_item'], 154 config['pubsub#send_last_published_item'],
155 config['pubsub#access_model']) 155 config[const.OPT_ACCESS_MODEL],
156 )
156 ) 157 )
157 except cursor._pool.dbapi.IntegrityError: 158 except cursor._pool.dbapi.IntegrityError:
158 raise error.NodeExists() 159 raise error.NodeExists()
159 160
161 cursor.execute("""SELECT node_id FROM nodes WHERE node=%s""", (nodeIdentifier,));
162 node_id = cursor.fetchone()[0]
163
160 cursor.execute("""SELECT 1 from entities where jid=%s""", 164 cursor.execute("""SELECT 1 from entities where jid=%s""",
161 (owner,)) 165 (owner,))
162 166
163 if not cursor.fetchone(): 167 if not cursor.fetchone():
164 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", 168 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
165 (owner,)) 169 (owner,))
166 170
167 cursor.execute("""INSERT INTO affiliations 171 cursor.execute("""INSERT INTO affiliations
168 (node_id, entity_id, affiliation) 172 (node_id, entity_id, affiliation)
169 SELECT node_id, entity_id, 'owner' FROM 173 SELECT %s, entity_id, 'owner' FROM
170 (SELECT node_id FROM nodes WHERE node=%s) as n
171 CROSS JOIN
172 (SELECT entity_id FROM entities 174 (SELECT entity_id FROM entities
173 WHERE jid=%s) as e""", 175 WHERE jid=%s) as e""",
174 (nodeIdentifier, owner)) 176 (node_id, owner))
177
178 #TODO: manage JID access
179 if config[const.OPT_ACCESS_MODEL] == const.VAL_AMODEL_ROSTER:
180 if const.OPT_ROSTER_GROUPS_ALLOWED in config:
181 allowed_groups = config[const.OPT_ROSTER_GROUPS_ALLOWED]
182 else:
183 allowed_groups = []
184 for group in allowed_groups:
185 #TODO: check that group are actually in roster
186 cursor.execute("""INSERT INTO node_groups_authorized (node_id, groupname)
187 VALUES (%s,%s)""" , (node_id, group))
175 188
176 189
177 def deleteNode(self, nodeIdentifier): 190 def deleteNode(self, nodeIdentifier):
178 return self.dbpool.runInteraction(self._deleteNode, nodeIdentifier) 191 return self.dbpool.runInteraction(self._deleteNode, nodeIdentifier)
179 192
532 publisher.full(), 545 publisher.full(),
533 data, 546 data,
534 access_model, 547 access_model,
535 self.nodeIdentifier)) 548 self.nodeIdentifier))
536 549
537 if access_model == const.VAL_ROSTER: 550 if access_model == const.VAL_AMODEL_ROSTER:
538 item_id = cursor.fetchone()[0]; 551 item_id = cursor.fetchone()[0];
539 if const.OPT_ROSTER_GROUPS_ALLOWED in item_config: 552 if const.OPT_ROSTER_GROUPS_ALLOWED in item_config:
540 item_config.fields[const.OPT_ROSTER_GROUPS_ALLOWED].fieldType='list-multi' #XXX: needed to force list if there is only one value 553 item_config.fields[const.OPT_ROSTER_GROUPS_ALLOWED].fieldType='list-multi' #XXX: needed to force list if there is only one value
541 allowed_groups = item_config[const.OPT_ROSTER_GROUPS_ALLOWED] 554 allowed_groups = item_config[const.OPT_ROSTER_GROUPS_ALLOWED]
542 else: 555 else: