2017-06-20 1 views
0

私はすべての登録ユーザを管理するレールアプリケーションを持っています。登録されたユーザーの詳細については、すべてのcrud操作を実行できます。私は今、adminユーザーだけが登録されたすべてのユーザーを表示するようにアプリケーションを制限する必要があります。管理者権限を持たないユーザーは、自分の投稿のみを表示、編集、削除することができます。私は認証のための宝石を使用し、認可のためにcancancan gemを使用する予定です。私はテキスト属性( "管理者"または "Nonadmin"を格納する)を含むUsersという名前の単一のモデルクラスのみを持っています。ここに私のコントローラのコードは次のとおりです。ID、電子メール、FIRST_NAME、LAST_NAME、伝記、電子メール、役割:レールアプリケーションでのCancancan gemの使用

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 

マイユーザテーブルには、次のフィールドがあります。また、すべての工夫関連fields.Belowは私の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場合(:役割=> 'Admin')。nil? User.create([{email: "[email protected]"、パスワード: "topsecret"、first_name: "admin"、last_name: "ums"、バイオグラフィー: "私はumsで管理者として働く"、年齢:20 、役割: "管理者"}]) 終了

ビューファイル: 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を使用するのを手伝ってもらえますか?管理者は、全員の記録に対してこれらの操作を実行できるはずです。

コントローラーコードに間違ったことがあるとわかったら改善してください。前もって感謝します。

+0

をUSER.ID能力クラス?あなたはそれを防ぐ必要があります/白いリストは、あなたのユーザーのための許容されるcrudアクション。 – BKSpurgeon

+0

@BKスパージョンと同じ質問があります。そして、私は、デコレータ(またはプレゼンター)のパターンを使用して、ビューから各タイプのユーザーの正しいデコレータにロジックを移動させることを提案したいと思います。これにより、このコードを簡単に維持することができます。 –

答えて

0

ability.rbのinitialize関数に次のコードを追加し、正しく動作させました。

user.role == "管理者" ができた場合:、管理:他のすべての ができる[:インデックス]、[ユーザー、IDは: ができUSER.ID [:ショー]、ユーザー、ID:user.idを[編集] ができ、ユーザー、ID: ができUSER.ID [:更新]、[ユーザー、IDは:、ユーザーが[削除] ID: できるがUSER.ID 終わり、あなたです

関連する問題