]> git.openstreetmap.org Git - rails.git/blobdiff - app/controllers/user_controller.rb
Add support for fetching attributes via AX as well as SReg
[rails.git] / app / controllers / user_controller.rb
index 57881881a56f6edee7fb92570d126be4df0cc9c6..a42f932a9226146bece195ee1f3891ec1c6b08ac 100644 (file)
@@ -16,56 +16,58 @@ class UserController < ApplicationController
 
   filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
 
-  cache_sweeper :user_sweeper, :only => [:account, :set_status, :delete]
+  cache_sweeper :user_sweeper, :only => [:account, :set_status, :delete], :unless => STATUS == :database_offline
 
   def terms
-    @title = t 'user.new.title'
-    @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || APP_CONFIG['default_legale']
+    @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || DEFAULT_LEGALE
     @text = OSM.legal_text_for_country(@legale)
 
     if request.xhr?
       render :update do |page|
-        page.replace_html "contributorTerms", :partial => "terms"
+        page.replace_html "contributorTerms", :partial => "terms", :locals => { :has_decline => params[:has_decline] }
+      end
+    elsif using_open_id?
+      # The redirect from the OpenID provider reenters here
+      # again and we need to pass the parameters through to
+      # the open_id_authentication function
+      @user = session.delete(:new_user)
+
+      openid_verify(nil, @user) do |user|
       end
-    elsif params[:user]
+
+      if @user.openid_url.nil? or @user.invalid?
+        render :action => 'new'
+      else
+        render :action => 'terms'
+      end
+    else
       session[:referer] = params[:referer]
 
-      @user = User.new(params[:user])
+      @title = t 'user.terms.title'
+      @user = User.new(params[:user]) if params[:user]
 
-      if params[:user][:openid_url] and @user.pass_crypt.empty?
+      if params[:user] and params[:user][:openid_url] and @user.pass_crypt.empty?
         # We are creating an account with OpenID and no password
         # was specified so create a random one
         @user.pass_crypt = ActiveSupport::SecureRandom.base64(16) 
         @user.pass_crypt_confirmation = @user.pass_crypt 
       end
 
-      if @user.valid?
-        if params[:user][:openid_url].nil? or
-            params[:user][:openid_url].empty?
-          # No OpenID so just move on to the terms
-          render :action => 'terms'
-        else
+      if @user
+        if @user.invalid?
+          # Something is wrong, so rerender the form
+          render :action => :new
+        elsif @user.terms_agreed?
+          # Already agreed to terms, so just show settings
+          redirect_to :action => :account, :display_name => @user.display_name
+        elsif params[:user] and params[:user][:openid_url]
           # Verify OpenID before moving on
           session[:new_user] = @user
           openid_verify(params[:user][:openid_url], @user)
         end
       else
-        # Something is wrong, so rerender the form
-        render :action => 'new'
-      end
-    elsif using_open_id?
-      # The redirect from the OpenID provider reenters here
-      # again and we need to pass the parameters through to
-      # the open_id_authentication function
-      @user = session.delete(:new_user)
-
-      openid_verify(nil, @user) do |user|
-      end
-
-      if @user.openid_url.nil? or @user.invalid?
-        render :action => 'new'
-      else
-        render :action => 'terms'
+        # Not logged in, so redirect to the login page
+        redirect_to :action => :login, :referer => request.request_uri
       end
     end
   end
@@ -77,6 +79,16 @@ class UserController < ApplicationController
       render :action => 'new'
     elsif params[:decline]
       redirect_to t('user.terms.declined')
+    elsif @user
+      if !@user.terms_agreed?
+        @user.consider_pd = params[:user][:consider_pd]
+        @user.terms_agreed = Time.now.getutc
+        if @user.save
+          flash[:notice] = t 'user.new.terms accepted'
+        end
+      end
+
+      redirect_to :action => :account, :display_name => @user.display_name
     else
       @user = User.new(params[:user])
 
@@ -219,8 +231,8 @@ class UserController < ApplicationController
       else
         password_authentication(params[:username], params[:password])
       end
-    else
-      @title = t 'user.login.title'
+    elsif flash[:notice].nil?
+      flash.now[:notice] =  t 'user.login.notice'
     end
   end
 
@@ -415,13 +427,13 @@ private
     # If we don't appear to have a user for this URL then ask the
     # provider for some extra information to help with signup
     if openid_url and User.find_by_openid_url(openid_url)
-      optional = nil
+      required = nil
     else
-      optional = [:nickname, :email]
+      required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
     end
 
     # Start the authentication
-    authenticate_with_open_id(openid_expand_url(openid_url), :optional => optional) do |result, identity_url, registration|
+    authenticate_with_open_id(openid_expand_url(openid_url), :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.
@@ -446,7 +458,9 @@ private
           # to the create account page with username and email filled
           # in if they have been given by the OpenID provider through
           # the simple registration protocol.
-          redirect_to :controller => 'user', :action => 'new', :nickname => registration['nickname'], :email => registration['email'], :openid => identity_url
+          nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"]
+          email = sreg["email"] || ax["http://axschema.org/contact/email"]
+          redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
         end
       elsif result.missing?
         failed_login t('user.login.openid missing provider')