Skip to content

Instantly share code, notes, and snippets.

@EvanCarroll
Created November 12, 2009 22:59
Show Gist options
  • Save EvanCarroll/233398 to your computer and use it in GitHub Desktop.
Save EvanCarroll/233398 to your computer and use it in GitHub Desktop.
SELECT * FROM "contacts"
WHERE (
id IN (
SELECT contact_id FROM customer_contacts
WHERE customer_id IN (
SELECT id FROM customers c WHERE is_potential = false
)
)
AND f_contact_acl(184, id)
) ORDER BY has_quit, lower(firstname)
ASC LIMIT 5 OFFSET 0;
-- Where the function is defined as:
CREATE OR REPLACE FUNCTION f_contact_acl(uid integer, cid integer)
RETURNS BOOL AS $$
declare
user record;
contact record;
cust_rel record;
begin
select into user * from users where id=uid;
select into contact * from contacts where id=cid;
if (user.org_id != contact.org_id) then
return false;
end if;
SELECT INTO cust_rel COUNT(*) AS acl_count
FROM customer_contacts cc
WHERE f_customer_acl(uid, cc.customer_id)
AND cc.contact_id = cid;
return cust_rel.acl_count > 0;
end;
$$ LANGUAGE 'plpgsql';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment