2010-12-06 75 views
0

我試圖使用聲明授權來控制對我的網站的訪問。但是當我使用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: "[email protected]", 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). 

回答

1

這裏有固定的一個問題:

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

試試這個,看看它是否工作。否則,請發佈任何錯誤消息。

+0

謝謝我相信它解決了這個問題。我沒有從rails那裏得到任何錯誤,但是Firefox說「Firefox已經檢測到服務器正以一種永遠不會完成的方式重定向這個地址的請求。」 – 2010-12-06 01:04:54