]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/XXXX.schema.z3950_credentials.sql
Z39.50 stored credentials
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / XXXX.schema.z3950_credentials.sql
1
2 BEGIN;
3
4 CREATE TABLE config.z3950_source_credentials (
5     id SERIAL PRIMARY KEY,
6     owner INTEGER NOT NULL REFERENCES actor.org_unit(id),
7     source TEXT NOT NULL REFERENCES config.z3950_source(name),
8     -- do some Z servers require a username but no password or vice versa?
9     username TEXT,
10     password TEXT,
11     CONSTRAINT czsc_source_once_per_lib UNIQUE (source, owner)
12 );
13
14 -- find the most relevant set of credentials for the Z source and org
15 CREATE OR REPLACE FUNCTION config.z3950_source_credentials_lookup
16         (source TEXT, owner INTEGER) 
17         RETURNS config.z3950_source_credentials AS $$
18
19     SELECT creds.* 
20     FROM config.z3950_source_credentials creds
21         JOIN actor.org_unit aou ON (aou.id = creds.owner)
22         JOIN actor.org_unit_type aout ON (aout.id = aou.ou_type)
23     WHERE creds.source = $1 AND creds.owner IN ( 
24         SELECT id FROM actor.org_unit_ancestors($2) 
25     )
26     ORDER BY aout.depth DESC LIMIT 1;
27
28 $$ LANGUAGE SQL STABLE;
29
30 -- since we are not exposing config.z3950_source_credentials
31 -- via the IDL, providing a stored proc gives us a way to
32 -- set values in the table via cstore
33 CREATE OR REPLACE FUNCTION config.z3950_source_credentials_apply
34         (src TEXT, org INTEGER, uname TEXT, passwd TEXT) 
35         RETURNS VOID AS $$
36 BEGIN
37     PERFORM 1 FROM config.z3950_source_credentials
38         WHERE owner = org AND source = src;
39
40     IF FOUND THEN
41         IF COALESCE(uname, '') = '' AND COALESCE(passwd, '') = '' THEN
42             DELETE FROM config.z3950_source_credentials 
43                 WHERE owner = org AND source = src;
44         ELSE 
45             UPDATE config.z3950_source_credentials 
46                 SET username = uname, password = passwd
47                 WHERE owner = org AND source = src;
48         END IF;
49     ELSE
50         IF COALESCE(uname, '') <> '' OR COALESCE(passwd, '') <> '' THEN
51             INSERT INTO config.z3950_source_credentials
52                 (source, owner, username, password) 
53                 VALUES (src, org, uname, passwd);
54         END IF;
55     END IF;
56 END;
57 $$ LANGUAGE PLPGSQL;
58
59
60 COMMIT;