Merge pull request #137 from CartoDB/136-Add_support_for_adding_a_batch_of_users_to_a_group

136 add support for adding a batch of users to a group
This commit is contained in:
Juan Ignacio Sánchez Lara 2015-09-07 11:48:53 +02:00
commit 7b04267366
3 changed files with 40 additions and 25 deletions

View File

@ -50,37 +50,45 @@ BEGIN
END END
$$ LANGUAGE PLPGSQL VOLATILE; $$ LANGUAGE PLPGSQL VOLATILE;
-- Adds a user to a group -- Adds users to a group
CREATE OR REPLACE CREATE OR REPLACE
FUNCTION cartodb.CDB_Group_AddMember(group_name text, username text) FUNCTION cartodb.CDB_Group_AddUsers(group_name text, usernames text[])
RETURNS VOID AS $$ RETURNS VOID AS $$
DECLARE DECLARE
group_role TEXT; group_role TEXT;
user_role TEXT; user_role TEXT;
username TEXT;
BEGIN BEGIN
group_role := cartodb._CDB_Group_GroupRole(group_name); group_role := cartodb._CDB_Group_GroupRole(group_name);
user_role := cartodb._CDB_User_RoleFromUsername(username); foreach username in array usernames
IF(group_role IS NULL OR user_role IS NULL) loop
THEN user_role := cartodb._CDB_User_RoleFromUsername(username);
RAISE EXCEPTION 'Group role (%) and user role (%) must be already existing', group_role, user_role; IF(group_role IS NULL OR user_role IS NULL)
END IF; THEN
EXECUTE format('GRANT %I TO %I', group_role, user_role); RAISE EXCEPTION 'Group role (%) and user role (%) must be already existing', group_role, user_role;
PERFORM cartodb._CDB_Group_AddMember_API(group_name, username); END IF;
EXECUTE format('GRANT %I TO %I', group_role, user_role);
end loop;
PERFORM cartodb._CDB_Group_AddUsers_API(group_name, usernames);
END END
$$ LANGUAGE PLPGSQL VOLATILE; $$ LANGUAGE PLPGSQL VOLATILE;
-- Removes a user from a group -- Removes users from a group
CREATE OR REPLACE CREATE OR REPLACE
FUNCTION cartodb.CDB_Group_RemoveMember(group_name text, username text) FUNCTION cartodb.CDB_Group_RemoveUsers(group_name text, usernames text[])
RETURNS VOID AS $$ RETURNS VOID AS $$
DECLARE DECLARE
group_role TEXT; group_role TEXT;
user_role TEXT; user_role TEXT;
username TEXT;
BEGIN BEGIN
group_role := cartodb._CDB_Group_GroupRole(group_name); group_role := cartodb._CDB_Group_GroupRole(group_name);
user_role := cartodb._CDB_User_RoleFromUsername(username); foreach username in array usernames
EXECUTE format('REVOKE %I FROM %I', group_role, user_role); loop
PERFORM cartodb._CDB_Group_RemoveMember_API(group_name, username); user_role := cartodb._CDB_User_RoleFromUsername(username);
EXECUTE format('REVOKE %I FROM %I', group_role, user_role);
end loop;
PERFORM cartodb._CDB_Group_RemoveUsers_API(group_name, usernames);
END END
$$ LANGUAGE PLPGSQL VOLATILE; $$ LANGUAGE PLPGSQL VOLATILE;

View File

