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