Class: Marty::ApplicationController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- Marty::ApplicationController
show all
- Defined in:
- app/controllers/marty/application_controller.rb
Instance Method Summary
collapse
Instance Method Details
#failed_authentication(login) ⇒ Object
120
121
122
123
|
# File 'app/controllers/marty/application_controller.rb', line 120
def failed_authentication(login)
logger.info("Failed authentication for '#{login}' " +
"from #{request.remote_ip} at #{Time.zone.now.utc}")
end
|
#find_current_user ⇒ Object
Returns the current user or nil if no user is logged in
67
68
69
70
71
72
73
74
75
76
|
# File 'app/controllers/marty/application_controller.rb', line 67
def find_current_user
user_id = session[:user_id]
if user_id
user = Marty::User.active.find(user_id) rescue nil
else
user = try_to_autologin
end
user
end
|
#get_conf ⇒ Object
17
18
19
|
# File 'app/controllers/marty/application_controller.rb', line 17
def get_conf
Rails.configuration.marty
end
|
#handle_unverified_request ⇒ Object
Marty’s ApplicationController is based on Redmine’s implementation.
9
10
11
12
|
# File 'app/controllers/marty/application_controller.rb', line 9
def handle_unverified_request
super
cookies.delete(:autologin)
end
|
#password_authentication ⇒ Object
113
114
115
116
117
118
|
# File 'app/controllers/marty/application_controller.rb', line 113
def password_authentication
user = Marty::User.try_to_login(params[:username], params[:password])
user.nil? ? failed_authentication(params[:username] || 'nil username') :
successful_authentication(user)
end
|
#reset_signed_cookies ⇒ Object
135
136
137
|
# File 'app/controllers/marty/application_controller.rb', line 135
def reset_signed_cookies
cookies.signed[:user_id] = nil
end
|
#session_expiration ⇒ Object
21
22
23
24
25
26
27
28
29
30
|
# File 'app/controllers/marty/application_controller.rb', line 21
def session_expiration
if session[:user_id]
if session_expired? && !try_to_autologin
reset_session
reset_signed_cookies
else
session[:atime] = Time.zone.now.utc.to_i
end
end
end
|
#session_expired? ⇒ Boolean
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'app/controllers/marty/application_controller.rb', line 32
def session_expired?
session_lifetime, session_timeout =
get_conf.session_lifetime, get_conf.session_timeout
if session_lifetime
return true unless session[:ctime] &&
(Time.zone.now.utc.to_i -
session[:ctime].to_i <= session_lifetime.to_i * 60)
end
if session_timeout
return true unless session[:atime] &&
(Time.zone.now.utc.to_i - session[:atime].to_i <= session_timeout.to_i * 60)
end
false
end
|
#set_signed_cookies ⇒ Object
131
132
133
|
# File 'app/controllers/marty/application_controller.rb', line 131
def set_signed_cookies
cookies.signed[:user_id] = session[:user_id]
end
|
#set_user(user) ⇒ Object
92
93
94
95
96
97
98
99
100
101
|
# File 'app/controllers/marty/application_controller.rb', line 92
def set_user(user)
reset_session
reset_signed_cookies
if user && user.is_a?(Marty::User)
Marty::User.current = user
start_user_session(user)
else
Marty::User.current = nil
end
end
|
#start_user_session(user) ⇒ Object
50
51
52
53
54
55
56
|
# File 'app/controllers/marty/application_controller.rb', line 50
def start_user_session(user)
session[:user_id] = user.id
session[:ctime] = Time.zone.now.utc.to_i
session[:atime] = Time.zone.now.utc.to_i
set_signed_cookies
end
|
#successful_authentication(user) ⇒ Object
125
126
127
128
129
|
# File 'app/controllers/marty/application_controller.rb', line 125
def successful_authentication(user)
logger.info("Successful authentication for '#{user.login}' " +
"from #{request.remote_ip} at #{Time.zone.now.utc}")
set_user(user)
end
|
#toggle_dark_mode ⇒ Object
139
140
141
|
# File 'app/controllers/marty/application_controller.rb', line 139
def toggle_dark_mode
cookies[:dark_mode] = cookies[:dark_mode] != 'true'
end
|
#try_to_autologin ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'app/controllers/marty/application_controller.rb', line 78
def try_to_autologin
if cookies[:autologin] && get_conf.autologin
user = Marty::User.try_to_autologin(cookies[:autologin])
if user
reset_session
reset_signed_cookies
start_user_session(user)
end
user
end
end
|
#user_setup ⇒ Object
58
59
60
61
62
63
64
|
# File 'app/controllers/marty/application_controller.rb', line 58
def user_setup
user = Marty::User.current = find_current_user
logger.info(" Current user: #{user.login} (id=#{user.id})") if
logger && user
end
|