+ session.delete(:remember_me)
+ session.delete(:referer)
+ end
+
+ ##
+ #
+ def unconfirmed_login(user)
+ session[:token] = user.tokens.create.token
+
+ redirect_to :action => "confirm", :display_name => user.display_name
+
+ session.delete(:remember_me)
+ session.delete(:referer)
+ end
+
+ ##
+ # update a user's details
+ def update_user(user, params)
+ user.display_name = params[:user][:display_name]
+ user.new_email = params[:user][:new_email]
+
+ unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
+ user.pass_crypt = params[:user][:pass_crypt]
+ user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
+ end
+
+ if params[:user][:description] != user.description
+ user.description = params[:user][:description]
+ user.description_format = "markdown"
+ end
+
+ user.languages = params[:user][:languages].split(",")
+
+ case params[:image_action]
+ when "new" then
+ user.image = params[:user][:image]
+ user.image_use_gravatar = false
+ when "delete" then
+ user.image = nil
+ user.image_use_gravatar = false
+ when "gravatar" then
+ user.image = nil
+ user.image_use_gravatar = true
+ end
+
+ user.home_lat = params[:user][:home_lat]
+ user.home_lon = params[:user][:home_lon]
+
+ user.preferred_editor = if params[:user][:preferred_editor] == "default"
+ nil
+ else
+ params[:user][:preferred_editor]
+ end
+
+ if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
+ user.auth_provider = nil
+ user.auth_uid = nil
+ end
+
+ if user.save
+ set_locale
+
+ if user.new_email.blank? || user.new_email == user.email
+ flash.now[:notice] = t "user.account.flash update success"
+ else
+ user.email = user.new_email
+
+ if user.valid?
+ flash.now[:notice] = t "user.account.flash update success confirm needed"
+
+ begin
+ Notifier.email_confirm(user, user.tokens.create).deliver_now
+ rescue
+ # Ignore errors sending email
+ end
+ else
+ @user.errors.set(:new_email, @user.errors.get(:email))
+ @user.errors.set(:email, [])
+ end
+
+ user.restore_email!
+ end
+ end
+ end
+
+ ##
+ # require that the user is a administrator, or fill out a helpful error message
+ # and return them to the user page.
+ def require_administrator
+ if @user && !@user.administrator?
+ flash[:error] = t("user.filter.not_an_administrator")
+
+ if params[:display_name]
+ redirect_to :action => "view", :display_name => params[:display_name]
+ else
+ redirect_to :action => "login", :referer => request.fullpath
+ end
+ elsif !@user
+ redirect_to :action => "login", :referer => request.fullpath
+ end
+ end
+
+ ##
+ # require that the user in the URL is the logged in user
+ def require_self
+ if params[:display_name] != @user.display_name
+ render :text => "", :status => :forbidden
+ end
+ end
+
+ ##
+ # ensure that there is a "this_user" instance variable
+ def lookup_user_by_id
+ @this_user = User.find(params[:id])
+ end
+
+ ##
+ # ensure that there is a "this_user" instance variable
+ def lookup_user_by_name
+ @this_user = User.find_by(:display_name => params[:display_name])
+ rescue ActiveRecord::RecordNotFound
+ redirect_to :action => "view", :display_name => params[:display_name] unless @this_user
+ end
+
+ ##
+ #
+ def disable_terms_redirect
+ # this is necessary otherwise going to the user terms page, when
+ # having not agreed already would cause an infinite redirect loop.
+ # it's .now so that this doesn't propagate to other pages.
+ flash.now[:skip_terms] = true
+ end
+
+ ##
+ # return permitted user parameters
+ def user_params
+ params.require(:user).permit(:email, :email_confirmation, :display_name,
+ :auth_provider, :auth_uid,
+ :pass_crypt, :pass_crypt_confirmation)
+ end
+
+ ##
+ # check signup acls
+ def check_signup_allowed(email = nil)
+ domain = if email.nil?
+ nil
+ else
+ email.split("@").last
+ end
+
+ if blocked = Acl.no_account_creation(request.remote_ip, domain)
+ logger.info "Blocked signup from #{request.remote_ip} for #{email}"
+
+ render :action => "blocked"
+ end
+
+ !blocked
+ end
+
+ ##
+ # check if this user has a gravatar and set the user pref is true
+ def gravatar_enable(user)
+ # code from example https://en.gravatar.com/site/implement/images/ruby/
+ return false if user.image.present?
+ hash = Digest::MD5.hexdigest(user.email.downcase)
+ url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
+ response = OSM.http_client.get(URI.parse(url))
+ oldsetting = user.image_use_gravatar
+ user.image_use_gravatar = response.success?
+ oldsetting != user.image_use_gravatar
+ end
+
+ ##
+ # display a message about th current status of the gravatar setting
+ def gravatar_status_message(user)
+ if user.image_use_gravatar
+ t "user.account.gravatar.enabled"
+ else
+ t "user.account.gravatar.disabled"
+ end
+ end
+end