Module: Card::Auth

Defined in:
lib/card/auth.rb

Constant Summary collapse

NON_CREATEABLE_TYPES =

NEED API

%w{ signup setting set }
NEED_SETUP_KEY =
'NEED_SETUP'
@@as_card =
@@as_id = @@current_id = @@current = @@simulating_setup_need = nil

Class Method Summary collapse

Class Method Details

.[](email) ⇒ Object

find accounted by email



35
36
37
38
39
40
41
42
# File 'lib/card/auth.rb', line 35

def [] email
  Auth.as_bot do
    Card.search( :right_plus=>[
      {:id=>Card::AccountID},
      {:right_plus=>[{:id=>Card::EmailID},{ :content=>email.strip.downcase }]}
    ]).first
  end
end

.always_ok?Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/card/auth.rb', line 145

def always_ok?
  #warn Rails.logger.warn("aok? #{as_id}, #{as_id&&Card[as_id].id}")
  return false unless usr_id = as_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.detect{|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)


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

def among? authzed
  as_card.among? authzed
end

.as(given_user) ⇒ Object



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

def as given_user
  tmp_id, tmp_card = @@as_id, @@as_card
  @@as_id, @@as_card = get_user_id( given_user ), nil  # we could go ahead and set as_card if given a card...

  @@current_id = @@as_id if @@current_id.nil?

  if block_given?
    value = yield
    @@as_id, @@as_card = tmp_id, tmp_card
    return value
  else
    #fail "BLOCK REQUIRED with Card#as"
  end
end

.as_bot(&block) ⇒ Object



108
109
110
# File 'lib/card/auth.rb', line 108

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

.as_cardObject



120
121
122
123
124
125
126
# File 'lib/card/auth.rb', line 120

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

.as_idObject



116
117
118
# File 'lib/card/auth.rb', line 116

def as_id
  @@as_id || current_id
end

.authenticate(email, password) ⇒ Object

Authenticates a user by their login name and unencrypted password.



15
16
17
18
19
20
21
22
# File 'lib/card/auth.rb', line 15

def authenticate email, password
  accounted = Auth[ email ]
  if accounted and  = accounted. and .active?
    if Card.config.no_authentication or password_authenticated?( , password.strip )
      accounted.id
    end
  end
end

.createable_typesObject



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

def createable_types
  type_names = Auth.as_bot do
    Card.search :type=>Card::CardtypeID, :return=>:name, :not => { :codename => ['in'] + NON_CREATEABLE_TYPES }
  end
  type_names.reject do |name|
    !Card.new( :type=>name ).ok? :create
  end.sort
end

.currentObject



69
70
71
72
73
74
75
# File 'lib/card/auth.rb', line 69

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

.current_idObject



65
66
67
# File 'lib/card/auth.rb', line 65

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

.current_id=(card_id) ⇒ Object



77
78
79
80
# File 'lib/card/auth.rb', line 77

def current_id= card_id
  @@current = @@as_id = @@as_card = nil
  @@current_id = card_id
end

.encrypt(password, salt) ⇒ Object

Encrypts some data with the salt.



30
31
32
# File 'lib/card/auth.rb', line 30

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

.get_user_id(user) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/card/auth.rb', line 82

def get_user_id user
  case user
  when NilClass;   nil
  when Card    ;   user.id
  when Integer ;   user
  else
    user = user.to_s
    Card::Codename[user] or (cd=Card[user] and cd.id)
  end
end

.needs_setup?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
# File 'lib/card/auth.rb', line 132

def needs_setup?
  test = Card.cache.read NEED_SETUP_KEY
  !test.nil? ? test : begin
    @@simulating_setup_need or Card.cache.write( NEED_SETUP_KEY, ( < 3) ) # 3, because
  end
end

.password_authenticated?(account, password) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/card/auth.rb', line 24

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

.sessionObject



49
50
51
# File 'lib/card/auth.rb', line 49

def session
  Card::Env[:session]
end

.set_current_from_sessionObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/card/auth.rb', line 53

def set_current_from_session
  self.current_id =
    if session
      if card_id=session[:user] and Card.exists? card_id
        card_id
      else
        session[:user] = nil
      end
    end
  current_id
end

.signed_in?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/card/auth.rb', line 128

def signed_in?
  current_id != Card::AnonymousID
end

.signin(signin_id) ⇒ Object



44
45
46
47
# File 'lib/card/auth.rb', line 44

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

.simulate_setup_need!(mode = true) ⇒ Object



139
140
141
142
# File 'lib/card/auth.rb', line 139

def simulate_setup_need! mode=true
  @@simulating_setup_need = mode
  Card.cache.write NEED_SETUP_KEY, nil
end