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=(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.
-
#find_account_by(fieldname, field_id, value) ⇒ +*account card?
general pattern for finding +*account card based on field cards.
-
#find_account_by_email(email) ⇒ +*account card?
find +*account card by +*email card.
-
#find_account_by_token(token) ⇒ +*account card?
find +*account card by +*token card.
- #serialize ⇒ Object
-
#session ⇒ Object
get session object from Env return [Session].
-
#set_current(token, current) ⇒ Object
set current from token or session.
-
#set_current_from_session ⇒ Object
get :user id from session and set Auth.current_id.
-
#set_current_from_token(token, current = nil) ⇒ Object
set the current user based on token.
-
#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
#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 = nil card_id = card_id.to_i if card_id.present? @current_id = card_id end |
#find_account_by(fieldname, field_id, value) ⇒ +*account card?
general pattern for finding +*account card based on field cards
151 152 153 154 155 156 157 158 |
# File 'lib/card/auth/current.rb', line 151 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_email(email) ⇒ +*account card?
find +*account card by +*email card
142 143 144 |
# File 'lib/card/auth/current.rb', line 142 def find_account_by_email email find_account_by "email", Card::EmailID, email.strip.downcase end |
#find_account_by_token(token) ⇒ +*account card?
find +*account card by +*token card
135 136 137 |
# File 'lib/card/auth/current.rb', line 135 def find_account_by_token token find_account_by "token", Card::TokenID, token.strip end |
#serialize ⇒ Object
52 53 54 |
# File 'lib/card/auth/current.rb', line 52 def serialize { as_id: as_id, current_id: current_id } end |
#session ⇒ Object
get session object from Env return [Session]
85 86 87 |
# File 'lib/card/auth/current.rb', line 85 def session Card::Env[:session] end |
#set_current(token, current) ⇒ Object
set current from token or session
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/card/auth/current.rb', line 90 def set_current token, current if token ok = set_current_from_token token, current raise Card::Error::Oops, "token authentication failed" unless ok # arguably should be PermissionDenied; that requires a card object, # and that's not loaded yet. else set_current_from_session end end |
#set_current_from_session ⇒ Object
get :user id from session and set Auth.current_id
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/card/auth/current.rb', line 102 def set_current_from_session self.current_id = if session if (card_id = session[:user]) && Card.exists?(card_id) card_id else session[:user] = nil end end current_id end |
#set_current_from_token(token, current = nil) ⇒ Object
set the current user based on token
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/card/auth/current.rb', line 115 def set_current_from_token token, current=nil account = find_account_by_token token if account && account.validate_token!(token) unless current && always_ok_usr_id?(account.left_id) # can override current only if admin current = account.left_id end self.current = current elsif Env.params[:live_token] true # Used for activations and resets. # Continue as anonymous and address problem later else false end 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 session[:user] = signin_id if session end |
#with(auth_data) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/card/auth/current.rb', line 59 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 # 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 end |