显示用户的权限表

时间:2019-03-07 14:34:05

标签: ruby-on-rails authorization pundit rolify

为用户实施权限管理员。编辑它们后,我想显示一个表,其中包含用户对每个操作的权限。如下图:  enter image description here

但是问题是仅显示当前用户的权限。如何传递我想显示的用户?

一些代码:

应用程序政策

    class ApplicationPolicy
      attr_reader :user, :record

      def initialize(user, record)
        @user = user
        @record = record
      end

      def index?
        false
      end

      def show?
        scope.where(:id => record.id).exists?
      end

      def create?
        false
      end

      def new?
        create?
      end

      def update?
        false
      end

      def edit?
        update?
      end

      def destroy?
        false
      end

      def scope
        Pundit.policy_scope!(user, record.class)
      end

      class Scope
        attr_reader :user, :scope

        def initialize(user, scope)
          @user = user
          @scope = scope
        end

        def resolve
          scope
        end
      end
    end

控制器操作

 def show
   authorize @user
 end

查看

      <table class="table">
        <thead>
          <tr>
            <th scope="col"></th>
            <th scope="col">Index</th>
            <th scope="col">Show</th>
            <th scope="col">New</th>
            <th scope="col">Edit</th>
            <th scope="col">Delete</th>
            <th scope="col">Report</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <th scope="row">Cars</th>
            <%  %>
            <td><span class="glyphicon glyphicon-<%= policy(Car).index? ? "ok text-success" : "remove text-danger"%>"></span></td>
            <td><span class="glyphicon glyphicon-<%= policy(Car.first).show? ? "ok text-success" : "remove text-danger"%>"></span></td>
            <td><span class="glyphicon glyphicon-<%= policy(Car).new? ? "ok text-success" : "remove text-danger"%>"></span></td>
            <td><span class="glyphicon glyphicon-<%= policy(Car.new).edit? ? "ok text-success" : "remove text-danger"%>"></span></td>
            <td><span class="glyphicon glyphicon-remove text-danger"></span></td>
            <td><span class="glyphicon glyphicon-remove text-danger"></span></td>
          </tr>
          <tr>
            <th scope="row">Animals</th>
            <td><span class="glyphicon glyphicon-ok text-success"></span></td>
            <td><span class="glyphicon glyphicon-ok text-success"></span></td>
            <td><span class="glyphicon glyphicon-remove text-danger"></span></td>
            <td><span class="glyphicon glyphicon-remove text-danger"></span></td>
            <td><span class="glyphicon glyphicon-remove text-danger"></span></td>
            <td><span class="glyphicon glyphicon-ok text-success"></span></td>
          </tr>
        </tbody>
      </table>

如您所见,在“ policy(Car)”行中,是我认为实现的方式。

0 个答案:

没有答案