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