]> git.openstreetmap.org Git - rails.git/blob - app/controllers/concerns/user_methods.rb
Merge remote-tracking branch 'upstream/pull/5405'
[rails.git] / app / controllers / concerns / user_methods.rb
1 module UserMethods
2   extend ActiveSupport::Concern
3
4   private
5
6   ##
7   # ensure that there is a "user" instance variable
8   def lookup_user
9     display_name = params[:display_name] || params[:user_display_name]
10     @user = User.active.find_by!(:display_name => display_name)
11   rescue ActiveRecord::RecordNotFound
12     render_unknown_user display_name
13   end
14
15   ##
16   # render a "no such user" page
17   def render_unknown_user(name)
18     @title = t "users.no_such_user.title"
19     @not_found_user = name
20
21     respond_to do |format|
22       format.html { render :template => "users/no_such_user", :status => :not_found, :layout => "site" }
23       format.all { head :not_found }
24     end
25   end
26
27   ##
28   # update a user's details
29   def update_user(user, params)
30     user.display_name = params[:display_name]
31     user.new_email = params[:new_email]
32
33     unless params[:pass_crypt].empty? && params[:pass_crypt_confirmation].empty?
34       user.pass_crypt = params[:pass_crypt]
35       user.pass_crypt_confirmation = params[:pass_crypt_confirmation]
36     end
37
38     if params[:auth_provider].nil? || params[:auth_provider].blank?
39       user.auth_provider = nil
40       user.auth_uid = nil
41     end
42
43     if user.save
44       session[:fingerprint] = user.fingerprint
45
46       if user.new_email.blank? || user.new_email == user.email
47         flash[:notice] = t "accounts.update.success"
48       else
49         token = user.generate_token_for(:new_email)
50
51         user.email = user.new_email
52
53         if user.valid?
54           flash[:notice] = t "accounts.update.success_confirm_needed"
55
56           begin
57             UserMailer.email_confirm(user, token).deliver_later
58           rescue StandardError
59             # Ignore errors sending email
60           end
61         else
62           current_user.errors.add(:new_email, current_user.errors[:email])
63           current_user.errors.add(:email, [])
64         end
65
66         user.restore_email!
67       end
68     end
69   end
70 end