##// END OF EJS Templates
Login field name changed to username (#755)....
Jean-Philippe Lang -
r1167:87742f23edb3
parent child
Show More
@@ -1,175 +1,175
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class AccountController < ApplicationController
19 19 layout 'base'
20 20 helper :custom_fields
21 21 include CustomFieldsHelper
22 22
23 23 # prevents login action to be filtered by check_if_login_required application scope filter
24 24 skip_before_filter :check_if_login_required, :only => [:login, :lost_password, :register, :activate]
25 25
26 26 # Show user's account
27 27 def show
28 28 @user = User.find_active(params[:id])
29 29 @custom_values = @user.custom_values.find(:all, :include => :custom_field)
30 30
31 31 # show only public projects and private projects that the logged in user is also a member of
32 32 @memberships = @user.memberships.select do |membership|
33 33 membership.project.is_public? || (User.current.member_of?(membership.project))
34 34 end
35 35 rescue ActiveRecord::RecordNotFound
36 36 render_404
37 37 end
38 38
39 39 # Login request and validation
40 40 def login
41 41 if request.get?
42 42 # Logout user
43 43 self.logged_user = nil
44 44 else
45 45 # Authenticate user
46 user = User.try_to_login(params[:login], params[:password])
46 user = User.try_to_login(params[:username], params[:password])
47 47 if user
48 48 self.logged_user = user
49 49 # generate a key and set cookie if autologin
50 50 if params[:autologin] && Setting.autologin?
51 51 token = Token.create(:user => user, :action => 'autologin')
52 52 cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now }
53 53 end
54 54 redirect_back_or_default :controller => 'my', :action => 'page'
55 55 else
56 56 flash.now[:error] = l(:notice_account_invalid_creditentials)
57 57 end
58 58 end
59 59 end
60 60
61 61 # Log out current user and redirect to welcome page
62 62 def logout
63 63 cookies.delete :autologin
64 64 Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) if User.current.logged?
65 65 self.logged_user = nil
66 66 redirect_to home_url
67 67 end
68 68
69 69 # Enable user to choose a new password
70 70 def lost_password
71 71 redirect_to(home_url) && return unless Setting.lost_password?
72 72 if params[:token]
73 73 @token = Token.find_by_action_and_value("recovery", params[:token])
74 74 redirect_to(home_url) && return unless @token and !@token.expired?
75 75 @user = @token.user
76 76 if request.post?
77 77 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
78 78 if @user.save
79 79 @token.destroy
80 80 flash[:notice] = l(:notice_account_password_updated)
81 81 redirect_to :action => 'login'
82 82 return
83 83 end
84 84 end
85 85 render :template => "account/password_recovery"
86 86 return
87 87 else
88 88 if request.post?
89 89 user = User.find_by_mail(params[:mail])
90 90 # user not found in db
91 91 flash.now[:error] = l(:notice_account_unknown_email) and return unless user
92 92 # user uses an external authentification
93 93 flash.now[:error] = l(:notice_can_t_change_password) and return if user.auth_source_id
94 94 # create a new token for password recovery
95 95 token = Token.new(:user => user, :action => "recovery")
96 96 if token.save
97 97 Mailer.deliver_lost_password(token)
98 98 flash[:notice] = l(:notice_account_lost_email_sent)
99 99 redirect_to :action => 'login'
100 100 return
101 101 end
102 102 end
103 103 end
104 104 end
105 105
106 106 # User self-registration
107 107 def register
108 108 redirect_to(home_url) && return unless Setting.self_registration?
109 109 if request.get?
110 110 @user = User.new(:language => Setting.default_language)
111 111 @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user) }
112 112 else
113 113 @user = User.new(params[:user])
114 114 @user.admin = false
115 115 @user.login = params[:user][:login]
116 116 @user.status = User::STATUS_REGISTERED
117 117 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
118 118 @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x,
119 119 :customized => @user,
120 120 :value => (params["custom_fields"] ? params["custom_fields"][x.id.to_s] : nil)) }
121 121 @user.custom_values = @custom_values
122 122 case Setting.self_registration
123 123 when '1'
124 124 # Email activation
125 125 token = Token.new(:user => @user, :action => "register")
126 126 if @user.save and token.save
127 127 Mailer.deliver_register(token)
128 128 flash[:notice] = l(:notice_account_register_done)
129 129 redirect_to :action => 'login'
130 130 end
131 131 when '3'
132 132 # Automatic activation
133 133 @user.status = User::STATUS_ACTIVE
134 134 if @user.save
135 135 flash[:notice] = l(:notice_account_activated)
136 136 redirect_to :action => 'login'
137 137 end
138 138 else
139 139 # Manual activation by the administrator
140 140 if @user.save
141 141 # Sends an email to the administrators
142 142 Mailer.deliver_account_activation_request(@user)
143 143 flash[:notice] = l(:notice_account_pending)
144 144 redirect_to :action => 'login'
145 145 end
146 146 end
147 147 end
148 148 end
149 149
150 150 # Token based account activation
151 151 def activate
152 152 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
153 153 token = Token.find_by_action_and_value('register', params[:token])
154 154 redirect_to(home_url) && return unless token and !token.expired?
155 155 user = token.user
156 156 redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED
157 157 user.status = User::STATUS_ACTIVE
158 158 if user.save
159 159 token.destroy
160 160 flash[:notice] = l(:notice_account_activated)
161 161 end
162 162 redirect_to :action => 'login'
163 163 end
164 164
165 165 private
166 166 def logged_user=(user)
167 167 if user && user.is_a?(User)
168 168 User.current = user
169 169 session[:user_id] = user.id
170 170 else
171 171 User.current = User.anonymous
172 172 session[:user_id] = nil
173 173 end
174 174 end
175 175 end
@@ -1,33 +1,33
1 1 <div id="login-form">
2 2 <% form_tag({:action=> "login"}) do %>
3 3 <table>
4 4 <tr>
5 <td align="right"><label for="login"><%=l(:field_login)%>:</label></td>
6 <td align="left"><p><%= text_field_tag 'login', nil, :size => 40 %></p></td>
5 <td align="right"><label for="username"><%=l(:field_login)%>:</label></td>
6 <td align="left"><p><%= text_field_tag 'username', nil, :size => 40 %></p></td>
7 7 </tr>
8 8 <tr>
9 9 <td align="right"><label for="password"><%=l(:field_password)%>:</label></td>
10 10 <td align="left"><%= password_field_tag 'password', nil, :size => 40 %></td>
11 11 </tr>
12 12 <tr>
13 13 <td></td>
14 14 <td align="left">
15 15 <% if Setting.autologin? %>
16 16 <label for="autologin"><%= check_box_tag 'autologin' %> <%= l(:label_stay_logged_in) %></label>
17 17 <% end %>
18 18 </td>
19 19 </tr>
20 20 <tr>
21 21 <td align="left">
22 22 <% if Setting.lost_password? %>
23 23 <%= link_to l(:label_password_lost), :controller => 'account', :action => 'lost_password' %>
24 24 <% end %>
25 25 </td>
26 26 <td align="right">
27 27 <input type="submit" name="login" value="<%=l(:button_login)%> &#187;" />
28 28 </td>
29 29 </tr>
30 30 </table>
31 <%= javascript_tag "Form.Element.focus('login');" %>
31 <%= javascript_tag "Form.Element.focus('username');" %>
32 32 <% end %>
33 33 </div>
@@ -1,73 +1,73
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19 require 'account_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class AccountController; def rescue_action(e) raise e end; end
23 23
24 24 class AccountControllerTest < Test::Unit::TestCase
25 25 fixtures :users
26 26
27 27 def setup
28 28 @controller = AccountController.new
29 29 @request = ActionController::TestRequest.new
30 30 @response = ActionController::TestResponse.new
31 31 User.current = nil
32 32 end
33 33
34 34 def test_show
35 35 get :show, :id => 2
36 36 assert_response :success
37 37 assert_template 'show'
38 38 assert_not_nil assigns(:user)
39 39 end
40 40
41 41 def test_show_inactive
42 42 get :show, :id => 5
43 43 assert_response 404
44 44 assert_nil assigns(:user)
45 45 end
46 46
47 47 def test_login_with_wrong_password
48 post :login, :login => 'admin', :password => 'bad'
48 post :login, :username => 'admin', :password => 'bad'
49 49 assert_response :success
50 50 assert_template 'login'
51 51 assert_tag 'div',
52 52 :attributes => { :class => "flash error" },
53 53 :content => /Invalid user or password/
54 54 end
55 55
56 56 def test_autologin
57 57 Setting.autologin = "7"
58 58 Token.delete_all
59 post :login, :login => 'admin', :password => 'admin', :autologin => 1
59 post :login, :username => 'admin', :password => 'admin', :autologin => 1
60 60 assert_redirected_to 'my/page'
61 61 token = Token.find :first
62 62 assert_not_nil token
63 63 assert_equal User.find_by_login('admin'), token.user
64 64 assert_equal 'autologin', token.action
65 65 end
66 66
67 67 def test_logout
68 68 @request.session[:user_id] = 2
69 69 get :logout
70 70 assert_redirected_to ''
71 71 assert_nil @request.session[:user_id]
72 72 end
73 73 end
@@ -1,77 +1,77
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 ENV["RAILS_ENV"] ||= "test"
19 19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
20 20 require 'test_help'
21 21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
22 22
23 23 class Test::Unit::TestCase
24 24 # Transactional fixtures accelerate your tests by wrapping each test method
25 25 # in a transaction that's rolled back on completion. This ensures that the
26 26 # test database remains unchanged so your fixtures don't have to be reloaded
27 27 # between every test method. Fewer database queries means faster tests.
28 28 #
29 29 # Read Mike Clark's excellent walkthrough at
30 30 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
31 31 #
32 32 # Every Active Record database supports transactions except MyISAM tables
33 33 # in MySQL. Turn off transactional fixtures in this case; however, if you
34 34 # don't care one way or the other, switching from MyISAM to InnoDB tables
35 35 # is recommended.
36 36 self.use_transactional_fixtures = true
37 37
38 38 # Instantiated fixtures are slow, but give you @david where otherwise you
39 39 # would need people(:david). If you don't want to migrate your existing
40 40 # test cases which use the @david style and don't mind the speed hit (each
41 41 # instantiated fixtures translates to a database query per test method),
42 42 # then set this back to true.
43 43 self.use_instantiated_fixtures = false
44 44
45 45 # Add more helper methods to be used by all tests here...
46 46
47 47 def log_user(login, password)
48 48 get "/account/login"
49 49 assert_equal nil, session[:user_id]
50 50 assert_response :success
51 51 assert_template "account/login"
52 post "/account/login", :login => login, :password => password
52 post "/account/login", :username => login, :password => password
53 53 assert_redirected_to "my/page"
54 54 assert_equal login, User.find(session[:user_id]).login
55 55 end
56 56
57 57 def test_uploaded_file(name, mime)
58 58 ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + "/files/#{name}", mime)
59 59 end
60 60 end
61 61
62 62
63 63 # ActionController::TestUploadedFile bug
64 64 # see http://dev.rubyonrails.org/ticket/4635
65 65 class String
66 66 def original_filename
67 67 "testfile.txt"
68 68 end
69 69
70 70 def content_type
71 71 "text/plain"
72 72 end
73 73
74 74 def read
75 75 self.to_s
76 76 end
77 77 end
General Comments 0
You need to be logged in to leave comments. Login now