声明_授权包含和继承资源
题
我有带有声明_ uthorization和Inherited_resources Gems安装的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
.
在posts / 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
.
看起来不错,但不起作用:(
测试用户将主持人的角色与主持人的成员资格有一个博客,但没有第二个博客中的任何会员资格。
具有以下规则和代码具有中文域的任何用户角色可以在任何博客中创建帖子。
你能告诉我吗 - 我需要改变的是,只允许博客作者和主持人向他们发送帖子,而不是其他博客?
解决方案
Not so elegant way, but i solved it with following:
changed access filter in posts_controller:
filter_resource_access :nested_in => :blog
Added method to posts controller
protected
def new_post_for_collection @post = Blog.find(params[:blog_id]).posts.new end
Changes in index.html.haml
- if permitted_to? :create, @post .button.add-post = link_to "New post", new_resource_path
不隶属于 StackOverflow