为我的应用创建版主授权用户角色
Making a Moderator Authorized User role for my app
目前我的用户有 2 种类型的角色 [:member , :admin]
,成员可以对他们创建的大多数 post 进行增删改查。 :admin 可以在任何 post 期间进行 CRUD。现在我正在尝试创建一个只能查看和更新所有 post 的版主。我已将 :moderator
添加到我的 enum role:
。我还包括
before_action :moderator_user, except: [:index, :show]
和
def authorize_user
unless current_user.admin?
flash[:alert] = "You must be an admin to do that."
redirect_to topics_path
end
end
def moderator_user
unless current_user.moderator?
flash[:alert] = "You must be an moderator to do that."
redirect_to topics_path
end
end
但似乎干扰了我的 before_action :authorize_user, except: [:index, :show]
,因为它导致我的 rspec 测试失败。
我想弄清楚如何创建一个介于成员和管理员之间但不影响任何一个的版主角色。
helper.rb :
def user_is_authorized_for_topics?
current_user && current_user.admin?
end
def user_is_moderator_for_topics?
current_user && current_user.moderator?
end
这是授权宝石之一的完美案例 -- Pundit
or CanCanCan
。 CanCanCan
可能是 user-centric 实施的最佳选择...
#Gemfile
gem 'cancancan', '~> 1.13'
#app/models/ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in) #-> looks for "current_user"
case true
when user.admin?
can :manage, Post #-> CRUD all
when user.moderator?
can [:read, :update], Post #-> update/read posts
when user.member?
can :manage, Post, user_id: user.id #-> CRUD their posts
end
end
end
以上将使您能够在控制器和视图中使用 can?
and authorize
methods:
#app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
load_and_authorize_resource
end
#app/views/articles/index.html.erb
<% @articles.each do |article| %>
<% if can? :update, article %>
<%= link_to "Edit", edit_article_path(article) %>
<% end %>
<% end %>
以上应该适合你。
load_and_authorize_resource
过滤器应该为您提供 范围 数据:
As of 1.4 the index action will load the collection resource using accessible_by.
def index
# @products automatically set to Product.accessible_by(current_ability)
end
--
有一个很棒的Railscast about this here。 Railscasts 的创建者在精疲力尽之前创作了 CanCan
,因此一个新的社区开始使用它 CanCanCan
。
目前我的用户有 2 种类型的角色 [:member , :admin]
,成员可以对他们创建的大多数 post 进行增删改查。 :admin 可以在任何 post 期间进行 CRUD。现在我正在尝试创建一个只能查看和更新所有 post 的版主。我已将 :moderator
添加到我的 enum role:
。我还包括
before_action :moderator_user, except: [:index, :show]
和
def authorize_user
unless current_user.admin?
flash[:alert] = "You must be an admin to do that."
redirect_to topics_path
end
end
def moderator_user
unless current_user.moderator?
flash[:alert] = "You must be an moderator to do that."
redirect_to topics_path
end
end
但似乎干扰了我的 before_action :authorize_user, except: [:index, :show]
,因为它导致我的 rspec 测试失败。
我想弄清楚如何创建一个介于成员和管理员之间但不影响任何一个的版主角色。
helper.rb :
def user_is_authorized_for_topics?
current_user && current_user.admin?
end
def user_is_moderator_for_topics?
current_user && current_user.moderator?
end
这是授权宝石之一的完美案例 -- Pundit
or CanCanCan
。 CanCanCan
可能是 user-centric 实施的最佳选择...
#Gemfile
gem 'cancancan', '~> 1.13'
#app/models/ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in) #-> looks for "current_user"
case true
when user.admin?
can :manage, Post #-> CRUD all
when user.moderator?
can [:read, :update], Post #-> update/read posts
when user.member?
can :manage, Post, user_id: user.id #-> CRUD their posts
end
end
end
以上将使您能够在控制器和视图中使用 can?
and authorize
methods:
#app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
load_and_authorize_resource
end
#app/views/articles/index.html.erb
<% @articles.each do |article| %>
<% if can? :update, article %>
<%= link_to "Edit", edit_article_path(article) %>
<% end %>
<% end %>
以上应该适合你。
load_and_authorize_resource
过滤器应该为您提供 范围 数据:
As of 1.4 the index action will load the collection resource using accessible_by.
def index # @products automatically set to Product.accessible_by(current_ability) end
--
有一个很棒的Railscast about this here。 Railscasts 的创建者在精疲力尽之前创作了 CanCan
,因此一个新的社区开始使用它 CanCanCan
。