postgresql7.1 p.template_id = t.template_id and p.portal_id = portal__id_for_user(:package_id,:user_id) select p.portal_id, p.name, p.owner_id, t.filename as template, case when acs_permission__permission_p(p.portal_id, :user_id, 'read') = 't' then 1 else 0 end as portal_read_p, case when acs_permission__permission_p(t.template_id, :user_id, 'read') = 't' then 1 else 0 end as layout_read_p from portals p, portal_templates t where $where