[英]Cancancan gem usage in rails application
我有一個管理所有注冊用戶的Rails應用程序。 可以對注冊用戶的詳細信息執行所有操作。 我現在需要以這樣一種方式限制應用程序,即只應允許管理員用戶查看所有注冊用戶。 沒有管理員權限的任何用戶都只能查看,編輯和刪除他/她的帖子。 我已使用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,電子郵件,名字,姓氏,傳記,電子郵件,角色。 它還包含所有與devise相關的字段。下面是我的User模型:
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
我已將我的管理員憑據硬編碼在seeds.rb中,如下所示:
如果User.find_by(:role =>'Admin')。nil? User.create([{{email:“ admin_user@usermanagementsystem.com”,密碼:“ topsecret”,名字:“ admin”,姓氏:“ ums”,傳記:“我在ums擔任管理員”,年齡:20,角色:“ Admin”}])結束
查看文件:Index.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來限制除管理員以外的任何用戶查看,編輯,更新和刪除自己的帖子。 管理員應該能夠在每個人的記錄上執行這些操作。
如果您發現我的控制器代碼也有問題,請幫助我改進。 提前致謝。
將以下代碼添加到capability.rb的初始化函數中,並使其正常工作。
如果user.role ==“ Admin”可以:manage,:all其他可以[:index],User,ID:user.id可以[:show],User,ID:user.id可以[:edit],User, id:user.id可以[:update],User,id:user.id可以[:delete],User,id:user.id結束
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.