How to show restricted model's parent data in dropdown list with belongs_to?

33 views
Skip to first unread message

Watee

unread,
May 22, 2013, 11:47:20 PM5/22/13
to rails...@googlegroups.com
Let's me describe scenario.
I have 2 model, User and Organization, for user authorization and authentication with CanCan and Devise.
User belongs to an organization and Organization has many user.

Each user has a role (admin, power_user or default_user) which defined ability in cancan.
  • Admin role can manage all organization and user data.
  • Power user role can manage user within the same  organization.
  • Default user role can't access rails_admin can only just login or delete themselves.
The problem is when I try to create new user using Power user role it won't show any option for Organization in dropdown.
When using admin role it working well.

Here is my CanCan ability.
  def admin_ability(user)
    can :manage, User
    can :manage, Organization
    can :access, :rails_admin   # grant access to rails_admin
    can :dashboard              # grant access to the dashboard
  end
  
  def poweruser_ability(user)
    can :manage, User, organization_id: user.organization.id
    cannot :manage, User, role: Role.admin_role
    can :access, :rails_admin   # grant access to rails_admin
    can :dashboard              # grant access to the dashboard    
  end

  def user_ability(user)
    can :read, User, id: user.id
    can :update, User, id: user.id
  end

Do you have any idea to work arround this problem?

Thank you.

Watee

unread,
May 22, 2013, 11:53:43 PM5/22/13
to rails...@googlegroups.com
Power user can assign only own organization to new user.
The option in the dropdown should be 1 option and set default.

Admin user can show all organization option in dropdown.
Reply all
Reply to author
Forward
0 new messages