module Card::Auth::Proxy

mechanism for assuming permissions of another user.

Public Instance Methods

as(given_user) { || ... } click to toggle source

operate with the permissions of another “proxy” user

# File lib/card/auth/proxy.rb, line 6
def as given_user
  tmp_id = @as_id
  tmp_card = @as_card

  @as_id = Card.id given_user
  @as_card = nil
  # we could go ahead and set as_card if given a card...

  @current_id = @as_id if @current_id.nil?

  return unless block_given?

  yield
ensure
  if block_given?
    @as_id = tmp_id
    @as_card = tmp_card
  end
end
as_bot(&block) click to toggle source

operate with the permissions of WagnBot (administrator)

# File lib/card/auth/proxy.rb, line 27
def as_bot &block
  as Card::WagnBotID, &block
end
as_card() click to toggle source

proxy user card @return [Card]

# File lib/card/auth/proxy.rb, line 39
def as_card
  return @as_card if @as_card&.id == as_id

  @as_card = Card[as_id]
end
as_id() click to toggle source

id of proxy user @return [Integer]

# File lib/card/auth/proxy.rb, line 33
def as_id
  @as_id || current_id
end
with(auth_data) { || ... } click to toggle source

@param auth_data [Integer|Hash] user id, user name, or a hash @option auth_data [Integer] current_id @option auth_data [Integer] as_id

# File lib/card/auth/proxy.rb, line 48
def with auth_data
  if auth_data.is_a?(Integer) || auth_data.is_a?(String)
    auth_data = { current_id: Card.id(auth_data) }
  end

  temporarily do
    # resets @as and @as_card
    self.current_id = auth_data[:current_id]
    @as_id = auth_data[:as_id] if auth_data[:as_id]
    yield
  end
end

Private Instance Methods

temporarily() { || ... } click to toggle source
# File lib/card/auth/proxy.rb, line 63
def temporarily
  tmp_current_id = current_id
  tmp_as_id = as_id
  tmp_current_card = @current_card
  tmp_as_card = @as_card
  tmp_current_roles = @current_roles
  yield
ensure
  @current_id = tmp_current_id
  @as_id = tmp_as_id
  @current = tmp_current_card
  @as_card = tmp_as_card
  @current_roles = tmp_current_roles
end