Skip to content

How to authorize in multi-membership app? #840

Answered by janpeterka
janpeterka asked this question in Q&A
Discussion options

You must be logged in to vote

Ok, so I probably solved this.

I realized I can approach this bit different way, and only focus on switching member before trying to authorize_resource.

So, I came up with this:

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

  # ...

  private

  def switch_member_if_needed
    memberships = [Current.member] + current_user.memberships.excluding(Current.member)

    memberships.each do |membership|
      @current_ability = Ability.new(membership)

      object = instance_variable_get(:"@#{controller_name.singularize}")

      next unless can? :show, object

      Current.member = membership
      set_current_member(Current.member)
      r…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by janpeterka
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant