Module: Card::Auth::Current

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

Overview

methods for setting current account

Instance Method Summary collapse

Instance Method Details

#currentCard

current accounted card (must have +*account)

Returns:



26
27
28
29
30
31
32
# File 'lib/card/auth/current.rb', line 26

def current
  if @current && @current.id == current_id
    @current
  else
    @current = Card[current_id]
  end
end

#current_idInteger

id of current user card.

Returns:

  • (Integer)


20
21
22
# File 'lib/card/auth/current.rb', line 20

def current_id
  @current_id ||= Card::AnonymousID
end

#current_id=(card_id) ⇒ Object

set the id of the current user.



151
152
153
154
155
# File 'lib/card/auth/current.rb', line 151

def current_id= card_id
  @current = @as_id = @as_card = @current_roles = nil
  card_id = card_id.to_i if card_id.present?
  @current_id = card_id
end

#current_rolesObject



34
35
36
37
# File 'lib/card/auth/current.rb', line 34

def current_roles
  @current_roles ||= [Card.fetch_name(:anyone_signed_in),
                      current.fetch(:roles)&.item_names].flatten.compact
end

#databaseObject



146
147
148
# File 'lib/card/auth/current.rb', line 146

def database
  Rails.configuration.database_configuration.dig Rails.env, "database"
end

#find_account_by(fieldcode, value) ⇒ +*account card?

general pattern for finding +*account card based on field cards

Parameters:

  • fieldcode (Symbol)

    code of account field

  • value (String)

    content of field

Returns:

  • (+*account card, nil)


126
127
128
129
130
131
132
# File 'lib/card/auth/current.rb', line 126

def  fieldcode, value
  Auth.as_bot do
    Card.search({ right_id: Card::AccountID,
                  right_plus: [Card::Codename.id(fieldcode), { content: value }] },
                "find +:account with +#{fieldcode} (#{value})").first
  end
end

#find_account_by_api_key(api_key) ⇒ +*account card?

find *account card by *api card

Parameters:

  • api_key (String)

Returns:

  • (+*account card, nil)


111
112
113
# File 'lib/card/auth/current.rb', line 111

def  api_key
   :api_key, api_key.strip
end

#find_account_by_email(email) ⇒ +*account card?

find *account card by *email card

Parameters:

  • email (String)

Returns:

  • (+*account card, nil)


118
119
120
# File 'lib/card/auth/current.rb', line 118

def  email
   :email, email.strip.downcase
end

#serializeObject



39
40
41
# File 'lib/card/auth/current.rb', line 39

def serialize
  { as_id: as_id, current_id: current_id }
end

#sessionObject

get session object from Env return [Session]



71
72
73
# File 'lib/card/auth/current.rb', line 71

def session
  Card::Env.session
end

#session_userObject



134
135
136
# File 'lib/card/auth/current.rb', line 134

def session_user
  session[session_user_key]
end

#session_user_keyObject



142
143
144
# File 'lib/card/auth/current.rb', line 142

def session_user_key
  "user_#{database.underscore}".to_sym
end

#set_session_user(card_id) ⇒ Object



138
139
140
# File 'lib/card/auth/current.rb', line 138

def set_session_user card_id
  session[session_user_key] = card_id
end

#signed_in?true/false

current user is not anonymous

Returns:

  • (true/false)


14
15
16
# File 'lib/card/auth/current.rb', line 14

def signed_in?
  current_id != Card::AnonymousID
end

#signin(cardish) ⇒ Object

set current user in process and session



6
7
8
9
10
# File 'lib/card/auth/current.rb', line 6

def  cardish
   = Card.id(cardish) || Card::AnonymousID
  self.current_id = 
  set_session_user 
end

#signin_with(opts = {}) ⇒ Object

set current from token, api_key, or session



76
77
78
79
80
81
82
83
84
# File 'lib/card/auth/current.rb', line 76

def  opts={}
  if opts[:token]
     opts[:token]
  elsif opts[:api_key]
     opts[:api_key]
  else
    
  end
end

#signin_with_api_key(api_key) ⇒ Object

set the current user based on api_key



93
94
95
96
97
98
99
100
# File 'lib/card/auth/current.rb', line 93

def  api_key
   =  api_key
  unless &.validate_api_key! api_key
    raise Card::Error::PermissionDenied, "API key authentication failed"
  end

   .left_id
end

#signin_with_sessionObject

get :user id from session and set Auth.current_id



103
104
105
106
# File 'lib/card/auth/current.rb', line 103

def 
  card_id = session_user
  (card_id && Card.exists?(card_id) ? card_id : nil)
end

#signin_with_token(token) ⇒ Object

set the current user based on token



87
88
89
90
# File 'lib/card/auth/current.rb', line 87

def  token
  payload = Token.validate! token
   payload[:anonymous] ? Card::AnonymousID : payload[:user_id]
end

#with(auth_data) ⇒ Object

Parameters:

  • auth_data (Integer|Hash)

    user id, user name, or a hash

Options Hash (auth_data):

  • current_id (Integer)
  • as_id (Integer)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/card/auth/current.rb', line 46

def with auth_data
  if auth_data.is_a?(Integer) || auth_data.is_a?(String)
    auth_data = { current_id: Card.id(auth_data) }
  end

  tmp_current_id = current_id
  tmp_as_id = as_id
  tmp_current = @current
  tmp_as_card = @as_card
  tmp_current_roles = @current_roles

  # resets @as and @as_card
  self.current_id = auth_data[:current_id]
  @as_id = auth_data[:as_id] if auth_data[:as_id]
  yield
ensure
  @current_id = tmp_current_id
  @as_id = tmp_as_id
  @current = tmp_current
  @as_card = tmp_as_card
  @current_roles = tmp_current_roles
end