Permalink
Browse files

generator with authlogic basic, user activation and password resetting

  • Loading branch information...
aledalgrande aledalgrande
aledalgrande authored and aledalgrande committed Aug 28, 2009
1 parent 9754fe4 commit 4abcf20a2c5533bfb07509935e1f42948f880860
Showing with 726 additions and 0 deletions.
  1. +1 −0 USAGE
  2. +82 −0 authlogic_generator.rb
  3. +45 −0 lib/insert_routes.rb
  4. +1 −0 templates/USAGE
  5. +82 −0 templates/authlogic_generator.rb
  6. +17 −0 templates/controllers/activations_controller.rb
  7. +58 −0 templates/controllers/password_resets_controller.rb
  8. +26 −0 templates/controllers/user_sessions_controller.rb
  9. +66 −0 templates/controllers/users_controller.rb
  10. +40 −0 templates/lib/authlogic_user.rb
  11. +45 −0 templates/lib/insert_routes.rb
  12. +39 −0 templates/migrate/create_users_and_sessions.rb
  13. +33 −0 templates/models/notifier.rb
  14. +29 −0 templates/models/user.rb
  15. +3 −0 templates/models/user_session.rb
  16. +8 −0 templates/views/activations/new.html.erb
  17. +13 −0 templates/views/layouts/_usernav.html.erb
  18. +5 −0 templates/views/notifier/activation_confirmation.text.html.erb
  19. +5 −0 templates/views/notifier/activation_confirmation.text.plain.erb
  20. +5 −0 templates/views/notifier/activation_instructions.text.html.erb
  21. +5 −0 templates/views/notifier/activation_instructions.text.plain.erb
  22. +5 −0 templates/views/notifier/password_reset_instructions.text.html.erb
  23. +5 −0 templates/views/notifier/password_reset_instructions.text.plain.erb
  24. +2 −0 templates/views/password_resets/activate.html.erb
  25. +14 −0 templates/views/password_resets/index.html.erb
  26. +15 −0 templates/views/password_resets/show.html.erb
  27. +19 −0 templates/views/user_sessions/new.html.erb
  28. +12 −0 templates/views/users/_form.html.erb
  29. +9 −0 templates/views/users/edit.html.erb
  30. +9 −0 templates/views/users/new.html.erb
  31. +27 −0 templates/views/users/private.html.erb
  32. +1 −0 templates/views/users/public.html.erb
