簡體   English   中英

如何通過Devise向我的Rails應用添加管理員限制功能?

[英]How to add an admin restriction feature through Devise to my rails app?

我的rails應用有幾個出租車司機,他們也有幾個出租車,它們之間的關系如下:

class Operator < ActiveRecord::Base
    has_many :cabs
end

我希望添加身份驗證系統,以便為每個操作員創建管理員。 我正在使用Devise。 由於需要將路徑創建為:operator /:operator_id / admins / sign_up,因此生成了Admin模型,如下所示:

rails generate devise Admin

然后,我修改了路線,以獲得上述路徑:

scope "operators/:operator_id" do 
    devise_for :admins
end

經過一些修改,我能夠添加新管理員並將其關聯到適當的操作員。 但是,我想確保管理員只能訪問與該管理員關聯的操作員的出租車。 將以下內容添加到cabs_controller不起作用:

before_action :authenticate_admin!

作為具有登錄名的管理員,可以訪問所有其他駕駛室。 我要確保:

1. If there is no current_admin, my app asks to sign_in or sign_up
2. If there already is a current__admin signed_in, he/she has access to only the cabs associated to that operator to which the current_admin is assigned.

我是Devise的新手。 請告知我應該如何進行。 謝謝!

Devise用於身份驗證,您的問題是Devise無法解決的授權問題。 您可以查看Cancan https://github.com/ryanb/cancan :)

您無需為管理員設計范圍。 我不確定是否可以解決訪問方面的任何問題。 這也使您的代碼難以維護。

我建議您改為專注於人際關系

class Operator < ActiveRecord::Base
    has_many :cabs
    has_many :admins
end


class Operator < ActiveRecord::Base
    has_many :cabs, through: operator
    belongs_to :operator
end

通常,以后我處理此類情況的方式是在cab模型中編寫特殊代碼:

  def is_admin?(admin)
      admin.cabs.include?(@cab)
      # you can put more logic here if you have different levels of access
  end

以及稍后要限制訪問權限的控制器中:

  if @cab.is_admin?(current_admin) 
     # do the stuff
  else
     # go away message
  end

或例如,如果您在索引中列出出租車:

  def index 
     current_admin.cabs.page(params[:page]) # this is assuming you're using pagination with something like Kaminari
  end 

來自divise的current_admin,您可能還需要運行:authenticate_admin! 在進行過濾之前,請先確保您擁有管理員權限,而且它不是nil

當然,您可以將其放入價格消息中,然后像之前過濾器一樣運行它。

最簡單的解決方案是使用STI並設置Admin <User,然后再使用幾個

devise_for :users
devise_for :admins

如果您不希望使用STI,則可以在向用戶表中添加角色方法作為示例時,使用某些自定義覆蓋來解決問題。

#routes
devise_for :users
namespace :admin do
  devise_for :admins,
           singular: :admin,
           plural: :admins,
           class_name: 'User',
           only: [:sessions],
           path: '/',
           module: :devise
end 


#lib/overrides/devise
require "devise/strategies/authenticatable"
require "devise/strategies/database_authenticatable"

module Devise
  module Strategies
    class DatabaseAuthenticatable
      def authentication_hash
        if mapping.name == :admin
          @authentication_hash.update(role: :admin) #will add a where(role: :admin) clause
        else
          @authentication_hash
        end
      end
    end
  end
end


class Admin::ApplicationController < ApplicationController
  skip_before_filter :authenticate_user!

  before_filter :authenticate_admin!
end 

您可以使用基於角色的身份驗證。

生成用戶模型

   class User < ActiveRecord::Base 
     enum role: [:operator, :admin]
     after_initialize :set_default_role, :if => :new_record?

     def set_default_role
       self.role ||= :operator
     end

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

產生遷移以增加角色

rails g AddRoleToUser role:integer

請看一下ENUM

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM