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
- #clear_current_roles ⇒ Object
-
#current ⇒ Card
current accounted card (must have +*account).
-
#current=(mark) ⇒ Integer
set current user from email or id.
-
#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(fieldname, field_id, 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.
- #no_special_roles? ⇒ Boolean
- #serialize ⇒ Object
-
#session ⇒ Object
get session object from Env return [Session].
- #session_user ⇒ Object
- #session_user_key ⇒ Object
-
#set_current(opts = {}) ⇒ Object
set current from token or session.
-
#set_current_from_api_key(api_key) ⇒ Object
set the current user based on api_key.
-
#set_current_from_session ⇒ Object
get :user id from session and set Auth.current_id.
-
#set_current_from_token(token) ⇒ Object
set the current user based on token.
- #set_session_user(card_id) ⇒ Object
-
#signed_in? ⇒ true/false
current user is not anonymous.
-
#signin(signin_id) ⇒ Object
set current user in process and session.
- #with(auth_data) ⇒ Object
Instance Method Details
#clear_current_roles ⇒ Object
62 63 64 |
# File 'lib/card/auth/current.rb', line 62 def clear_current_roles @current_roles = nil end |
#current ⇒ Card
current accounted card (must have +*account)
25 26 27 28 29 30 31 |
# File 'lib/card/auth/current.rb', line 25 def current if @current && @current.id == current_id @current else @current = Card[current_id] end end |
#current=(mark) ⇒ Integer
set current user from email or id
42 43 44 45 46 47 48 49 50 |
# File 'lib/card/auth/current.rb', line 42 def current= mark self.current_id = if mark.to_s =~ /@/ account = Auth.find_account_by_email mark account && account.active? ? account.left_id : Card::AnonymousID else mark end end |
#current_id ⇒ Integer
id of current user card.
19 20 21 |
# File 'lib/card/auth/current.rb', line 19 def current_id @current_id ||= Card::AnonymousID end |
#current_id=(card_id) ⇒ Object
set the id of the current user.
34 35 36 37 38 |
# File 'lib/card/auth/current.rb', line 34 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
52 53 54 55 |
# File 'lib/card/auth/current.rb', line 52 def current_roles @current_roles ||= [Card.fetch_name(:anyone_signed_in), current.fetch(trait: :roles)&.item_names].flatten.compact end |
#database ⇒ Object
182 183 184 |
# File 'lib/card/auth/current.rb', line 182 def database Rails.configuration.database_configuration.dig Rails.env, "database" end |
#find_account_by(fieldname, field_id, value) ⇒ +*account card?
general pattern for finding +*account card based on field cards
161 162 163 164 165 166 167 168 |
# File 'lib/card/auth/current.rb', line 161 def find_account_by fieldname, field_id, value Auth.as_bot do Card.search({ right_id: Card::AccountID, right_plus: [{ id: field_id }, { content: value }] }, "find +:account for #{fieldname} (#{value})").first end end |
#find_account_by_api_key(api_key) ⇒ +*account card?
find +*account card by +*api card
145 146 147 |
# File 'lib/card/auth/current.rb', line 145 def find_account_by_api_key api_key find_account_by "api_key", Card::ApiKeyID, api_key.strip end |
#find_account_by_email(email) ⇒ +*account card?
find +*account card by +*email card
152 153 154 |
# File 'lib/card/auth/current.rb', line 152 def find_account_by_email email find_account_by "email", Card::EmailID, email.strip.downcase end |
#no_special_roles? ⇒ Boolean
57 58 59 60 |
# File 'lib/card/auth/current.rb', line 57 def no_special_roles? Auth.current_roles.size == 1 # && # Auth.current_roles.first == Card.fetch_name(:anyone_signed_in) end |
#serialize ⇒ Object
66 67 68 |
# File 'lib/card/auth/current.rb', line 66 def serialize { as_id: as_id, current_id: current_id } end |
#session ⇒ Object
get session object from Env return [Session]
101 102 103 |
# File 'lib/card/auth/current.rb', line 101 def session Card::Env.session end |
#session_user ⇒ Object
170 171 172 |
# File 'lib/card/auth/current.rb', line 170 def session_user session[session_user_key] end |
#session_user_key ⇒ Object
178 179 180 |
# File 'lib/card/auth/current.rb', line 178 def session_user_key "user_#{database.underscore}".to_sym end |
#set_current(opts = {}) ⇒ Object
set current from token or session
106 107 108 109 110 111 112 113 114 |
# File 'lib/card/auth/current.rb', line 106 def set_current opts={} if opts[:token] set_current_from_token opts[:token] elsif opts[:api_key] set_current_from_api_key opts[:api_key] else set_current_from_session end end |
#set_current_from_api_key(api_key) ⇒ Object
set the current user based on api_key
123 124 125 126 127 128 129 130 |
# File 'lib/card/auth/current.rb', line 123 def set_current_from_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 self.current = account.left_id end |
#set_current_from_session ⇒ Object
get :user id from session and set Auth.current_id
133 134 135 136 137 138 139 140 |
# File 'lib/card/auth/current.rb', line 133 def set_current_from_session self.current_id = if (card_id = session_user) && Card.exists?(card_id) card_id else set_session_user Card::AnonymousID end end |
#set_current_from_token(token) ⇒ Object
set the current user based on token
117 118 119 120 |
# File 'lib/card/auth/current.rb', line 117 def set_current_from_token token payload = Token.validate! token self.current_id = payload[:anonymous] ? Card::AnonymousID : payload[:user_id] end |
#set_session_user(card_id) ⇒ Object
174 175 176 |
# File 'lib/card/auth/current.rb', line 174 def set_session_user card_id session[session_user_key] = card_id end |
#signed_in? ⇒ true/false
current user is not anonymous
13 14 15 |
# File 'lib/card/auth/current.rb', line 13 def signed_in? current_id != Card::AnonymousID end |
#signin(signin_id) ⇒ Object
set current user in process and session
6 7 8 9 |
# File 'lib/card/auth/current.rb', line 6 def signin signin_id self.current_id = signin_id set_session_user signin_id end |
#with(auth_data) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/card/auth/current.rb', line 73 def with auth_data case auth_data when Integer auth_data = { current_id: auth_data } when String auth_data = { current_id: Card.fetch_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 |