Rails 3 company account with many users, restrict access to data

血红的双手。 提交于 2019-12-04 15:35:57

I'd suggest using CanCan for authorization.

Your Ability model would look something like

can :manage, :isbn do |isbn|
  isbn.account == current_user.account
end

Then you can use conditionals like can? :manage, @post in your Controller/Views.

You should have a look at https://github.com/stffn/declarative_authorization I think that one do exactly as you're asking for (you can limit access to certain records)

so, in your example you could set up the authorization with something like this:

has_permission_on :isbn do 
    to :manage
    if_attribute :account_id => is_in {user.account_ids}
end

Have you looked at XACML? It's a policy-based, declarative framework for authorization. It's also a standard from OASIS.

With XACML you can take any attributes you need (user, resource, action, or context attributes) and express the use case that you have.

Your initial requirement was

users can only access records that were created by themselves or another user with the same account.

In XACML that would become the following policy:

A user can access a record if and only if the record.owner==user.id OR user.accountId==record.accountId

That's all! XACML's architecture defines the policy enforcement point (PEP) which is the component that protects your app/data and sends the authorization request to the policy decision point (PDP). The PEP says: "Can Alice access record #123?" The PDP checks Alice's account Id and the record's account id as well as owner. The PDP eventually returns a "Permit" or a "Deny".

HTH

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!