]> git.openstreetmap.org Git - rails.git/blob - app/controllers/passwords_controller.rb
Bump qs from 6.11.2 to 6.12.0
[rails.git] / app / controllers / passwords_controller.rb
1 class PasswordsController < ApplicationController
2   include SessionMethods
3
4   layout "site"
5
6   before_action :authorize_web
7   before_action :set_locale
8   before_action :check_database_readable
9
10   authorize_resource :class => false
11
12   before_action :check_database_writable
13
14   def new
15     @title = t ".title"
16   end
17
18   def edit
19     @title = t ".title"
20
21     if params[:token]
22       self.current_user = User.find_by_token_for(:password_reset, params[:token]) ||
23                           UserToken.unexpired.find_by(:token => params[:token])&.user
24
25       if current_user.nil?
26         flash[:error] = t ".flash token bad"
27         redirect_to :action => "new"
28       end
29     else
30       head :bad_request
31     end
32   end
33
34   def create
35     user = User.visible.find_by(:email => params[:email])
36
37     if user.nil?
38       users = User.visible.where("LOWER(email) = LOWER(?)", params[:email])
39
40       user = users.first if users.count == 1
41     end
42
43     if user
44       token = user.generate_token_for(:password_reset)
45       UserMailer.lost_password(user, token).deliver_later
46     end
47
48     flash[:notice] = t ".send_paranoid_instructions"
49     redirect_to login_path
50   end
51
52   def update
53     if params[:token]
54       self.current_user = User.find_by_token_for(:password_reset, params[:token]) ||
55                           UserToken.unexpired.find_by(:token => params[:token])&.user
56
57       if current_user
58         if params[:user]
59           current_user.pass_crypt = params[:user][:pass_crypt]
60           current_user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
61           current_user.activate if current_user.may_activate?
62           current_user.email_valid = true
63
64           if current_user.save
65             UserToken.delete_by(:token => params[:token])
66             session[:fingerprint] = current_user.fingerprint
67             flash[:notice] = t ".flash changed"
68             successful_login(current_user)
69           else
70             render :edit
71           end
72         end
73       else
74         flash[:error] = t ".flash token bad"
75         redirect_to :action => "new"
76       end
77     else
78       head :bad_request
79     end
80   end
81 end