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
-
#current ⇒ Card
current accounted card (must have +*account).
-
#current_id ⇒ Integer
id of current user card.
-
#current_id=(card_id) ⇒ Object
set the id of the current user.
- #current_roles ⇒ Object
- #database ⇒ Object
-
#find_account_by(fieldcode, value) ⇒ +*account card?
general pattern for finding +*account card based on field cards.
-
#find_account_by_api_key(api_key) ⇒ +*account card?
find *account card by *api card.
-
#find_account_by_email(email) ⇒ +*account card?
find *account card by *email card.
- #serialize ⇒ Object
-
#session ⇒ Object
get session object from Env return [Session].
- #session_user ⇒ Object
- #session_user_key ⇒ Object
- #set_session_user(card_id) ⇒ Object
-
#signed_in? ⇒ true/false
current user is not anonymous.
-
#signin(cardish) ⇒ Object
set current user in process and session.
-
#signin_with(opts = {}) ⇒ Object
set current from token, api_key, or session.
-
#signin_with_api_key(api_key) ⇒ Object
set the current user based on api_key.
-
#signin_with_session ⇒ Object
get :user id from session and set Auth.current_id.
-
#signin_with_token(token) ⇒ Object
set the current user based on token.
- #with(auth_data) ⇒ Object
Instance Method Details
#current ⇒ Card
current accounted card (must have +*account)
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_id ⇒ Integer
id of current user card.
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_roles ⇒ Object
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 |
#database ⇒ Object
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
126 127 128 129 130 131 132 |
# File 'lib/card/auth/current.rb', line 126 def find_account_by 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
111 112 113 |
# File 'lib/card/auth/current.rb', line 111 def find_account_by_api_key api_key find_account_by :api_key, api_key.strip end |
#find_account_by_email(email) ⇒ +*account card?
find *account card by *email card
118 119 120 |
# File 'lib/card/auth/current.rb', line 118 def find_account_by_email email find_account_by :email, email.strip.downcase end |
#serialize ⇒ Object
39 40 41 |
# File 'lib/card/auth/current.rb', line 39 def serialize { as_id: as_id, current_id: current_id } end |
#session ⇒ Object
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_user ⇒ Object
134 135 136 |
# File 'lib/card/auth/current.rb', line 134 def session_user session[session_user_key] end |
#session_user_key ⇒ Object
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
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 signin cardish signin_id = Card.id(cardish) || Card::AnonymousID self.current_id = signin_id set_session_user signin_id 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 signin_with opts={} if opts[:token] signin_with_token opts[:token] elsif opts[:api_key] signin_with_api_key opts[:api_key] else signin_with_session 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 signin_with_api_key api_key account = find_account_by_api_key api_key unless account&.validate_api_key! api_key raise Card::Error::PermissionDenied, "API key authentication failed" end signin account.left_id end |
#signin_with_session ⇒ Object
get :user id from session and set Auth.current_id
103 104 105 106 |
# File 'lib/card/auth/current.rb', line 103 def signin_with_session card_id = session_user signin(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 signin_with_token token payload = Token.validate! token signin payload[:anonymous] ? Card::AnonymousID : payload[:user_id] end |
#with(auth_data) ⇒ Object
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 |