Created
February 6, 2013 10:39
-
-
Save rogercampos/4721805 to your computer and use it in GitHub Desktop.
Example of how to use sorcery to authenticate outside the controller scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Sorcery | |
class FreeAuth | |
include Controller | |
def initialize(request) | |
@request = request | |
end | |
def session | |
@session ||= @request.session | |
end | |
def cookies | |
@cookies ||= ActionDispatch::Cookies::CookieJar.build(@request) | |
end | |
end | |
def self.with_request(request) | |
yield(FreeAuth.new(request)) if block_given? | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CanCanConstraint | |
def initialize(action, context) | |
@action, @context = action, context | |
end | |
def matches?(request) | |
Sorcery.with_request(request) do |c| | |
c.current_user && Ability.new(c.current_user).can?(@action, @context) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment