Rails 设计角色模型和 CanCanCan - 定义能力

Posted

技术标签:

【中文标题】Rails 设计角色模型和 CanCanCan - 定义能力【英文标题】:Rails Devise Role Model & CanCanCan - defining abilities 【发布时间】:2015-11-27 12:27:54 【问题描述】:

我正在尝试在 CanCanCan 中定义能力。

我不知道如何开始。

我使用角色模型作为我的角色,角色在我的 Profile.rb 中定义。 Profile.rb 属于 User.rb。

我正在尝试检查用户是否具有角色 :student。

当我尝试时:

if user_signed_in?, user.profile.has_role? :student

我收到一个语法错误:

syntax error, unexpected ',', expecting =>
    if user_signed_in?, user.profile.has_role? :student

当我尝试时:

if user_signed_in? && user.profile.has_role? :student

我收到一个语法错误:

syntax error, unexpected tSYMBEG, expecting =>
    if user_signed_in? && user.profile.has_role? :student

我也尝试用普通括号替换花括号并将它们完全删除。

当我尝试删除设计部分 (user_signed_in) 并使用下面 cmets 中的建议时,我尝试:

if  user.profile.has_role?(:student) 

我得到这个错误:

undefined method `has_role?' for nil:NilClass

当我尝试时:

if  user.profile.has_role? :student 

我收到此错误:

undefined method `has_role?' for nil:NilClass

当我尝试时:

if  user.profile.has_role?(student) 

我收到此错误:

undefined local variable or method `student' for #<Ability:0x007fd034a78968>

我的 profile.rb 中定义了以下角色:

  roles :admin, :manager, #
        :student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager,  # for universities
        :sponsor, # for industry
        :project_manager, :representative, # for both uni and industry
        :grantor, :investor, :adviser, :innovation_consultant, :panel_member, # external
        :participant, :guest # public

当我尝试时:

can :crud, Profile, :user_id => user.id if  user.profile.has_role? :student

我没有收到任何错误,但我对这种方法的问题是学生可以做很多事情(有 10 行权限,所以我需要将 if 语句单独添加到 10 'can ' 语句,除非有一种方法可以将 if 语句应用于下一个 'elsif' 语句之前的所有行。

我的ability.rb的第一部分贴在下面(角色很多,能力也很多,所以我没有贴全)。

class Ability
  include CanCan::Ability

  def initialize(user)

      alias_action :create, :read, :update, :destroy, :to => :crud


    # Define abilities for the passed in user here. For example:
    #
    user ||= User.new # guest user (not logged in)

      #users who are not signed in can create registration or login 

      # can read publicly available projects, programs and proposals
      can :read, Project, :active => true, :closed => false, :sweep =>  :disclosure =>  :allusers => true   

      # :active => true, :closed => false  &&  :Project.sweep.disclosure.allusers => true
      # if user role is student

      can :crud, Profile, :user_id => user.id if  user.profile.has_role? :student #[for themselves]
      can :read, ProjectInvitation, :student_id => @current_user && :expiry_date_for_students_to_claim_project > Time.now
      #  can read project invitations addressed to them
      # can read projects to which they are invited whilst the invite is available to accept;
      can :read, Project,  #if invited to the project?
      # and they can create responses to invitations to those projects
      can :update, ProjectInvitation.student_accepted
      # they can create questions on those projects before the invite expiry date;
      can :read, ProjectQuestion,  #if intvited
      can [:create, :update, :destroy], ProjectQuestion #if they created the question
      can :read, ProjectAnswer #if its on a project they can see
      # they can update term sheets and template agreements for those projects
      can [:read, :update], TermSheet #where created for project in which they are participating
      can [:read, :update], ProjectAgreement #where created for a project in which they are participating
      can [:read, :update], FinanceAgreement #where created for a project in which they are participating
      can [:read, :update], Nda #where created for a project in which they are participating
      can [:create, :update], Feedback #where created for a project in which they are participating and the feedback is on other project team members and the project is completed


    elsif user.profile.has_role? :educator

当我尝试时(以下建议):

if  user.try(:profile).present? && user.profile.has_role? :student 

我收到此错误:

syntax error, unexpected tSYMBEG, expecting keyword_then or ';' or '\n'
...nt? && user.profile.has_role? :student

请有人看看我做错了什么?

【问题讨论】:

你可以试试user.profile.has_role吗?(:student) 嗨 - 我试过并得到上面发布的错误 你能发一下你的ability类吗? 嗨 Rokibul,我添加了它的第一部分。如果用户是学生,我正在尝试使从个人资料到反馈的所有功能都可用 请查看编辑后的答案。 【参考方案1】:

以下代码

if user_signed_in?, user.profile.has_role? :student
if user_signed_in? && user.profile.has_role? :student
if user_signed_in? && user.profile.has_role? :student

您不能使用 来执行 ruby​​ 语句,它需要一个键值对。你可以重写你的代码如下

if user_signed_in? && user.profile.has_role? :student

但是你得到一个空指针错误,所以你已经在你的代码中修复它,首先检查user.try(:profile).present?然后你可以调用user.profile.has_role? :student,因为你的profile得到nil

if user.try(:profile).present? && user.profile.has_role?(:student)

【讨论】:

嗨,我试过这个建议 - 它给出了一个错误(上面发布)。感谢您对括号的解释 - 我真的不知道如何学习预期的语法。 没有在编辑器上编写代码,有一个错字,已修复,您可以现在尝试。 @user2860931

以上是关于Rails 设计角色模型和 CanCanCan - 定义能力的主要内容,如果未能解决你的问题,请参考以下文章

Rails、Devise、Role Model 和 CanCanCan - 定义能力

rails+devise+cancancan+rolify注册登录多角色权限管理

使用 Rolify/Devise/Cancancan 与角色关联

通过 Rails 5 中的另一个模型继承关联

基于部门的 Rails CanCanCan 资源能力

Rails的基于部门CanCanCan资源的能力