I'm working on a Rails web application, and it's currently being used by some 20 users.
Some parts of the application are only accessible by some users, so we already have a basic authorization framework in place, which I implemented using the acts_as_authenticated plugin.
The users' privileges depend on which department they work in, so for example administration has access to all parts of the application, while accounting only has access to the accounting-related parts, and sales only has access to sales-related parts, etc.
On the other hand, users see the links to actions for which they have unsufficient privileges. For example, those in the sales department see a link to the financial records in the main menu, but when they click on it, nothing happens. This is so because AFAIK there's no efficient way to query user privileges using acts_as_authenticated.
I want to change this in two ways:
I want to introduce more fine-grained authorization. Currently, the authorization is done at the controller level. I want to do this at the action or model level. For example, I want those in the sales department to be able to create and update payments, but not delete them.
I want to be able to query user privileges efficiently, so I can remove unnecessary (and confusing) links from the interface.
What do you think is the most elegant way to implement this?
Rails-specific answers aren't necessary, I just want to know how this should be implemented in a data-driven application.
Finally, here's how it's implemented currently:
def authorized?
current_user.role.foo? or current_user.role.bar?
end
And here's my initial idea, which I think is not the best way to solve this:
+------------+------------+---------+ | department | controller | action | +------------+------------+---------+ | accounting | payments | index | | accounting | payments | new | | accounting | payments | create | | accounting | payments | edit | | accounting | payments | update | | accounting | payments | destroy | | sales | payments | new | | sales | payments | create | | sales | payments | edit | | sales | payments | update | +------------+------------+---------+
or
+------------+----------+-------+--------+------+--------+--------+ | department | model | list | create | read | update | delete | +------------+----------+-------+--------+------+--------+--------+ | accounting | payments | TRUE | TRUE | TRUE | TRUE | TRUE | | sales | payments | FALSE | TRUE | TRUE | TRUE | FALSE | +------------+----------+-------+--------+------+--------+--------+