3 CREATE TABLE config.remoteauth_profile (
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,
13 usr_activity_type INT REFERENCES config.usr_activity_type(id) ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED
16 CREATE OR REPLACE FUNCTION actor.permit_remoteauth (profile_name TEXT, userid BIGINT) RETURNS TEXT AS $func$
18 usr actor.usr%ROWTYPE;
19 profile config.remoteauth_profile%ROWTYPE;
21 context_org_list INT[];
27 SELECT INTO usr * FROM actor.usr WHERE id = userid AND NOT deleted;
32 IF usr.barred IS TRUE THEN
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 );
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
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
49 IF usr.expire_date < NOW() AND profile.allow_expired IS FALSE THEN
53 IF usr.active IS FALSE AND profile.allow_inactive IS FALSE THEN
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;
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.
79 -- User has passed all tests.
83 $func$ LANGUAGE plpgsql;