Module: Card::Auth::Proxy

Included in:
Card::Auth
Defined in:
lib/card/auth/proxy.rb

Overview

mechanism for assuming permissions of another user.

Instance Method Summary collapse

Instance Method Details

#as(given_user) ⇒ Object

operate with the permissions of another "proxy" user



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/card/auth/proxy.rb', line 6

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

#as_bot(&block) ⇒ Object

operate with the permissions of WagnBot (administrator)



27
28
29
# File 'lib/card/auth/proxy.rb', line 27

def as_bot &block
  as Card::WagnBotID, &block
end

#as_cardCard

proxy user card

Returns:



39
40
41
42
43
44
45
# File 'lib/card/auth/proxy.rb', line 39

def as_card
  if @as_card && @as_card.id == as_id
    @as_card
  else
    @as_card = Card[as_id]
  end
end

#as_idInteger

id of proxy user

Returns:

  • (Integer)


33
34
35
# File 'lib/card/auth/proxy.rb', line 33

def as_id
  @as_id || current_id
end

#get_user_id(user) ⇒ Object

TODO:

replace with general mechanism, eg #quick_fetch

get card id from args of unknown type



49
50
51
52
53
54
55
56
# File 'lib/card/auth/proxy.rb', line 49

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