Невозможно авторизоваться с помощью CanCan и Authlogic - PullRequest
0 голосов
/ 17 мая 2011

Я пытаюсь реализовать роли с Authlogic, чтобы ограничить доступ контроллера в моем приложении rails.Как только я реализую его с помощью load_and_authorize и filter_resource_access, я не могу получить доступ к контроллеру ни под какой ролью.
В моей модели User у меня есть поле ролей, которое has_many role_users указывает на модель Roles.Таким образом, пользователь 1 - «администратор», имеет назначение роли 1, которое связывается с ролью 1, которая называется «администратор».

способность.rb

include CanCan::Ability

def initialize(user)
 user ||= User.new # guest user
 can :read, InstallQuote
 can :create, InstallQuote
 if user.role? :admin
  can :manage, :all
 end

application_controller.rb

helper :all
protect_from_forgery # See ActionController::RequestForgeryProtection for details
helper_method :current_user_session, :current_user  

rescue_from CanCan::AccessDenied do |exception|
 flash[:error] = exception.message
   redirect_back_or_default(root_path)
 end

before_filter { |c| Authorization.current_user = c.current_user }
filter_parameter_logging :password, :password_confirmation 

protected
  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.user
end

clients_controller.rb

class ClientsController < ApplicationController
# before_filter :authenticate, :only => [:edit, :update, :show, :index]

load_and_authorize_resource # For declarative authorization
filter_resource_access

# belongs_to :company
# before_filter :require_user, :only => [:edit, :update, :index, :destroy]
# before_filter :admin_user, :only => :destroy
helper_method :sort_column, :sort_direction
before_filter :correct_user, :only => [:edit, :update, :show, :index]

user.rb

acts_as_authentic
has_many :roles_users
has_many :roles, :through => :roles_users
before_create :setup_role
attr_accessible :email, :login, :first_name, :last_name, :role_id, :password, :password_confirmation, :active

(я закомментировал старый код, но пока не хочу отказываться от него).

Кто-нибудь знает, что мне не хватает?

1 Ответ

0 голосов
/ 22 мая 2011

Убедитесь, что вы вошли в систему и user.role? :admin вернет true.

Если это не причина, вы можете отлаживать прямо в консоли:

user = User.first
ability = Ability.new(user)
ability.can? :read, Client
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...