]> git.openstreetmap.org Git - rails.git/blob - app/controllers/concerns/user_methods.rb
Bump osm-community-index from 5.6.1 to 5.6.2
[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     @user = User.active.find_by!(:display_name => params[:display_name])
10   rescue ActiveRecord::RecordNotFound
11     render_unknown_user params[:display_name]
12   end
13
14   ##
15   # render a "no such user" page
16   def render_unknown_user(name)
17     @title = t "users.no_such_user.title"
18     @not_found_user = name
19
20     respond_to do |format|
21       format.html { render :template => "users/no_such_user", :status => :not_found, :layout => "site" }
22       format.all { head :not_found }
23     end
24   end
25
26   ##
27   # update a user's details
28   def update_user(user, params)
29     user.display_name = params[:display_name]
30     user.new_email = params[:new_email]
31
32     unless params[:pass_crypt].empty? && params[:pass_crypt_confirmation].empty?
33       user.pass_crypt = params[:pass_crypt]
34       user.pass_crypt_confirmation = params[:pass_crypt_confirmation]
35     end
36
37     if params[:auth_provider].nil? || params[:auth_provider].blank?
38       user.auth_provider = nil
39       user.auth_uid = nil
40     end
41
42     if user.save
43       session[:fingerprint] = user.fingerprint
44
45       if user.new_email.blank? || user.new_email == user.email
46         flash[:notice] = t "accounts.update.success"
47       else
48         user.email = user.new_email
49
50         if user.valid?
51           flash[:notice] = t "accounts.update.success_confirm_needed"
52
53           begin
54             UserMailer.email_confirm(user, user.generate_token_for(:new_email)).deliver_later
55           rescue StandardError
56             # Ignore errors sending email
57           end
58         else
59           current_user.errors.add(:new_email, current_user.errors[:email])
60           current_user.errors.add(:email, [])
61         end
62
63         user.restore_email!
64       end
65     end
66   end
67 end