From: Jean-Philippe Lang
Date: Sun, 6 May 2007 12:49:32 +0000 (+0000)
Subject: Added autologin feature (disabled by default).
X-Git-Tag: 0.5.1~79
X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7eb64715595abded9e5ef4bcbc06aa203c6f23a8;p=redmine.git
Added autologin feature (disabled by default).
To enable this feature, go to administration settings and choose a duration for autologin.
When enabled, a checkbox on the login form lets users activate autologin.
git-svn-id: http://redmine.rubyforge.org/svn/trunk@514 e93f8b46-1217-0410-a6f0-8f06a7374b81
---
diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb
index 61c71557a..9b54a90ec 100644
--- a/app/controllers/account_controller.rb
+++ b/app/controllers/account_controller.rb
@@ -42,6 +42,11 @@ class AccountController < ApplicationController
user = User.try_to_login(params[:login], params[:password])
if user
self.logged_in_user = user
+ # generate a key and set cookie if autologin
+ if params[:autologin] && Setting.autologin?
+ token = Token.create(:user => user, :action => 'autologin')
+ cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now }
+ end
redirect_back_or_default :controller => 'my', :action => 'page'
else
flash.now[:notice] = l(:notice_account_invalid_creditentials)
@@ -51,6 +56,8 @@ class AccountController < ApplicationController
# Log out current user and redirect to welcome page
def logout
+ cookies.delete :autologin
+ Token.delete_all(["user_id = ? AND action = ?", logged_in_user.id, "autologin"]) if logged_in_user
self.logged_in_user = nil
redirect_to :controller => 'welcome'
end
diff --git a/app/controllers/application.rb b/app/controllers/application.rb
index 2a8e15155..54e4768b6 100644
--- a/app/controllers/application.rb
+++ b/app/controllers/application.rb
@@ -40,6 +40,13 @@ class ApplicationController < ActionController::Base
# check if login is globally required to access the application
def check_if_login_required
+ # no check needed if user is already logged in
+ return true if logged_in_user
+ # auto-login feature
+ autologin_key = cookies[:autologin]
+ if autologin_key && Setting.autologin?
+ self.logged_in_user = User.find_by_autologin_key(autologin_key)
+ end
require_login if Setting.login_required?
end
diff --git a/app/models/setting.rb b/app/models/setting.rb
index 56f7a5242..ecca01c28 100644
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -49,7 +49,7 @@ class Setting < ActiveRecord::Base
end
def self.#{name}?
- self[:#{name}].to_s == "1"
+ self[:#{name}].to_i > 0
end
def self.#{name}=(value)
diff --git a/app/models/user.rb b/app/models/user.rb
index 6ae1301e7..1c5613856 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -141,6 +141,11 @@ class User < ActiveRecord::Base
token = Token.find_by_value(key)
token && token.user.active? ? token.user : nil
end
+
+ def self.find_by_autologin_key(key)
+ token = Token.find_by_action_and_value('autologin', key)
+ token && (token.created_on > Setting.autologin.to_i.day.ago) && token.user.active? ? token.user : nil
+ end
def <=>(user)
lastname == user.lastname ? firstname <=> user.firstname : lastname <=> user.lastname
diff --git a/app/views/account/login.rhtml b/app/views/account/login.rhtml
index 7148bdd7d..08d462c61 100644
--- a/app/views/account/login.rhtml
+++ b/app/views/account/login.rhtml
@@ -3,23 +3,26 @@
<%=l(:label_please_login)%>
<% form_tag({:action=> "login"}, :class => "tabular") do %>
+
<%=l(:field_login)%>:
<%= text_field_tag 'login', nil, :size => 25 %>
<%=l(:field_password)%>:
<%= password_field_tag 'password', nil, :size => 25 %>
-
+<% if Setting.autologin? %>
+<%= check_box_tag 'autologin' %> <%= l(:label_stay_logged_in) %>
+<% end %>
+
+
<% end %>
<%= javascript_tag "Form.Element.focus('login');" %>
-
<% links = []
links << link_to(l(:label_register), :action => 'register') if Setting.self_registration?
links << link_to(l(:label_password_lost), :action => 'lost_password') if Setting.lost_password?
%>
<%= links.join(" | ") %>
-
\ No newline at end of file
diff --git a/app/views/settings/edit.rhtml b/app/views/settings/edit.rhtml
index 057a988de..f88d3f14a 100644
--- a/app/views/settings/edit.rhtml
+++ b/app/views/settings/edit.rhtml
@@ -15,15 +15,6 @@
<%= l(:setting_default_language) %>
<%= select_tag 'settings[default_language]', options_for_select( lang_options_for_select(false), Setting.default_language) %>
-<%= l(:setting_login_required) %>
-<%= check_box_tag 'settings[login_required]', 1, Setting.login_required? %><%= hidden_field_tag 'settings[login_required]', 0 %>
-
-<%= l(:setting_self_registration) %>
-<%= check_box_tag 'settings[self_registration]', 1, Setting.self_registration? %><%= hidden_field_tag 'settings[self_registration]', 0 %>
-
-<%= l(:label_password_lost) %>
-<%= check_box_tag 'settings[lost_password]', 1, Setting.lost_password? %><%= hidden_field_tag 'settings[lost_password]', 0 %>
-
<%= l(:setting_attachment_max_size) %>
<%= text_field_tag 'settings[attachment_max_size]', Setting.attachment_max_size, :size => 6 %> KB
@@ -52,6 +43,20 @@
<%= check_box_tag 'settings[sys_api_enabled]', 1, Setting.sys_api_enabled? %><%= hidden_field_tag 'settings[sys_api_enabled]', 0 %>
+<%= l(:label_authentication) %>
+<%= l(:setting_login_required) %>
+<%= check_box_tag 'settings[login_required]', 1, Setting.login_required? %><%= hidden_field_tag 'settings[login_required]', 0 %>
+
+<%= l(:setting_autologin) %>
+<%= select_tag 'settings[autologin]', options_for_select( [[l(:label_disabled), "0"]] + [1, 7, 30, 365].collect{|days| [lwr(:actionview_datehelper_time_in_words_day, days), days.to_s]}, Setting.autologin) %>
+
+<%= l(:setting_self_registration) %>
+<%= check_box_tag 'settings[self_registration]', 1, Setting.self_registration? %><%= hidden_field_tag 'settings[self_registration]', 0 %>
+
+<%= l(:label_password_lost) %>
+<%= check_box_tag 'settings[lost_password]', 1, Setting.lost_password? %><%= hidden_field_tag 'settings[lost_password]', 0 %>
+
+
<%= l(:text_issues_ref_in_commit_messages) %>
<%= l(:setting_commit_ref_keywords) %>
<%= text_field_tag 'settings[commit_ref_keywords]', Setting.commit_ref_keywords, :size => 30 %><%= l(:text_coma_separated) %>
diff --git a/config/settings.yml b/config/settings.yml
index 46324785b..39e2e8531 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -61,4 +61,8 @@ commit_fix_keywords:
commit_fix_status_id:
format: int
default: 0
-
\ No newline at end of file
+# autologin duration in days
+# 0 means autologin is disabled
+autologin:
+ format: int
+ default: 0
diff --git a/lang/bg.yml b/lang/bg.yml
index 6041f71b3..1e348cb75 100644
--- a/lang/bg.yml
+++ b/lang/bg.yml
@@ -171,6 +171,7 @@ setting_autofetch_changesets: ÐвÑомаÑиÑно обÑабоÑване на
setting_sys_api_enabled: РазÑеÑаване на WS за ÑпÑавление на SVN Ñклада
setting_commit_ref_keywords: ÐÑбелÑзваÑи клÑÑови дÑми
setting_commit_fix_keywords: ÐÑиклÑÑваÑи клÑÑови дÑми
+setting_autologin: Autologin
label_user: ÐоÑÑебиÑел
label_user_plural: ÐоÑÑебиÑели
@@ -380,6 +381,8 @@ label_end_to_start: start to end
label_end_to_end: end to end
label_start_to_start: start to start
label_start_to_end: start to end
+label_stay_logged_in: Stay logged in
+label_disabled: disabled
button_login: ÐÑ
од
button_submit: ÐзпÑаÑане
diff --git a/lang/de.yml b/lang/de.yml
index 35e4328cc..1fc529bad 100644
--- a/lang/de.yml
+++ b/lang/de.yml
@@ -171,6 +171,7 @@ setting_autofetch_changesets: Autofetch SVN commits
setting_sys_api_enabled: Enable WS for repository management
setting_commit_ref_keywords: Referencing keywords
setting_commit_fix_keywords: Fixing keywords
+setting_autologin: Autologin
label_user: Benutzer
label_user_plural: Benutzer
@@ -380,6 +381,8 @@ label_end_to_start: start to end
label_end_to_end: end to end
label_start_to_start: start to start
label_start_to_end: start to end
+label_stay_logged_in: Stay logged in
+label_disabled: disabled
button_login: Einloggen
button_submit: OK
diff --git a/lang/en.yml b/lang/en.yml
index fadcaad3f..1800b5d33 100644
--- a/lang/en.yml
+++ b/lang/en.yml
@@ -171,6 +171,7 @@ setting_autofetch_changesets: Autofetch SVN commits
setting_sys_api_enabled: Enable WS for repository management
setting_commit_ref_keywords: Referencing keywords
setting_commit_fix_keywords: Fixing keywords
+setting_autologin: Autologin
label_user: User
label_user_plural: Users
@@ -380,6 +381,8 @@ label_end_to_start: start to end
label_end_to_end: end to end
label_start_to_start: start to start
label_start_to_end: start to end
+label_stay_logged_in: Stay logged in
+label_disabled: disabled
button_login: Login
button_submit: Submit
diff --git a/lang/es.yml b/lang/es.yml
index e6c9002e7..3a66e0bcf 100644
--- a/lang/es.yml
+++ b/lang/es.yml
@@ -171,6 +171,7 @@ setting_autofetch_changesets: Autofetch SVN commits
setting_sys_api_enabled: Enable WS for repository management
setting_commit_ref_keywords: Referencing keywords
setting_commit_fix_keywords: Fixing keywords
+setting_autologin: Autologin
label_user: Usuario
label_user_plural: Usuarios
@@ -380,6 +381,8 @@ label_end_to_start: start to end
label_end_to_end: end to end
label_start_to_start: start to start
label_start_to_end: start to end
+label_stay_logged_in: Stay logged in
+label_disabled: disabled
button_login: Conexión
button_submit: Someter
diff --git a/lang/fr.yml b/lang/fr.yml
index 394417ac9..bd1973b4d 100644
--- a/lang/fr.yml
+++ b/lang/fr.yml
@@ -171,6 +171,7 @@ setting_autofetch_changesets: Récupération auto. des commits SVN
setting_sys_api_enabled: Activer les WS pour la gestion des dépôts
setting_commit_ref_keywords: Mot-clés de référencement
setting_commit_fix_keywords: Mot-clés de résolution
+setting_autologin: Autologin
label_user: Utilisateur
label_user_plural: Utilisateurs
@@ -380,6 +381,8 @@ label_end_to_start: début à fin
label_end_to_end: fin à fin
label_start_to_start: début à début
label_start_to_end: début à fin
+label_stay_logged_in: Rester connecté
+label_disabled: désactivé
button_login: Connexion
button_submit: Soumettre
diff --git a/lang/it.yml b/lang/it.yml
index 2deb09115..c1e964548 100644
--- a/lang/it.yml
+++ b/lang/it.yml
@@ -171,6 +171,7 @@ setting_autofetch_changesets: Acquisisci automaticamente le commit SVN
setting_sys_api_enabled: Abilita WS per la gestione del repository
setting_commit_ref_keywords: Referencing keywords
setting_commit_fix_keywords: Fixing keywords
+setting_autologin: Autologin
label_user: Utente
label_user_plural: Utenti
@@ -380,6 +381,8 @@ label_end_to_start: start to end
label_end_to_end: end to end
label_start_to_start: start to start
label_start_to_end: start to end
+label_stay_logged_in: Stay logged in
+label_disabled: disabled
button_login: Login
button_submit: Invia
diff --git a/lang/ja.yml b/lang/ja.yml
index f746faf97..955799494 100644
--- a/lang/ja.yml
+++ b/lang/ja.yml
@@ -172,6 +172,7 @@ setting_autofetch_changesets: SVNã³ããããèªååå¾ãã
setting_sys_api_enabled: ãªãã¸ããªç®¡çç¨ã®Web Serviceãæå¹åãã
setting_commit_ref_keywords: Referencing keywords
setting_commit_fix_keywords: Fixing keywords
+setting_autologin: Autologin
label_user: ã¦ã¼ã¶
label_user_plural: ã¦ã¼ã¶
@@ -381,6 +382,8 @@ label_end_to_start: start to end
label_end_to_end: end to end
label_start_to_start: start to start
label_start_to_end: start to end
+label_stay_logged_in: Stay logged in
+label_disabled: disabled
button_login: ãã°ã¤ã³
button_submit: å¤æ´
diff --git a/lang/pt.yml b/lang/pt.yml
index 0d8bfc67c..3c6debfbe 100644
--- a/lang/pt.yml
+++ b/lang/pt.yml
@@ -171,6 +171,7 @@ setting_autofetch_changesets: Autofetch SVN commits
setting_sys_api_enabled: Ativa WS para gerenciamento do repositorio
setting_commit_ref_keywords: Referencing keywords
setting_commit_fix_keywords: Fixing keywords
+setting_autologin: Autologin
label_user: Usuario
label_user_plural: Usuarios
@@ -380,6 +381,8 @@ label_end_to_start: start to end
label_end_to_end: end to end
label_start_to_start: start to start
label_start_to_end: start to end
+label_stay_logged_in: Stay logged in
+label_disabled: disabled
button_login: Login
button_submit: Enviar
diff --git a/lang/zh.yml b/lang/zh.yml
index 344d8152b..f9f2a75a7 100644
--- a/lang/zh.yml
+++ b/lang/zh.yml
@@ -174,6 +174,7 @@ setting_autofetch_changesets: Autofetch SVN commits
setting_sys_api_enabled: Enable WS for repository management
setting_commit_ref_keywords: Referencing keywords
setting_commit_fix_keywords: Fixing keywords
+setting_autologin: Autologin
label_user: ç¨æ·
label_user_plural: ç¨æ·å表
@@ -383,6 +384,8 @@ label_end_to_start: start to end
label_end_to_end: end to end
label_start_to_start: start to start
label_start_to_end: start to end
+label_stay_logged_in: Stay logged in
+label_disabled: disabled
button_login: ç»å½
button_submit: æ交