Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing get params in CanCan

Is it possible to access parameters passed in the url in CanCan? I'm trying to authenticate guests based on a token in the url.

Thanks!

like image 944
stringo0 Avatar asked Feb 23 '23 21:02

stringo0


2 Answers

I recently achieved something similar using the method described here:

https://github.com/ryanb/cancan/wiki/Accessing-request-data

In my case, it looked something like this:

app/controllers/application_controller.rb:

class ApplicationController < ActionController::Base

  ...

  def current_ability
    @current_ability ||= Ability.new(current_user, params[:token])
  end

end

app/models/ability.rb:

class Ability
  include CanCan::Ability

  def initialize(user, token=nil)
    ...
    can :read, Article, :tokens => { :token => token }
    ...
  end

end
like image 54
pix Avatar answered Feb 26 '23 10:02

pix


Currently, we are using something similar to authorize! action, resource, session[:access_token] in a before filter - from this page: http://spreecommerce.com/documentation/security.html

like image 27
stringo0 Avatar answered Feb 26 '23 11:02

stringo0