]> git.openstreetmap.org Git - rails.git/blobdiff - app/controllers/user_controller.rb
Only use SystemTimer on MRI 1.8 as it is superfluous elsewhere
[rails.git] / app / controllers / user_controller.rb
index 16b76196b712925f5d491e75d127aed93334be16..89f35a8996e6cea3c9b20808259ae16b76e8d0b2 100644 (file)
@@ -23,9 +23,7 @@ class UserController < ApplicationController
     @text = OSM.legal_text_for_country(@legale)
 
     if request.xhr?
-      render :update do |page|
-        page.replace_html "contributorTerms", :partial => "terms"
-      end
+      render :partial => "terms"
     elsif using_open_id?
       # The redirect from the OpenID provider reenters here
       # again and we need to pass the parameters through to
@@ -168,9 +166,10 @@ class UserController < ApplicationController
         @user.preferred_editor = params[:user][:preferred_editor]
       end
 
-      @user.openid_url = nil if params[:user][:openid_url].empty?
+      @user.openid_url = nil if params[:user][:openid_url].blank?
 
-      if params[:user][:openid_url].length > 0 and
+      if params[:user][:openid_url] and
+         params[:user][:openid_url].length > 0 and
          params[:user][:openid_url] != @user.openid_url
         # If the OpenID has changed, we want to check that it is a
         # valid OpenID and one the user has control over before saving
@@ -188,13 +187,6 @@ class UserController < ApplicationController
       openid_verify(nil, @user) do |user|
         update_user(user)
       end
-    else
-      if flash[:errors]
-        flash[:errors].each do |attr,msg|
-          attr = "new_email" if attr == "email" and !@user.new_email.nil?
-          @user.errors.add(attr,msg)
-        end
-      end
     end
   end
 
@@ -209,7 +201,15 @@ class UserController < ApplicationController
     @title = t 'user.lost_password.title'
 
     if params[:user] and params[:user][:email]
-      user = User.visible.where(:email => params[:user][:email]).first
+      user = User.visible.find_by_email(params[:user][:email])
+
+      if user.nil?
+        users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
+
+        if users.count == 1
+          user = users.first
+        end
+      end
 
       if user
         token = user.tokens.create
@@ -254,11 +254,20 @@ class UserController < ApplicationController
     @title = t 'user.new.title'
     @referer = params[:referer] || session[:referer]
 
-    if session[:user]
+    if @user
       # The user is logged in already, so don't show them the signup
       # page, instead send them to the home page
-      redirect_to :controller => 'site', :action => 'index'
-    elsif not params['openid'].nil?
+      if @referer
+        redirect_to @referer
+      else
+        redirect_to :controller => 'site', :action => 'index'
+      end
+    elsif params.key?(:openid)
+      @user = User.new(:email => params[:email],
+                       :email_confirmation => params[:email],
+                       :display_name => params[:nickname],
+                       :openid_url => params[:openid])
+
       flash.now[:notice] = t 'user.new.openid association'
     end
   end
@@ -273,6 +282,8 @@ class UserController < ApplicationController
       else
         password_authentication(params[:username], params[:password])
       end
+    elsif params[:notice]
+      flash.now[:notice] = t "user.login.notice_#{params[:notice]}"
     elsif flash[:notice].nil?
       flash.now[:notice] =  t 'user.login.notice'
     end
@@ -327,6 +338,7 @@ class UserController < ApplicationController
             token.destroy
 
             session[:user] = user.id
+            cookies.permanent["_osm_username"] = user.display_name
 
             if referer.nil?
               flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
@@ -379,6 +391,7 @@ class UserController < ApplicationController
         end
         token.destroy
         session[:user] = @user.id
+        cookies.permanent["_osm_username"] = @user.display_name
         redirect_to :action => 'account', :display_name => @user.display_name
       else
         flash[:error] = t 'user.confirm_email.failure'
@@ -518,7 +531,7 @@ private
     end
 
     # Start the authentication
-    authenticate_with_open_id(openid_expand_url(openid_url), :required => required) do |result, identity_url, sreg, ax|
+    authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
       if result.successful?
         # We need to use the openid url passed back from the OpenID provider
         # rather than the one supplied by the user, as these can be different.
@@ -566,7 +579,7 @@ private
   def openid_verify(openid_url, user)
     user.openid_url = openid_url
 
-    authenticate_with_open_id(openid_expand_url(openid_url)) do |result, identity_url|
+    authenticate_with_open_id(openid_expand_url(openid_url), :method => :get) do |result, identity_url|
       if result.successful?
         # We need to use the openid url passed back from the OpenID provider
         # rather than the one supplied by the user, as these can be different.
@@ -606,6 +619,8 @@ private
   ##
   # process a successful login
   def successful_login(user)
+    cookies.permanent["_osm_username"] = user.display_name
+
     session[:user] = user.id
     session_expires_after 1.month if session[:remember_me]
 
@@ -646,16 +661,25 @@ private
     if user.save
       set_locale
 
-      if user.new_email.nil? or user.new_email.empty?
+      if user.new_email.blank?
         flash.now[:notice] = t 'user.account.flash update success'
       else
-        flash.now[:notice] = t 'user.account.flash update success confirm needed'
+        user.email = user.new_email
 
-        begin
-          Notifier.email_confirm(user, user.tokens.create).deliver
-        rescue
-          # Ignore errors sending email
+        if user.valid?
+          flash.now[:notice] = t 'user.account.flash update success confirm needed'
+
+          begin
+            Notifier.email_confirm(user, user.tokens.create).deliver
+          rescue
+            # Ignore errors sending email
+          end
+        else
+          @user.errors.set(:new_email, @user.errors.get(:email))
+          @user.errors.set(:email, [])
         end
+
+        user.reset_email!
       end
     end
   end