From e4a56371c045b26842a6536b6d1c43d70d9a3d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20S=C3=A1nchez=20Lara?= Date: Mon, 17 Aug 2015 15:05:09 +0200 Subject: [PATCH] _CDB_Group_AddMember_API --- scripts-available/CDB_Groups.sql | 1 + scripts-available/CDB_Groups_API.sql | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/scripts-available/CDB_Groups.sql b/scripts-available/CDB_Groups.sql index aabe6a4..a4244a7 100644 --- a/scripts-available/CDB_Groups.sql +++ b/scripts-available/CDB_Groups.sql @@ -59,6 +59,7 @@ BEGIN RAISE EXCEPTION 'Group role (%) and user role (%) must be already existing', cdb_group_role, cdb_user_role; END IF; EXECUTE format('GRANT "%s" TO "%s"', cdb_group_role, cdb_user_role); + PERFORM cartodb._CDB_Group_AddMember_API(current_database(), group_name, username); END $$ LANGUAGE PLPGSQL VOLATILE; diff --git a/scripts-available/CDB_Groups_API.sql b/scripts-available/CDB_Groups_API.sql index be3583d..bca575f 100644 --- a/scripts-available/CDB_Groups_API.sql +++ b/scripts-available/CDB_Groups_API.sql @@ -69,6 +69,29 @@ $$ raise err $$ LANGUAGE 'plpythonu' VOLATILE; +CREATE OR REPLACE +FUNCTION cartodb._CDB_Group_AddMember_API(database_name text, group_name text, username text) + RETURNS VOID AS +$$ + import httplib + import string + + try: + params = plpy.execute("select c.host, c.port, c.timeout, c.auth from cartodb._CDB_Group_API_Conf() c;")[0] + if params['host'] is None: + return + + client = httplib.HTTPConnection(params['host'], params['port'], False, params['timeout']) + body = '{ "username": "%s" }' % username + headers = { 'Authorization': params['auth'], 'Content-Type': 'application/json' } + client.request('POST', '/api/v1/databases/%s/groups/%s/users' % (database_name, group_name), body, headers) + response = client.getresponse() + assert response.status == 200 + except Exception as err: + plpy.warning('group creation error: ' + str(err)) + raise err +$$ LANGUAGE 'plpythonu' VOLATILE; + DO LANGUAGE 'plpgsql' $$ BEGIN DROP FUNCTION IF EXISTS cartodb._CDB_Group_API_Conf();