From ba7422634683d92e8f55e7c8ed22c47788194a96 Mon Sep 17 00:00:00 2001
From: Neil Lalonde <neillalonde@gmail.com>
Date: Tue, 21 Jan 2014 12:42:20 -0500
Subject: [PATCH] FIX: invited users get clearer messaging when setting a
 password for the first time

---
 .../templates/user/preferences.js.handlebars     |  9 ++++++++-
 app/mailers/user_notifications.rb                |  4 +++-
 app/models/user.rb                               |  4 ++++
 app/serializers/current_user_serializer.rb       | 11 ++++++++++-
 app/views/users/password_reset.html.erb          | 10 ++++++++--
 config/locales/client.en.yml                     |  1 +
 config/locales/server.en.yml                     | 16 ++++++++++++++--
 7 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/app/assets/javascripts/discourse/templates/user/preferences.js.handlebars b/app/assets/javascripts/discourse/templates/user/preferences.js.handlebars
index 903003ed1b3..0d0f723bd74 100644
--- a/app/assets/javascripts/discourse/templates/user/preferences.js.handlebars
+++ b/app/assets/javascripts/discourse/templates/user/preferences.js.handlebars
@@ -43,7 +43,14 @@
     <div class="control-group">
       <label class="control-label">{{i18n user.password.title}}</label>
       <div class="controls">
-        <a href="#" {{action changePassword}} class='btn'><i class="fa fa-envelope"></i>{{i18n user.change_password.action}}</a> {{passwordProgress}}
+        <a href="#" {{action changePassword}} class='btn'><i class="fa fa-envelope"></i>
+          {{#if no_password}}
+            {{i18n user.change_password.set_password}}
+          {{else}}
+            {{i18n user.change_password.action}}
+          {{/if}}
+        </a>
+        {{passwordProgress}}
       </div>
     </div>
 
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index 1a41b04ce7f..06b93155e2e 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -25,7 +25,9 @@ class UserNotifications < ActionMailer::Base
   end
 
   def forgot_password(user, opts={})
-    build_email(user.email, template: "user_notifications.forgot_password", email_token: opts[:email_token])
+    build_email( user.email,
+                 template: user.has_password? ? "user_notifications.forgot_password" : "user_notifications.set_password",
+                 email_token: opts[:email_token])
   end
 
   def digest(user, opts={})
diff --git a/app/models/user.rb b/app/models/user.rb
index fb494487756..ba82d50f41c 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -260,6 +260,10 @@ class User < ActiveRecord::Base
     !!@password_required
   end
 
+  def has_password?
+    password_hash.present?
+  end
+
   def password_validator
     PasswordValidator.new(attributes: :password).validate_each(self, :password, @raw_password)
   end
diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rb
index 74ef7cbbb11..1ddba2f7160 100644
--- a/app/serializers/current_user_serializer.rb
+++ b/app/serializers/current_user_serializer.rb
@@ -15,7 +15,8 @@ class CurrentUserSerializer < BasicUserSerializer
              :dynamic_favicon,
              :trust_level,
              :can_edit,
-             :can_invite_to_forum
+             :can_invite_to_forum,
+             :no_password
 
   def include_site_flagged_posts_count?
     object.staff?
@@ -45,4 +46,12 @@ class CurrentUserSerializer < BasicUserSerializer
     scope.can_invite_to_forum?
   end
 
+  def no_password
+    true
+  end
+
+  def include_no_password?
+    !object.has_password?
+  end
+
 end
diff --git a/app/views/users/password_reset.html.erb b/app/views/users/password_reset.html.erb
index b5b73e2778c..89d56585393 100644
--- a/app/views/users/password_reset.html.erb
+++ b/app/views/users/password_reset.html.erb
@@ -23,14 +23,20 @@
     </p>
   <% else %>
     <%if @user.present? %>
-      <h3><%= t 'password_reset.choose_new' %></h3>
+      <h3>
+        <% if @user.has_password? %>
+          <%= t 'password_reset.choose_new' %>
+        <% else %>
+          <%= t 'password_reset.choose' %>
+        <% end %>
+      </h3>
 
       <%=form_tag({}, method: :put) do %>
         <p>
         <input id="user_password" name="password" size="30" type="password">
         </p>
         <p>
-        <%=submit_tag(t('password_reset.update'), class: 'btn')%>
+        <%=submit_tag( @user.has_password? ? t('password_reset.update') : t('password_reset.save'), class: 'btn')%>
         </p>
       <%end%>
     <%end%>
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 35c33a3b95c..e1c96f6398d 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -246,6 +246,7 @@ en:
         in_progress: "(sending email)"
         error: "(error)"
         action: "Send Password Reset Email"
+        set_password: "Set Password"
 
       change_about:
         title: "Change About Me"
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 07c574e9fd2..ce46a7a9848 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -300,8 +300,10 @@ en:
   password_reset:
     no_token: "Sorry, your token has expired. Please try resetting your password again."
     choose_new: "Please choose a new password"
-    update: 'update password'
-    title: 'reset password'
+    choose: "Please choose a password"
+    update: 'Update Password'
+    save: 'Set Password'
+    title: 'Reset Password'
     success: "You successfully changed your password and are now logged in."
     success_unapproved: "You successfully changed your password."
     continue: "Continue to %{site_name}"
@@ -1218,6 +1220,16 @@ en:
         Click the following link to choose a new password:
         %{base_url}/users/password-reset/%{email_token}
 
+    set_password:
+      subject_template: "[%{site_name}] Set Password"
+      text_body_template: |
+        Somebody asked to add a password to your account on [%{site_name}](%{base_url}). Alternatively, you can log in using any supported online service (Google, Facebook, etc) that is associated with this validated email address.
+
+        If you did not make this request, you can safely ignore this email.
+
+        Click the following link to choose a password:
+        %{base_url}/users/password-reset/%{email_token}
+
     authorize_email:
       subject_template: "[%{site_name}] Confirm your new email address"
       text_body_template: |