Gem cancan 的配置

xjz19901211 · 2012年11月23日 · 最后由 xjz19901211 回复于 2012年11月24日 · 3358 次阅读

才刚玩 cancan 两天,cancan 配置放一个 Ability 里感觉太乱了,由于不知道怎么把 cancan 的权限配置分离出来,google 了一会也没找到,于是自己弄出一个怪胎 cancan_config: https://github.com/xjz19901211/cancan_config

然后随便个 rb 文件里配置:

CanCanConfig.roles_define do
  group Role::SuperAdmin do
    can :manage, :all
  end

  group Role::CommonUser do
    can [:update, :destroy], File do |file|
      helper :can_manager_file file, current_user
    end
  end

  group :public do

  end

  helper do
    def can_manager_file(question, user)
      question.user_id == user.id
    end
  end
end

Ability 类中只要简单的定义一个 user_role 方法返回当前用户的在配置文件中所在的组就好了

class Ability
  include CanCan::Ability
  include CanCanConfig::AbilityConfig

  # default load path
  # @@roles_paht = "config/cancan_roles.rb"

  def user_role
    if current_user.role
      current_user.role
    else
      Role::CommonUser
    end
  end
end

感觉我这 gem 写的太烂了,现在请教问题:单纯的 cancan 权限多了大家怎么管理的? 如果单纯的 cancan 不好管理的话,大家有什么好用的 Gem 么?

多了就存数据库里呗

你的所有权限都配置在 AbilityConfig 里面吗?

@ywencn 汗,能放数据库里么,像

can :edit, User do |user|
   user.id == current_user.id
end

我要怎么折腾进去呢

·@kaka 恩都在这一个文件里,写的时候,分开到没想过。。哈哈

@ywencn 看到 Ability in database 的文档,我去看看,才上手两天,这些还真不了解,哈哈

还是不怎么怎么放。。。

看看 cancan 里头的 Role Based Authorization 呢 https://github.com/ryanb/cancan/wiki/Role-Based-Authorization

@clark 恩,谢谢,但或许是我英文烂到不能再烂了吧,看资料都借用工具半翻译的看,哈哈

我发现光看没感觉啊,还是要有时间再去写写,这个周末要加班。。。

另外谢谢大家了,嘿嘿

需要 登录 后方可回复, 如果你还没有账号请 注册新账号