-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc7a237
commit 6401c82
Showing
15 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Place all the behaviors and hooks related to the matching controller here. | ||
// All this logic will automatically be available in application.js. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
Place all the styles related to the matching controller here. | ||
They will automatically be included in application.css. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class PasswordResetsController < ApplicationController | ||
before_filter :require_no_user | ||
before_filter :load_account_using_perishable_token, :only => [:edit, :update] | ||
|
||
def new | ||
render | ||
end | ||
|
||
def create | ||
@account = Account.find_by_email(params[:email]) | ||
if @account | ||
@account.deliver_password_reset_instructions! | ||
flash[:notice] = "Instructions to reset your password have been emailed to you. Please check your email." | ||
redirect_to login_path | ||
else | ||
flash[:notice] = "No account was found with that email address" | ||
render :action => :new | ||
end | ||
end | ||
|
||
def edit | ||
render | ||
end | ||
|
||
def update | ||
@account.password = params[:account][:password] | ||
@account.password_confirmation = params[:account][:password_confirmation] | ||
if @account.save | ||
flash[:notice] = "Password successfully updated" | ||
redirect_to root_path | ||
else | ||
render :action => :edit | ||
end | ||
end | ||
|
||
private | ||
|
||
def load_account_using_perishable_token | ||
@account = Account.find_using_perishable_token(params[:id]) | ||
unless @account | ||
flash[:notice] = "We're sorry, but we could not locate your account. " + | ||
"If you are having issues try copying and pasting the URL " + | ||
"from your email into your browser or restarting the " + | ||
"reset password process." | ||
redirect_to login_path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module PasswordResetsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
="Hi [#{@account.username}]!" | ||
|
||
="Password Reset: #{edit_password_reset_url(@account.perishable_token)}" | ||
|
||
Navigate to the above link to reset your password. | ||
The link will expire in 24 hours. | ||
|
||
Cheers, | ||
[plans] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
%h1 Change My Password | ||
|
||
=form_for @account, :url => password_reset_path, :method => :put do |f| | ||
= f.error_messages | ||
= f.label :password | ||
= f.password_field :password | ||
%br | ||
= f.label :password_confirmation | ||
= f.password_field :password_confirmation | ||
%br | ||
= f.submit "Update my password" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
%h1 Forgot Password | ||
|
||
- if flash[:notice] | ||
%p.notice | ||
= flash[:notice] | ||
|
||
%p Fill out the form below and instructions to reset your password will be emailed to you: | ||
|
||
=form_tag password_resets_path do | ||
|
||
%label Email: | ||
= text_field_tag "email" | ||
= submit_tag "Reset my password" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
db/migrate/20130125033218_add_accounts_perishable_token.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class AddAccountsPerishableToken < ActiveRecord::Migration | ||
def up | ||
add_column :accounts, :perishable_token, :string, :default => "", :null => false | ||
add_index :accounts, :perishable_token | ||
end | ||
|
||
def down | ||
remove_column :accounts, :perishable_token | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'spec_helper' | ||
|
||
describe PasswordResetsController do | ||
describe "new" do | ||
subject { get :new } | ||
it { response.should be_success } | ||
it { should render_template :new } | ||
end | ||
|
||
context "with user" do | ||
before do | ||
@account = Account.create!( :username => "foobar", :password => "foobar", :password_confirmation => "foobar", :email => '[email protected]' ) | ||
ActionMailer::Base.deliveries.clear | ||
end | ||
|
||
describe "create" do | ||
before { post :create, :email => @account.email } | ||
it { response.should be_redirect } | ||
it "should send an email" do | ||
email = ActionMailer::Base.deliveries.first | ||
email.to[0].should == @account.email | ||
email.body.should =~ /#{@account.reload.perishable_token}/ | ||
end | ||
end | ||
|
||
describe "failed create" do | ||
before { post :create, :password_reset => { :email => '[email protected]'} } | ||
it { response.should be_success } | ||
it { should render_template :new } | ||
it "should not have sent an email" do | ||
ActionMailer::Base.deliveries.first.should == nil | ||
end | ||
end | ||
end | ||
|
||
context "with a reset token" do | ||
before do | ||
@account = Account.create!( :username => "foobar", :password => "foobar", :password_confirmation => "foobar", :email => '[email protected]' ) | ||
@account.reset_perishable_token! | ||
end | ||
|
||
describe "edit" do | ||
before { get :edit, :id => @account.perishable_token } | ||
it { should render_template :edit } | ||
it { @controller.current_account.should == nil} | ||
end | ||
|
||
describe "update" do | ||
before do | ||
@password_was = @account.reload.crypted_password | ||
put :update, :id => @account.perishable_token, :account => {:password => 'newpassword', :password_confirmation => 'newpassword'} | ||
end | ||
it { response.should be_redirect } | ||
it { @controller.current_account.should == @account} | ||
it "should have reset the password" do | ||
@password_was.should_not == @account.reload.crypted_password | ||
end | ||
end | ||
end | ||
|
||
end |