Hi John Paul
Apologies for the slow response - perhaps you've found a solution to this
already.
I'd recommend creating one attribute that stores either 0 if the lead is not
private, or the user id otherwise. Then create a second attribute that stores
the group id.
has "IF(leads.private = 0 OR ISNULL(leads.private), 0, leads.user_id)", :as
=> :user_id_or_public, :type => :integer
has group_id
Then make use of :sphinx_select (Sphinx's custom SELECT statement):
Lead.search(
:sphinx_select => "*, IF(IN(user_id_or_public, 0, #{user.id}) OR
IN(group_id, #{group_ids.join(', ')}), 1, 0) AS has_permission",
:with => {'has_permission' => 1}
)
Granted, I've not tested this, or used something like this before, but
hopefully it should work without too much fiddling around ;)
Cheers
--
Pat
On 03/12/2010, at 2:31 AM, John Paul wrote:
> Hi Everyone,
>
> I just started using Sphinx and its been great so far. I hit a
> roadblock when trying to re-implement my permissions to ensure users
> aren't returning results that they shouldn't see.
>
> It seems the conditional support of Sphinx is much different than
> standard ActiveRecord, and I am struggling with how to recreate this
> scope in Sphinx. I have listed the full method below, but the most
> important part I want to be able to duplicate in Sphinx is:
> ------------------------------------------------------
> :conditions => ["
> (
> ISNULL(leads.group_id) AND ((leads.user_id = ? AND
> leads.private = 1) OR (leads.private = 0 OR ISNULL(leads.private)))
> )
> OR (leads.group_id IS NOT NULL AND leads.group_id
> IN(#{group_ids}))", user.id])
> ------------------------------------------------------
>
> Full Method:
> def self.with_permission(user)
> groups = user.cached_group_ids.present? ? user.cached_group_ids :
> "''"
> if !user.owner? && !user.admin?
> scoped(:conditions => ["
> (
> ISNULL(leads.group_id) AND ((leads.user_id = ? AND
> leads.private = 1) OR (leads.private = 0 OR ISNULL(leads.private)))
> )
> OR (leads.group_id IS NOT NULL AND leads.group_id
> IN(#{groups}))", user.id])
> else
> scoped(:conditions => [])
> end
> end
>
> --
> You received this message because you are subscribed to the Google Groups
> "Thinking Sphinx" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/thinking-sphinx?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Thinking Sphinx" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/thinking-sphinx?hl=en.