ae_users 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/README +47 -0
  2. data/Rakefile +36 -0
  3. data/VERSION +1 -0
  4. data/ae_users.gemspec +117 -0
  5. data/app/controllers/account_controller.rb +167 -0
  6. data/app/controllers/auth_controller.rb +202 -0
  7. data/app/controllers/permission_controller.rb +172 -0
  8. data/app/helpers/account_helper.rb +2 -0
  9. data/app/helpers/auth_helper.rb +5 -0
  10. data/app/helpers/permission_helper.rb +2 -0
  11. data/app/models/account.rb +50 -0
  12. data/app/models/auth_notifier.rb +34 -0
  13. data/app/models/auth_ticket.rb +39 -0
  14. data/app/models/email_address.rb +17 -0
  15. data/app/models/login.rb +23 -0
  16. data/app/models/open_id_identity.rb +5 -0
  17. data/app/models/permission.rb +57 -0
  18. data/app/models/person.rb +156 -0
  19. data/app/models/role.rb +7 -0
  20. data/app/views/account/_personal_info.rhtml +35 -0
  21. data/app/views/account/_procon_profile.rhtml +3 -0
  22. data/app/views/account/_signup_form.html.erb +39 -0
  23. data/app/views/account/activate.rhtml +6 -0
  24. data/app/views/account/activation_error.rhtml +11 -0
  25. data/app/views/account/change_password.rhtml +3 -0
  26. data/app/views/account/edit_profile.rhtml +117 -0
  27. data/app/views/account/signup.rhtml +9 -0
  28. data/app/views/account/signup_noactivation.rhtml +7 -0
  29. data/app/views/account/signup_success.rhtml +8 -0
  30. data/app/views/auth/_auth_form.rhtml +54 -0
  31. data/app/views/auth/_forgot_form.html.erb +12 -0
  32. data/app/views/auth/_mini_auth_form.rhtml +17 -0
  33. data/app/views/auth/_openid_auth_form.html.erb +14 -0
  34. data/app/views/auth/_other_login_options.html.erb +24 -0
  35. data/app/views/auth/auth_form.js.erb +63 -0
  36. data/app/views/auth/forgot.rhtml +3 -0
  37. data/app/views/auth/forgot_form.rhtml +6 -0
  38. data/app/views/auth/index.css.erb +23 -0
  39. data/app/views/auth/login.rhtml +6 -0
  40. data/app/views/auth/needs_activation.rhtml +6 -0
  41. data/app/views/auth/needs_person.html.erb +32 -0
  42. data/app/views/auth/needs_profile.rhtml +14 -0
  43. data/app/views/auth/openid_login.html.erb +6 -0
  44. data/app/views/auth/resend_activation.rhtml +3 -0
  45. data/app/views/auth_notifier/account_activation.rhtml +13 -0
  46. data/app/views/auth_notifier/generated_password.rhtml +10 -0
  47. data/app/views/permission/_add_grantee.rhtml +47 -0
  48. data/app/views/permission/_role_member.rhtml +8 -0
  49. data/app/views/permission/_show.rhtml +81 -0
  50. data/app/views/permission/_userpicker.rhtml +0 -0
  51. data/app/views/permission/add_role_member.rhtml +3 -0
  52. data/app/views/permission/admin.rhtml +45 -0
  53. data/app/views/permission/edit.rhtml +9 -0
  54. data/app/views/permission/edit_role.rhtml +63 -0
  55. data/app/views/permission/grant.rhtml +10 -0
  56. data/db/migrate/002_create_accounts.rb +17 -0
  57. data/db/migrate/003_create_email_addresses.rb +17 -0
  58. data/db/migrate/004_create_people.rb +24 -0
  59. data/db/migrate/013_simplify_signup.rb +15 -0
  60. data/db/migrate/014_create_permissions.rb +16 -0
  61. data/db/migrate/015_create_roles.rb +18 -0
  62. data/db/migrate/016_refactor_people.rb +36 -0
  63. data/db/migrate/017_people_permissions.rb +9 -0
  64. data/generators/ae_users/USAGE +14 -0
  65. data/generators/ae_users/ae_users_generator.rb +12 -0
  66. data/generators/ae_users/templates/add.png +0 -0
  67. data/generators/ae_users/templates/admin.png +0 -0
  68. data/generators/ae_users/templates/group.png +0 -0
  69. data/generators/ae_users/templates/logout.png +0 -0
  70. data/generators/ae_users/templates/migration.rb +25 -0
  71. data/generators/ae_users/templates/openid.gif +0 -0
  72. data/generators/ae_users/templates/remove.png +0 -0
  73. data/generators/ae_users/templates/user.png +0 -0
  74. data/init.rb +1 -0
  75. data/install.rb +1 -0
  76. data/lib/ae_users.rb +781 -0
  77. data/rails/init.rb +20 -0
  78. data/tasks/ae_users_tasks.rake +4 -0
  79. data/test/ae_users_test.rb +8 -0
  80. data/uninstall.rb +1 -0
  81. metadata +134 -0
