Reputation: 21
I am creating an app where there are many Users and many Organisations.
A user can be in multiple organisations, and within each Organisation they can have a single role. Currently, only 'Organisation Admin' and 'Basic User'. EDIT: For more context, I would like a User to be able to be an Org Admin for Org 1 and a Basic User for Org 2, meaning this user would have different abilities for each Organisation.
Org Admins can add users to the Org, but the way I have abilities set does not currently prevent Basic User's from also doing it.
...
else
#Can manage Orgs where Role = OrgAdmin
can :manage, Organisation do |org|
user.users_roles.where(:organisation_id => org.id).first.role.name == 'Organisation Admin'
end
#Can view Orgs where Role = Basic User
can :read, Organisation do |org|
user.users_roles.where(:organisation_id => org.id).first.role.name == 'Basic User'
end
After this, I currently have...
can :create, User
...which of course, allows anyone to create a User
I require something like
if can? :manage, @org
can :create, User
end
but only for the Org they are OrgAdmin for, so this would prevent a URL change. However, this doesn't work, as any form either return false and prevents any User creation, or return :manage Org as true for everyone to do everything, regardless of role.
Any ideas or pointing in the right direction would be appreciated.
EDIT: Additionally, the way I can created the :manage/:read, Organisation part correctly prevents people from managing/viewing what they shouldn't, but throws up a
undefined method `role' for nil:NilClass
instead of redirecting the user.
Thanks! - Jaliso
Upvotes: 0
Views: 726
Reputation: 824
you can render differents partial form into the register form: An example:
<%= render partial: 'admin/admin/form', locals: {f: f} if resource.company? %>
<%= render partial: 'admin/agents/form', locals: {f: f} if resource.agent? %>
<%= render partial: 'admin/managers/form', locals: {f: f} if resource.building_manager? %>
Upvotes: 0
Reputation: 525
I suggest you to use gem Devise. However you can create a Boolean field in User where you say if each user belongs or not to org Admin. Then you create a controller (a page) that only admins can access where they define the Boolean field created before of each users and so if they belong or not to org Admin. Tell me if I'm not clear.
Upvotes: 1