Module: Sinatra::SimpleAuthentication::Controllers::Session

Defined in:
lib/sinatra/simple_authentication/controllers/session.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.base_pathObject

Returns the value of attribute base_path.



12
13
14
# File 'lib/sinatra/simple_authentication/controllers/session.rb', line 12

def base_path
  @base_path
end

.model_classObject

Returns the value of attribute model_class.



12
13
14
# File 'lib/sinatra/simple_authentication/controllers/session.rb', line 12

def model_class
  @model_class
end

Class Method Details

.load_user_classObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sinatra/simple_authentication/controllers/session.rb', line 114

def self.load_user_class
  if Object.const_defined?("DataMapper")
    orm_directory_name = "datamapper"
    require File.join(self.base_path, "models/datamapper/adapter")
    base_module = Sinatra::SimpleAuthentication::Models::DataMapper
  elsif Object.const_defined?("ActiveRecord")
    orm_directory_name = "active_record"
    require File.join(self.base_path, "models/active_record/adapter")
    base_module = Sinatra::SimpleAuthentication::Models::ActiveRecord
  else
    throw "Not DataMapper nor ActiveRecord connection detected."
  end

  if !base_module::Adapter.model_class
    require File.join(self.base_path, "models/#{orm_directory_name}/user")
  end

  base_module::Adapter.model_class
end

.registered(app) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sinatra/simple_authentication/controllers/session.rb', line 18

def self.registered(app)
  require File.join(self.base_path, "controllers/defaults")
  #load_user_class will set the corresponding model class
  Session.model_class = self.load_user_class
  app.helpers Helpers
  app = Defaults.setDefaults(app)
  Session.model_class.settings = app.settings
  Session.model_class.set_validation_rules

  app.get "/signup" do
    @password_confirmation = settings.use_password_confirmation
    @user = Session.model_class.new
    @actionUrl = ""
    #Try to load an user view otherwise load the default
    begin
      haml :"/signup"
    rescue
      haml get_view_as_string("signup.haml")
    end
  end

  app.post "/signup" do
    @user = Session.model_class.new
    #Support custom user models with differents attributs (But always have to receive email and password)
    params.each do |k, v|
      @user.respond_to?(k)
      @user.send(k + "=", v)
    end

    if @user.save
      redirect '/'
    else
      @password_confirmation = settings.use_password_confirmation
      if Rack.const_defined?('Flash')
        if Object.const_defined?("DataMapper")
          flash[:error] = @user.errors.full_messages
        else
          flash[:error] = @user.errors.map {|i, m| m}
        end
      end
      #Try to load a local user view otherwise load the default
      begin
        haml :"/signup"
      rescue
        haml get_view_as_string("signup.haml")
      end
    end
  end

  app.get "/login" do
    if !!session[:user]
      redirect '/'
    else
      #Try to load a local user view otherwise load the default
      begin
        haml :"/login"
      rescue
        haml get_view_as_string("login.haml")
      end
    end
  end

  app.post "/login" do
    if user = Session.model_class.find_user(:email => params[:email])
      if user.authenticate(params[:password])
        session[:user] = user.id
        if Rack.const_defined?('Flash')
          flash[:notice] = [app.settings.]
        end

        if !!session[:return_to]
          redirect_url = session[:return_to]
          session[:return_to] = false
          redirect redirect_url
        else
          redirect '/'
        end
      else
        if Rack.const_defined?('Flash')
          flash[:error] = [app.settings.]
        end
      end
    else
      if Rack.const_defined?('Flash')
        flash[:error] = [app.settings.]
      end
    end
    redirect '/login'
  end

  app.get '/logout' do
    session[:user] = nil
    redirect '/'
  end
end