1
現在,在我的Rails應用程序中,我使用簡單身份驗證(電子郵件,密碼)並授予帳戶role
來定義帳戶可以執行的操作。Rails如何實現角色並定義角色的權力?
def login
email = params[:email]
@account = Account.find_by_email(email)
if @account && @account.authenticate(params[:password])
session[:user_id] = @account.id
redirect_to books_path
else
render :login
end
end
def current_user
@current_user ||= Account.find(session[:user_id]) if session[:user_id]
end
例如:二角色管理器(ROLE_ID:1)和用戶(ROLE_ID:2 ... N)在控制器 ,我使用笨方法對於像每個角色設定數據:
每本書ACCOUNT_ID
def index
@books = Book.where("account_id = ?,@current_user.id)
@books = Book.all if @current_user.role_id == 1
end
它似乎很愚蠢,我試圖用cancancan來解決這一點,但我知道,cancancan只是可以定義爲Restful方法,並且可以區分角色權限,就像管理者可以做的一切方法一樣可以顯示... cancancan可以通過某種方式來解決什麼角色可以看到他們擁有的數據?使用cancancan或以其他方式解決?請給我一些建議!謝謝。
可能我的回答可以幫助您? – rob