首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >declarative_authorization的包含和继承的资源

declarative_authorization的包含和继承的资源
EN

Stack Overflow用户
提问于 2011-02-17 22:20:48
回答 1查看 579关注 0票数 1

我有与declarative_authorization和inherited_resources宝石安装的rails3应用程序。让我向您展示我的应用程序中的一些代码:

代码语言:javascript
复制
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

我的授权规则:

代码语言:javascript
复制
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

在post/index.html.haml中,我使用

代码语言:javascript
复制
- if permitted_to? :create, :posts
  .button.add-post
    = link_to "New post", new_resource_path

在我的posts_controller中

代码语言:javascript
复制
class PostsController < InheritedResources::Base
  respond_to :html

  belongs_to :blog
  filter_access_to :all
end

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

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

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-02-22 05:09:30

不是很优雅的方式,但我用下面的方法解决了它:

Posts_controller中的

  1. 已更改访问筛选器:

filter_resource_access :nested_in =>:博客

  • 将方法添加到帖子控制器

受保护

index.html.haml中的def new_post_for_collection @post = Blog.find(params:blog_id).posts.new end

  • 更改

代码语言:javascript
复制
- if permitted\_to? :create, @post  .button.add-post  = link\_to "New post", new\_resource\_path 

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5030139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档