【问题标题】:using declarative auth使用声明式身份验证
【发布时间】:2010-12-06 00:43:10
【问题描述】:

我正在尝试使用声明式身份验证来控制对我网站的访问。但是当我使用 filter_resource_access 我得到这个错误。我还试图找出如何使默认角色成为订阅者

未定义的方法“名称” “管理员”:字符串

用户模型

class User < ActiveRecord::Base

  acts_as_authentic

  ROLES = %w[admin  moderator subscriber]

  #Each user can subscribe to many channels
  has_and_belongs_to_many :channels

  #Each user who is a moderator can moderate many channels
  #has_many :channel_mods
  #has_many :channels, :through => :channel_mods

  #Each user can receive many messages
  has_and_belongs_to_many :messages

  #Filter users by role(s)
  named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }

  def roles  
    ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }  
  end

  def roles=(roles)  
    self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum  
  end

  def role_symbols
    roles.map do |role|
      role.name.underscore.to_sym
    end
  end

end

通道控制器

class ChannelsController < ApplicationController

  filter_resource_access
  helper_method :require_user

  def index
    if current_user
    @channels = Channel.find(:all)
    else
      flash[:notice] = "You must first login or register before accessing or site"
      redirect_to :login
    end

  end

  def show
    if current_user
    #@channel = Channel.find(params[:id])
    @message = Message.new(:channel => @channel)
      else
      flash[:notice] = "You must first login or register before accessing or site"
      redirect_to :login
    end
  end

  def new
    if current_user
    #@channel = Channel.new
      else
      flash[:notice] = "You must first login or register before accessing or site"
      redirect_to :login
    end
  end

  def create
    #@channel = Channel.new(params[:channel])
    if @channel.save
      flash[:notice] = "Successfully created channel."
      redirect_to @channel
    else
      render :action => 'new'
    end
  end

  def edit
    if current_user
    #@channel = Channel.find(params[:id])
      else
      flash[:notice] = "You must first login or register before accessing or site"
      redirect_to :login
    end
  end

  def update
    #@channel = Channel.find(params[:id])
    if @channel.update_attributes(params[:channel])
      flash[:notice] = "Successfully updated channel."
      redirect_to @channel
    else
      render :action => 'edit'
    end
  end

  def destroy
    #@channel = Channel.find(params[:id])
    @channel.destroy
    flash[:notice] = "Successfully destroyed channel."
    redirect_to channels_url
  end

end

authorization_rules.rb

authorization do

  role :admin do
    has_permission_on [:all], :to => [:index, :show, :new, :create, :edit, :update, :destroy]
  end

  role :subscriber do
    includes :guest
    has_permission_on :channels_users, :to => [:new, :create, :edit, :update, :destroy] do
      if_attribute :user_id => is{user_id}
  end


  end

  role :guest do
    has_permission_on :channels, :to => [:index, :show]
    has_permission_on :messages, :to => [:index, :show]
    has_permission_on :users, :to => [:index, :show]
  end

  role :moderator do
    includes :guest
    has_permission_on [:channels] , :to=>  [:edit, :update] do
      if_attribute :moderator => is{user}
    end
    has_permission_on [:messages], :to=> [:edit, :update] do
      if_attribute :moderator => is{user}
    end
    has_permission_on [:messages], :to =>[:create, :new]
  end


end

webrick 错误

Permission denied: No matching rules found for index for #<User id: 1, login: "antarrbyrd", crypted_password: "2116af494
6914553db0589fe78e957122c9d5c017d5f99b4f0b...", password_salt: "9M9OIdBcQs11sF0ycn1b", persistence_token: "923c03ca2989b
0d7e862c6e6beb02ab09ec97b1675c27900142...", first_name: "Antarr", last_name: "Byrd", login_count: 13, last_request_at: "
2010-12-06 01:06:14", telephone: "8324051056", email: "antarr.byrd@gmail.com", last_login_at: "2010-12-05 09:10:26", cur
rent_login_at: "2010-12-06 01:02:22", last_login_ip: "127.0.0.1", current_login_ip: "127.0.0.1", carrier_name: nil, mode
rator: nil, created_at: "2010-12-04 05:47:16", updated_at: "2010-12-06 01:06:14", roles_mask: 1, perishable_token: "3ssc
XJhlfYE8tIKSRa0U"> (roles [:admin], privileges [:index], context :channels).

【问题讨论】:

    标签: ruby-on-rails authlogic declarative-authorization


    【解决方案1】:

    这里解决了一个问题:

    def role_symbols
      roles.map do |role| 
        role.underscore.to_sym  # NOT role.name.underscore.to_sym (role is a string)
      end
    end
    

    试试这个,看看它是否有效。否则,请发布任何错误消息。

    【讨论】:

    • 谢谢,我相信它解决了这个问题。我没有从 rails 收到任何错误,但 Firefox 说“Firefox 检测到服务器正在以永远不会完成的方式重定向该地址的请求。”
    • 好的,我发现服务器控制台中的错误将在上面发布。但我猜我必须修改我的权限。
    • 也许您可以将此问题标记为已回答或将正确答案放入您的问题中,以便其他人知道您做了什么。另一方面,我也遇到了 decl_auth 的问题...你们中的任何人都可以看看是否可以帮助我解决这个问题:stackoverflow.com/questions/4631218/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 2014-02-20
    • 1970-01-01
    相关资源
    最近更新 更多