data/README ADDED
@@ -0,0 +1,47 @@
1
+ AeUsers
2
+ =======
3
+
4
+ This is the authentication system used in Alleged Entertainment Rails
5
+ applications such as Journey and ProCon. For more information, go to
6
+ www.aegames.org.
7
+
8
+
9
+ Migrating from AeUsers 0.1
10
+ ==========================
11
+
12
+ To migrate from AeUsers 0.1, run the following SQL commands in your ae_users
13
+ database:
14
+
15
+ alter table email_addresses add column person_id int;
16
+ update email_addresses, accounts, people set email_addresses.person_id=people.id
17
+ where email_addresses.account_id = accounts.id
18
+ and people.account_id = accounts.id;
19
+ alter table email_addresses drop column account_id;
20
+
21
+ alter table accounts add column person_id int;
22
+ update accounts, people set accounts.person_id=people.id
23
+ where accounts.id = people.account_id;
24
+ alter table people drop column account_id;
25
+
26
+ create table open_id_identities (id int not null auto_increment primary key,
27
+ person_id int, identity_url varchar(4000));
28
+
29
+ You'll also want to run this command in each of your application databases:
30
+
31
+ create table auth_tickets (id int not null auto_increment primary key,
32
+ secret varchar(40) unique, person_id int, created_at datetime,
33
+ updated_at datetime, expires_at datetime);
34
+
35
+ And if you want to enable permission caching (experimental, but can dramatically
36
+ increase performance in some cases), run these commands in each of your
37
+ application databases for which you want to enable it:
38
+
39
+ create table permission_caches (id int not null auto_increment primary key,
40
+ person_id int, permissioned_id int, permissioned_type varchar(255),
41
+ permission_name varchar(255), result tinyint(1));
42
+ create index index_permission_caches_on_person_id on permission_caches
43
+ (person_id);
44
+ create index index_permission_caches_on_permissioned on permission_caches
45
+ (permissioned_id, permissioned_type);
46
+ create index index_permission_caches_on_permission_name on permission_caches
47
+ (permission_name);
@@ -0,0 +1,36 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the ae_users plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the ae_users plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'AeUsers'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ begin
25
+ require 'jeweler'
26
+ Jeweler::Tasks.new do |gemspec|
27
+ gemspec.name = "ae_users"
28
+ gemspec.summary = "An authentication and authorization system for Rails"
29
+ gemspec.email = "natbudin@gmail.com"
30
+ gemspec.homepage = "http://github.com/nbudin/ae_users"
31
+ gemspec.authors = ["Nat Budin"]
32
+ end
33
+ Jeweler::GemcutterTasks.new
34
+ rescue LoadError
35
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
36
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.6.0
@@ -0,0 +1,117 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ae_users}
8
+ s.version = "0.6.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nat Budin"]
12
+ s.date = %q{2009-10-05}
13
+ s.email = %q{natbudin@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "README"
16
+ ]
17
+ s.files = [
18
+ "README",
19
+ "Rakefile",
20
+ "VERSION",
21
+ "ae_users.gemspec",
22
+ "app/controllers/account_controller.rb",
23
+ "app/controllers/auth_controller.rb",
24
+ "app/controllers/permission_controller.rb",
25
+ "app/helpers/account_helper.rb",
26
+ "app/helpers/auth_helper.rb",
27
+ "app/helpers/permission_helper.rb",
28
+ "app/models/account.rb",
29
+ "app/models/auth_notifier.rb",
30
+ "app/models/auth_ticket.rb",
31
+ "app/models/email_address.rb",
32
+ "app/models/login.rb",
33
+ "app/models/open_id_identity.rb",
34
+ "app/models/permission.rb",
35
+ "app/models/person.rb",
36
+ "app/models/role.rb",
37
+ "app/views/account/_personal_info.rhtml",
38
+ "app/views/account/_procon_profile.rhtml",
39
+ "app/views/account/_signup_form.html.erb",
40
+ "app/views/account/activate.rhtml",
41
+ "app/views/account/activation_error.rhtml",
42
+ "app/views/account/change_password.rhtml",
43
+ "app/views/account/edit_profile.rhtml",
44
+ "app/views/account/signup.rhtml",
45
+ "app/views/account/signup_noactivation.rhtml",
46
+ "app/views/account/signup_success.rhtml",
47
+ "app/views/auth/_auth_form.rhtml",
48
+ "app/views/auth/_forgot_form.html.erb",
49
+ "app/views/auth/_mini_auth_form.rhtml",
50
+ "app/views/auth/_openid_auth_form.html.erb",
51
+ "app/views/auth/_other_login_options.html.erb",
52
+ "app/views/auth/auth_form.js.erb",
53
+ "app/views/auth/forgot.rhtml",
54
+ "app/views/auth/forgot_form.rhtml",
55
+ "app/views/auth/index.css.erb",
56
+ "app/views/auth/login.rhtml",
57
+ "app/views/auth/needs_activation.rhtml",
58
+ "app/views/auth/needs_person.html.erb",
59
+ "app/views/auth/needs_profile.rhtml",
60
+ "app/views/auth/openid_login.html.erb",
61
+ "app/views/auth/resend_activation.rhtml",
62
+ "app/views/auth_notifier/account_activation.rhtml",
63
+ "app/views/auth_notifier/generated_password.rhtml",
64
+ "app/views/permission/_add_grantee.rhtml",
65
+ "app/views/permission/_role_member.rhtml",
66
+ "app/views/permission/_show.rhtml",
67
+ "app/views/permission/_userpicker.rhtml",
68
+ "app/views/permission/add_role_member.rhtml",
69
+ "app/views/permission/admin.rhtml",
70
+ "app/views/permission/edit.rhtml",
71
+ "app/views/permission/edit_role.rhtml",
72
+ "app/views/permission/grant.rhtml",
73
+ "db/migrate/002_create_accounts.rb",
74
+ "db/migrate/003_create_email_addresses.rb",
75
+ "db/migrate/004_create_people.rb",
76
+ "db/migrate/013_simplify_signup.rb",
77
+ "db/migrate/014_create_permissions.rb",
78
+ "db/migrate/015_create_roles.rb",
79
+ "db/migrate/016_refactor_people.rb",
80
+ "db/migrate/017_people_permissions.rb",
81
+ "generators/ae_users/USAGE",
82
+ "generators/ae_users/ae_users_generator.rb",
83
+ "generators/ae_users/templates/add.png",
84
+ "generators/ae_users/templates/admin.png",
85
+ "generators/ae_users/templates/group.png",
86
+ "generators/ae_users/templates/logout.png",
87
+ "generators/ae_users/templates/migration.rb",
88
+ "generators/ae_users/templates/openid.gif",
89
+ "generators/ae_users/templates/remove.png",
90
+ "generators/ae_users/templates/user.png",
91
+ "init.rb",
92
+ "install.rb",
93
+ "lib/ae_users.rb",
94
+ "rails/init.rb",
95
+ "tasks/ae_users_tasks.rake",
96
+ "test/ae_users_test.rb",
97
+ "uninstall.rb"
98
+ ]
99
+ s.homepage = %q{http://github.com/nbudin/ae_users}
100
+ s.rdoc_options = ["--charset=UTF-8"]
101
+ s.require_paths = ["lib"]
102
+ s.rubygems_version = %q{1.3.5}
103
+ s.summary = %q{An authentication and authorization system for Rails}
104
+ s.test_files = [
105
+ "test/ae_users_test.rb"
106
+ ]
107
+
108
+ if s.respond_to? :specification_version then
109
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
110
+ s.specification_version = 3
111
+
112
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
113
+ else
114
+ end
115
+ else
116
+ end
117
+ end
@@ -0,0 +1,167 @@
1
+ class AccountController < ApplicationController
2
+ unloadable
3
+ require_login :only => [:edit_profile, :edit_email_addresses, :change_password, :add_openid, :delete_openid]
4
+ before_filter :check_signup_allowed, :only => [:signup, :signup_success]
5
+
6
+ filter_parameter_logging :password
7
+
8
+ def activate
9
+ if logged_in?
10
+ redirect_to "/"
11
+ return
12
+ end
13
+
14
+ @account = Account.find params[:account]
15
+
16
+ if not @account.nil? and @account.activation_key == params[:activation_key]
17
+ @account.active = true
18
+ @account.activation_key = nil
19
+ @account.save
20
+ else
21
+ redirect_to :action => :activation_error
22
+ end
23
+ end
24
+
25
+ def edit_profile
26
+ @person = logged_in_person
27
+ if not AeUsers.profile_class.nil?
28
+ @app_profile = AeUsers.profile_class.find_by_person_id(@person.id)
29
+ end
30
+
31
+ if request.post?
32
+ @person.update_attributes params[:person]
33
+ if @app_profile
34
+ @app_profile.update_attributes params[:app_profile]
35
+ end
36
+ end
37
+ end
38
+
39
+ def edit_email_addresses
40
+ errs = []
41
+
42
+ if params[:new_address] and params[:new_address].length > 0
43
+ existing_ea = EmailAddress.find_by_address params[:new_address]
44
+ if existing_ea
45
+ errs.push "A different person is already associated with the email address you tried to add."
46
+ else
47
+ newea = EmailAddress.create :person => logged_in_person, :address => params[:new_address]
48
+ if params[:primary] == 'new'
49
+ newea.primary = true
50
+ newea.save
51
+ end
52
+ end
53
+ end
54
+
55
+ if params[:primary] and params[:primary] != 'new'
56
+ id = params[:primary].to_i
57
+ if id != 0
58
+ addr = EmailAddress.find id
59
+ if addr.person != logged_in_person
60
+ errs.push "The email address you've selected as primary belongs to a different person."
61
+ else
62
+ addr.primary = true
63
+ addr.save
64
+ end
65
+ else
66
+ errs.push "The email address you've selected as primary doesn't exist."
67
+ end
68
+ end
69
+
70
+ if params[:delete]
71
+ params[:delete].each do |id|
72
+ addr = EmailAddress.find id
73
+ if addr.person != logged_in_person
74
+ errs.push "The email address you've selected to delete belongs to a different person."
75
+ elsif addr.primary
76
+ errs.push "You can't delete your primary email address. Try making a different email address your primary address first."
77
+ else
78
+ addr.destroy
79
+ end
80
+ end
81
+ end
82
+
83
+ if errs.length > 0
84
+ flash[:error_messages] = errs
85
+ end
86
+
87
+ redirect_to :action => :edit_profile
88
+ end
89
+
90
+ def change_password
91
+ password = params[:password]
92
+ if password[:password1].nil? or password[:password2].nil?
93
+ redirect_to :action => :edit_profile
94
+ elsif password[:password1] != password[:password2]
95
+ flash[:error_messages] = ["The passwords you entered don't match. Please try again."]
96
+ redirect_to :action => :edit_profile
97
+ else
98
+ acct = logged_in_person.account
99
+ acct.password = password[:password1]
100
+ acct.save
101
+ end
102
+ end
103
+
104
+ def activation_error
105
+ end
106
+
107
+ def signup_success
108
+ end
109
+
110
+ def add_openid
111
+ if using_open_id?
112
+ authenticate_with_open_id(params[:openid_url]) do |result, identity_url|
113
+ if result.successful?
114
+ id = OpenIdIdentity.find_by_identity_url(identity_url)
115
+ if id.nil?
116
+ id = OpenIdIdentity.new :person => logged_in_person, :identity_url => identity_url
117
+ else
118
+ if id.person.nil?
119
+ id.person = logged_in_person
120
+ elsif id.person != logged_in_person
121
+ flash[:error_messages] = ["That OpenID belongs to a different person (#{id.person.name})."]
122
+ return
123
+ end
124
+ end
125
+ if not id.save
126
+ flash[:error_messages] = id.errors.collect { |e| e[0].humanize + " " + e[1] }
127
+ end
128
+ else
129
+ flash[:error_messages] = [result.message]
130
+ end
131
+ redirect_to :action => 'edit_profile'
132
+ end
133
+ else
134
+ flash[:error_messages] = ["Please enter an OpenID url."]
135
+ end
136
+ end
137
+
138
+ def delete_openid
139
+ id = OpenIdIdentity.find(params[:id])
140
+ if id.person == logged_in_person
141
+ if logged_in_person.account or logged_in_person.open_id_identities.length > 1
142
+ id.destroy
143
+ else
144
+ flash[:error_messages] = ["Deleting that OpenID would leave you no way of logging in!"]
145
+ end
146
+ else
147
+ flash[:error_messages] = ["That OpenID does not belong to you!"]
148
+ end
149
+ redirect_to :action => 'edit_profile'
150
+ end
151
+
152
+ def signup
153
+ ret = create_account_and_person()
154
+ if ret == :success
155
+ redirect_to :action => 'signup_success'
156
+ elsif ret == :no_activation
157
+ redirect_to :action => :signup_noactivation
158
+ end
159
+ end
160
+
161
+ private
162
+ def check_signup_allowed
163
+ if not AeUsers.signup_allowed?
164
+ access_denied "Account signup is not allowed on this site."
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,202 @@
1
+ class AuthController < ApplicationController
2
+ unloadable
3
+ filter_parameter_logging :password
4
+ before_filter :construct_login, :only => [:login, :openid_login, :forgot_form]
5
+
6
+ def index
7
+ respond_to do |format|
8
+ format.css { render :layout => false }
9
+ end
10
+ end
11
+
12
+ def openid_login
13
+ params[:openid_url] ||= cookies['openid_url']
14
+ if using_open_id?
15
+ if attempt_open_id_login(@login.return_to)
16
+ successful_login_redirect
17
+ end
18
+ end
19
+ end
20
+
21
+ def login
22
+ if request.post?
23
+ unless @login.password or @login.have_password
24
+ redirect_to :controller => "account", :action => "signup", :email => @login.email
25
+ end
26
+ end
27
+ if request.post? and not logged_in?
28
+ if attempt_login(@login)
29
+ successful_login_redirect
30
+ end
31
+ end
32
+ end
33
+
34
+ def needs_person
35
+ @open_id_identity = OpenIdIdentity.find_or_create_by_identity_url(session[:identity_url])
36
+ @person = Person.new
37
+ if not AeUsers.profile_class.nil?
38
+ @app_profile = AeUsers.profile_class.send(:new, :person => @person)
39
+ end
40
+
41
+ if params[:registration]
42
+ person_map = HashWithIndifferentAccess.new(Person.sreg_map)
43
+ profile_map = if AeUsers.profile_class and AeUsers.profile_class.respond_to?("sreg_map")
44
+ HashWithIndifferentAccess.new(AeUsers.profile_class.sreg_map)
45
+ else
46
+ nil
47
+ end
48
+
49
+ params[:registration].each_pair do |key, value|
50
+ if key == 'email'
51
+ params[:email] = value
52
+ elsif person_map.has_key?(key.to_s)
53
+ mapper = person_map[key]
54
+ attrs = mapper.call(value)
55
+ @person.attributes = attrs
56
+ elsif (profile_map and profile_map.has_key?(key))
57
+ mapper = profile_map[key]
58
+ @app_profile.attributes = mapper.call(value)
59
+ end
60
+ end
61
+ end
62
+ if params[:person]
63
+ @person.attributes = params[:person]
64
+ end
65
+ if params[:app_profile] and @app_profile
66
+ @app_profile.attributes = params[:app_profile]
67
+ end
68
+
69
+ if request.post?
70
+ error_messages = []
71
+ error_fields = []
72
+
73
+ ["firstname", "lastname", "gender"].each do |field|
74
+ if not @person.send(field)
75
+ error_fields.push field
76
+ error_messages.push "You must enter a value for #{field}."
77
+ end
78
+ end
79
+
80
+ if not params[:email]
81
+ error_fields.push("email")
82
+ error_messages.push "You must enter a value for email."
83
+ end
84
+
85
+ if error_messages.length > 0
86
+ flash[:error_fields] = error_fields
87
+ flash[:error_messages] = error_messages
88
+ else
89
+ @person.save
90
+ @person.primary_email_address = params[:email]
91
+ @open_id_identity.person = @person
92
+ @open_id_identity.save
93
+ if @app_profile
94
+ @app_profile.save
95
+ end
96
+
97
+ session[:person] = @person
98
+ redirect_to session[:return_to]
99
+ end
100
+ end
101
+ end
102
+
103
+ def auth_form
104
+ respond_to do |format|
105
+ format.js { render :layout => false }
106
+ end
107
+ end
108
+
109
+ def needs_profile
110
+ @person = Person.find session[:provisional_person]
111
+ if @person.nil?
112
+ flash[:error_messages] = ["Couldn't find a person record with that ID.
113
+ Something may have gone wrong internally. Please try again, and if the problem persists, please contact
114
+ the site administrator."]
115
+ redirect_to :back
116
+ end
117
+
118
+ if not AeUsers.signup_allowed?
119
+ flash[:error_messages] = ['Your account is not valid for this site.']
120
+ redirect_to url_for("/")
121
+ else
122
+ if not AeUsers.profile_class.nil?
123
+ @app_profile = AeUsers.profile_class.send(:new, :person_id => session[:provisional_person])
124
+ @app_profile.attributes = params[:app_profile]
125
+
126
+ if request.post?
127
+ @app_profile.save
128
+ session[:person] = @person
129
+ redirect_to params[:return_to]
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ def forgot
136
+ ActionMailer::Base.default_url_options[:host] = request.host
137
+
138
+ @account = Account.find_by_email_address(params[:email])
139
+ if not @account.nil?
140
+ @account.generate_password
141
+ else
142
+ flash[:error_messages] = ["There's no account matching that email address. Please try again, or sign up for an account."]
143
+ redirect_to :action => :forgot_form
144
+ end
145
+ end
146
+
147
+ def resend_validation
148
+ ActionMailer::Base.default_url_options[:host] = request.host
149
+
150
+ @email_address = Account.find params[:email]
151
+ if not @email_address.nil?
152
+ @email_address.generate_validation
153
+ else
154
+ flash[:error_messages] = ["Email address #{params[:email]} not found!"]
155
+ redirect_to url_for("/")
156
+ end
157
+ end
158
+
159
+ def logout
160
+ reset_session
161
+ redirect_to url_for("/")
162
+ end
163
+
164
+ private
165
+
166
+ def construct_login
167
+ @login = Login.new(params[:login])
168
+ @login.email ||= cookies['email']
169
+ if @login.return_to.nil? or @login.return_to == ""
170
+ if params[:return_to]
171
+ @login.return_to = params[:return_to]
172
+ else
173
+ @login.return_to = request.env["HTTP_REFERER"]
174
+ end
175
+ end
176
+
177
+ # prevent infinite redirect loops
178
+ begin
179
+ if URI(@login.return_to).path == URI(request.url).path
180
+ @login.return_to = url_for("/")
181
+ end
182
+ rescue
183
+ end
184
+
185
+ # if they're already logged in, don't let them view this page
186
+ if logged_in?
187
+ successful_login_redirect
188
+ end
189
+ end
190
+
191
+ def successful_login_redirect
192
+ if @login.return_to
193
+ redirect_to @login.return_to
194
+ elsif session[:return_to]
195
+ rt = session[:return_to]
196
+ session[:return_to] = nil
197
+ redirect_to rt
198
+ else
199
+ redirect_to url_for('/')
200
+ end
201
+ end
202
+ end