##// END OF EJS Templates
Backported r2609 from trunk....
Jean-Philippe Lang -
r2563:aa38f755c649
parent child
Show More
@@ -1,194 +1,195
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 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 helper :custom_fields
20 20 include CustomFieldsHelper
21 21
22 22 # prevents login action to be filtered by check_if_login_required application scope filter
23 23 skip_before_filter :check_if_login_required, :only => [:login, :lost_password, :register, :activate]
24 24
25 25 # Show user's account
26 26 def show
27 27 @user = User.active.find(params[:id])
28 28 @custom_values = @user.custom_values
29 29
30 30 # show only public projects and private projects that the logged in user is also a member of
31 31 @memberships = @user.memberships.select do |membership|
32 32 membership.project.is_public? || (User.current.member_of?(membership.project))
33 33 end
34 34
35 35 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
36 36 @events_by_day = events.group_by(&:event_date)
37 37
38 38 rescue ActiveRecord::RecordNotFound
39 39 render_404
40 40 end
41 41
42 42 # Login request and validation
43 43 def login
44 44 if request.get?
45 45 # Logout user
46 46 self.logged_user = nil
47 47 else
48 48 # Authenticate user
49 49 user = User.try_to_login(params[:username], params[:password])
50 50 if user.nil?
51 51 # Invalid credentials
52 52 flash.now[:error] = l(:notice_account_invalid_creditentials)
53 53 elsif user.new_record?
54 54 # Onthefly creation failed, display the registration form to fill/fix attributes
55 55 @user = user
56 56 session[:auth_source_registration] = {:login => user.login, :auth_source_id => user.auth_source_id }
57 57 render :action => 'register'
58 58 else
59 59 # Valid user
60 60 self.logged_user = user
61 61 # generate a key and set cookie if autologin
62 62 if params[:autologin] && Setting.autologin?
63 63 token = Token.create(:user => user, :action => 'autologin')
64 64 cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now }
65 65 end
66 call_hook(:controller_account_success_authentication_after, {:user => user })
66 67 redirect_back_or_default :controller => 'my', :action => 'page'
67 68 end
68 69 end
69 70 end
70 71
71 72 # Log out current user and redirect to welcome page
72 73 def logout
73 74 cookies.delete :autologin
74 75 Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) if User.current.logged?
75 76 self.logged_user = nil
76 77 redirect_to home_url
77 78 end
78 79
79 80 # Enable user to choose a new password
80 81 def lost_password
81 82 redirect_to(home_url) && return unless Setting.lost_password?
82 83 if params[:token]
83 84 @token = Token.find_by_action_and_value("recovery", params[:token])
84 85 redirect_to(home_url) && return unless @token and !@token.expired?
85 86 @user = @token.user
86 87 if request.post?
87 88 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
88 89 if @user.save
89 90 @token.destroy
90 91 flash[:notice] = l(:notice_account_password_updated)
91 92 redirect_to :action => 'login'
92 93 return
93 94 end
94 95 end
95 96 render :template => "account/password_recovery"
96 97 return
97 98 else
98 99 if request.post?
99 100 user = User.find_by_mail(params[:mail])
100 101 # user not found in db
101 102 flash.now[:error] = l(:notice_account_unknown_email) and return unless user
102 103 # user uses an external authentification
103 104 flash.now[:error] = l(:notice_can_t_change_password) and return if user.auth_source_id
104 105 # create a new token for password recovery
105 106 token = Token.new(:user => user, :action => "recovery")
106 107 if token.save
107 108 Mailer.deliver_lost_password(token)
108 109 flash[:notice] = l(:notice_account_lost_email_sent)
109 110 redirect_to :action => 'login'
110 111 return
111 112 end
112 113 end
113 114 end
114 115 end
115 116
116 117 # User self-registration
117 118 def register
118 119 redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
119 120 if request.get?
120 121 session[:auth_source_registration] = nil
121 122 @user = User.new(:language => Setting.default_language)
122 123 else
123 124 @user = User.new(params[:user])
124 125 @user.admin = false
125 126 @user.status = User::STATUS_REGISTERED
126 127 if session[:auth_source_registration]
127 128 @user.status = User::STATUS_ACTIVE
128 129 @user.login = session[:auth_source_registration][:login]
129 130 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
130 131 if @user.save
131 132 session[:auth_source_registration] = nil
132 133 self.logged_user = @user
133 134 flash[:notice] = l(:notice_account_activated)
134 135 redirect_to :controller => 'my', :action => 'account'
135 136 end
136 137 else
137 138 @user.login = params[:user][:login]
138 139 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
139 140 case Setting.self_registration
140 141 when '1'
141 142 # Email activation
142 143 token = Token.new(:user => @user, :action => "register")
143 144 if @user.save and token.save
144 145 Mailer.deliver_register(token)
145 146 flash[:notice] = l(:notice_account_register_done)
146 147 redirect_to :action => 'login'
147 148 end
148 149 when '3'
149 150 # Automatic activation
150 151 @user.status = User::STATUS_ACTIVE
151 152 if @user.save
152 153 self.logged_user = @user
153 154 flash[:notice] = l(:notice_account_activated)
154 155 redirect_to :controller => 'my', :action => 'account'
155 156 end
156 157 else
157 158 # Manual activation by the administrator
158 159 if @user.save
159 160 # Sends an email to the administrators
160 161 Mailer.deliver_account_activation_request(@user)
161 162 flash[:notice] = l(:notice_account_pending)
162 163 redirect_to :action => 'login'
163 164 end
164 165 end
165 166 end
166 167 end
167 168 end
168 169
169 170 # Token based account activation
170 171 def activate
171 172 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
172 173 token = Token.find_by_action_and_value('register', params[:token])
173 174 redirect_to(home_url) && return unless token and !token.expired?
174 175 user = token.user
175 176 redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED
176 177 user.status = User::STATUS_ACTIVE
177 178 if user.save
178 179 token.destroy
179 180 flash[:notice] = l(:notice_account_activated)
180 181 end
181 182 redirect_to :action => 'login'
182 183 end
183 184
184 185 private
185 186 def logged_user=(user)
186 187 if user && user.is_a?(User)
187 188 User.current = user
188 189 session[:user_id] = user.id
189 190 else
190 191 User.current = User.anonymous
191 192 session[:user_id] = nil
192 193 end
193 194 end
194 195 end
General Comments 0
You need to be logged in to leave comments. Login now