diff --git a/ruby/lib/jam_ruby/app/views/layouts/user_mailer.html.erb b/ruby/lib/jam_ruby/app/views/layouts/user_mailer.html.erb index 3b11eebd1..4f184062f 100644 --- a/ruby/lib/jam_ruby/app/views/layouts/user_mailer.html.erb +++ b/ruby/lib/jam_ruby/app/views/layouts/user_mailer.html.erb @@ -39,7 +39,7 @@ -

This email was sent to you because you have an account at JamKazam.  Click here to unsubscribe and update your profile settings. +

This email was sent to you because you have an account at JamKazam.  Click here to unsubscribe and update your profile settings.

diff --git a/ruby/lib/jam_ruby/app/views/layouts/user_mailer.text.erb b/ruby/lib/jam_ruby/app/views/layouts/user_mailer.text.erb index 5c8262f63..78d40b50c 100644 --- a/ruby/lib/jam_ruby/app/views/layouts/user_mailer.text.erb +++ b/ruby/lib/jam_ruby/app/views/layouts/user_mailer.text.erb @@ -5,7 +5,7 @@ <% end %> <% unless @suppress_user_has_account_footer == true %> -This email was sent to you because you have an account at JamKazam / http://www.jamkazam.com. Visit your profile page to unsubscribe: http://www.jamkazam.com/client#/account/profile. +This email was sent to you because you have an account at JamKazam / http://www.jamkazam.com. Visit your profile page to unsubscribe: http://www.jamkazam.com/unsubscribe/<%=@user.unsubscribe_token%>. <% end %> Copyright <%= Time.now.year %> JamKazam, Inc. All rights reserved. diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index 4d9b3b21a..2b966b895 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -1533,6 +1533,28 @@ module JamRuby ShoppingCart.where("user_id=?", self).destroy_all end + def unsubscribe_token + self.class.create_access_token(self) + end + + # Verifier based on our application secret + def self.verifier + ActiveSupport::MessageVerifier.new(APP_CONFIG.secret_token) + end + + # Get a user from a token + def self.read_access_token(signature) + uid = self.verifier.verify(signature) + User.find_by_id uid + rescue ActiveSupport::MessageVerifier::InvalidSignature + nil + end + + # Class method for token generation + def self.create_access_token(user) + verifier.generate(user.id) + end + private def create_remember_token self.remember_token = SecureRandom.urlsafe_base64 diff --git a/ruby/spec/mailers/user_mailer_spec.rb b/ruby/spec/mailers/user_mailer_spec.rb index 62b1472c6..c3d041060 100644 --- a/ruby/spec/mailers/user_mailer_spec.rb +++ b/ruby/spec/mailers/user_mailer_spec.rb @@ -12,6 +12,7 @@ describe UserMailer do let(:user) { FactoryGirl.create(:user) } before(:each) do + stub_const("APP_CONFIG", app_config) UserMailer.deliveries.clear end diff --git a/ruby/spec/support/utilities.rb b/ruby/spec/support/utilities.rb index 94294b86c..61e2daca5 100644 --- a/ruby/spec/support/utilities.rb +++ b/ruby/spec/support/utilities.rb @@ -170,6 +170,10 @@ def app_config true end + def secret_token + 'foobar' + end + private @@ -240,4 +244,4 @@ end def friend(user1, user2) FactoryGirl.create(:friendship, user: user1, friend: user2) FactoryGirl.create(:friendship, user: user2, friend: user1) -end \ No newline at end of file +end diff --git a/web/app/controllers/users_controller.rb b/web/app/controllers/users_controller.rb index bcac17d3e..10619b08e 100644 --- a/web/app/controllers/users_controller.rb +++ b/web/app/controllers/users_controller.rb @@ -396,6 +396,19 @@ JS end end + def unsubscribe + unless @user = User.read_access_token(params[:user_token]) + redirect_to '/' + end if params[:user_token].present? + + if request.get? + + elsif request.post? + @user.subscribe_email = false + @user.save! + end + end + private def is_native_client diff --git a/web/app/views/users/unsubscribe.html.haml b/web/app/views/users/unsubscribe.html.haml new file mode 100644 index 000000000..158578179 --- /dev/null +++ b/web/app/views/users/unsubscribe.html.haml @@ -0,0 +1,12 @@ += provide(:title, 'Unsubscribe') + +- if request.get? + %h2 Unsubscribe from all JamKazam email for address #{@user} ? + %br + = form_tag("") do + = submit_tag('Unsubscribe') + = hidden_field_tag(:user_token, params[:user_token]) +- elsif request.post? + - if @user && ! @user.subscribe_email + %h2 You have been unsubscribed. + diff --git a/web/config/routes.rb b/web/config/routes.rb index b2e89c9d0..8d66bcc22 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -79,6 +79,8 @@ SampleApp::Application.routes.draw do match '/reset_password_token' => 'users#reset_password_token', :via => :get match '/reset_password_complete' => 'users#reset_password_complete', :via => :post + match '/unsubscribe/:user_token' => 'users#unsubscribe', via: [:get, :post] + # email update match '/confirm_email' => 'users#finalize_update_email', :as => 'confirm_email' # NOTE: if you change this, you break outstanding email changes because links in user inboxes are broken