Skip to content
Browse files

Moved the passwords, sessions, and users controllers into app

  • Loading branch information...
1 parent 02bb2fe commit 8c9deabd6aee78b754f1906c028a83278a99afc0 @jferris jferris committed Mar 5, 2009
View
64 app/controllers/passwords_controller.rb
@@ -0,0 +1,64 @@
+class PasswordsController < ActionController::Base
+
+ include Clearance::App::Controllers::ApplicationController
+
+ before_filter :forbid_missing_token, :only => [:edit, :update]
+ before_filter :forbid_non_existant_user, :only => [:edit, :update]
+ filter_parameter_logging :password, :password_confirmation
+
+ def new
+ end
+
+ def create
+ if user = User.find_by_email(params[:password][:email])
+ user.forgot_password!
+ ClearanceMailer.deliver_change_password user
+ flash[:notice] = "You will receive an email within the next few minutes. " <<
+ "It contains instructions for changing your password."
+ redirect_to url_after_create
+ else
+ flash.now[:notice] = "Unknown email"
+ render :action => :new
+ end
+ end
+
+ def edit
+ @user = User.find_by_id_and_token(params[:user_id], params[:token])
+ end
+
+ def update
+ @user = User.find_by_id_and_token(params[:user_id], params[:token])
+
+ if @user.update_password(params[:user][:password],
+ params[:user][:password_confirmation])
+ @user.confirm_email! unless @user.email_confirmed?
+ sign_user_in(@user)
+ redirect_to url_after_update
+ else
+ render :action => :edit
+ end
+ end
+
+ private
+
+ def forbid_missing_token
+ if params[:token].blank?
+ raise ActionController::Forbidden, "missing token"
+ end
+ end
+
+ def forbid_non_existant_user
+ unless User.find_by_id_and_token(params[:user_id], params[:token])
+ raise ActionController::Forbidden, "non-existant user"
+ end
+ end
+
+ def url_after_create
+ new_session_url
+ end
+
+ def url_after_update
+ root_url
+ end
+
+end
View
59 app/controllers/sessions_controller.rb
@@ -0,0 +1,59 @@
+class SessionsController < ActionController::Base
+
+ include Clearance::App::Controllers::ApplicationController
+
+ protect_from_forgery :except => :create
+ filter_parameter_logging :password
+
+ def create
+ @user = User.authenticate(params[:session][:email],
+ params[:session][:password])
+ if @user.nil?
+ flash.now[:notice] = "Bad email or password."
+ render :action => :new, :status => :unauthorized
+ else
+ if @user.email_confirmed?
+ remember(@user) if remember?
+ sign_user_in(@user)
+ flash[:notice] = "Signed in successfully."
+ redirect_back_or url_after_create
+ else
+ ClearanceMailer.deliver_confirmation(@user)
+ deny_access("User has not confirmed email. Confirmation email will be resent.")
+ end
+ end
+ end
+
+ def destroy
+ forget(current_user)
+ reset_session
+ flash[:notice] = "You have been signed out."
+ redirect_to url_after_destroy
+ end
+
+ private
+
+ def remember?
+ params[:session] && params[:session][:remember_me] == "1"
+ end
+
+ def remember(user)
+ user.remember_me!
+ cookies[:remember_token] = { :value => user.token,
+ :expires => user.token_expires_at }
+ end
+
+ def forget(user)
+ user.forget_me! if user
+ cookies.delete :remember_token
+ end
+
+ def url_after_create
+ root_url
+ end
+
+ def url_after_destroy
+ new_session_url
+ end
+
+end
View
30 app/controllers/users_controller.rb
@@ -0,0 +1,30 @@
+class UsersController < ActionController::Base
+
+ include Clearance::App::Controllers::ApplicationController
+
+ before_filter :redirect_to_root, :only => [:new, :create], :if => :signed_in?
+ filter_parameter_logging :password
+
+ def new
+ @user = User.new(params[:user])
+ end
+
+ def create
+ @user = User.new params[:user]
+ if @user.save
+ ClearanceMailer.deliver_confirmation @user
+ flash[:notice] = "You will receive an email within the next few minutes. " <<
+ "It contains instructions for confirming your account."
+ redirect_to url_after_create
+ else
+ render :action => "new"
+ end
+ end
+
+ private
+
+ def url_after_create
+ new_session_url
+ end
+
+end
View
6 generators/clearance/clearance_generator.rb
@@ -18,12 +18,6 @@ def manifest
m.file file, file
end
- ["app/controllers/passwords_controller.rb",
- "app/controllers/sessions_controller.rb",
- "app/controllers/users_controller.rb"].each do |file|
- m.file file, file
- end
-
m.directory File.join("app", "models")
["app/models/user.rb", "app/models/clearance_mailer.rb"].each do |file|
m.file file, file
View
3 generators/clearance/templates/app/controllers/passwords_controller.rb
@@ -1,3 +0,0 @@
-class PasswordsController < ApplicationController
- include Clearance::App::Controllers::PasswordsController
-end
View
3 generators/clearance/templates/app/controllers/sessions_controller.rb
@@ -1,3 +0,0 @@
-class SessionsController < ApplicationController
- include Clearance::App::Controllers::SessionsController
-end
View
3 generators/clearance/templates/app/controllers/users_controller.rb
@@ -1,3 +0,0 @@
-class UsersController < ApplicationController
- include Clearance::App::Controllers::UsersController
-end
View
3 lib/clearance.rb
@@ -1,9 +1,6 @@
require 'clearance/lib/extensions/errors'
require 'clearance/lib/extensions/rescue'
require 'clearance/app/controllers/application_controller'
-require 'clearance/app/controllers/passwords_controller'
-require 'clearance/app/controllers/sessions_controller'
-require 'clearance/app/controllers/users_controller'
require 'clearance/app/models/clearance_mailer'
require 'clearance/app/models/user'
require 'clearance/test/functional/confirmations_controller_test'
View
79 lib/clearance/app/controllers/passwords_controller.rb
@@ -1,79 +0,0 @@
-module Clearance
- module App
- module Controllers
- module PasswordsController
-
- def self.included(controller)
- controller.send(:include, Actions)
- controller.send(:include, PrivateMethods)
-
- controller.class_eval do
- before_filter :forbid_missing_token, :only => [:edit, :update]
- before_filter :forbid_non_existant_user, :only => [:edit, :update]
- filter_parameter_logging :password, :password_confirmation
- end
- end
-
- module Actions
- def new
- end
-
- def create
- if user = User.find_by_email(params[:password][:email])
- user.forgot_password!
- ClearanceMailer.deliver_change_password user
- flash[:notice] = "You will receive an email within the next few minutes. " <<
- "It contains instructions for changing your password."
- redirect_to url_after_create
- else
- flash.now[:notice] = "Unknown email"
- render :action => :new
- end
- end
-
- def edit
- @user = User.find_by_id_and_token(params[:user_id], params[:token])
- end
-
- def update
- @user = User.find_by_id_and_token(params[:user_id], params[:token])
-
- if @user.update_password(params[:user][:password],
- params[:user][:password_confirmation])
- @user.confirm_email! unless @user.email_confirmed?
- sign_user_in(@user)
- redirect_to url_after_update
- else
- render :action => :edit
- end
- end
- end
-
- module PrivateMethods
- private
-
- def forbid_missing_token
- if params[:token].blank?
- raise ActionController::Forbidden, "missing token"
- end
- end
-
- def forbid_non_existant_user
- unless User.find_by_id_and_token(params[:user_id], params[:token])
- raise ActionController::Forbidden, "non-existant user"
- end
- end
-
- def url_after_create
- new_session_url
- end
-
- def url_after_update
- root_url
- end
- end
-
- end
- end
- end
-end
View
74 lib/clearance/app/controllers/sessions_controller.rb
@@ -1,74 +0,0 @@
-module Clearance
- module App
- module Controllers
- module SessionsController
-
- def self.included(controller)
- controller.send(:include, Actions)
- controller.send(:include, PrivateMethods)
-
- controller.class_eval do
- protect_from_forgery :except => :create
- filter_parameter_logging :password
- end
- end
-
- module Actions
- def create
- @user = User.authenticate(params[:session][:email],
- params[:session][:password])
- if @user.nil?
- flash.now[:notice] = "Bad email or password."
- render :action => :new, :status => :unauthorized
- else
- if @user.email_confirmed?
- remember(@user) if remember?
- sign_user_in(@user)
- flash[:notice] = "Signed in successfully."
- redirect_back_or url_after_create
- else
- ClearanceMailer.deliver_confirmation(@user)
- deny_access("User has not confirmed email. Confirmation email will be resent.")
- end
- end
- end
-
- def destroy
- forget(current_user)
- reset_session
- flash[:notice] = "You have been signed out."
- redirect_to url_after_destroy
- end
- end
-
- module PrivateMethods
- private
-
- def remember?
- params[:session] && params[:session][:remember_me] == "1"
- end
-
- def remember(user)
- user.remember_me!
- cookies[:remember_token] = { :value => user.token,
- :expires => user.token_expires_at }
- end
-
- def forget(user)
- user.forget_me! if user
- cookies.delete :remember_token
- end
-
- def url_after_create
- root_url
- end
-
- def url_after_destroy
- new_session_url
- end
- end
-
- end
- end
- end
-end
View
45 lib/clearance/app/controllers/users_controller.rb
@@ -1,45 +0,0 @@
-module Clearance
- module App
- module Controllers
- module UsersController
-
- def self.included(controller)
- controller.send(:include, Actions)
- controller.send(:include, PrivateMethods)
-
- controller.class_eval do
- before_filter :redirect_to_root, :only => [:new, :create], :if => :signed_in?
- filter_parameter_logging :password
- end
- end
-
- module Actions
- def new
- @user = User.new(params[:user])
- end
-
- def create
- @user = User.new params[:user]
- if @user.save
- ClearanceMailer.deliver_confirmation @user
- flash[:notice] = "You will receive an email within the next few minutes. " <<
- "It contains instructions for confirming your account."
- redirect_to url_after_create
- else
- render :action => "new"
- end
- end
- end
-
- module PrivateMethods
- private
-
- def url_after_create
- new_session_url
- end
- end
-
- end
- end
- end
-end

0 comments on commit 8c9deab

Please sign in to comment.
Something went wrong with that request. Please try again.