]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/XXXX.schema.remoteauth.sql
0b6df554fa53b89bd121d76a59b55cc038d17fa9
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / XXXX.schema.remoteauth.sql
1 BEGIN;
2
3 INSERT INTO permission.perm_list ( id, code, description ) VALUES
4  ( 615, 'ADMIN_REMOTEAUTH', oils_i18n_gettext( 615,
5     'Administer remote patron authentication', 'ppl', 'description' ));
6
7 CREATE TABLE config.remoteauth_profile (
8     name TEXT PRIMARY KEY,
9     description TEXT,
10     context_org INT NOT NULL REFERENCES actor.org_unit(id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
11     enabled BOOLEAN NOT NULL DEFAULT FALSE,
12     perm INT NOT NULL REFERENCES permission.perm_list(id) ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED,
13     restrict_to_org BOOLEAN NOT NULL DEFAULT TRUE,
14     allow_inactive BOOL NOT NULL DEFAULT FALSE,
15     allow_expired BOOL NOT NULL DEFAULT FALSE,
16     block_list TEXT,
17     usr_activity_type INT REFERENCES config.usr_activity_type(id) ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED
18 );
19
20 CREATE OR REPLACE FUNCTION actor.permit_remoteauth (profile_name TEXT, userid BIGINT) RETURNS TEXT AS $func$
21 DECLARE
22     usr               actor.usr%ROWTYPE;
23     profile           config.remoteauth_profile%ROWTYPE;
24     perm              TEXT;
25     context_org_list  INT[];
26     home_prox         INT;
27     block             TEXT;
28     penalty_count     INT;
29 BEGIN
30
31     SELECT INTO usr * FROM actor.usr WHERE id = userid AND NOT deleted;
32     IF usr IS NULL THEN
33         RETURN 'not_found';
34     END IF;
35
36     IF usr.barred IS TRUE THEN
37         RETURN 'blocked';
38     END IF;
39
40     SELECT INTO profile * FROM config.remoteauth_profile WHERE name = profile_name;
41     SELECT INTO context_org_list ARRAY_AGG(id) FROM actor.org_unit_full_path( profile.context_org );
42
43     -- user's home library must be within the context org
44     IF profile.restrict_to_org IS TRUE AND usr.home_ou NOT IN (SELECT * FROM UNNEST(context_org_list)) THEN
45         RETURN 'not_found';
46     END IF;
47
48     SELECT INTO perm code FROM permission.perm_list WHERE id = profile.perm;
49     IF permission.usr_has_perm(usr.id, perm, profile.context_org) IS FALSE THEN
50         RETURN 'not_found';
51     END IF;
52     
53     IF usr.expire_date < NOW() AND profile.allow_expired IS FALSE THEN
54         RETURN 'expired';
55     END IF;
56
57     IF usr.active IS FALSE AND profile.allow_inactive IS FALSE THEN
58         RETURN 'blocked';
59     END IF;
60
61     -- Proximity of user's home_ou to context_org to see if penalties should be ignored.
62     SELECT INTO home_prox prox FROM actor.org_unit_proximity WHERE from_org = usr.home_ou AND to_org = profile.context_org;
63
64     -- Loop through the block list to see if the user has any matching penalties.
65     IF profile.block_list IS NOT NULL THEN
66         FOR block IN SELECT UNNEST(STRING_TO_ARRAY(profile.block_list, '|')) LOOP
67             SELECT INTO penalty_count COUNT(DISTINCT csp.*)
68                 FROM  actor.usr_standing_penalty usp
69                         JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
70                 WHERE usp.usr = usr.id
71                         AND usp.org_unit IN ( SELECT * FROM UNNEST(context_org_list) )
72                         AND ( usp.stop_date IS NULL or usp.stop_date > NOW() )
73                         AND ( csp.ignore_proximity IS NULL OR csp.ignore_proximity < home_prox )
74                         AND csp.block_list ~ block;
75             IF penalty_count > 0 THEN
76                 -- User has penalties that match this block, so auth is not permitted.
77                 -- Don't bother testing the rest of the block list.
78                 RETURN 'blocked';
79             END IF;
80         END LOOP;
81     END IF;
82
83     -- User has passed all tests.
84     RETURN 'success';
85
86 END;
87 $func$ LANGUAGE plpgsql;
88
89 COMMIT;
90