Roles simplification, without md5 and prepending database name

This commit is contained in:
Juan Ignacio Sánchez Lara 2015-08-12 20:01:07 +02:00
parent 2ece2979a6
commit 3ee4978240
3 changed files with 15 additions and 9 deletions

View File

@ -2,14 +2,8 @@
CREATE OR REPLACE
FUNCTION cartodb.CDB_Group_CreateGroup(group_name text)
RETURNS VOID AS $$
DECLARE
cdb_group_role TEXT;
BEGIN
cdb_group_role := cartodb._CDB_Group_GroupRole(group_name);
IF NOT EXISTS ( SELECT 1 FROM pg_roles WHERE rolname = cdb_group_role )
THEN
EXECUTE 'CREATE ROLE "' || cdb_group_role || '" NOLOGIN;';
END IF;
EXECUTE 'CREATE ROLE "' || cartodb._CDB_Group_GroupRole(group_name) || '" NOLOGIN;';
END
$$ LANGUAGE PLPGSQL;
@ -114,12 +108,18 @@ FUNCTION cartodb._CDB_Group_GroupRole(group_name text)
RETURNS TEXT AS $$
DECLARE
group_role TEXT;
max_length constant INTEGER := 60;
BEGIN
IF group_name !~ '^[a-zA-Z_][a-zA-Z0-9_]*$'
THEN
RAISE EXCEPTION 'Group name (%) must be a valid identifier. See http://www.postgresql.org/docs/9.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS', group_name;
END IF;
RETURN 'g_' || md5(current_database()) || '_' || group_name;
group_role := current_database() || '_g_' || group_name;
IF LENGTH(group_role) > max_length
THEN
RAISE EXCEPTION 'Group name should be shorter. Resulting role must have less than % characters, but it is longer: %', max_length, group_role;
END IF;
RETURN group_role;
END
$$ LANGUAGE PLPGSQL;

View File

@ -31,9 +31,10 @@ $$ LANGUAGE PLPGSQL VOLATILE;
CREATE OR REPLACE
FUNCTION cartodb._CDB_Organization_Admin_Role_Name()
RETURNS TEXT
AS 'SELECT ''cdb_org_admin''::text || ''_'' || md5(current_database());'
AS 'SELECT current_database() || ''_admin''::text;'
LANGUAGE SQL IMMUTABLE;
-- Administrator role creation on extension install
DO LANGUAGE 'plpgsql' $$
DECLARE
cdb_org_admin_role_name TEXT;

View File

@ -530,6 +530,11 @@ function test_not_valid_group_names() {
sql postgres "select cartodb._CDB_Group_GroupRole(' group_1$_a ');" fails
sql postgres "select cartodb._CDB_Group_GroupRole('group _1$_a');" fails
sql postgres "select cartodb._CDB_Group_GroupRole('groupña');" fails
sql postgres "select cartodb._CDB_Group_GroupRole('a123456789012345678901234567890123456789012345678901234567890');" fails
}
function test_administrator_name_generation() {
sql postgres "select cartodb._CDB_Organization_Admin_Role_Name();"
}
#################################################### TESTS END HERE ####################################################