LP#1750894 Unified org/usr/ws settings / WIP
authorBill Erickson <berickxx@gmail.com>
Mon, 19 Mar 2018 16:33:00 +0000 (12:33 -0400)
committerBill Erickson <berickxx@gmail.com>
Tue, 29 May 2018 14:13:36 +0000 (10:13 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.workstation-settings.sql
Open-ILS/src/sql/Pg/upgrade/ZZZZ.schema.workstation-settings.UNDO.sql

index fafe1f5..7b77369 100644 (file)
@@ -98,12 +98,11 @@ DROP TABLE config.usr_setting_type;
 
 CREATE VIEW config.org_unit_setting_type AS
     SELECT name, label, grp, description, 
-        datatype, fm_class, view_perm, update_perm, org_setting
+        datatype, fm_class, view_perm, update_perm
     FROM config.setting_type WHERE org_setting;
 
 CREATE VIEW config.usr_setting_type AS
-    SELECT name, label, grp, description, 
-        datatype, fm_class, org_setting
+    SELECT name, label, grp, description, datatype, fm_class
     FROM config.setting_type WHERE usr_setting;
 
 CREATE OR REPLACE FUNCTION actor.setting_type_is_valid() 
@@ -134,6 +133,90 @@ CREATE CONSTRAINT TRIGGER valid_setting_type
   AFTER INSERT OR UPDATE ON actor.workstation_setting
   FOR EACH ROW EXECUTE PROCEDURE actor.setting_type_is_valid();
 
+
+CREATE OR REPLACE FUNCTION actor.get_setting(setting_name TEXT, 
+    org_id INT, user_id INT, workstation_id INT) RETURNS JSON AS
+$FUNC$
+DECLARE
+    setting_type config.setting_type%ROWTYPE;
+    setting_value JSON;
+BEGIN
+
+    SELECT INTO setting_type * FROM config.setting_type 
+        WHERE name = setting_name;
+
+    IF NOT FOUND THEN
+        RETURN NULL;
+    END IF;
+
+    -- User and workstation settings have the same priority.  We do not
+    -- check view_perm's on user settings since values apply only to the
+    -- calling user.
+    IF user_id IS NOT NULL AND setting_type.usr_setting THEN
+        SELECT INTO setting_value value FROM actor.usr_setting
+            WHERE usr = user_id AND name = setting_name;
+        IF FOUND THEN
+            RETURN setting_value;
+        END IF;
+    END IF;
+
+    -- Org and workstation settings enforce the view_perm.
+
+    -- Get the org_id from the workstation when present Org value is
+    -- used in perm checks and org_unit_setting lookups when needed.
+    IF workstation_id IS NOT NULL THEN
+        SELECT INTO org_id owning_lib 
+            FROM actor.workstation WHERE id = workstation_id;
+    END IF;
+
+    IF org_id IS NULL THEN
+        -- No workstation or org_id has been provided. nothnig we can
+        -- do.  This is not necessarily an error condition, since the
+        -- caller may only care about user settings.
+        RETURN NULL;
+    END IF;
+
+    IF setting_type.view_perm IS NOT NULL THEN
+
+        -- settings with view perms need to know who is requesting them.
+        IF user_id IS NULL THEN
+            RAISE NOTICE 'Perm check required but no user_id provided';
+            RETURN NULL;
+        END IF;
+
+        IF NOT permission.usr_has_perm(
+            user_id, 
+            (SELECT code FROM permission.perm_list WHERE id = setting_type.view_perm),
+            org_id) 
+        THEN
+            RAISE NOTICE 'Perm check failed for user % on %',
+                user_id, setting_type.view_perm;
+            RETURN NULL;
+        END IF;
+    END IF;
+
+    IF workstation_id IS NOT NULL AND setting_type.ws_setting THEN
+        SELECT INTO setting_value value FROM actor.workstation_setting
+            WHERE workstation = workstation_id AND name = setting_name;
+        IF FOUND THEN
+            RETURN setting_value;
+        END IF;
+    END IF;
+
+    IF setting_type.org_setting THEN
+        SELECT INTO setting_value value 
+            FROM actor.org_unit_ancestor_setting(setting_name, org_id);
+
+        IF FOUND THEN
+            RETURN setting_value;
+        END IF;
+    END IF;
+
+    RETURN NULL;
+END;
+$FUNC$ LANGUAGE PLPGSQL;
+
+
 COMMIT;
 
 
index 4b922d2..5f91e04 100644 (file)
@@ -1,5 +1,7 @@
 BEGIN;
 
+DROP FUNCTION actor.get_setting(TEXT, INT, INT, INT);
+
 DROP TRIGGER IF EXISTS valid_setting_type ON actor.workstation_setting;
 DROP TRIGGER IF EXISTS valid_setting_type ON actor.usr_setting;
 DROP TRIGGER IF EXISTS valid_setting_type ON actor.org_unit_setting;