@ -5,6 +5,12 @@
-- Requires configuration parameter. Example: SELECT cartodb.CDB_Conf_SetConf('groups_api', '{ "host": "127.0.0.1", "port": 3000, "timeout": 10, "username": "extension", "password": "elephant" }'); -- Requires configuration parameter. Example: SELECT cartodb.CDB_Conf_SetConf('groups_api', '{ "host": "127.0.0.1", "port": 3000, "timeout": 10, "username": "extension", "password": "elephant" }');
---------------------------------- ----------------------------------
-- TODO: delete this development cleanup before final merge
DROP FUNCTION IF EXISTS cartodb.CDB_Group_AddMember(group_name text, username text);
DROP FUNCTION IF EXISTS cartodb.CDB_Group_RemoveMember(group_name text, username text);
DROP FUNCTION IF EXISTS cartodb._CDB_Group_AddMember_API(group_name text, username text);
DROP FUNCTION IF EXISTS cartodb._CDB_Group_RemoveMember_API(group_name text, username text);
-- Sends the create group request -- Sends the create group request
CREATE OR REPLACE CREATE OR REPLACE
FUNCTION cartodb._CDB_Group_CreateGroup_API(group_name text, group_role text) FUNCTION cartodb._CDB_Group_CreateGroup_API(group_name text, group_role text)
@ -45,27 +51,28 @@ $$
$$ LANGUAGE 'plpythonu' VOLATILE SECURITY DEFINER; $$ LANGUAGE 'plpythonu' VOLATILE SECURITY DEFINER;
CREATE OR REPLACE CREATE OR REPLACE
FUNCTION cartodb._CDB_Group_AddMember_API(group_name text, username text) FUNCTION cartodb._CDB_Group_AddUsers_API(group_name text, usernames text[])
RETURNS VOID AS RETURNS VOID AS
$$ $$
import string import string
import urllib import urllib
url = '/api/v1/databases/{0}/groups/%s/users' % (urllib.pathname2url(group_name)) url = '/api/v1/databases/{0}/groups/%s/users' % (urllib.pathname2url(group_name))
body = '{ "username": "%s" }' % username body = "{ \"users\": [\"%s\"] }" % "\",\"".join(usernames)
query = "select cartodb._CDB_Group_API_Request('POST', '%s', '%s', '{200, 409}') as response_status" % (url, body) query = "select cartodb._CDB_Group_API_Request('POST', '%s', '%s', '{200, 409}') as response_status" % (url, body)
plpy.execute(query) plpy.execute(query)
$$ LANGUAGE 'plpythonu' VOLATILE SECURITY DEFINER; $$ LANGUAGE 'plpythonu' VOLATILE SECURITY DEFINER;
CREATE OR REPLACE CREATE OR REPLACE
FUNCTION cartodb._CDB_Group_RemoveMember_API(group_name text, username text) FUNCTION cartodb._CDB_Group_RemoveUsers_API(group_name text, usernames text[])
RETURNS VOID AS RETURNS VOID AS
$$ $$
import string import string
import urllib import urllib
url = '/api/v1/databases/{0}/groups/%s/users/%s' % (urllib.pathname2url(group_name), username) url = '/api/v1/databases/{0}/groups/%s/users' % (urllib.pathname2url(group_name))
query = "select cartodb._CDB_Group_API_Request('DELETE', '%s', '', '{200, 404}') as response_status" % url body = "{ \"users\": [\"%s\"] }" % "\",\"".join(usernames)
query = "select cartodb._CDB_Group_API_Request('DELETE', '%s', '%s', '{200, 404}') as response_status" % (url, body)
plpy.execute(query) plpy.execute(query)
$$ LANGUAGE 'plpythonu' VOLATILE SECURITY DEFINER; $$ LANGUAGE 'plpythonu' VOLATILE SECURITY DEFINER;

View File

