constellation-authentication 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,71 @@
1
+ class ApplicationController < ActionController::Base
2
+
3
+ helper_method :current_user_session, :current_user
4
+
5
+ protected
6
+
7
+ # Returns the current user session
8
+ def current_user_session
9
+ return @current_user_session if defined?(@current_user_session)
10
+ @current_user_session = UserSession.find
11
+ end
12
+
13
+ # Returns the currently logged in user, or nil if the user is not logged in.
14
+ def current_user
15
+ return @current_user if defined?(@current_user)
16
+ @current_user = current_user_session && current_user_session.user
17
+ end
18
+
19
+ # A class level shortcut to declaritively define a before filter that requires
20
+ # the current user to be logged in.
21
+ def self.require_user(options = {})
22
+ before_filter :require_user, options
23
+ end
24
+
25
+ # A class level shortcut to declaritively define a before filter that requires
26
+ # the current user to be logged out.
27
+ def self.require_no_user(options = {})
28
+ before_filter :require_no_user, options
29
+ end
30
+
31
+ # The instance level method that can be a before filter to require a logged in user.
32
+ #
33
+ # config.constellation.authentication.require_user_flash determines the flash key, the default is :notice
34
+ # config.constellation.authentication.require_user_redirection determines where the user is redirected, the default is user_sessions#new
35
+ # The locale's constellation.authentication.require_user key is the flash message and can be configured or translated at will
36
+ def require_user(options = {})
37
+ unless current_user
38
+ store_location
39
+ flash[Rails.application.config.constellation.authentication.require_user_flash] = t('constellation.authentication.require_user')
40
+ redirect_to Rails.application.config.constellation.authentication.require_user_redirection
41
+ return false
42
+ end
43
+ end
44
+
45
+ # The instance level method that can be a before filter to require a logged out user.
46
+ #
47
+ # config.constellation.authentication.require_no_user_flash determines the flash key, the default is :notice
48
+ # config.constellation.authentication.require_no_user_redirection determines where the user is redirected, the default is '/'
49
+ # The locale's constellation.authentication.require_no_user key is the flash message and can be configured or translated at will
50
+ def require_no_user(options = {})
51
+ if current_user
52
+ store_location
53
+ flash[Rails.application.config.constellation.authentication.require_no_user_flash] = t('constellation.authentication.require_no_user')
54
+ redirect_to Rails.application.config.constellation.authentication.require_no_user_redirection
55
+ return false
56
+ end
57
+ end
58
+
59
+ # A method to store the currently requested URI in the session so that after logging
60
+ # in the user can be redirected to the page they were previously trying to access.
61
+ def store_location
62
+ session[:constellation_stored_location] = request.fullpath
63
+ end
64
+
65
+ # If a previously requested URI was stored, this redirects the user to that page and
66
+ # clears the session variable; if not the user is redirected to the parameter passed.
67
+ def redirect_back_or_default(default)
68
+ redirect_to(session[:constellation_stored_location] || default)
69
+ session[:constellation_stored_location] = nil
70
+ end
71
+ end
@@ -0,0 +1,45 @@
1
+ class PasswordResetsController < ApplicationController
2
+ before_filter :load_user_using_perishable_token, :only => [:edit, :update]
3
+ require_no_user
4
+
5
+ def new
6
+ end
7
+
8
+ def create
9
+ @user = User.find_by_email(params[:email])
10
+ if @user
11
+ @user.reset_perishable_token!
12
+ UserMailer.password_reset(@user).deliver
13
+ flash[Rails.application.config.constellation.authentication.success_flash] = t("constellation.authentication.password_resets.create.success_flash")
14
+ redirect_to Rails.application.config.constellation.authentication.password_reset_redirection
15
+ else
16
+ flash.now[Rails.application.config.constellation.authentication.failure_flash] = t("constellation.authentication.password_resets.create.failure_flash")
17
+ render :action => :new
18
+ end
19
+ end
20
+
21
+ def edit
22
+ render
23
+ end
24
+
25
+ def update
26
+ @user.password = params[:user][:password]
27
+ @user.password_confirmation = params[:user][:password_confirmation]
28
+ if @user.save
29
+ flash[Rails.application.config.constellation.authentication.success_flash] = t("constellation.authentication.password_resets.update.success_flash")
30
+ redirect_to Rails.application.config.constellation.authentication.signin_redirection
31
+ else
32
+ render :action => :edit
33
+ end
34
+ end
35
+
36
+ private
37
+ def load_user_using_perishable_token
38
+ @user = User.find_using_perishable_token(params[:id])
39
+ unless @user
40
+ flash[Rails.application.config.constellation.authentication.failure_flash] = t("constellation.authentication.password_resets.load_user_failure_flash")
41
+ redirect_to Rails.application.config.constellation.authentication.password_reset_redirection
42
+ return false
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,24 @@
1
+ class UserSessionsController < ApplicationController
2
+ require_no_user :only => [:new, :create]
3
+ require_user :only => :destroy
4
+
5
+ def new
6
+ @user_session = UserSession.new
7
+ end
8
+
9
+ def create
10
+ @user_session = UserSession.new(params[:user_session])
11
+ if @user_session.save
12
+ flash[Rails.application.config.constellation.authentication.success_flash] = t("constellation.authentication.user_sessions.create.success_flash")
13
+ redirect_back_or_default Rails.application.config.constellation.authentication.signin_redirection
14
+ else
15
+ render :action => :new
16
+ end
17
+ end
18
+
19
+ def destroy
20
+ current_user_session.destroy
21
+ flash[Rails.application.config.constellation.authentication.success_flash] = t("constellation.authentication.user_sessions.destroy.success_flash")
22
+ redirect_to signin_path
23
+ end
24
+ end
@@ -0,0 +1,67 @@
1
+ class UsersController < ApplicationController
2
+ require_no_user :only => [:new, :create]
3
+ require_user :only => [:show, :edit, :update]
4
+
5
+ def new
6
+ @user = User.new
7
+ end
8
+
9
+ def create
10
+ @user = User.new(params[:user])
11
+ if @user.save
12
+ UserMailer.confirmation(@user).deliver if Rails.application.config.constellation.authentication.confirm_signup
13
+ flash[Rails.application.config.constellation.authentication.success_flash] = t("constellation.authentication.users.create.success_flash")
14
+ redirect_to Rails.application.config.constellation.authentication.signup_redirection
15
+ else
16
+ render :action => :new
17
+ end
18
+ end
19
+
20
+ def show
21
+ @user = @current_user
22
+ end
23
+
24
+ def edit
25
+ @user = @current_user
26
+ end
27
+
28
+ def update
29
+ @user = @current_user
30
+ if @user.update_attributes(params[:user])
31
+ flash[Rails.application.config.constellation.authentication.success_flash] = t("constellation.authentication.users.update.success_flash")
32
+ redirect_to account_url
33
+ else
34
+ render :action => :edit
35
+ end
36
+ end
37
+
38
+ if Rails.application.config.constellation.authentication.confirm_signup
39
+ def confirm
40
+ @user = User.find_using_perishable_token params[:token]
41
+ if @user
42
+ @user.confirmed_at = Time.now
43
+ @user.save
44
+ UserSession.create @user
45
+ flash[Rails.application.config.constellation.authentication.success_flash] = t("constellation.authentication.users.confirm.success_flash")
46
+ redirect_to Rails.application.config.constellation.authentication.confirm_redirection
47
+ else
48
+ flash.now[Rails.application.config.constellation.authentication.failure_flash] = t("constellation.authentication.users.confirm.failure_flash")
49
+ redirect_to resend_confirmation_path
50
+ end
51
+ end
52
+
53
+ def resend_confirmation
54
+ if request.post?
55
+ @user = User.find_by_email params[:email]
56
+ if @user
57
+ @user.reset_perishable_token!
58
+ UserMailer.confirmation(@user).deliver
59
+ flash.now[Rails.application.config.constellation.authentication.success_flash] = t("constellation.authentication.users.resend_confirmation.success_flash")
60
+ redirect_to Rails.application.config.constellation.authentication.resend_confirmation_redirection
61
+ else
62
+ flash.now[Rails.application.config.constellation.authentication.failure_flash] = t("constellation.authentication.users.resend_confirmation.failure_flash")
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,13 @@
1
+ class UserMailer < ActionMailer::Base
2
+ def confirmation(user)
3
+ @url = confirm_url :token => user.perishable_token
4
+ mail :to => user.email,
5
+ :subject => "Please confirm your email address!"
6
+ end
7
+
8
+ def password_reset(user)
9
+ @url = edit_password_reset_url user.perishable_token
10
+ mail :to => user.email,
11
+ :subject => "Please reset your password!"
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class User < ActiveRecord::Base
2
+ acts_as_authentic
3
+
4
+ attr_accessible :email, :password, :password_confirmation
5
+
6
+ if Rails.application.config.constellation.authentication.confirm_signup
7
+ # This is a magic method that Authlogic uses to determine if the user is confirmed
8
+ # and should be able to log in.
9
+ def confirmed?
10
+ !!confirmed_at
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ class UserSession < Authlogic::Session::Base
2
+ # TODO: This will likely be unnecessary in a future version of Authlogic. Rails 3 renamed key -> to_key
3
+ def to_key
4
+ new_record? ? nil : [ self.send(self.class.primary_key) ]
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ <h1><%= t("constellation.authentication.password_resets.edit.headline") %></h1>
2
+
3
+ <%= semantic_form_for @user, :url => password_reset_path, :method => :put do |form| %>
4
+ <%= form.inputs do %>
5
+ <%= form.input :password %>
6
+ <%= form.input :password_confirmation %>
7
+ <% end %>
8
+ <%= form.buttons do %>
9
+ <%= form.commit_button :label => t("constellation.authentication.password_resets.edit.button_label") %>
10
+ <% end %>
11
+ <% end %>
@@ -0,0 +1,9 @@
1
+ <h1><%= t("constellation.authentication.password_resets.new.headline") %></h1>
2
+
3
+ <p><%= t("constellation.authentication.password_resets.new.description") %></p>
4
+
5
+ <%= form_tag password_resets_path do %>
6
+ <%= label_tag :email %>
7
+ <%= text_field_tag :email, params[:email] %>
8
+ <br/><%= submit_tag t("constellation.authentication.password_resets.new.button_label") %>
9
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <p>
2
+ Thank you for signing up for an account! Please take this time to confirm your email address and activate your account with the link below.
3
+ </p>
4
+
5
+ <p>
6
+ <%= link_to @url, @url %>
7
+ </p>
@@ -0,0 +1,3 @@
1
+ Thank you for signing up for an account! Please take this time to confirm your email address and activate your account with the link below.
2
+
3
+ <%= @url %>
@@ -0,0 +1,12 @@
1
+ <p>
2
+ A request to reset your password has been made. If you did not make this request, simply ignore this email. If you did make this request just click the link below.
3
+ </p>
4
+
5
+ <p>
6
+ <%= link_to @url, @url %>
7
+ </p>
8
+
9
+ <p>
10
+ 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.
11
+ </p>
12
+
@@ -0,0 +1,5 @@
1
+ A request to reset your password has been made. If you did not make this request, simply ignore this email. If you did make this request just click the link below.
2
+
3
+ <%= @url %>
4
+
5
+ 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.
@@ -0,0 +1,16 @@
1
+ <h1><%= t("constellation.authentication.user_sessions.new.headline")%></h1>
2
+
3
+ <%= semantic_form_for @user_session, :url => user_session_path, :method => :post do |form| %>
4
+ <%= form.semantic_errors %>
5
+ <%= form.inputs do %>
6
+ <%= form.input :email %>
7
+ <%= form.input :password %>
8
+ <%= form.check_box :remember_me %><%= form.label :remember_me %>
9
+ <% end %>
10
+ <%= form.buttons do %>
11
+ <%= form.commit_button :label => t("constellation.authentication.user_sessions.new.button_label") %>
12
+ <% end %>
13
+ <% end %>
14
+
15
+ <%= link_to t("constellation.authentication.users.resend_confirmation.link_text"), resend_confirmation_path %>
16
+ <%= link_to t("constellation.authentication.password_resets.new.link_text"), new_password_reset_path %>
@@ -0,0 +1,11 @@
1
+ <h1><%= t("constellation.authentication.users.edit.headline") %></h1>
2
+
3
+ <%= semantic_form_for @user do |form| %>
4
+ <%= form.inputs do %>
5
+ <%= form.input :password %>
6
+ <%= form.input :password_confirmation %>
7
+ <% end %>
8
+ <%= form.buttons do %>
9
+ <%= form.commit_button :label => t("constellation.authentication.users.edit.button_label") %>
10
+ <% end %>
11
+ <% end %>
@@ -0,0 +1,12 @@
1
+ <h1><%= t("constellation.authentication.users.new.headline") %></h1>
2
+
3
+ <%= semantic_form_for @user do |form| %>
4
+ <%= form.inputs do %>
5
+ <%= form.input :email %>
6
+ <%= form.input :password %>
7
+ <%= form.input :password_confirmation %>
8
+ <% end %>
9
+ <%= form.buttons do %>
10
+ <%= form.commit_button :label => t("constellation.authentication.users.new.button_label") %>
11
+ <% end %>
12
+ <% end %>
@@ -0,0 +1,9 @@
1
+ <h1><%= t("constellation.authentication.users.resend_confirmation.headline") %></h1>
2
+
3
+ <p><%= t("constellation.authentication.users.resend_confirmation.description") %></p>
4
+
5
+ <% form_tag resend_confirmation_path do %>
6
+ <%= label_tag :email %>
7
+ <%= text_field_tag :email, params[:email] %>
8
+ <br/><%= submit_tag t("constellation.authentication.users.resend_confirmation.button_label") %>
9
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <h1><%= t("constellation.authentication.users.show.headline") %></h1>
2
+
3
+ <p>
4
+ <%=h @user.email %>
5
+ </p>
6
+
7
+ <%= link_to t("constellation.authentication.users.edit.link_text"), edit_account_path %>
@@ -0,0 +1,51 @@
1
+ en:
2
+ constellation:
3
+ authentication:
4
+ require_user: "You must be signed in to access this page."
5
+ require_no_user: "You must be signed out to access this page."
6
+ users:
7
+ new:
8
+ headline: "Sign Up"
9
+ button_label: "Sign Up"
10
+ create:
11
+ success_flash: "Thanks for signing up!"
12
+ show:
13
+ headline: "My Account"
14
+ edit:
15
+ headline: "Edit My Account"
16
+ button_label: "Update"
17
+ link_text: "Edit"
18
+ update:
19
+ success_flash: "Account updated!"
20
+ confirm:
21
+ success_flash: "Account successfully activated!"
22
+ failure_flash: "Your account could not be confirmed. This usually happens if you wait too long before clicking on the link in your email."
23
+ resend_confirmation:
24
+ headline: "Resend Confirmation Email"
25
+ description: "Please check your spam folder! But if you don't find it there, enter your email address below and we'll send you a fresh confirmation email."
26
+ button_label: "Resend Email"
27
+ link_text: "Never got your confirmation email? Click here to resend it."
28
+ failure_flash: "We couldn't find that user. Is your email right? Did you sign up properly?"
29
+ user_sessions:
30
+ new:
31
+ headline: "Sign In"
32
+ button_label: "Sign In"
33
+ create:
34
+ success_flash: "Welcome back!"
35
+ destroy:
36
+ success_flash: "You have been signed out."
37
+ password_resets:
38
+ new:
39
+ headline: "Reset Password"
40
+ description: "Fill out the form below and instructions to reset your password will be emailed to you. Please check your spam folder if you don't see it."
41
+ button_label: "Reset Password"
42
+ link_text: "Forget your password? Click here to reset it."
43
+ create:
44
+ success_flash: "Instructions to reset your password have been emailed to you. Please check your email."
45
+ failure_flash: "No user was found with that email address."
46
+ edit:
47
+ headline: "Change My Password"
48
+ button_label: "Update my password and log me in"
49
+ update:
50
+ success_flash: "Password successfully updated."
51
+ load_user_failure_flash: "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."
data/config/routes.rb ADDED
@@ -0,0 +1,18 @@
1
+ if Rails.application.config.constellation.authentication.include_routes
2
+ Rails::Application.routes.draw do |map|
3
+
4
+ resource :user_session
5
+ resources :users
6
+ resource :account, :controller => "users"
7
+
8
+ match "signout", :to => "user_sessions#destroy", :as => "signout"
9
+ match "signin", :to => "user_sessions#new", :as => "signin"
10
+ match "signup", :to => "users#new", :as => "signup"
11
+
12
+ match 'confirm/:token', :to => 'users#confirm', :as => 'confirm'
13
+ match "resend_confirmation", :to => "users#resend_confirmation", :as => "resend_confirmation"
14
+
15
+ resources :password_resets
16
+
17
+ end
18
+ end
@@ -0,0 +1 @@
1
+ require 'constellation/authentication/engine' if defined?(Rails)
@@ -0,0 +1,40 @@
1
+ require 'constellation/authentication'
2
+ require 'rails'
3
+ require 'active_record'
4
+
5
+ module Constellation
6
+ module Authentication
7
+ class Engine < Rails::Engine
8
+ config.constellation = ActiveSupport::OrderedOptions.new unless config.respond_to? :constellation
9
+ config.constellation.authentication = ActiveSupport::OrderedOptions.new
10
+ config.constellation.authentication.root = __FILE__.gsub('/lib/constellation/authentication/engine.rb', '')
11
+
12
+ # Flash keys
13
+ config.constellation.authentication.success_flash = :success
14
+ config.constellation.authentication.failure_flash = :error
15
+
16
+ # Include the engine's routes? Disable if you wish to provide your own.
17
+ config.constellation.authentication.include_routes = true
18
+
19
+ # Settings for checking if the current user is signed in or not
20
+ config.constellation.authentication.require_user_redirection = { :controller => 'user_sessions', :action => 'new' }
21
+ config.constellation.authentication.require_no_user_redirection = '/'
22
+ config.constellation.authentication.require_user_flash = :notice
23
+ config.constellation.authentication.require_no_user_flash = :notice
24
+
25
+ # Signing Up
26
+ config.constellation.authentication.signup_redirection = "/"
27
+
28
+ # Confirming the user's email
29
+ config.constellation.authentication.confirm_signup = true
30
+ config.constellation.authentication.confirm_redirection = "/"
31
+ config.constellation.authentication.resend_confirmation_redirection = "/"
32
+
33
+ # Signing In
34
+ config.constellation.authentication.signin_redirection = "/"
35
+
36
+ # Forgot password
37
+ config.constellation.authentication.password_reset_redirection = "/"
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,25 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.string "email", :null => false
5
+ t.string "crypted_password", :null => false
6
+ t.string "password_salt", :null => false
7
+ t.string "persistence_token", :null => false
8
+ t.string "single_access_token", :null => false
9
+ t.string "perishable_token", :null => false
10
+ t.integer "login_count", :default => 0, :null => false
11
+ t.integer "failed_login_count", :default => 0, :null => false
12
+ t.datetime "last_request_at"
13
+ t.datetime "current_login_at"
14
+ t.datetime "last_login_at"
15
+ t.string "current_login_ip"
16
+ t.string "last_login_ip"
17
+ t.datetime "confirmed_at"
18
+ t.timestamps
19
+ end
20
+ end
21
+
22
+ def self.down
23
+ drop_table :users
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Constellation
5
+ class UserGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ # Implement the required interface for Rails::Generators::Migration.
9
+ # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
10
+ def self.next_migration_number(dirname)
11
+ if ActiveRecord::Base.timestamped_migrations
12
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
13
+ else
14
+ "%.3d" % (current_migration_number(dirname) + 1)
15
+ end
16
+ end
17
+
18
+ def create_migration_file
19
+ migration_template 'migration.rb', 'db/migrate/create_users.rb'
20
+ end
21
+
22
+ def self.source_root
23
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
24
+ end
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: constellation-authentication
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Ian Terrell
13
+ - Jeff Bozek
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-04 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A full featured authentication solution built around Authlogic.
23
+ email: ian@constellationsoft.com;jeff@constellationsoft.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/constellation/authentication/engine.rb
32
+ - lib/constellation/authentication.rb
33
+ - lib/generators/constellation/templates/migration.rb
34
+ - lib/generators/constellation/user_generator.rb
35
+ - app/controllers/application_controller.rb
36
+ - app/controllers/password_resets_controller.rb
37
+ - app/controllers/user_sessions_controller.rb
38
+ - app/controllers/users_controller.rb
39
+ - app/mailers/user_mailer.rb
40
+ - app/models/user.rb
41
+ - app/models/user_session.rb
42
+ - app/views/password_resets/edit.html.erb
43
+ - app/views/password_resets/new.html.erb
44
+ - app/views/user_mailer/confirmation.html.erb
45
+ - app/views/user_mailer/confirmation.text.erb
46
+ - app/views/user_mailer/password_reset.html.erb
47
+ - app/views/user_mailer/password_reset.text.erb
48
+ - app/views/user_sessions/new.html.erb
49
+ - app/views/users/edit.html.erb
50
+ - app/views/users/new.html.erb
51
+ - app/views/users/resend_confirmation.html.erb
52
+ - app/views/users/show.html.erb
53
+ - config/locales/en.yml
54
+ - config/routes.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/constellationsoft/authentication
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.6
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: A full featured authentication solution built around Authlogic.
85
+ test_files: []
86
+