View
1 USAGE
@@ -0,0 +1 @@
+./script/generate authlogic
View
@@ -0,0 +1,82 @@
+require File.expand_path(File.dirname(__FILE__) + "/lib/insert_routes.rb")
+
+class AuthlogicGenerator < Rails::Generator::Base
+ default_options :skip_activation => false,
+ :skip_password_reset => false,
+ :skip_migration => false,
+ :skip_routes => false
+
+ def manifest
+ recorded_session = record do |m|
+
+ m.directory 'app/views/layouts'
+ m.directory 'app/views/notifier'
+ m.directory 'app/views/user_sessions'
+ m.directory 'app/views/users'
+
+ m.file 'controllers/user_sessions_controller.rb', 'app/controllers/user_sessions_controller.rb'
+ m.template 'controllers/users_controller.rb', 'app/controllers/users_controller.rb'
+ m.template 'models/notifier.rb', 'app/models/notifier.rb'
+ m.template 'models/user.rb', 'app/models/user.rb'
+ m.file 'models/user_session.rb', 'app/models/user_session.rb'
+ m.template 'views/layouts/_usernav.html.erb', 'app/views/layouts/_usernav.html.erb'
+ m.file 'views/notifier/activation_confirmation.text.html.erb', 'app/views/notifier/activation_confirmation.text.html.erb'
+ m.file 'views/notifier/activation_confirmation.text.plain.erb', 'app/views/notifier/activation_confirmation.text.plain.erb'
+ m.file 'views/user_sessions/new.html.erb', 'app/views/user_sessions/new.html.erb'
+ m.file 'views/users/_form.html.erb', 'app/views/users/_form.html.erb'
+ m.file 'views/users/edit.html.erb', 'app/views/users/edit.html.erb'
+ m.file 'views/users/new.html.erb', 'app/views/users/new.html.erb'
+ m.file 'views/users/private.html.erb', 'app/views/users/private.html.erb'
+ m.file 'views/users/public.html.erb', 'app/views/users/public.html.erb'
+ m.file 'lib/authlogic_user.rb', 'lib/authlogic_user.rb'
+
+ unless options[:skip_password_reset]
+ m.directory 'app/views/password_resets'
+ m.template 'controllers/password_resets_controller.rb', 'app/controllers/password_resets_controller.rb'
+ m.file 'views/notifier/password_reset_instructions.text.html.erb', 'app/views/notifier/password_reset_instructions.text.html.erb'
+ m.file 'views/notifier/password_reset_instructions.text.plain.erb', 'app/views/notifier/password_reset_instructions.text.plain.erb'
+ m.file 'views/password_resets/index.html.erb', 'app/views/password_resets/index.html.erb'
+ m.file 'views/password_resets/show.html.erb', 'app/views/password_resets/show.html.erb'
+ end
+
+ unless options[:skip_activation]
+ m.directory 'app/views/activations'
+ m.file 'controllers/activations_controller.rb', 'app/controllers/activations_controller.rb'
+ m.file 'views/activations/new.html.erb', 'app/views/activations/new.html.erb'
+ m.file 'views/notifier/activation_instructions.text.html.erb', 'app/views/notifier/activation_instructions.text.html.erb'
+ m.file 'views/notifier/activation_instructions.text.plain.erb', 'app/views/notifier/activation_instructions.text.plain.erb'
+ m.file 'views/password_resets/activate.html.erb', 'app/views/password_resets/activate.html.erb' unless options[:skip_password_reset]
+ end
+
+ unless options[:skip_routes]
+ m.route_resources :users
+ m.route_resources :password_resets unless options[:skip_password_reset]
+ m.route_resource :user_session
+ m.route_name('login', '/login', { :controller => 'user_sessions', :action => 'new' })
+ m.route_name('logout', '/logout', { :controller => 'user_sessions', :action => 'destroy' })
+ m.route_name('register', '/register/:activation_code', { :controller => 'activations', :action => 'new' }) unless options[:skip_activation]
+ end
+
+ unless options[:skip_migration]
+ m.migration_template "migrate/create_users_and_sessions.rb", "db/migrate", :migration_file_name => "create_users_and_sessions" unless options[:skip_migration]
+ end
+
+ end
+
+ puts "\n1) Please add these lines to your ApplicationController\n\ninclude AuthlogicUser\nfilter_parameter_logging :password, :password_confirmation\nhelper_method :current_user_session, :current_user\n\n"
+ puts "2) Please add these lines to your environment and launch rake gems:install\n\nconfig.gem 'binarylogic-authlogic', :lib => 'authlogic', :source => 'http://gems.github.com'\n\n"
+ puts "3) Please remember to launch rake db:migrate\n\n" unless options[:skip_migration]
+
+ recorded_session
+ end
+
+ protected
+ def add_options!(opt)
+ opt.separator ''
+ opt.separator 'Options:'
+ opt.on("--skip-activation", "Don't generate activation views and controller.") { |v| options[:skip_activation] = true }
+ opt.on("--skip-password-reset", "Don't generate controller and views for password resetting.") { |v| options[:skip_password_reset] = true }
+ opt.on("--skip-migration", "Don't generate a migration file for this model.") { |v| options[:skip_migration] = true }
+ opt.on("--skip-routes", "Don't add lines to the route file.") { |v| options[:skip_routes] = true }
+ end
+end
View
@@ -0,0 +1,45 @@
+Rails::Generator::Commands::Create.class_eval do
+ def route_resources(*resources)
+ write_route 'resources', resources
+ end
+
+ def route_resource(*resources)
+ write_route 'resource', resources
+ end
+
+ def route_name(name, path, options={})
+ write_route nil, nil, name, path, options
+ end
+end
+
+Rails::Generator::Commands::Destroy.class_eval do
+ def route_resource(*resources)
+ remove_route look_for, 'resource', resource_list
+ end
+
+ def route_resource(*resources)
+ remove_route look_for, 'resource', resource_list
+ end
+
+ def route_name(name, path, options = {})
+ remove_route look_for, nil, nil, name, path, options
+ end
+end
+
+def write_route(type=nil, resources=nil, name=nil, path=nil, options={})
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ') if name.nil?
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
+ logger.route name.nil? ? "map.#{type} #{resource_list}" : "map.#{name} '#{path}', :controller => '#{options[:controller]}', :action => '#{options[:action]}'"
+ unless options[:pretend]
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
+ name.nil? ? "#{match}\n map.#{type} #{resource_list}" : "#{match}\n map.#{name} '#{path}', :controller => '#{options[:controller]}', :action => '#{options[:action]}'"
+ end
+ end
+end
+
+def remove_route(look_for, type=nil, resource_list=nil, name=nil, path=nil, options={})
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ') if name.nil?
+ look_for = name.nil? ? "\n map.resources #{resource_list}\n" : "\n map.#{name} '#{path}', :controller => '#{options[:controller]}', :action => '#{options[:action]}'"
+ logger.route name.nil? ? "map.#{type} #{resource_list}" : "map.#{name} '#{path}', :controller => '#{options[:controller]}', :action => '#{options[:action]}'"
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
+end
View
@@ -0,0 +1 @@
+./script/generate authlogic
@@ -0,0 +1,82 @@
+require File.expand_path(File.dirname(__FILE__) + "/lib/insert_routes.rb")
+
+class AuthlogicGenerator < Rails::Generator::Base
+ default_options :skip_activation => false,
+ :skip_password_reset => false,
+ :skip_migration => false,
+ :skip_routes => false
+
+ def manifest
+ recorded_session = record do |m|
+
+ m.directory 'app/views/layouts'
+ m.directory 'app/views/notifier'
+ m.directory 'app/views/user_sessions'
+ m.directory 'app/views/users'
+
+ m.file 'controllers/user_sessions_controller.rb', 'app/controllers/user_sessions_controller.rb'
+ m.template 'controllers/users_controller.rb', 'app/controllers/users_controller.rb'
+ m.template 'models/notifier.rb', 'app/models/notifier.rb'
+ m.template 'models/user.rb', 'app/models/user.rb'
+ m.file 'models/user_session.rb', 'app/models/user_session.rb'
+ m.template 'views/layouts/_usernav.html.erb', 'app/views/layouts/_usernav.html.erb'
+ m.file 'views/notifier/activation_confirmation.text.html.erb', 'app/views/notifier/activation_confirmation.text.html.erb'
+ m.file 'views/notifier/activation_confirmation.text.plain.erb', 'app/views/notifier/activation_confirmation.text.plain.erb'
+ m.file 'views/user_sessions/new.html.erb', 'app/views/user_sessions/new.html.erb'
+ m.file 'views/users/_form.html.erb', 'app/views/users/_form.html.erb'
+ m.file 'views/users/edit.html.erb', 'app/views/users/edit.html.erb'
+ m.file 'views/users/new.html.erb', 'app/views/users/new.html.erb'
+ m.file 'views/users/private.html.erb', 'app/views/users/private.html.erb'
+ m.file 'views/users/public.html.erb', 'app/views/users/public.html.erb'
+ m.file 'lib/authlogic_user.rb', 'lib/authlogic_user.rb'
+
+ unless options[:skip_password_reset]
+ m.directory 'app/views/password_resets'
+ m.template 'controllers/password_resets_controller.rb', 'app/controllers/password_resets_controller.rb'
+ m.file 'views/notifier/password_reset_instructions.text.html.erb', 'app/views/notifier/password_reset_instructions.text.html.erb'
+ m.file 'views/notifier/password_reset_instructions.text.plain.erb', 'app/views/notifier/password_reset_instructions.text.plain.erb'
+ m.file 'views/password_resets/index.html.erb', 'app/views/password_resets/index.html.erb'
+ m.file 'views/password_resets/show.html.erb', 'app/views/password_resets/show.html.erb'
+ end
+
+ unless options[:skip_activation]
+ m.directory 'app/views/activations'
+ m.file 'controllers/activations_controller.rb', 'app/controllers/activations_controller.rb'
+ m.file 'views/activations/new.html.erb', 'app/views/activations/new.html.erb'
+ m.file 'views/notifier/activation_instructions.text.html.erb', 'app/views/notifier/activation_instructions.text.html.erb'
+ m.file 'views/notifier/activation_instructions.text.plain.erb', 'app/views/notifier/activation_instructions.text.plain.erb'
+ m.file 'views/password_resets/activate.html.erb', 'app/views/password_resets/activate.html.erb' unless options[:skip_password_reset]
+ end
+
+ unless options[:skip_routes]
+ m.route_resources :users
+ m.route_resources :password_resets unless options[:skip_password_reset]
+ m.route_resource :user_session
+ m.route_name('login', '/login', { :controller => 'user_sessions', :action => 'new' })
+ m.route_name('logout', '/logout', { :controller => 'user_sessions', :action => 'destroy' })
+ m.route_name('register', '/register/:activation_code', { :controller => 'activations', :action => 'new' }) unless options[:skip_activation]
+ end
+
+ unless options[:skip_migration]
+ m.migration_template "migrate/create_users_and_sessions.rb", "db/migrate", :migration_file_name => "create_users_and_sessions" unless options[:skip_migration]
+ end
+
+ end
+
+ puts "\n1) Please add these lines to your ApplicationController\n\ninclude AuthlogicUser\nfilter_parameter_logging :password, :password_confirmation\nhelper_method :current_user_session, :current_user\n\n"
+ puts "2) Please add these lines to your environment and launch rake gems:install\n\nconfig.gem 'binarylogic-authlogic', :lib => 'authlogic', :source => 'http://gems.github.com'\n\n"
+ puts "3) Please remember to launch rake db:migrate\n\n" unless options[:skip_migration]
+
+ recorded_session
+ end
+
+ protected
+ def add_options!(opt)
+ opt.separator ''
+ opt.separator 'Options:'
+ opt.on("--skip-activation", "Don't generate activation views and controller.") { |v| options[:skip_activation] = true }
+ opt.on("--skip-password-reset", "Don't generate controller and views for password resetting.") { |v| options[:skip_password_reset] = true }
+ opt.on("--skip-migration", "Don't generate a migration file for this model.") { |v| options[:skip_migration] = true }
+ opt.on("--skip-routes", "Don't add lines to the route file.") { |v| options[:skip_routes] = true }
+ end
+end
@@ -0,0 +1,17 @@
+class ActivationsController < ApplicationController
+ before_filter :require_no_user
+
+ def new
+ @user = User.find_using_perishable_token(params[:activation_code], 100.year)
+ if !@user.nil? && !@user.active? && @user.activate!
+ @user.deliver_activation_confirmation!
+ UserSession.create(@user)
+ flash[:notice] = "Account activated!"
+ redirect_to root_url
+ else
+ flash[:error] = "Account can't be activated!"
+ redirect_to new_user_session_path
+ end
+ end
+
+end
@@ -0,0 +1,58 @@
+class PasswordResetsController < ApplicationController
+ before_filter :load_user_using_perishable_token, :only => [:show, :update]
+ before_filter :require_no_user
+
+ def index
+ end
+
+ def create
+ @user = User.find_by_email(params[:email])
+ <% if options[:skip_activation] %>
+ if @user
+ <% else %>
+ if @user && @user.active?
+ <% end %>
+ @user.deliver_password_reset_instructions!
+ flash[:notice] = "Instructions to reset your password have been emailed to you. " +
+ "Please check your email."
+ redirect_to new_user_session_path
+ <% if options[:skip_activation] %>
+ elsif @user && !@user.active?
+ @user.deliver_activation_instructions!
+ flash[:error] = 'Activation needed'
+ render :action => 'activate'
+ <% end %>
+ else
+ flash[:notice] = "No user was found with that email address."
+ render :action => :index
+ end
+
+ end
+
+ def show
+ end
+
+ def update
+ @user.password = params[:user][:password]
+ @user.password_confirmation = params[:user][:password_confirmation]
+ if @user.changed? && @user.save
+ flash[:notice] = "Password successfully updated"
+ redirect_to user_path(@user)
+ else
+ flash[:error] = 'Password not updated'
+ render :action => :show
+ end
+ end
+
+ private
+ def load_user_using_perishable_token
+ @user = User.find_using_perishable_token(params[:id])
+ unless @user
+ 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 root_url
+ end
+ end
+end
@@ -0,0 +1,26 @@
+class UserSessionsController < ApplicationController
+ before_filter :require_no_user, :only => [:new, :create]
+ before_filter :require_user, :only => :destroy
+
+ def new
+ @user_session = UserSession.new
+ end
+
+ def create
+ @user_session = UserSession.new(params[:user_session])
+ if @user_session.save
+ flash[:notice] = "Login successful!"
+ redirect_back_or_default root_url
+ else
+ @user_session.errors.clear
+ flash[:error] = 'Login incorrect!'
+ render :action => :new
+ end
+ end
+
+ def destroy
+ current_user_session.destroy
+ flash[:notice] = "Logout successful!"
+ redirect_back_or_default new_user_session_path
+ end
+end
@@ -0,0 +1,66 @@
+class UsersController < ApplicationController
+ before_filter :require_no_user, :only => [:new, :create]
+ before_filter :require_user, :only => [:show, :edit, :update, :index]
+
+ def new
+ @user = User.new
+ end
+
+ def create
+ <% if options[:skip_activation] -%>
+ @user = User.new(params[:user])
+ if @user.save
+ flash[:notice] = "Your account has been created!"
+ redirect_to user_path(@user)
+ else
+ flash[:error] = "User already exists!"
+ render :action => :new
+ end
+ <% else -%>
+ @user = User.find_by_email(params[:user][:email]) || User.new(params[:user])
+ if @user.active?
+ flash[:error] = "User already exists and has been activated."
+ render :action => :new
+ else
+ @user.attributes = params[:user] unless @user.new_record?
+ if @user.save_without_session_maintenance
+ @user.deliver_activation_instructions!
+ flash[:notice] = "Your account has been created. Please check your e-mail for your account activation instructions!"
+ redirect_to new_user_session_path
+ else
+ render :action => :new
+ end
+ end
+ <% end %>
+ end
+
+ def show
+ @user = User.find(params[:id])
+ <% unless options[:skip_activation] %>
+ raise ActiveRecord::RecordNotFound unless @user.active?
+ <% end %>
+ respond_to do |format|
+ format.html {
+ if @user == @current_user
+ render :action => 'private'
+ else
+ render :action => 'public'
+ end
+ }
+ end
+ end
+
+ def edit
+ @user = @current_user
+ end
+
+ def update
+ @user = @current_user
+ if @user.update_attributes(params[:user])
+ flash[:notice] = "Account updated!"
+ redirect_to user_url
+ else
+ render :action => :edit
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 4abcf20

Please sign in to comment.