Module: Clearance::App::Controllers::PasswordsController

Defined in:
lib/clearance/app/controllers/passwords_controller.rb

Class Method Summary collapse

Class Method Details

.included(controller) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/clearance/app/controllers/passwords_controller.rb', line 6

def self.included(controller)
  controller.class_eval do
    
    before_filter :existing_user?, :only => [:edit, :update]
    filter_parameter_logging :password, :password_confirmation
    
    def new
    end

    def create
      user = User.find_by_email(params[:password][:email])
      if user.nil?
        flash.now[:notice] = "Unknown email"
        render :action => :new
      else
        user.forgot_password!
        ClearanceMailer.deliver_change_password user
        flash[:notice] = "Details for changing your password have been sent"
        redirect_to url_after_create
      end
    end

    def edit
    end

    def update
      if @user.update_password(params[:user])
        sign_user_in(@user)
        redirect_to url_after_update
      else
        render :action => :edit
      end
    end
    
    private
    
    def existing_user?
      @user = User.find_by_id_and_token(params[:user_id], params[:token])
      if @user.nil?
        render :nothing => true, :status => :not_found
      end
    end

    def url_after_create
      new_session_url
    end
    
    def url_after_update
      root_url
    end
    
  end
end