0

I'm trying to figure out how to assign a role to a user in Rails 4, using rolify.

My use case is:

I want to assign global roles to users who operate the app.

I want to assign scoped roles to all customers. Each customer belongs to an organisation. Any role they are assigned will be confined to the organisation they belong to.

How can I achieve this in rails? At this stage, I'm stuck with the logic of how to do this.

Mel
  • 2,481
  • 26
  • 113
  • 273

1 Answers1

0

Checkout the docs here. It has examples to define roles scoped to resource instance and resource class.

To define a global role:

customer = Customer.find(1)
customer.add_role :admin

To define a role scoped to a resource instance:

customer = Customer.find(2)
customer.add_role :moderator, Organization.first // you are looking for this

To define a role scoped to a resource class:

customer = Customer.find(3)
customer.add_role :moderator, Organization
Rahul Singh
  • 3,417
  • 2
  • 25
  • 32
  • thanks- I've seen the docs. I can't figure out how to implement that in the code. I don't know how to dynamically constrain the Organisation attribute to the relevant organisation. I don't know if I need to define a new controller action to assign a role - and if I do, I don't know which controller to put the action in – Mel Aug 07 '16 at 08:49
  • When they create an account. I have setout the full context here: http://stackoverflow.com/questions/38810323/rails-4-rolify-setting-scoped-roles – Mel Aug 07 '16 at 08:58