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
チャンネル:私はまた、デフォルトの役割は、加入者
未定義のメソッド ` 『admin』の名前」であるとようにする方法を見つけるためにしようとしていましたコントローラ
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).
ありがとう、私はそれがその問題を修正したと信じています。私はレールから何のエラーも出ていませんが、Firefoxは "サーバーがこのアドレスの要求を決して完了しないようにリダイレクトしていることを検出しました"と言っています。 –
さて、私は上記のサーバーコンソールのエラーが見つかりました。しかし、私は私のアクセス許可を変更する必要があります推測します。 –
おそらく、あなたはこの質問に答えているか、あなたの質問に正しい答えを入れることができます。別のノートでは、私もdecl_authの問題を抱えています...あなたのいずれかが見て、あなたが私にそれを手伝ってくれるかどうか見てください:http://stackoverflow.com/questions/4631218/how-do-i -access-a-user-a-specific-role-in-rails-3 – marcamillion