Ruby CanCanCan 动态定义方法

灵感来自这里https://github.com/kristianmandrup/cantango/wiki/CanCan-vs-CanTango

如果权限不多,我们可以通过这种方式来定义

class CanCan::Ability
  def initialize user, options = {}
    if !user
      can :read, :all
    end
    if user
      admin_rules if user.roles.include? :admin
      editor_rules if user.roles.include? :editor
      default_rules
    end
  end

  def admin_rules
    can :manage, :all
  end

  def editor_rules
    can :manage, [Article, Post]
  end
end

改的灵活点

class CanCan::Ability
  def initialize user, options = {}
    user ? user_rules : guest_user_rules
  end

  def user_rules
    user.roles.each do |role|
      exec_role_rules(role) if user.roles.include? role
    end
    default_rules
  end

  def exec_role_rules role
    meth = :"#{role}_rules"
    send(meth) if respond_to? meth
  end
  
  # various rules methods for each role
  def admin_rules
    can :manage, :all
  end

  def editor_rules
    can :manage, [Article, Post]
  end
  ...

  def default_rules
    can :read, :all
  end
end

上面是原文出处,实际使用的时候还是不方便,我改成这样了(非一比一还原)。使用define_method 定义那个每个方法:

class Ability
  include CanCan::Ability

  @@permissions = xxx
  def initialize(user)
    @user= user|| User.new
    @user.roles.each do |role|
       exec_role_rules(role) if @user.has_role?(role)
    end
  end

  Role.all.each do |role|
    define_method "rules_#{role.name.gsub(" ", "_")}" do
      role_permissions = RolePermission.where(role_id: role.id)
      unless role_permissions.blank?
        role_permissions.each do |rp|
          permission_value = @@permissions[rp.permission_key]
          entities = permission_value["entities"].map{|e| e.constantize}
          actions = permission_value["actions"].map{|e| e.to_sym}
          can actions, entities
        end
      end
    end
  end

  def exec_role_rules(role)
    meth = :"rules_#{role.name.gsub(" ", "_")}"
    send(meth) if respond_to? meth
  end

end

经测试嘎嘎好用,就是我还需要添加一些条件筛选,导致我这个不能用,得删掉,有点可惜,记录一下~

相关推荐

  1. Ruby CanCanCan 动态定义方法

    2024-03-10 22:02:07       48 阅读
  2. 定制化正则化:在Mojo模型中动态应用自定义方法

    2024-03-10 22:02:07       25 阅读
  3. 动态规划方法介绍

    2024-03-10 22:02:07       41 阅读
  4. go方法定义

    2024-03-10 22:02:07       39 阅读
  5. 在Go中定义方法

    2024-03-10 22:02:07       67 阅读
  6. iOS自定义初始化方法

    2024-03-10 22:02:07       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 22:02:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 22:02:07       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 22:02:07       82 阅读
  4. Python语言-面向对象

    2024-03-10 22:02:07       91 阅读

热门阅读

  1. 如何系统的去学c#

    2024-03-10 22:02:07       42 阅读
  2. 【Demo】游戏小地图

    2024-03-10 22:02:07       39 阅读
  3. Pytest教程:Pytest的跳过与标记功能用法

    2024-03-10 22:02:07       39 阅读
  4. C#拾遗补漏之goto跳转语句

    2024-03-10 22:02:07       48 阅读
  5. GRU-深度学习循环神经网络情感分类模型搭建

    2024-03-10 22:02:07       47 阅读
  6. 学习数据结构和算法的地13天

    2024-03-10 22:02:07       39 阅读
  7. 函数柯里化(function currying)及部分求值

    2024-03-10 22:02:07       47 阅读
  8. vue中常用的指令和自定义指令

    2024-03-10 22:02:07       40 阅读
  9. 打印前端代码

    2024-03-10 22:02:07       46 阅读
  10. [leetcode] 283. 移动零

    2024-03-10 22:02:07       45 阅读
  11. 我耀学IT—day06-导航栏

    2024-03-10 22:02:07       34 阅读
  12. 用spark读取及存储数据

    2024-03-10 22:02:07       45 阅读
  13. Gson(List<Object>转String 、String转List<Object>)

    2024-03-10 22:02:07       43 阅读
  14. 手机中常用的传感器

    2024-03-10 22:02:07       41 阅读
  15. vuej介绍

    2024-03-10 22:02:07       49 阅读
  16. 常见的排序算法-(字解版)

    2024-03-10 22:02:07       45 阅读