2016-02-06 00:57:56 +08:00
|
|
|
CREATE TYPE cdb_dataservices_client._entity_config AS (
|
2015-12-03 18:03:10 +08:00
|
|
|
username text,
|
|
|
|
organization_name text
|
|
|
|
);
|
|
|
|
|
2015-12-03 01:54:27 +08:00
|
|
|
--
|
2015-12-03 18:03:10 +08:00
|
|
|
-- Get entity config function
|
2015-12-03 01:54:27 +08:00
|
|
|
--
|
2015-12-03 18:03:10 +08:00
|
|
|
-- The purpose of this function is to retrieve the username and organization name from
|
2015-12-03 01:54:27 +08:00
|
|
|
-- a) schema where he/her is the owner in case is an organization user
|
|
|
|
-- b) entity_name from the cdb_conf database in case is a non organization user
|
2016-02-06 00:57:56 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_client._cdb_entity_config()
|
2015-12-03 18:03:10 +08:00
|
|
|
RETURNS record AS $$
|
2015-12-03 01:54:27 +08:00
|
|
|
DECLARE
|
2016-02-06 00:57:56 +08:00
|
|
|
result cdb_dataservices_client._entity_config;
|
2015-12-03 18:03:10 +08:00
|
|
|
is_organization boolean;
|
|
|
|
username text;
|
|
|
|
organization_name text;
|
2015-12-03 01:54:27 +08:00
|
|
|
BEGIN
|
|
|
|
SELECT cartodb.cdb_conf_getconf('user_config')->'is_organization' INTO is_organization;
|
|
|
|
IF is_organization IS NULL THEN
|
|
|
|
RAISE EXCEPTION 'User must have user configuration in the config table';
|
|
|
|
ELSIF is_organization = TRUE THEN
|
|
|
|
SELECT nspname
|
|
|
|
FROM pg_namespace s
|
|
|
|
LEFT JOIN pg_roles r ON s.nspowner = r.oid
|
|
|
|
WHERE r.rolname = session_user INTO username;
|
2015-12-03 18:03:10 +08:00
|
|
|
SELECT cartodb.cdb_conf_getconf('user_config')->'entity_name' INTO organization_name;
|
2015-12-03 01:54:27 +08:00
|
|
|
ELSE
|
2015-12-03 18:03:10 +08:00
|
|
|
SELECT cartodb.cdb_conf_getconf('user_config')->'entity_name' INTO username;
|
|
|
|
organization_name = NULL;
|
2015-12-03 01:54:27 +08:00
|
|
|
END IF;
|
2015-12-03 18:03:10 +08:00
|
|
|
result.username = username;
|
|
|
|
result.organization_name = organization_name;
|
|
|
|
RETURN result;
|
2015-12-03 01:54:27 +08:00
|
|
|
END;
|
2016-02-06 00:57:56 +08:00
|
|
|
$$ LANGUAGE 'plpgsql' SECURITY DEFINER;
|