Class: Ezframe::Auth

Inherits:
Object show all
Defined in:
lib/ezframe/auth.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, account) ⇒ Auth

Returns a new instance of Auth.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ezframe/auth.rb', line 61

def initialize(model, )
  self. = 
  dataset = model.db.dataset(Config[:login_table])
  if .is_a?(Integer)
    @user = dataset.where(id: ).first
  else
    @user = dataset.where(Config[:login_account].to_sym => ).first
  end
  unless @user
    mylog "Auth.initialize: This user does not exist: #{}"
  end
  self.password = @user[:password]
  @user.delete(:password)
end

Class Attribute Details

.modelObject

Returns the value of attribute model.



4
5
6
# File 'lib/ezframe/auth.rb', line 4

def model
  @model
end

.userObject

Returns the value of attribute user.



4
5
6
# File 'lib/ezframe/auth.rb', line 4

def user
  @user
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



59
60
61
# File 'lib/ezframe/auth.rb', line 59

def 
  @account
end

#idObject

Returns the value of attribute id.



59
60
61
# File 'lib/ezframe/auth.rb', line 59

def id
  @id
end

#modelObject

Returns the value of attribute model.



59
60
61
# File 'lib/ezframe/auth.rb', line 59

def model
  @model
end

#passwordObject

Returns the value of attribute password.



59
60
61
# File 'lib/ezframe/auth.rb', line 59

def password
  @password
end

#userObject

Returns the value of attribute user.



59
60
61
# File 'lib/ezframe/auth.rb', line 59

def user
  @user
end

Class Method Details

.authenticate(env, account, pass) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ezframe/auth.rb', line 39

def authenticate(env, , pass)
  model = env["model"]
  raise "model is not initialized" unless model
  @user = model.db.dataset(Config[:login_table]).where(Config[:login_account].to_sym =>  ).first
  if @user
    mylog "Auth: authenticate: user=#{@user.inspect}"
  else
    mylog "authenticate: this user does not exist: #{}"
    return nil
  end
  mylog "env=#{env.inspect}"
  env['rack.session'][:user] = @user[:id]
  password = @user[:password]
  @user.delete(:password)

  return nil if !pass || !password
  !!(password == pass)
end

.get(model, account) ⇒ Object



35
36
37
# File 'lib/ezframe/auth.rb', line 35

def get(model, )
  new(model, )
end

.init_wardenObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ezframe/auth.rb', line 6

def init_warden
  Warden::Manager.serialize_into_session do |auth|
    mylog "serialize_into: #{auth.inspect}"
    auth.user[:id]
  end
  Warden::Manager.serialize_from_session do ||
    mylog "serialize_from: account = #{}"
    inst = Auth.get(env['model'], )
    mylog "inst = #{inst.inspect}"
    inst
  end
  Warden::Strategies.add(:mystrategy) do
    def valid?
      # mylog "valid?"
      params["account"] || params["password"]
    end

    def authenticate!
      mylog "authenticate!: #{params}"
      if Auth.authenticate(env, params["account"], params["password"])
        success!(Auth.get(env['model'], params["account"]))
      else
        env['x-rack.flash'].error = 'ユーザーが登録されていないか、パスワードが違っています。'
        fail!("authenticate failure")
      end
    end
  end 
end