我使用 cancan 作为我的授权引擎。
我已经在用户中拥有角色:
ROLES = %w[admin normal author corp]
我也有添加和检查角色的方法:
#cancan
def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
end
def roles
ROLES.reject do |r|
((roles_mask || 0) & 2**ROLES.index(r)).zero?
end
end
def is?(role)
roles.include?(role.to_s)
end
我# roles_mask :integer
在用户模型中。
但是,我想要一个after_save :add_normal_role
将正常角色分配给用户的。
基本上,我无法(不知道)如何为每个用户分配角色。
这就是我所拥有的,它不起作用:
private
def add_normal_role
self.roles=(ROLES[1])
end
谢谢