Module: Card::Auth

Defined in:
lib/card/auth.rb

Constant Summary collapse

SETUP_COMPLETED_KEY =
'SETUP_COMPLETED'.freeze
DEFAULT_RECAPTCHA_SETTINGS =
{
  recaptcha_public_key: '6LeoHfESAAAAAN1NdQeYHREq4jTSQhu1foEzv6KC',
  recaptcha_private_key: '6LeoHfESAAAAAHLZpn7ijrO4_KGLEr2nGL4qjjis'
}.freeze
@@as_card =
@@as_id = @@current_id = @@current = nil
@@simulating_setup_need =
nil

Class Method Summary collapse

Class Method Details

.[](email) ⇒ Object

find account by email



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

def [] email
  email = email.strip.downcase
  Auth.as_bot do
    Card.search(
      { right_id: Card::AccountID,
        right_plus: [{ id: Card::EmailID }, { content: email }]
        }, "find +*account for email(#{email})"
    ).first
  end
end

.always_ok?Boolean

Returns:

  • (Boolean)


194
195
196
197
198
# File 'lib/card/auth.rb', line 194

def always_ok?
  usr_id = as_id
  return false unless usr_id
  always_ok_usr_id? usr_id
end

.always_ok_usr_id?(usr_id) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/card/auth.rb', line 200

def always_ok_usr_id? usr_id
  return true if usr_id == Card::WagnBotID # cannot disable

  always = Card.cache.read('ALWAYS') || {}
  # warn(Rails.logger.warn "Auth.always_ok? #{usr_id}")
  if always[usr_id].nil?
    always = always.dup if always.frozen?
    always[usr_id] =
      !!Card[usr_id].all_roles.find { |r| r == Card::AdministratorID }
    # warn(Rails.logger.warn "update always hash #{always[usr_id]},
    # #{always.inspect}")
    Card.cache.write 'ALWAYS', always
  end
  # warn Rails.logger.warn("aok? #{usr_id}, #{always[usr_id]}")
  always[usr_id]
end

.among?(authzed) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/card/auth.rb', line 155

def among? authzed
  as_card.among? authzed
end

.as(given_user) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/card/auth.rb', line 132

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



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

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

.as_cardObject



163
164
165
166
167
168
169
# File 'lib/card/auth.rb', line 163

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

.as_idObject



159
160
161
# File 'lib/card/auth.rb', line 159

def as_id
  @@as_id || current_id
end

.authenticate(email, password) ⇒ Object

Authenticates a user by their login name and unencrypted password.



18
19
20
21
22
23
24
25
26
# File 'lib/card/auth.rb', line 18

def authenticate email, password
   = Auth[email]
  case
  when !                                 then nil
  when !.active?                         then nil
  when Card.config.no_authentication            then 
  when password_valid?(, password.strip) then 
  end
end

.createable_typesObject

PERMISSIONS



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/card/auth.rb', line 219

def createable_types
  type_names =
    Auth.as_bot do
      Card.search(
        { type: Card::CardtypeID, return: :name,
          not: { codename: ['in'] + Card.config.non_createable_types } },
        'find createable types'
      )
    end

  type_names.select do |name|
    Card.new(type: name).ok? :create
  end.sort
end

.currentObject



110
111
112
113
114
115
116
# File 'lib/card/auth.rb', line 110

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

.current_idObject



106
107
108
# File 'lib/card/auth.rb', line 106

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

.current_id=(card_id) ⇒ Object



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

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

.encrypt(password, salt) ⇒ Object

Encrypts some data with the salt.



69
70
71
# File 'lib/card/auth.rb', line 69

def encrypt password, salt
  Digest::SHA1.hexdigest "#{salt}--#{password}--"
end

.find_by_token(token) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/card/auth.rb', line 58

def find_by_token token
  Auth.as_bot do
    Card.search(
      { right_id: Card::AccountID,
        right_plus: [{ id: Card::TokenID }, { content: token.strip }]
        }, 'find +*account card by token'
    ).first
  end
end

.get_user_id(user) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/card/auth.rb', line 124

def get_user_id user
  case user
  when NilClass then nil
  when Card     then user.id
  else Card.fetch_id(user)
  end
end

.instant_account_activationObject



187
188
189
190
191
192
# File 'lib/card/auth.rb', line 187

def 
  simulate_setup_need!
  yield
ensure
  simulate_setup_need! false
end

.needs_setup?Boolean

Returns:

  • (Boolean)


175
176
177
178
179
180
181
# File 'lib/card/auth.rb', line 175

def needs_setup?
  @@simulating_setup_need || (
    !Card.cache.read(SETUP_COMPLETED_KEY) &&
    !Card.cache.write(SETUP_COMPLETED_KEY,  > 2)
  )
  # every deck starts with WagnBot and Anonymous account
end

.password_valid?(account, password) ⇒ Boolean

Returns:

  • (Boolean)


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

def password_valid? , password
  .password == encrypt(password, .salt)
end

.sessionObject



90
91
92
# File 'lib/card/auth.rb', line 90

def session
  Card::Env[:session]
end

.set_current_from_mark(mark) ⇒ Object



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

def set_current_from_mark mark
  self.current_id =
    if mark.to_s =~ /@/
       = Auth[mark.downcase]
       && .active? ? .left_id : Card::AnonymousID
    else
      mark
    end
end

.set_current_from_sessionObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/card/auth.rb', line 94

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/card/auth.rb', line 32

def set_current_from_token token, current=nil
   = find_by_token token
  if  && .validate_token!(token)
    unless current && always_ok_usr_id?(.left_id)
      current = .left_id
    end
    set_current_from_mark 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?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/card/auth.rb', line 171

def signed_in?
  current_id != Card::AnonymousID
end

.signin(signin_id) ⇒ Object



85
86
87
88
# File 'lib/card/auth.rb', line 85

def  
  self.current_id = 
  session[:user] =  if session
end

.simulate_setup_need!(mode = true) ⇒ Object



183
184
185
# File 'lib/card/auth.rb', line 183

def simulate_setup_need! mode=true
  @@simulating_setup_need = mode
end