diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 4cdd8f2..a4b9dc6 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,6 +1,6 @@ class UsersController < ApplicationController layout "application" - before_filter :authenticate_user, :except => [:new, :create] + before_filter :authenticate_user, :except => [:new, :create, :activate] def index @users = User.all @@ -38,15 +38,35 @@ def new def create @user = User.new(params[:user]) - if @user.save - flash[:notice] = 'Registration successful.' + # Saving without session maintenance to skip + # auto-login which can't happen here because + # the User has not yet been activated + if @user.save_without_session_maintenance + @user.deliver_activation_instructions + flash[:notice] = 'Registration successful. Please check your email to activate your account.' redirect_to shares_path else flash[:error] = 'Registration failed. Please try again.' redirect_to root_path end end - + + + def activate + @user = User.find_using_perishable_token(params[:token], 0) # Token does not expire. + if !@user + redirect_to root_url + else + if !@user.active? + @user.activated = true + @user.save + UserSession.create(@user) # Log the user in. + flash[:notice] = 'Account activated successfully.' + end + + redirect_to shares_path + end + end def edit @user = current_user diff --git a/app/models/activation_mailer.rb b/app/models/activation_mailer.rb new file mode 100644 index 0000000..ef95569 --- /dev/null +++ b/app/models/activation_mailer.rb @@ -0,0 +1,11 @@ +class ActivationMailer < ActionMailer::Base + + def activation_instructions(user, sent_at = Time.now) + from "A-List Account Activation " + reply_to "no-reply@alist.heroku.com" + recipients user.email + subject "Activate Your A-List Account" + body :account_activation_url => activate_url(user.perishable_token) + end + +end diff --git a/app/models/user.rb b/app/models/user.rb index 52a4ef3..014f413 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -28,4 +28,10 @@ def unread_shares def shares_from(submitter) shares.select {|s| s.link.submitter == submitter } end + + def deliver_activation_instructions + reset_perishable_token! + ActivationMailer.deliver_activation_instructions(self) + end + end diff --git a/app/views/activation_mailer/activation_instructions.erb b/app/views/activation_mailer/activation_instructions.erb new file mode 100644 index 0000000..57991cc --- /dev/null +++ b/app/views/activation_mailer/activation_instructions.erb @@ -0,0 +1,5 @@ +Thank you for creating an A-List account! Click the URL below to activate your account. + +<%= @account_activation_url %> + +If the above URL does not work try copying and pasting it into your browser. If you continue to have problem, please feel free to contact us. \ No newline at end of file diff --git a/app/views/activation_mailer/activation_instructions.text.html.erb b/app/views/activation_mailer/activation_instructions.text.html.erb new file mode 100644 index 0000000..cd7d972 --- /dev/null +++ b/app/views/activation_mailer/activation_instructions.text.html.erb @@ -0,0 +1,5 @@ +

Thank you for creating an A-List account! Click the URL below to activate your account.

+ +

<%= link_to "Activate", @account_activation_url %>

+ +

If the above URL does not work try copying and pasting it into your browser. If you continue to have problem, please feel free to contact us.

\ No newline at end of file diff --git a/app/views/activation_mailer/activation_instructions.text.plain.erb b/app/views/activation_mailer/activation_instructions.text.plain.erb new file mode 100644 index 0000000..5e119b5 --- /dev/null +++ b/app/views/activation_mailer/activation_instructions.text.plain.erb @@ -0,0 +1,5 @@ +Thank you for creating an A-List account! Click the URL below to activate your account. + +<%= link_to "Activate", @account_activation_url %> + +If the above URL does not work try copying and pasting it into your browser. If you continue to have problem, please feel free to contact us. \ No newline at end of file diff --git a/app/views/user_sessions/new.html.erb b/app/views/user_sessions/new.html.erb index 73dff1b..c1c1a26 100644 --- a/app/views/user_sessions/new.html.erb +++ b/app/views/user_sessions/new.html.erb @@ -1,7 +1,7 @@

Log In

<% form_for @user_session do |f| %> - <%= f.error_messages %> + <%= f.error_messages :header_message => "", :message => "Problem logging in:" %>

<%= f.label :email %>
<%= f.text_field :email %> diff --git a/config/routes.rb b/config/routes.rb index ab07b96..c07c6b2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,8 +1,9 @@ ActionController::Routing::Routes.draw do |map| - map.log_in "log_in", :controller => "user_sessions", :action => "new" - map.log_out "log_out", :controller => "user_sessions", :action => "destroy" + map.log_in "/log_in", :controller => "user_sessions", :action => "new" + map.log_out "/log_out", :controller => "user_sessions", :action => "destroy" map.connect "/users/friends", :controller => "users", :action => "friends" + map.activate "/users/activate/:token", :controller => "users", :action => "activate" map.resources :shares map.resources :links diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index 0bc4a41..86316c5 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -33,6 +33,15 @@ a:hover { color: #000; } +#errorExplanation { + color: #CC0000; +} + + +#errorExplanation ul { + list-style: disc; +} + #main { background: #FCFCFC; max-width: 1000px; diff --git a/test/fixtures/activation_mailer/activation_instructions b/test/fixtures/activation_mailer/activation_instructions new file mode 100644 index 0000000..45ecfc8 --- /dev/null +++ b/test/fixtures/activation_mailer/activation_instructions @@ -0,0 +1,3 @@ +ActivationMailer#activation_instructions + +Find me in app/views/activation_mailer/activation_instructions.erb diff --git a/test/unit/activation_mailer_test.rb b/test/unit/activation_mailer_test.rb new file mode 100644 index 0000000..33c3fde --- /dev/null +++ b/test/unit/activation_mailer_test.rb @@ -0,0 +1,12 @@ +require 'test_helper' + +class ActivationMailerTest < ActionMailer::TestCase + test "activation_instructions" do + @expected.subject = 'ActivationMailer#activation_instructions' + @expected.body = read_fixture('activation_instructions') + @expected.date = Time.now + + assert_equal @expected.encoded, ActivationMailer.create_activation_instructions(@expected.date).encoded + end + +end