Вопрос

I understand the rather basic workings of CanCan and have tried to follow some tutorials, but none of them really discuss how to assign the user roles in my application.

Furthermore, I want my application to allow users to assign roles to other users. For example, my users can create projects, and assign other users to the project team. I am currently using a Role model to hold my user roles.

How do I allow users to specifically set another user to that project team role, where they can manage the project?

Это было полезно?

Решение

You can use a model with a field like can_manage_projects

class User < ActiveRecord::Base

  has_many :roles

  field :can_manage_projects, type: boolean
end

And on your ability, you set a permission correspondent...

class Ability
  def initialize(user)
    can :manage Project if user.can_manage_project?
  end 
end

And so, you can use that permission whenever you want, doing things like:

class ProjectController

  def assign_user(project, user)
     project.users << user if can? :manage, Project
  end
end

or

<body>
  <% if can? :manage, Project %>
    <%= link_to 'Assign selected user to Project', assign_user_project_path(project, user) %>
  <% end %>
</body>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top