您无权访问此页面。尝试与管理员员工联系时

问题描述 投票:0回答:1

[每次我尝试通过管理员连接到我的后台时,我都会收到此信息。您无权访问此页面。甚至所有内容都应设置为可以接受管理员。我已经检查过,我所有的员工都是管理员,因此不应该这样做!

我尝试限制基本用户权限,尝试使用每个帐户访问该页面,但是即使所有员工都是管理员,我总是会收到此错误!

ability.rb


class Ability
  include CanCan::Ability

  def initialize(employee)
  employee ||= Employee.new # guest employee (not logged in)
  if employee.admin?
    can :access, :rails_admin       # only allow admin employees to access Rails Admin
    can :read, :dashboard
    can :manage, :all
    authorize!(:dashboard, @employee)
  end
end
end 

employees_controller.rb

class EmployeesController < ApplicationController
    def employee_params
        params.require(:employee).permit(:email, :encrypted_password, :password_confirmation, :role)
      end
end

accessible.rb


module Accessible
    extend ActiveSupport::Concern
    included do
      before_action :check_employee
    end

    protected
    def check_employee
      if current_employee.admin
        flash.clear
        redirect_to(rails_admin.dashboard_path) && return
      elsif current_employee
        flash.clear
        redirect_to(new_employee_session_path) && return
      end
    end
  end

rails_admin.rb


RailsAdmin.config do |config|

  config.parent_controller = "::ApplicationController"

  config.authorize_with do |controller|
    if current_employee.admin?
      redirect_to main_app.new_account_session_path, flash: {error: 'Please Login to Continue..'}
    elsif !current_employee.admin?
      redirect_to main_app.root_path, flash: {error: 'You are not Admin'}
    end
  end


  ## == CancanCan ==
  config.authorize_with :cancancan

  config.actions do
    dashboard                     # mandatory
    index                         # mandatory
    new
    export
    bulk_delete
    show
    edit
    delete
    show_in_app

  end
end

routes.rb

Rails.application.routes.draw do

devise_for :users, path: 'users'

devise_for :employees, path: 'employee'

  namespace :user do
    resources :users
  end

  namespace :employee do
    resources :employees
  end

  mount RailsAdmin::Engine => '/admin', as: 'rails_admin'

  root 'index#Index'

  get '/Index', to: 'index#Index'

  get '/ResidentialServices', to: 'pages#ResidentialServices'

  get '/CorporateServices', to: 'pages#CorporateServices'

  get '/Quotes', to: 'pages#Quotes'

  get '/Awards', to: 'pages#Awards'

end

将管理员添加到员工迁移中

class AddAdminToEmployees < ActiveRecord::Migration[5.2]
  def change
    add_column :employees, :admin, :boolean, default: false
  end
end

员工(您可以看到我将admin设置为true:

Employee.create!(lastName: 'xxx', firstName: 'xxx', title: 'Comm Rep', email: '[email protected]', encrypted_password: BCrypt::Password.create('123456'), admin: 'true')

我希望管理员能够访问后台

ruby-on-rails rails-admin cancancan
1个回答
0
投票

您是否应该在Capacity.rb中传递变量而不是实例变量?

authorize!(:dashboard, employee)

代替

authorize!(:dashboard, @employee)
© www.soinside.com 2019 - 2024. All rights reserved.