使用声明式身份验证

发布于 2024-10-06 11:54:19 字数 4594 浏览 2 评论 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).

I'm trying to use declarative auth to control access to my site. But when I use filter_resource_access I get this error . I was also trying to find out how to make the default role to be subscriber

undefined method `name' for
"admin":String

user model

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

channel controller

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 error

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亢潮 2024-10-13 11:54:19

这里解决了一个问题:

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

尝试一下,看看它是否有效。否则,请发布任何错误消息。

Here's one problem fixed:

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

Try this and see if it works. Otherwise, please post any error messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文