Вопрос

I have one menu entry called "admin" with sub-entries "client", "users" and "sales". I then have 3 different roles (admin, manager, employee). Role "admin" can access "users" and role "employee" can access "client" and "sales". "Manager" can't access any option in the menu.

I'm able to hide other menus but as this have two different roles I'm having some issues... I'm using rails 3 with devise and cancan (with roles defined in User model).

For example, this works:

<% if can? :read, Client %>

Inside the menu entry I then evaluate each sub-entry to show/hide accordingly to the user role, and that also works. Following the same logic I tried this to hide the main entry, more than one role:

<% if can? :manage, (Client || User || Sales) %>

But this only evaluates the first one (in this case "Client"). Is it possible to do what I want? What's the best way to "evaluate" more than one role at the same time?

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

Решение

You can do what you want this way:

<% if can?(:manage, Client) || can?(:manage, User) || can?(:manage, Sales) %>

But I think in this case a better solution is just to check if the User is not a Manager. It is depending of how you save users roles. For example:

 <% if current_admin_user.role != 'Manager' %>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top