dataservices-api/client/renderer/templates/20_public_functions.erb

37 lines
1.5 KiB
Plaintext
Raw Normal View History

--
2016-04-19 21:16:10 +08:00
-- Public dataservices API function
--
-- These are the only ones with permissions to publicuser role
-- and should also be the only ones with SECURITY DEFINER
CREATE OR REPLACE FUNCTION <%= DATASERVICES_CLIENT_SCHEMA %>.<%= name %> (<%= params_with_type_and_default.join(' ,') %>)
2015-11-19 01:38:06 +08:00
RETURNS <%= return_type %> AS $$
DECLARE
<%= return_declaration if not multi_row %>
<%= user_org_declaration %>
2018-09-05 23:28:15 +08:00
<% unless superuser_function? -%>
appname TEXT;
apikey_permissions JSON;
<% end %>
BEGIN
IF session_user = 'publicuser' OR session_user ~ 'cartodb_publicuser_*' THEN
RAISE EXCEPTION 'The api_key must be provided';
END IF;
2018-09-05 23:02:02 +08:00
<% unless superuser_function? -%>
SELECT u, o, a, p INTO username, orgname, appname, apikey_permissions FROM cdb_dataservices_client._cdb_entity_config() AS (u text, o text, a text, p json);
2018-09-05 23:28:15 +08:00
<% if requires_permission %>
2018-09-05 23:02:02 +08:00
IF NOT apikey_permissions::jsonb ? '<%= permission_name %>' THEN
RAISE EXCEPTION '<%= permission_error %>';
END IF;
<% else %>
SELECT u, o INTO username, orgname FROM <%= DATASERVICES_CLIENT_SCHEMA %>._cdb_entity_config() AS (u text, o text);
<% end %><% end %>
-- 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;
<% return_statement do %><%= DATASERVICES_CLIENT_SCHEMA %>._<%= name %>(<%= params(_with_user_org=true).join(', ') %>)<% end %>
END;
2017-11-07 18:45:07 +08:00
$$ LANGUAGE 'plpgsql' SECURITY DEFINER STABLE PARALLEL UNSAFE;