@ -187,7 +187,7 @@ function setup() {
sql "SELECT cartodb.CDB_Group_CreateGroup('group_a_tmp')" sql "SELECT cartodb.CDB_Group_CreateGroup('group_a_tmp')"
sql "SELECT cartodb.CDB_Group_RenameGroup('group_a_tmp', 'group_a')" sql "SELECT cartodb.CDB_Group_RenameGroup('group_a_tmp', 'group_a')"
sql "SELECT cartodb.CDB_Group_AddMember('group_a', 'cdb_testmember_1')" sql "SELECT cartodb.CDB_Group_AddUsers('group_a', ARRAY['cdb_testmember_1'])"
sql "SELECT cartodb.CDB_Group_CreateGroup('group_b')" sql "SELECT cartodb.CDB_Group_CreateGroup('group_b')"
} }
@ -203,7 +203,7 @@ function tear_down() {
sql "select cartodb.CDB_Group_DropGroup('group_b')" sql "select cartodb.CDB_Group_DropGroup('group_b')"
sql "SELECT cartodb.CDB_Group_RemoveMember('group_a', 'cdb_testmember_1')" sql "SELECT cartodb.CDB_Group_RemoveUsers('group_a', ARRAY['cdb_testmember_1'])"
sql "select cartodb.CDB_Group_DropGroup('group_a')" sql "select cartodb.CDB_Group_DropGroup('group_a')"
sql "SELECT cartodb.CDB_Organization_RemoveAdmin('cdb_org_admin');" sql "SELECT cartodb.CDB_Organization_RemoveAdmin('cdb_org_admin');"
@ -474,8 +474,8 @@ function test_group_management_functions_cant_be_used_by_normal_members() {
sql cdb_testmember_1 "SELECT cartodb.CDB_Group_CreateGroup('group_x_1');" fails sql cdb_testmember_1 "SELECT cartodb.CDB_Group_CreateGroup('group_x_1');" fails
sql cdb_testmember_1 "SELECT cartodb.CDB_Group_RenameGroup('group_a', 'group_x_2');" fails sql cdb_testmember_1 "SELECT cartodb.CDB_Group_RenameGroup('group_a', 'group_x_2');" fails
sql cdb_testmember_1 "SELECT cartodb.CDB_Group_DropGroup('group_a');" fails sql cdb_testmember_1 "SELECT cartodb.CDB_Group_DropGroup('group_a');" fails
sql cdb_testmember_1 "SELECT cartodb.CDB_Group_AddMember('group_a', 'cdb_testmember_2');" fails sql cdb_testmember_1 "SELECT cartodb.CDB_Group_AddUsers('group_a', ARRAY['cdb_testmember_2']);" fails
sql cdb_testmember_1 "SELECT cartodb.CDB_Group_RemoveMember('group_a', 'cdb_testmember_1');" fails sql cdb_testmember_1 "SELECT cartodb.CDB_Group_RemoveUsers('group_a', ARRAY['cdb_testmember_1']);" fails
} }
function test_group_permission_functions_cant_be_used_by_normal_members() { function test_group_permission_functions_cant_be_used_by_normal_members() {
@ -496,8 +496,8 @@ function test_group_permission_functions_cant_be_used_by_normal_members() {
function test_group_management_functions_can_be_used_by_org_admin() { function test_group_management_functions_can_be_used_by_org_admin() {
sql cdb_org_admin "SELECT cartodb.CDB_Group_CreateGroup('group_x_tmp');" sql cdb_org_admin "SELECT cartodb.CDB_Group_CreateGroup('group_x_tmp');"
sql cdb_org_admin "SELECT cartodb.CDB_Group_RenameGroup('group_x_tmp', 'group_x');" sql cdb_org_admin "SELECT cartodb.CDB_Group_RenameGroup('group_x_tmp', 'group_x');"
sql cdb_org_admin "SELECT cartodb.CDB_Group_AddMember('group_x', 'cdb_testmember_1');" sql cdb_org_admin "SELECT cartodb.CDB_Group_AddUsers('group_x', ARRAY['cdb_testmember_1', 'cdb_testmember_2']);"
sql cdb_org_admin "SELECT cartodb.CDB_Group_RemoveMember('group_x', 'cdb_testmember_1');" sql cdb_org_admin "SELECT cartodb.CDB_Group_RemoveUsers('group_x', ARRAY['cdb_testmember_1', 'cdb_testmember_2']);"
# TODO: workaround superadmin limitation # TODO: workaround superadmin limitation
sql "SELECT cartodb.CDB_Group_DropGroup('group_x');" sql "SELECT cartodb.CDB_Group_DropGroup('group_x');"
} }