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