设计可邀请 - 如果访问被拒绝发送邀请,则重定向到基于角色的仪表板

时间:2018-04-01 22:23:23

标签: ruby-on-rails devise devise-invitable

嘿所有我有一个简单的应用..它有一些角色..但现在不确定我将使用哪个授权库..

因此,它只是设置,管理员或调度员可以向系统发送新的用户邀请..

如果具有非允许角色的用户尝试访问邀请页面,则会重定向到root_path,但是我希望将它们重定向回各自的仪表板。

我在登录时做了类似的事情,如下所示。

我正在尝试为authenticate_inviter!方法实现类似的功能。

有什么建议吗?我似乎无法想出一个完成这项工作的好方法。

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?

def after_sign_in_path_for(resource)
  if current_user.role == "admin"
    dashboard_admin_user_path
  elsif current_user.role == "dispatch"
    dashboard_dispatch_user_path
  elsif current_user.role == "owner"
    dashboard_owner_user_path
  elsif current_user.role == "driver"
    dashboard_driver_user_path
  elsif current_user.role == "client"
    dashboard_client_user_path
  elsif current_user.role == "guest"
    dashboard_guest_user_path
  else
    root_path
  end
end



protected

  def authenticate_inviter!
    unless current_user.role=='admin' && unless current_user.role == 'dispatch'
      redirect_to root_path, :alert => "Access Denied, only Administrators can invite new users"
    end
    super
  end

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:accept_invitation, keys: [:f_name, :l_name, :email, :password, :password_confirmation])
  devise_parameter_sanitizer.permit(:invite, keys: [:f_name, :l_name, :email, :password, :password_confirmation])
end

end

预先感谢您的协助!

1 个答案:

答案 0 :(得分:0)

玩完之后我想出了这个解决方案:

  def authenticate_inviter!
    unless current_user.role=='admin' or current_user.role == 'dispatch'
  if current_user.role == "owner"
    redirect_to dashboard_owner_user_path, :alert => "Access Denied, only Administrators or Dispatchers can invite new users"
  elsif current_user.role == "driver"
    redirect_to dashboard_driver_user_path, :alert => "Access Denied, only Administrators or Dispatchers can invite new users"
  elsif current_user.role == "client"
    redirect_to dashboard_client_user_path, :alert => "Access Denied, only Administrators or Dispatchers can invite new users"
  elsif current_user.role == "guest"
    redirect_to dashboard_guest_user_path, :alert => "Access Denied, only Administrators or Dispatchers can invite new users"
  else
    redirect_to root_path
  end
    end
    super
  end