Code Monkey home page Code Monkey logo

can_sphinx's Introduction

CanSphinx

CanSphinx is a Ruby on Rails library which allows you to use CanCan for authorizing resources searched by ThinkingSphinx.

Installation

In Rails 3, add this to your Gemfile and run the bundle command.

gem 'can_sphinx', :git => 'git://github.com/sylogix/can_sphinx.git'

Getting Started

CanSphinx uses sphinx attributes in order to check authorization. So the first thing we need to do is to make sure that all attributes needed are indexed. In this example, we are authorized to see all our coworkers (= people with the same company_id as us) and all our friends (= people with a friendship association with us)

class Person < ActiveRecord::Base

  define_index do
    indexes first_name
    indexes last_name

    has company_id
    has friendships(:person_id), :as => :friends

  end

  belongs_to :company
  has_many :friendships
  has_many :friends, :through => :friendships    

end

class Friendship < ActiveRecord::Base
  belongs_to :person
  belongs_to :friend, :class_name => 'Person', :foreign_key => 'friend_id'
end

Then we need to update our ability.rb so we can add a condition in a langage Sphinx can understand :

can :read, Person, Person.include(:friendships).where(['company_id = ? OR friendships.user_id = ?', user.company_id, user.id]) do |p|
  p.company_id == user.company_id or p.friendships.where(:user_id => user.id).empty?
end

Simply becomes :

can :read, Person, Person.include(:friendships).where(['company_id = ? OR friendships.user_id = ?', user.company_id, user.id]), "company_id = #{user.company_id} OR IN(friends,#{user.id})" do |p|
  p.company_id == user.company_id or p.friendships.where(:user_id => user.id).empty?
end

You can find the complete syntax of Sphinx select here. In a future release, CanSphinx might directly use CanCan conditions when it’s a Hash. We will need to it manually for complex conditions. Now we simply have to pass our abilities to the ThinkingSphinx search. In our People controller we might have something like this :

def index
  @people = Person.search params[:search], :authorize_with => current_ability
end

can_sphinx's People

Contributors

flop avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

saragibby

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.