为什么输出没有通过 Role Based Authorization (One Role Per User) 中的能力检查
Why does the output is not passing through the ability check in Role Based Authorization (One Role Per User)
我想在我的 rails 应用程序中设置 3 个用户级别,如管理员、经理、客户。因此,我创建了一个作为用户的设计模型,并添加了一个迁移以在用户注册时将用户角色添加到 it.So,它存储用户角色(无论他是管理员、经理还是客户)。在我的应用程序中,有用于产品、交付和服务的模型和控制器。我想为每个模型设置访问级别。
因此管理员可以访问所有模型、控制器
经理有权访问产品、交付
客户可以访问服务
而我写的能力模型如下。
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.roles == "admin"
can :manage , :all
elsif user.roles == "manager"
can :read, Products, Delivery
elsif user.roles == "customer"
can :read, Services
end
end
end
我的产品展示视图如下。
<% if can? :manage ,@products%>
<h1>Products</h1>
<% @products.each do |product| %>
<p> <%= product.name%>
<p> <%= product.price %><br>
<p> <%= product.qty %><br>
<%end%>
<%end%>
但即使我以管理员身份登录,也不会显示数据。
我指的是以下 cancan 文档。
https://github.com/CanCanCommunity/cancancan/wiki/Role-Based-Authorization
"One role per user" 代码似乎没问题
但是没有资料displayed.Please帮我解决这个问题
我不是真正的 CanCan 专家,但你可以试试:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
cannot :manage, :all # we can do this since the abilities are OR'ed
if user.roles.include?('admin')
can :manage , :all
elsif user.roles.include?('manager')
can :read, Products, Delivery
elsif user.roles.include?('customer')
can :read, Services
end
end
end
另外,如果是项目启动,考虑CanCanCan
https://github.com/CanCanCommunity/cancancan
它是 CanCan 的更新版本,仍然由社区维护。
所有的代码都是正确的,但问题是强 parameters.Therefor 在注册时 "role" 没有保存在 database.Therefor 中,当检查能力时用户没有通过查看内容,因为他们不是管理员、经理或客户
我想在我的 rails 应用程序中设置 3 个用户级别,如管理员、经理、客户。因此,我创建了一个作为用户的设计模型,并添加了一个迁移以在用户注册时将用户角色添加到 it.So,它存储用户角色(无论他是管理员、经理还是客户)。在我的应用程序中,有用于产品、交付和服务的模型和控制器。我想为每个模型设置访问级别。
因此管理员可以访问所有模型、控制器
经理有权访问产品、交付
客户可以访问服务
而我写的能力模型如下。
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.roles == "admin"
can :manage , :all
elsif user.roles == "manager"
can :read, Products, Delivery
elsif user.roles == "customer"
can :read, Services
end
end
end
我的产品展示视图如下。
<% if can? :manage ,@products%>
<h1>Products</h1>
<% @products.each do |product| %>
<p> <%= product.name%>
<p> <%= product.price %><br>
<p> <%= product.qty %><br>
<%end%>
<%end%>
但即使我以管理员身份登录,也不会显示数据。 我指的是以下 cancan 文档。 https://github.com/CanCanCommunity/cancancan/wiki/Role-Based-Authorization "One role per user" 代码似乎没问题 但是没有资料displayed.Please帮我解决这个问题
我不是真正的 CanCan 专家,但你可以试试:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
cannot :manage, :all # we can do this since the abilities are OR'ed
if user.roles.include?('admin')
can :manage , :all
elsif user.roles.include?('manager')
can :read, Products, Delivery
elsif user.roles.include?('customer')
can :read, Services
end
end
end
另外,如果是项目启动,考虑CanCanCan https://github.com/CanCanCommunity/cancancan
它是 CanCan 的更新版本,仍然由社区维护。
所有的代码都是正确的,但问题是强 parameters.Therefor 在注册时 "role" 没有保存在 database.Therefor 中,当检查能力时用户没有通过查看内容,因为他们不是管理员、经理或客户