Allow user_role to execute some fuctions without defined vessel

This commit is contained in:
xbgmsharp
2022-09-21 09:57:53 +02:00
parent 0f399293eb
commit 8af527f574

View File

@@ -377,9 +377,9 @@ CREATE FUNCTION logbook_update_geojson_fn(IN _id integer, IN _start text, IN _en
) )
) AS t; ) AS t;
-- -- Merge jsonb
select log_geojson::jsonb || metrics_geojson::jsonb into _map; select log_geojson::jsonb || metrics_geojson::jsonb into _map;
-- output
SELECT SELECT
json_build_object( json_build_object(
'type', 'FeatureCollection', 'type', 'FeatureCollection',
@@ -473,7 +473,7 @@ CREATE OR REPLACE FUNCTION process_stay_queue_fn(IN _id integer) RETURNS void AS
stay_rec record; stay_rec record;
_name varchar; _name varchar;
BEGIN BEGIN
RAISE WARNING 'process_stay_queue_fn'; RAISE NOTICE 'process_stay_queue_fn';
-- If _id is not NULL -- If _id is not NULL
IF _id IS NULL OR _id < 1 THEN IF _id IS NULL OR _id < 1 THEN
RAISE WARNING '-> process_stay_queue_fn invalid input %', _id; RAISE WARNING '-> process_stay_queue_fn invalid input %', _id;
@@ -510,6 +510,7 @@ CREATE OR REPLACE FUNCTION process_moorage_queue_fn(IN _id integer) RETURNS void
stay_rec record; stay_rec record;
moorage_rec record; moorage_rec record;
BEGIN BEGIN
RAISE NOTICE 'process_moorage_queue_fn';
-- If _id is not NULL -- If _id is not NULL
IF _id IS NULL OR _id < 1 THEN IF _id IS NULL OR _id < 1 THEN
RAISE WARNING '-> process_moorage_queue_fn invalid input %', _id; RAISE WARNING '-> process_moorage_queue_fn invalid input %', _id;
@@ -677,7 +678,7 @@ AS $get_user_settings_from_log$
'logbook_link', l.id) INTO user_settings 'logbook_link', l.id) INTO user_settings
FROM auth.accounts a, auth.vessels v, api.metadata m, api.logbook l FROM auth.accounts a, auth.vessels v, api.metadata m, api.logbook l
WHERE lower(a.email) = lower(v.owner_email) WHERE lower(a.email) = lower(v.owner_email)
-- AND lower(v.name) = lower(m.name) AND lower(v.name) = lower(m.name)
AND m.client_id = l.client_id AND m.client_id = l.client_id
AND l.client_id = logbook_rec.client_id AND l.client_id = logbook_rec.client_id
AND l.id = logbook_rec.id; AND l.id = logbook_rec.id;
@@ -764,7 +765,7 @@ AS $get_user_settings_from_clientid$
'logbook_name', logbook_name ) INTO user_settings 'logbook_name', logbook_name ) INTO user_settings
FROM auth.accounts a, auth.vessels v, api.metadata m FROM auth.accounts a, auth.vessels v, api.metadata m
WHERE lower(a.email) = lower(v.owner_email) WHERE lower(a.email) = lower(v.owner_email)
--AND lower(v.name) = lower(m.name) AND lower(v.name) = lower(m.name)
AND m.mmsi = v.mmsi AND m.mmsi = v.mmsi
AND m.client_id = clientid; AND m.client_id = clientid;
END; END;
@@ -923,6 +924,7 @@ DECLARE
_role name; _role name;
_email name; _email name;
_mmsi name; _mmsi name;
_path name;
account_rec record; account_rec record;
vessel_rec record; vessel_rec record;
BEGIN BEGIN
@@ -939,7 +941,16 @@ BEGIN
WHERE auth.accounts.email = _email; WHERE auth.accounts.email = _email;
IF account_rec.email IS NULL THEN IF account_rec.email IS NULL THEN
RAISE EXCEPTION 'Invalid user' RAISE EXCEPTION 'Invalid user'
USING HINT = 'Unkown user'; USING HINT = 'Unkown user or password';
END IF;
RAISE WARNING 'req path %', current_setting('request.path', true);
-- Function allow without defined vessel
-- openapi doc, user settings and vessel registration
SELECT current_setting('request.path', true) into _path;
IF _path = '/rpc/settings_fn'
OR _path = '/rpc/register_vessel'
OR _path = '/' THEN
RETURN;
END IF; END IF;
-- Check a vessel and user exist -- Check a vessel and user exist
SELECT * INTO vessel_rec SELECT * INTO vessel_rec
@@ -948,7 +959,12 @@ BEGIN
AND auth.accounts.email = _email; AND auth.accounts.email = _email;
-- check if boat exist yet? -- check if boat exist yet?
IF vessel_rec.owner_email IS NULL THEN IF vessel_rec.owner_email IS NULL THEN
RETURN; -- ignore if not exist -- Return http status code 551 with message
RAISE sqlstate 'PT551' using
message = 'Vessel Required',
detail = 'Invalid vessel',
hint = 'Unkown vessel';
--RETURN; -- ignore if not exist
END IF; END IF;
IF vessel_rec.mmsi IS NULL THEN IF vessel_rec.mmsi IS NULL THEN
RAISE EXCEPTION 'Invalid vessel' RAISE EXCEPTION 'Invalid vessel'