@@ -1,143 +1,143 | |||
|
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] |
|
25 | 25 | before_filter :require_login, :only => :logout |
|
26 | 26 | |
|
27 | 27 | # Show user's account |
|
28 | 28 | def show |
|
29 | @user = User.find(params[:id]) | |
|
29 | @user = User.find_active(params[:id]) | |
|
30 | 30 | @custom_values = @user.custom_values.find(:all, :include => :custom_field) |
|
31 | 31 | |
|
32 | 32 | # show only public projects and private projects that the logged in user is also a member of |
|
33 | 33 | @memberships = @user.memberships.select do |membership| |
|
34 | 34 | membership.project.is_public? || (logged_in_user && logged_in_user.role_for_project(membership.project)) |
|
35 | 35 | end |
|
36 | 36 | rescue ActiveRecord::RecordNotFound |
|
37 | 37 | render_404 |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | 40 | # Login request and validation |
|
41 | 41 | def login |
|
42 | 42 | if request.get? |
|
43 | 43 | # Logout user |
|
44 | 44 | self.logged_in_user = nil |
|
45 | 45 | else |
|
46 | 46 | # Authenticate user |
|
47 | 47 | user = User.try_to_login(params[:login], params[:password]) |
|
48 | 48 | if user |
|
49 | 49 | self.logged_in_user = user |
|
50 | 50 | # generate a key and set cookie if autologin |
|
51 | 51 | if params[:autologin] && Setting.autologin? |
|
52 | 52 | token = Token.create(:user => user, :action => 'autologin') |
|
53 | 53 | cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now } |
|
54 | 54 | end |
|
55 | 55 | redirect_back_or_default :controller => 'my', :action => 'page' |
|
56 | 56 | else |
|
57 | 57 | flash.now[:error] = l(:notice_account_invalid_creditentials) |
|
58 | 58 | end |
|
59 | 59 | end |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | # Log out current user and redirect to welcome page |
|
63 | 63 | def logout |
|
64 | 64 | cookies.delete :autologin |
|
65 | 65 | Token.delete_all(["user_id = ? AND action = ?", logged_in_user.id, "autologin"]) if logged_in_user |
|
66 | 66 | self.logged_in_user = nil |
|
67 | 67 | redirect_to :controller => 'welcome' |
|
68 | 68 | end |
|
69 | 69 | |
|
70 | 70 | # Enable user to choose a new password |
|
71 | 71 | def lost_password |
|
72 | 72 | redirect_to :controller => 'welcome' and return unless Setting.lost_password? |
|
73 | 73 | if params[:token] |
|
74 | 74 | @token = Token.find_by_action_and_value("recovery", params[:token]) |
|
75 | 75 | redirect_to :controller => 'welcome' and return unless @token and !@token.expired? |
|
76 | 76 | @user = @token.user |
|
77 | 77 | if request.post? |
|
78 | 78 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
|
79 | 79 | if @user.save |
|
80 | 80 | @token.destroy |
|
81 | 81 | flash[:notice] = l(:notice_account_password_updated) |
|
82 | 82 | redirect_to :action => 'login' |
|
83 | 83 | return |
|
84 | 84 | end |
|
85 | 85 | end |
|
86 | 86 | render :template => "account/password_recovery" |
|
87 | 87 | return |
|
88 | 88 | else |
|
89 | 89 | if request.post? |
|
90 | 90 | user = User.find_by_mail(params[:mail]) |
|
91 | 91 | # user not found in db |
|
92 | 92 | flash.now[:error] = l(:notice_account_unknown_email) and return unless user |
|
93 | 93 | # user uses an external authentification |
|
94 | 94 | flash.now[:error] = l(:notice_can_t_change_password) and return if user.auth_source_id |
|
95 | 95 | # create a new token for password recovery |
|
96 | 96 | token = Token.new(:user => user, :action => "recovery") |
|
97 | 97 | if token.save |
|
98 | 98 | Mailer.deliver_lost_password(token) |
|
99 | 99 | flash[:notice] = l(:notice_account_lost_email_sent) |
|
100 | 100 | redirect_to :action => 'login' |
|
101 | 101 | return |
|
102 | 102 | end |
|
103 | 103 | end |
|
104 | 104 | end |
|
105 | 105 | end |
|
106 | 106 | |
|
107 | 107 | # User self-registration |
|
108 | 108 | def register |
|
109 | 109 | redirect_to :controller => 'welcome' and return unless Setting.self_registration? |
|
110 | 110 | if params[:token] |
|
111 | 111 | token = Token.find_by_action_and_value("register", params[:token]) |
|
112 | 112 | redirect_to :controller => 'welcome' and return unless token and !token.expired? |
|
113 | 113 | user = token.user |
|
114 | 114 | redirect_to :controller => 'welcome' and return unless user.status == User::STATUS_REGISTERED |
|
115 | 115 | user.status = User::STATUS_ACTIVE |
|
116 | 116 | if user.save |
|
117 | 117 | token.destroy |
|
118 | 118 | flash[:notice] = l(:notice_account_activated) |
|
119 | 119 | redirect_to :action => 'login' |
|
120 | 120 | return |
|
121 | 121 | end |
|
122 | 122 | else |
|
123 | 123 | if request.get? |
|
124 | 124 | @user = User.new(:language => Setting.default_language) |
|
125 | 125 | @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user) } |
|
126 | 126 | else |
|
127 | 127 | @user = User.new(params[:user]) |
|
128 | 128 | @user.admin = false |
|
129 | 129 | @user.login = params[:user][:login] |
|
130 | 130 | @user.status = User::STATUS_REGISTERED |
|
131 | 131 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] |
|
132 | 132 | @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user, :value => params["custom_fields"][x.id.to_s]) } |
|
133 | 133 | @user.custom_values = @custom_values |
|
134 | 134 | token = Token.new(:user => @user, :action => "register") |
|
135 | 135 | if @user.save and token.save |
|
136 | 136 | Mailer.deliver_register(token) |
|
137 | 137 | flash[:notice] = l(:notice_account_register_done) |
|
138 | 138 | redirect_to :controller => 'account', :action => 'login' |
|
139 | 139 | end |
|
140 | 140 | end |
|
141 | 141 | end |
|
142 | 142 | end |
|
143 | 143 | end |
General Comments 0
You need to be logged in to leave comments.
Login now