【问题标题】:Cancancan gem usage in rails application在 Rails 应用程序中使用 Cancancan gem
【发布时间】:2017-11-22 10:16:13
【问题描述】:

我有一个管理所有注册用户的 rails 应用程序。所有的 crud 操作都可以在注册用户的详细信息上进行。我现在需要限制应用程序,只允许管理员用户查看所有注册用户。任何没有管理员权限的用户都应该只能查看、编辑和删除他/她的帖子。我已经使用 devise gem 进行身份验证,并计划使用 cancancan gem 进行授权。我只有一个名为 Users 的模型类,其中包含一个文本属性(用于存储“Admin”或“Nonadmin”)。这是我的控制器代码:

class UsersController < ApplicationController
before_filter :authenticate_user!


    def index
        if current_user.role ==  "Admin"
          @users = User.all
        else
          @user = User.find(current_user.id)
       end
        redirect_to new_user_registration_path if current_user.nil?
        respond_to do |format|
          format.html
        end
      end

       def show
        if (User.all.pluck(:id).include?params[:id].to_i) == false
          flash[:notice] = "You cannot perform such an action"
        else
         @user = User.find(params[:id])
       end
         respond_to do |format|
            format.html
         end
       end

       def new
        @user = User.new
       end

       def create
        @user = User.new(user_params)

       if @user.save
          redirect_to :action => 'index'
       else
          render :action => 'new'
       end
       end

       def user_params
          params.require(:user).permit(:first_name, :last_name, :age, :biography, :email)
       end

       def edit
        if (User.all.pluck(:id).include?params[:id].to_i) == false
          flash[:notice] = "You cannot perform such an action"
        else
        @user = User.find(params[:id])
        end
        respond_to do |format|
          format.html
        end
       end

       def update
        params.inspect
        @user = User.find(params[:id])

       if @user.update_attributes(user_params)
          redirect_to :action => 'show', :id => @user
       else
          render :action => 'edit'
       end
       end

       def delete
        User.find(params[:id]).destroy
       redirect_to :action => 'index'
       end

    end

我的用户表有以下字段:id、email、first_name、last_name、biography、email、role。它还包含所有与设计相关的字段。下面是我的用户模型:

    class User < ApplicationRecord
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable

    after_initialize :default_values

      private
        def default_values
          self.role ||= "Non-admin"
        end
end

我已在 seed.rb 中硬编码我的管理员凭据,如下所示:

如果 User.find_by(:role => 'Admin').nil? User.create([{email:"admin_user@usermanagementsystem.com",password:"topsecret",first_name:"admin",last_name:"ums", bio: "我在 ums 担任管理员", age:20,角色:“管理员”}]) 结束

查看文件: 索引.html.erb:-

<table id="albums" cellspacing="0">
  <thead>
    <tr>
      <th>        NAME        </th>
      <th>        EMAIL       </th>
      <% if current_user.role == "Admin" %>
      <th>        EDIT        </th>
      <th>        SHOW        </th>
      <th>        DESTROY     </th>
      <% end %>
      <th colspan="15"></th>
    </tr>
  </thead>
<% if current_user.role == "Admin" %>
<% @users.each do |user| %>
  <tr>
    <td><%= user.first_name %></td>
    <td><%= user.email %></td>
    <td><%= link_to 'Edit', users_edit_path(id: user.id), class: "small button"%></td>
    <td><%= link_to 'Show', users_show_path(id: user.id), class: "small button"%></td>
    <td><%= link_to 'Destroy', users_delete_path(id: user.id),  method: :delete, data: { confirm: 'Are you sure?' } , class: "small button"%></td>
  </tr>
<% end %>
<% else %>
<tr>
    <td><%= @user.first_name %></td>
    <td><%= @user.email %></td>
    <td><%= link_to 'Edit', users_edit_path(id: @user.id), class: "small button" %></td>
    <td><%= link_to 'Destroy', users_delete_path(id: @user.id),  method: :delete, data: { confirm: 'Are you sure?' } , class: "small button"%></td>
  </tr>
<%end %>
</table>

<%#= link_to 'Change password', edit_user_registration_path,  class: "small button" %>

edit.html.erb:-

<% if @user.nil? == false %>
<%= form_for @user , :as => :user, :url => users_update_path(@user), :method => :PUT do |f| %>
  <%= f.label :first_name %>:
  <%= f.text_field :first_name %><br>

  <%= f.label :last_name %>:
  <%= f.text_field :last_name %><br>

  <%= f.label :age %>:
  <%= f.number_field :age %><br>

  <%= f.label :biography %>:
  <%= f.text_field :biography %><br>

  <%= f.label :email %>:
  <%= f.text_field :email %><br>

  <%= f.submit :class => 'small button' %>
<% end %>
<%else%>
<h3> Such a user record does not exist. Please click on a specific user </p>
<%end%>

show.html.erb

<br>
<br>
<% if @user.nil? == false %>
<h3>User details</h3>
<table id="albums" cellspacing="0">
  <thead>
    <tr>
      <th>FIRSTNAME</th>
      <th>LASTNAME</th>
      <th>EMAIL</th>
      <th>BIOGRAPHY</th>
      <th>AGE</th>
      <th colspan="20"></th>
    </tr>
  </thead>
  <tr>
    <td> <%= @user.first_name %> </td>
    <td> <%= @user.last_name %> </td>
    <td> <%= @user.email %> </td>
    <td> <%= @user.biography %> </td>
    <td> <%= @user.age %> </td>
  </tr>
</table>
 <%= link_to '  Edit  ', users_edit_path(id: @user.id) , class: "small button"%>
<%= link_to 'Delete     ', users_delete_path(id: @user.id),  method: :delete, data: { confirm: 'Are you sure?' }, class: "small button" %>
<%else%>
<h3> Such a user record does not exist. Please click on a specific user </p>
<%= link_to "Logout", destroy_user_session_path, method: :delete, class: "small button"  %>
</footer>
<%end%>

谁能帮助我使用 cancancan gem 来限制管理员以外的任何用户查看、编辑、更新和删除他自己的帖子。管理员应该能够对每个人的记录执行这些操作。

如果您发现我的控制器代码也有问题,请帮助我改进。提前致谢。

【问题讨论】:

  • 你的技能课在哪里?您需要它来阻止/白名单允许您的用户执行的 crud 操作。
  • 我和@BKSpureon 有同样的问题。我想建议尝试使用装饰器(或演示者)模式将逻辑从视图移动到每种用户类型的正确装饰器中。这将使您更容易维护此代码。

标签: ruby-on-rails ruby ruby-on-rails-5 cancancan


【解决方案1】:

将以下代码添加到ability.rb 的初始化函数中并使其正常工作。

如果 user.role == "管理员" 可以:管理,:所有 别的 可以[:index], 用户, id: user.id can [:show],User, id: user.id 可以[:编辑],用户,id:user.id 可以[:更新],用户,id:user.id 可以 [:delete],User, id: user.id 结束

【讨论】:

    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 2014-06-16
    • 2023-04-05
    相关资源
    最近更新 更多