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