1

我安装了带有声明性授权和继承资源宝石的 rails3 应用程序。让我向您展示我的应用程序中的一些代码:

class Blog < ActiveRecord::Base
  has_many :posts
  has_many :memberships, :class_name => "BlogMembership"

  has_many :subscribers, :through => :memberships, :source => :user, :conditions => "blog_memberships.membership_type = #{BlogMembership::SUBSCRIBER} or blog_memberships.membership_type = #{BlogMembership::AUTHOR} or blog_memberships.membership_type = #{BlogMembership::MODERATOR}"
  has_many :authors, :through => :memberships, :source => :user, :conditions => "blog_memberships.membership_type = #{BlogMembership::AUTHOR} or blog_memberships.membership_type = #{BlogMembership::MODERATOR}"
  has_many :moderators, :through => :memberships, :source => :user, :conditions => "blog_memberships.membership_type = #{BlogMembership::MODERATOR}"
end


class Post < ActiveRecord::Base
  belongs_to :blog, :counter_cache => true
  belongs_to :author, :class_name => "User", :foreign_key => "user_id"
end


class BlogMembership < ActiveRecord::Base
  belongs_to :user
  belongs_to :blog

  # Membership types:
  SUBSCRIBER = 0
  AUTHOR = 1
  MODERATOR = 2
end

我的授权规则:

authorization do
  role :guest do
    description "Not logged in users and users not assigned to any group"

    ##### Blogs and Posts
    has_permission_on :blogs, :to => [ :read, :list ]

    has_permission_on :posts, :to => [ :read, :feed ]
    has_permission_on :posts, :to => :flag if User.current
  end

  role :admin do
    description "Administrators"
    has_omnipotence # Can manage all
  end

  role :moderator do
    description "Blog moderators"

    includes [ :guest, :blogger ]

    has_permission_on :posts, :to => :manage do
      if_attribute :blog => { :moderators => contains { user } }
    end
  end

  role :blogger do
    description "Blog authors"

    includes :guest
    has_permission_on :posts, :to => :create do
      if_attribute :blog => { :authors => contains { user } }
    end
    has_permission_on :posts, :to => :manage do
      if_attribute :author => is { user }
    end
  end
end

privileges do
  # default privilege hierarchies to facilitate RESTful Rails apps
  privilege :manage, :includes => [:create, :read, :update, :delete]
  privilege :read, :includes => [:index, :show]
  privilege :create, :includes => :new
  privilege :update, :includes => :edit
  privilege :delete, :includes => :destroy
end

在帖子/index.html.haml 我使用

- if permitted_to? :create, :posts
  .button.add-post
    = link_to "New post", new_resource_path

在我的posts_controller

class PostsController < InheritedResources::Base
  respond_to :html

  belongs_to :blog
  filter_access_to :all
end

看起来不错,但不起作用:(

测试用户具有版主角色,其中一个博客具有版主成员资格,但在第二个博客中没有任何成员资格。

通过以下规则和代码,任何具有版主角色的用户都可以在任何博客中创建帖子。

你能告诉我吗 - 我需要改变什么以只允许博客作者和版主将帖子发送到他们的博客而不是其他博客?

4

1 回答 1

2

不是那么优雅的方式,但我通过以下方式解决了它:

  1. 更改了posts_controller 中的访问过滤器:

    filter_resource_access :nested_in => :blog

  2. 向帖子控制器添加方法

    受保护

    def new_post_for_collection @post = Blog.find(params[:blog_id]).posts.new end

  3. index.html.haml 的变化

    • 如果允许?:create, @post .button.add-post = link_to "新帖子", new_resource_path
于 2011-02-21T21:09:30.120 回答