2015-11-17 23:28:33 +08:00
|
|
|
--
|
2016-04-19 21:16:10 +08:00
|
|
|
-- Public dataservices API function
|
2015-11-17 23:28:33 +08:00
|
|
|
--
|
|
|
|
-- These are the only ones with permissions to publicuser role
|
|
|
|
-- and should also be the only ones with SECURITY DEFINER
|
|
|
|
|
2016-02-06 00:57:56 +08:00
|
|
|
CREATE OR REPLACE FUNCTION <%= DATASERVICES_CLIENT_SCHEMA %>.<%= name %> (<%= params_with_type_and_default %>)
|
2015-11-19 01:38:06 +08:00
|
|
|
RETURNS <%= return_type %> AS $$
|
2015-11-17 23:28:33 +08:00
|
|
|
DECLARE
|
2016-02-11 18:10:12 +08:00
|
|
|
<% if not multi_row %>ret <%= return_type %>;<% end %>
|
2015-12-03 01:54:49 +08:00
|
|
|
username text;
|
2015-12-03 18:03:10 +08:00
|
|
|
orgname text;
|
2015-11-17 23:28:33 +08:00
|
|
|
BEGIN
|
2015-11-27 20:20:55 +08:00
|
|
|
IF session_user = 'publicuser' OR session_user ~ 'cartodb_publicuser_*' THEN
|
2015-11-24 19:25:43 +08:00
|
|
|
RAISE EXCEPTION 'The api_key must be provided';
|
|
|
|
END IF;
|
2016-02-06 00:57:56 +08:00
|
|
|
SELECT u, o INTO username, orgname FROM <%= DATASERVICES_CLIENT_SCHEMA %>._cdb_entity_config() AS (u text, o text);
|
2015-12-03 18:03:10 +08:00
|
|
|
-- JSON value stored "" is taken as literal
|
|
|
|
IF username IS NULL OR username = '' OR username = '""' THEN
|
2015-12-03 02:12:11 +08:00
|
|
|
RAISE EXCEPTION 'Username is a mandatory argument, check it out';
|
|
|
|
END IF;
|
2016-02-11 18:10:12 +08:00
|
|
|
<% if multi_row %>
|
|
|
|
RETURN QUERY
|
|
|
|
SELECT * FROM <%= DATASERVICES_CLIENT_SCHEMA %>._<%= name %>(username, orgname, <%= params %>);
|
2016-02-24 23:58:14 +08:00
|
|
|
<% elsif multi_field %>
|
|
|
|
SELECT * FROM <%= DATASERVICES_CLIENT_SCHEMA %>._<%= name %>(username, orgname, <%= params %>) INTO ret;
|
|
|
|
RETURN ret;
|
2016-02-11 18:10:12 +08:00
|
|
|
<% else %>
|
|
|
|
SELECT <%= DATASERVICES_CLIENT_SCHEMA %>._<%= name %>(username, orgname, <%= params %>) INTO ret;
|
|
|
|
RETURN ret;
|
|
|
|
<% end %>
|
2015-11-17 23:28:33 +08:00
|
|
|
END;
|
|
|
|
$$ LANGUAGE 'plpgsql' SECURITY DEFINER;
|
|
|
|
|