Sha256: 96c50f2f48ec93b54a5dc79c401763fc82e9e705daf35aa38c3aabdf1e9259ff

Contents?: true

Size: 1.32 KB

Versions: 18

Compression:

Stored size: 1.32 KB

Contents

class Card
  module Auth
    # mechanism for assuming permissions of another user.
    module Proxy
      # operate with the permissions of another "proxy" user
      def as given_user
        tmp_id   = @as_id
        tmp_card = @as_card

        @as_id   = get_user_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

      # operate with the permissions of WagnBot (administrator)
      def as_bot &block
        as Card::WagnBotID, &block
      end

      # id of proxy user
      # @return [Integer]
      def as_id
        @as_id || current_id
      end

      # proxy user card
      # @return [Card]
      def as_card
        if @as_card && @as_card.id == as_id
          @as_card
        else
          @as_card = Card[as_id]
        end
      end

      # get card id from args of unknown type
      # @todo replace with general mechanism, eg #quick_fetch
      def get_user_id user
        case user
        when NilClass then nil
        when Card     then user.id
        when Integer   then user
        else Card.fetch_id(user)
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
card-1.101.3 lib/card/auth/proxy.rb
card-1.101.2 lib/card/auth/proxy.rb
card-1.101.1 lib/card/auth/proxy.rb
card-1.101.0 lib/card/auth/proxy.rb
card-1.100.0 lib/card/auth/proxy.rb
card-1.99.6 lib/card/auth/proxy.rb
card-1.99.5 lib/card/auth/proxy.rb
card-1.99.4 lib/card/auth/proxy.rb
card-1.99.3 lib/card/auth/proxy.rb
card-1.99.2 lib/card/auth/proxy.rb
card-1.99.1 lib/card/auth/proxy.rb
card-1.99.0 lib/card/auth/proxy.rb
card-1.98.3 lib/card/auth/proxy.rb
card-1.98.2 lib/card/auth/proxy.rb
card-1.98.1 lib/card/auth/proxy.rb
card-1.98.0 lib/card/auth/proxy.rb
card-1.97.0.1 lib/card/auth/proxy.rb
card-1.97.0 lib/card/auth/proxy.rb