On the main CanTango wiki there should be a page on how to set up a model with a User having multiple Accounts.
> 2. There are two types of accounts (lead account and support account)
> 3. Users can access specific projects base on the accounts they have
Set up the USer to have multiple accounts via a User.has_many :accounts, Account.belongs_to :user
Cantango will looks for a #current_account method on the User object (the account which the user session is currently on.
If this method call returns an (Account) object it will use this account for authorization (policies).
> 1. Users login with their own credentials (email & password)
The authentication can get the User credentials from the User, no problem. This is just normal Devise or whatever you like.
> 4. Users can access specific projects base on the accounts they have
This is just normal CanTango (CanCan) Authorization policies using current_user from User to get the current Account instance to use to determine authorization policies.
> Lead accounts and support accounts are specific to Projects
I guess this is just a modeling fact. Account.belongs_to :project, Project.has_one :account ?
So the question is… if the role is directly based on the current Account the user is on, or more granular? A combination of Account and role of that account?
In principle you could have a #role on the Account and dynamically change this role and use this attribute for Authorization. You could then set up both a AccountPermits, fx
SupportAccountPermit and LeadAccountPermit and role permits, fx AdminRolePermit, BasicRolePermit.
I also suggest you look into using the cantango-permits gem instead of the cantango 0.9.4.7 version, which is unfortunately not entirely stable.
For the roles part, consider using my old roles gem, fx role_active_record or similar. Not sure the last remaining issues on troles were fixed. Or perhaps use an alternative roles solution and use the cantango config to configure it correctly into cantango.
Hope this helps!
Kristian
Good to hear :) If you get it all configured and working let me know and put it up on github as a working example demo app. Cheers!
You should check out the Ability specs and Ability namespace of the repository
CanTango was designed in order to specifically allow Ability checks outside of the CanCan limitation of doing it on current_user only. Fx to allow rules for multiple devise users and also accounts.
See the examples on the wiki and in the specs and the two dummy apps included.
I think you can do sth like:
admin_can?
admin_account_can?
For you it would be
lead_acount_can?
But you must register the LeadAccount with CanTango for this to work (using tango_account macro)
I also think you can use the CanCan Ability directly (this is what those generated methods do under the covers).
> can :manage, Project do |project|
> project.lead_account.id == current_user.lead_account.id
> end
I don't think you need to specify current_user here. Within the permit, both #user and #permit are available and many other helper method as well (see docs and examples)
There is also a Rules DSL in order to facilitate this kind of complex logic. Can't remember the syntax just now. But you are def. on the right track :)
Sorry, is very late now (midnight) and I have been coding since 8:30 this morning
Good luck!
Kris
gem 'cantango', :path => 'local path to cantango'
And then start debugging using puts or whatever to see what the hell is going on!
Unfortunately you really have to "dive in", in order to get it working for your solution I think.
Good luck!
Kristian
PS: I think you might have a better chance with the newer cantango-permits gem. It is much better tested, well-designed and should be easier to work with. Much of the old documentation still applies in general, however the namespaces have been cleaned up a bit. Go through the specs and see… I'm still working on the store and cache solution for that version however, but they can be added later as plugins.