diff 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
line wrap: on
line diff
--- a/sat_pubsub/pgsql_storage.py	Sun Apr 28 19:29:58 2013 +0200
+++ b/sat_pubsub/pgsql_storage.py	Mon May 06 00:11:44 2013 +0200
@@ -73,12 +73,12 @@
                 "pubsub#persist_items": True,
                 "pubsub#deliver_payloads": True,
                 "pubsub#send_last_published_item": 'on_sub',
-                "pubsub#access_model": 'open',
+                const.OPT_ACCESS_MODEL: const.VAL_AMODEL_DEFAULT,
             },
             'collection': {
                 "pubsub#deliver_payloads": True,
                 "pubsub#send_last_published_item": 'on_sub',
-                "pubsub#access_model": 'open',
+                const.OPT_ACCESS_MODEL: const.VAL_AMODEL_DEFAULT,
             }
     }
 
@@ -95,7 +95,7 @@
                                  persist_items,
                                  deliver_payloads,
                                  send_last_published_item,
-                                 access_model
+                                 access_model,
                           FROM nodes
                           WHERE node=%s""",
                        (nodeIdentifier,))
@@ -108,18 +108,18 @@
             configuration = {
                     'pubsub#persist_items': row[1],
                     'pubsub#deliver_payloads': row[2],
-                    'pubsub#send_last_published_item':
-                        row[3],
-                    'pubsub#access_model':row[4]}
+                    'pubsub#send_last_published_item': row[3],
+                    const.OPT_ACCESS_MODEL:row[4],
+                    }
             node = LeafNode(nodeIdentifier, configuration)
             node.dbpool = self.dbpool
             return node
         elif row[0] == 'collection':
             configuration = {
                     'pubsub#deliver_payloads': row[2],
-                    'pubsub#send_last_published_item':
-                        row[3],
-                    'pubsub#access_model':row[4]}
+                    'pubsub#send_last_published_item': row[3],
+                    const.OPT_ACCESS_MODEL: row[4],
+                    }
             node = CollectionNode(nodeIdentifier, configuration)
             node.dbpool = self.dbpool
             return node
@@ -152,11 +152,15 @@
                             config['pubsub#persist_items'],
                             config['pubsub#deliver_payloads'],
                             config['pubsub#send_last_published_item'],
-                            config['pubsub#access_model'])
+                            config[const.OPT_ACCESS_MODEL],
+                            )
                            )
         except cursor._pool.dbapi.IntegrityError:
             raise error.NodeExists()
 
+        cursor.execute("""SELECT node_id FROM nodes WHERE node=%s""", (nodeIdentifier,));
+        node_id = cursor.fetchone()[0]
+
         cursor.execute("""SELECT 1 from entities where jid=%s""",
                        (owner,))
 
@@ -166,12 +170,21 @@
 
         cursor.execute("""INSERT INTO affiliations
                           (node_id, entity_id, affiliation)
-                          SELECT node_id, entity_id, 'owner' FROM
-                          (SELECT node_id FROM nodes WHERE node=%s) as n
-                          CROSS JOIN
+                          SELECT %s, entity_id, 'owner' FROM
                           (SELECT entity_id FROM entities
                                             WHERE jid=%s) as e""",
-                       (nodeIdentifier, owner))
+                       (node_id, owner))
+
+        #TODO: manage JID access
+        if config[const.OPT_ACCESS_MODEL] == const.VAL_AMODEL_ROSTER:
+            if const.OPT_ROSTER_GROUPS_ALLOWED in config:
+                allowed_groups = config[const.OPT_ROSTER_GROUPS_ALLOWED]
+            else:
+                allowed_groups = []
+            for group in allowed_groups:
+                #TODO: check that group are actually in roster
+                cursor.execute("""INSERT INTO node_groups_authorized (node_id, groupname)
+                                  VALUES (%s,%s)""" , (node_id, group))
 
 
     def deleteNode(self, nodeIdentifier):
@@ -534,7 +547,7 @@
                         access_model,
                         self.nodeIdentifier))
 
-        if access_model == const.VAL_ROSTER:
+        if access_model == const.VAL_AMODEL_ROSTER:
             item_id = cursor.fetchone()[0];
             if const.OPT_ROSTER_GROUPS_ALLOWED in item_config:
                 item_config.fields[const.OPT_ROSTER_GROUPS_ALLOWED].fieldType='list-multi' #XXX: needed to force list if there is only one value