I have a system where a User
can be associated with many Portals
, however a user's permissions may differ between portals.
For example, a user might be able to see unpublished posts in one portal, but not in another portal.
For methods like show?
, I can grab the portal off the record.
def show?
portal = record.portal
# logic to check whether, for this particular portal,
# this user has permission to view this record
end
However that solution doesn't work for policy scopes.
Is there any way I can, say, pass in the portal to policy_scope
method in the controller?
One solution I've seen around the place is to set a (temporary) attribute against the user, so that policy methods can use it. e.g.
# model
class User < ActiveRecord::Base
attr_accessor :current_portal
...
end
# controller
posts = portal.posts
current_user.current_portal = current_portal
policy_scope posts
# policy Scope
def resolve
portal = user.current_portal
# logic to scope these records by user's portal permissions
end
However this seems like a workaround, and I can definitely think of other scenarios where I'd like to be able to give authorisation logic more context as well, and I don't want this workaround to become a bad habit.
Does anyone have any suggestions?