salted_login_generator 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,15 @@
1
1
  require File.dirname(__FILE__) + '/../test_helper'
2
- require 'account_controller'
2
+ require '<%= file_name %>_controller'
3
3
 
4
4
  # Raise errors beyond the default web-based presentation
5
- class AccountController; def rescue_action(e) raise e end; end
5
+ class <%= class_name %>Controller; def rescue_action(e) raise e end; end
6
6
 
7
- class AccountControllerTest < Test::Unit::TestCase
7
+ class <%= class_name %>ControllerTest < Test::Unit::TestCase
8
8
 
9
- fixtures :users
9
+ fixtures :<%= plural_name %>
10
10
 
11
11
  def setup
12
- @controller = AccountController.new
12
+ @controller = <%= class_name %>Controller.new
13
13
  @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
14
14
  @request.host = "localhost"
15
15
  end
@@ -17,23 +17,22 @@ class AccountControllerTest < Test::Unit::TestCase
17
17
  def test_auth_bob
18
18
  @request.session['return-to'] = "/bogus/location"
19
19
 
20
- post :login, "user" => { "login" => "bob", "password" => "atest" }
21
- assert_session_has "user"
20
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
21
+ assert_session_has "<%= singular_name %>"
22
22
 
23
- assert_equal @bob, @response.session["user"]
23
+ assert_equal @bob, @response.session["<%= singular_name %>"]
24
24
 
25
25
  assert_redirect_url "/bogus/location"
26
26
  end
27
27
 
28
28
  def do_test_signup(bad_password, bad_email)
29
29
  ActionMailer::Base.deliveries = []
30
- CONFIG['inject_mailer_error'] = false
31
30
 
32
31
  @request.session['return-to'] = "/bogus/location"
33
32
 
34
33
  if not bad_password and not bad_email
35
- post :signup, "user" => { "login" => "newbob", "password" => "newpassword", "password_confirmation" => "newpassword", "email" => "newbob@test.com" }
36
- assert_session_has_no "user"
34
+ post :signup, "<%= singular_name %>" => { "login" => "newbob", "password" => "newpassword", "password_confirmation" => "newpassword", "email" => "newbob@test.com" }
35
+ assert_session_has_no "<%= singular_name %>"
37
36
 
38
37
  assert_redirect_url(@controller.url_for(:action => "login"))
39
38
  assert_equal 1, ActionMailer::Base.deliveries.size
@@ -41,30 +40,44 @@ class AccountControllerTest < Test::Unit::TestCase
41
40
  assert_equal "newbob@test.com", mail.to_addrs[0].to_s
42
41
  assert_match /login:\s+\w+\n/, mail.encoded
43
42
  assert_match /password:\s+\w+\n/, mail.encoded
44
-
45
- user = User.find_by_email("newbob@test.com")
46
- assert_not_nil user
47
- assert_equal 0, user.verified
48
- post :verify, "id" => user.uuid.to_s
49
- user = User.find_by_email("newbob@test.com")
50
- assert_equal 1, user.verified
51
- assert_redirect_url(@controller.url_for(:action => "login"))
52
- post :login, "user" => { "login" => "newbob", "password" => "newpassword" }
53
- assert_session_has "user"
43
+ mail.encoded =~ /key=(.*?)"/
44
+ key = $1
45
+
46
+ <%= singular_name %> = <%= class_name %>.find_by_email("newbob@test.com")
47
+ assert_not_nil <%= singular_name %>
48
+ assert_equal 0, <%= singular_name %>.verified
49
+
50
+ # First past the expiration.
51
+ Time.advance_one_day = true
52
+ get :welcome, "<%= singular_name %>"=> { "id" => "#{<%= singular_name %>.id}" }, "key" => "#{key}"
53
+ Time.advance_one_day = false
54
+ <%= singular_name %> = <%= class_name %>.find_by_email("newbob@test.com")
55
+ assert_equal 0, <%= singular_name %>.verified
56
+
57
+ # Then a bogus key.
58
+ get :welcome, "<%= singular_name %>"=> { "id" => "#{<%= singular_name %>.id}" }, "key" => "boguskey"
59
+ <%= singular_name %> = <%= class_name %>.find_by_email("newbob@test.com")
60
+ assert_equal 0, <%= singular_name %>.verified
61
+
62
+ # Now the real one.
63
+ get :welcome, "<%= singular_name %>"=> { "id" => "#{<%= singular_name %>.id}" }, "key" => "#{key}"
64
+ <%= singular_name %> = <%= class_name %>.find_by_email("newbob@test.com")
65
+ assert_equal 1, <%= singular_name %>.verified
66
+
67
+ post :login, "<%= singular_name %>" => { "login" => "newbob", "password" => "newpassword" }
68
+ assert_session_has "<%= singular_name %>"
54
69
  get :logout
55
70
  elsif bad_password
56
- post :signup, "user" => { "login" => "newbob", "password" => "bad", "password_confirmation" => "bad", "email" => "newbob@test.com" }
57
- assert_session_has_no "user"
58
- assert_invalid_column_on_record "user", "password"
71
+ post :signup, "<%= singular_name %>" => { "login" => "newbob", "password" => "bad", "password_confirmation" => "bad", "email" => "newbob@test.com" }
72
+ assert_session_has_no "<%= singular_name %>"
73
+ assert_invalid_column_on_record "<%= singular_name %>", "password"
59
74
  assert_success
60
75
  assert_equal 0, ActionMailer::Base.deliveries.size
61
76
  elsif bad_email
62
- CONFIG['inject_mailer_error'] = true
63
- post :signup, "user" => { "login" => "newbob", "password" => "newpassword", "password_confirmation" => "newpassword", "email" => "newbob@test.com" }
64
- CONFIG['inject_mailer_error'] = false
65
- assert_session_has_no "user"
77
+ ActionMailer::Base.inject_one_error = true
78
+ post :signup, "<%= singular_name %>" => { "login" => "newbob", "password" => "newpassword", "password_confirmation" => "newpassword", "email" => "newbob@test.com" }
79
+ assert_session_has_no "<%= singular_name %>"
66
80
  assert_equal 0, ActionMailer::Base.deliveries.size
67
- assert_flash_has "message"
68
81
  else
69
82
  # Invalid test case
70
83
  assert false
@@ -79,48 +92,43 @@ class AccountControllerTest < Test::Unit::TestCase
79
92
 
80
93
  def do_change_password(bad_password, bad_email)
81
94
  ActionMailer::Base.deliveries = []
82
- CONFIG['inject_mailer_error'] = false
83
95
 
84
- post :login, "user" => { "login" => "bob", "password" => "atest" }
85
- assert_session_has "user"
96
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
97
+ assert_session_has "<%= singular_name %>"
86
98
 
87
- @request.session['return-to'] = "/bogus/location"
88
99
  if not bad_password and not bad_email
89
- post :change_password, "user" => { "password" => "changed_password", "password_confirmation" => "changed_password" }
100
+ post :change_password, "<%= singular_name %>" => { "password" => "changed_password", "password_confirmation" => "changed_password" }
90
101
  assert_equal 1, ActionMailer::Base.deliveries.size
91
102
  mail = ActionMailer::Base.deliveries[0]
92
103
  assert_equal "bob@test.com", mail.to_addrs[0].to_s
93
104
  assert_match /login:\s+\w+\n/, mail.encoded
94
105
  assert_match /password:\s+\w+\n/, mail.encoded
95
- assert_redirect_url "/bogus/location"
96
106
  elsif bad_password
97
- post :change_password, "user" => { "password" => "bad", "password_confirmation" => "bad" }
98
- assert_invalid_column_on_record "user", "password"
107
+ post :change_password, "<%= singular_name %>" => { "password" => "bad", "password_confirmation" => "bad" }
108
+ assert_invalid_column_on_record "<%= singular_name %>", "password"
99
109
  assert_success
100
110
  assert_equal 0, ActionMailer::Base.deliveries.size
101
111
  elsif bad_email
102
- CONFIG['inject_mailer_error'] = true
103
- post :change_password, "user" => { "password" => "changed_password", "password_confirmation" => "changed_password" }
104
- CONFIG['inject_mailer_error'] = false
112
+ ActionMailer::Base.inject_one_error = true
113
+ post :change_password, "<%= singular_name %>" => { "password" => "changed_password", "password_confirmation" => "changed_password" }
105
114
  assert_equal 0, ActionMailer::Base.deliveries.size
106
- assert_flash_has "message"
107
115
  else
108
116
  # Invalid test case
109
117
  assert false
110
118
  end
111
119
 
112
120
  get :logout
113
- assert_session_has_no "user"
121
+ assert_session_has_no "<%= singular_name %>"
114
122
 
115
123
  if not bad_password and not bad_email
116
- post :login, "user" => { "login" => "bob", "password" => "changed_password" }
117
- assert_session_has "user"
118
- post :change_password, "user" => { "password" => "atest", "password_confirmation" => "atest" }
124
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "changed_password" }
125
+ assert_session_has "<%= singular_name %>"
126
+ post :change_password, "<%= singular_name %>" => { "password" => "atest", "password_confirmation" => "atest" }
119
127
  get :logout
120
128
  end
121
129
 
122
- post :login, "user" => { "login" => "bob", "password" => "atest" }
123
- assert_session_has "user"
130
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
131
+ assert_session_has "<%= singular_name %>"
124
132
 
125
133
  get :logout
126
134
  end
@@ -133,33 +141,38 @@ class AccountControllerTest < Test::Unit::TestCase
133
141
 
134
142
  def do_forgot_password(bad_address, bad_email, logged_in)
135
143
  ActionMailer::Base.deliveries = []
136
- CONFIG['inject_mailer_error'] = false
137
144
 
138
145
  if logged_in
139
- post :login, "user" => { "login" => "bob", "password" => "atest" }
140
- assert_session_has "user"
146
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
147
+ assert_session_has "<%= singular_name %>"
141
148
  end
142
149
 
143
150
  @request.session['return-to'] = "/bogus/location"
144
151
  if not bad_address and not bad_email
145
- post :forgot_password, "user" => { "email" => "bob@test.com" }
146
- assert_equal 1, ActionMailer::Base.deliveries.size
147
- mail = ActionMailer::Base.deliveries[0]
148
- assert_equal "bob@test.com", mail.to_addrs[0].to_s
149
- assert_match /login:\s+\w+\n/, mail.encoded
150
- assert_match /password:\s+\w{8}\n/, mail.encoded
151
- mail.encoded =~ /password:\s+(\w{8})\n/
152
- password = $1
152
+ post :forgot_password, "<%= singular_name %>" => { "email" => "bob@test.com" }
153
+ password = "anewpassword"
154
+ if logged_in
155
+ assert_equal 0, ActionMailer::Base.deliveries.size
156
+ assert_redirect_url(@controller.url_for(:action => "change_password"))
157
+ post :change_password, "<%= singular_name %>" => { "password" => "#{password}", "password_confirmation" => "#{password}" }
158
+ else
159
+ assert_equal 1, ActionMailer::Base.deliveries.size
160
+ mail = ActionMailer::Base.deliveries[0]
161
+ assert_equal "bob@test.com", mail.to_addrs[0].to_s
162
+ mail.encoded =~ /user\[id\]=(.*?)&key=(.*?)"/
163
+ id = $1
164
+ key = $2
165
+ post :change_password, "<%= singular_name %>" => { "password" => "#{password}", "password_confirmation" => "#{password}", "id" => "#{id}" }, "key" => "#{key}"
166
+ assert_session_has "<%= singular_name %>"
167
+ get :logout
168
+ end
153
169
  elsif bad_address
154
- post :forgot_password, "user" => { "email" => "bademail@test.com" }
170
+ post :forgot_password, "<%= singular_name %>" => { "email" => "bademail@test.com" }
155
171
  assert_equal 0, ActionMailer::Base.deliveries.size
156
- assert_flash_has "message"
157
172
  elsif bad_email
158
- CONFIG['inject_mailer_error'] = true
159
- post :forgot_password, "user" => { "email" => "bob@test.com" }
160
- CONFIG['inject_mailer_error'] = false
173
+ ActionMailer::Base.inject_one_error = true
174
+ post :forgot_password, "<%= singular_name %>" => { "email" => "bob@test.com" }
161
175
  assert_equal 0, ActionMailer::Base.deliveries.size
162
- assert_flash_has "message"
163
176
  else
164
177
  # Invalid test case
165
178
  assert false
@@ -167,25 +180,24 @@ class AccountControllerTest < Test::Unit::TestCase
167
180
 
168
181
  if not bad_address and not bad_email
169
182
  if logged_in
170
- assert_redirect_url "/bogus/location"
171
183
  get :logout
172
184
  else
173
185
  assert_redirect_url(@controller.url_for(:action => "login"))
174
186
  end
175
- post :login, "user" => { "login" => "bob", "password" => "#{password}" }
187
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "#{password}" }
176
188
  else
177
189
  # Okay, make sure the database did not get changed
178
190
  if logged_in
179
191
  get :logout
180
192
  end
181
- post :login, "user" => { "login" => "bob", "password" => "atest" }
193
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
182
194
  end
183
195
 
184
- assert_session_has "user"
196
+ assert_session_has "<%= singular_name %>"
185
197
 
186
198
  # Put the old settings back
187
199
  if not bad_address and not bad_email
188
- post :change_password, "user" => { "password" => "atest", "password_confirmation" => "atest" }
200
+ post :change_password, "<%= singular_name %>" => { "password" => "atest", "password_confirmation" => "atest" }
189
201
  end
190
202
 
191
203
  get :logout
@@ -201,35 +213,34 @@ class AccountControllerTest < Test::Unit::TestCase
201
213
  def test_bad_signup
202
214
  @request.session['return-to'] = "/bogus/location"
203
215
 
204
- post :signup, "user" => { "login" => "newbob", "password" => "newpassword", "password_confirmation" => "wrong" }
205
- assert_invalid_column_on_record "user", "password"
216
+ post :signup, "<%= singular_name %>" => { "login" => "newbob", "password" => "newpassword", "password_confirmation" => "wrong" }
217
+ assert_invalid_column_on_record "<%= singular_name %>", "password"
206
218
  assert_success
207
219
 
208
- post :signup, "user" => { "login" => "yo", "password" => "newpassword", "password_confirmation" => "newpassword" }
209
- assert_invalid_column_on_record "user", "login"
220
+ post :signup, "<%= singular_name %>" => { "login" => "yo", "password" => "newpassword", "password_confirmation" => "newpassword" }
221
+ assert_invalid_column_on_record "<%= singular_name %>", "login"
210
222
  assert_success
211
223
 
212
- post :signup, "user" => { "login" => "yo", "password" => "newpassword", "password_confirmation" => "wrong" }
213
- assert_invalid_column_on_record "user", ["login", "password"]
224
+ post :signup, "<%= singular_name %>" => { "login" => "yo", "password" => "newpassword", "password_confirmation" => "wrong" }
225
+ assert_invalid_column_on_record "<%= singular_name %>", ["login", "password"]
214
226
  assert_success
215
227
  end
216
228
 
217
229
  def test_invalid_login
218
- post :login, "user" => { "login" => "bob", "password" => "not_correct" }
230
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "not_correct" }
219
231
 
220
- assert_session_has_no "user"
232
+ assert_session_has_no "<%= singular_name %>"
221
233
 
222
- assert_flash_has "message"
223
234
  assert_template_has "login"
224
235
  end
225
236
 
226
237
  def test_login_logoff
227
238
 
228
- post :login, "user" => { "login" => "bob", "password" => "atest" }
229
- assert_session_has "user"
239
+ post :login, "<%= singular_name %>" => { "login" => "bob", "password" => "atest" }
240
+ assert_session_has "<%= singular_name %>"
230
241
 
231
242
  get :logout
232
- assert_session_has_no "user"
243
+ assert_session_has_no "<%= singular_name %>"
233
244
 
234
245
  end
235
246
 
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.dirname(__FILE__) + '/../config/environment'
4
+ require 'rubygems'
5
+ require_gem 'db_structure'
6
+
7
+ DBStructure::db_structure
data/templates/en.yaml ADDED
@@ -0,0 +1,69 @@
1
+ active_record_errors_inclusion: Inclusion error
2
+ active_record_errors_inclusion: is not included in the list
3
+ active_record_errors_invalid: is invalid
4
+ active_record_errors_confirmation: doesn't match confirmation
5
+ active_record_errors_accepted: must be accepted
6
+ active_record_errors_empty: can't be empty
7
+ active_record_errors_too_long: is too long (max is %d characters)
8
+ active_record_errors_too_short: is too short (min is %d characters)
9
+ active_record_errors_wrong_length: is the wrong length (should be %d characters)
10
+ active_record_errors_taken: has already been taken
11
+ active_record_errors_not_a_number: is not a number
12
+
13
+ # Controller
14
+ <%= singular_name %>_login_succeeded: Login successful
15
+ <%= singular_name %>_login_failed: Login unsuccessful
16
+ <%= singular_name %>_signup_succeeded: Signup successful! Please check your registered email account to verify your account registration and continue with the login.
17
+ <%= singular_name %>_confirmation_email_error: 'Error creating account: confirmation email not sent'
18
+ <%= singular_name %>_updated_password: Your updated password has been emailed to %s
19
+ <%= singular_name %>_change_password_email_error: Your password could not be changed at this time. Please retry.
20
+ <%= singular_name %>_enter_valid_email_address: Please enter a valid email address
21
+ <%= singular_name %>_email_address_not_found: We could not find a user with the email address %s
22
+ <%= singular_name %>_forgotten_password_emailed: Instructions on resetting your password have been emailed to %s
23
+ <%= singular_name %>_forgotten_password_email_error: Your password could not be emailed to %s
24
+ <%= singular_name %>_account_verified: Account verified!
25
+
26
+ # Views
27
+ # Welcome
28
+ <%= singular_name %>_welcome_head: Welcome
29
+ <%= singular_name %>_now_logged_in_message: You are now logged into the system...
30
+ <%= singular_name %>_redirect_message: Since you are here it's safe to assume the application never called store_location, otherwise you would have been redirected somewhere else after a successful login.
31
+ <%= singular_name %>_welcome_logout_link: '&#171; logout'
32
+
33
+ # Signup
34
+ <%= singular_name %>_signup_head: Signup
35
+ <%= singular_name %>_signup_button: Signup
36
+
37
+ # Login
38
+ <%= singular_name %>_login_head: Please login
39
+ <%= singular_name %>_firstname_form: First name
40
+ <%= singular_name %>_lastname_form: Last name
41
+ <%= singular_name %>_login_form: Login ID
42
+ <%= singular_name %>_email_form: Email
43
+ <%= singular_name %>_password_form: Password
44
+ <%= singular_name %>_password_confirmation_form: Password confirmation
45
+ <%= singular_name %>_login_button: Login
46
+ <%= singular_name %>_login_signup_link: Register for an account
47
+ <%= singular_name %>_login_forgot_password_link: Forgot my password
48
+
49
+ # Logout
50
+ <%= singular_name %>_logoff_head: Logoff
51
+ <%= singular_name %>_logged_off_message: You are now logged out of the system...
52
+ <%= singular_name %>_logout_login_link: '&#171; login'
53
+
54
+ # Change password
55
+ <%= singular_name %>_change_password_head: Change Password
56
+ <%= singular_name %>_change_message: Enter your new password in the fields below and click 'Change Password' to have a new password sent to your email inbox.
57
+ <%= singular_name %>_change_password_button: Change password
58
+ <%= singular_name %>_change_cancel_welcome_link: Cancel
59
+
60
+ # Forgotten password
61
+ <%= singular_name %>_forgot_password_head: Forgotten Password
62
+ <%= singular_name %>_forgot_reset_message: Enter your email address in the field below and click 'Reset Password' to have instructions on how to retrieve your forgotten password emailed to you.
63
+ <%= singular_name %>_forgot_password_logged_in: You are currently logged in. You may change your password now.
64
+ <%= singular_name %>_forgot_reset_button: Submit request
65
+ <%= singular_name %>_forgot_cancel_login_link: Cancel
66
+
67
+ # Edit
68
+ <%= singular_name %>_edit_head: Edit <%= singular_name %>
69
+ <%= singular_name %>_change_settings_button: Change settings
data/templates/helper.rb CHANGED
@@ -1,2 +1,124 @@
1
1
  module <%= class_name %>Helper
2
+
3
+ DEFAULT_HEAD_OPTIONS = {
4
+ :notice => true,
5
+ :message => true,
6
+ :error => false
7
+ }.freeze
8
+
9
+ # Abstraction to make views a little cleaner
10
+ def form_input(helper_method, field_name, options = {}, form_name = nil)
11
+ form_name = "<%= singular_name %>" if form_name.nil?
12
+ case helper_method.to_s
13
+ when 'hidden_field'
14
+ self.hidden_field(form_name, field_name, options)
15
+ when /^.*button$/
16
+ prompt = l(:"#{@controller.controller_name}_#{field_name}_button")
17
+ <<-EOL
18
+ <tr><td class="button" colspan="2">
19
+ #{self.send(helper_method, form_name, prompt, options)}
20
+ </td></tr>
21
+ EOL
22
+ else
23
+ field = (
24
+ case helper_method
25
+ when :select
26
+ self.send(helper_method, form_name, field_name, options.delete('values'), options)
27
+ when :password_field
28
+ options[:value] = ""
29
+ self.send(helper_method, form_name, field_name, options)
30
+ else
31
+ self.send(helper_method, form_name, field_name, options)
32
+ end)
33
+ lname = "#{form_name}_#{field_name}_form"
34
+ prompt = l(:"#{lname}")
35
+ if <%= class_name %>System::CONFIG[:two_column_input]
36
+ <<-EOL
37
+ <tr class="two_columns">
38
+ <td class="prompt"><label>#{prompt}:</label></td>
39
+ <td class="value">#{field}</td>
40
+ </tr>
41
+ EOL
42
+ else
43
+ <<-EOL
44
+ <tr><td class="prompt"><label>#{prompt}:</label></td></tr>
45
+ <tr><td class="value">#{field}</td></tr>
46
+ EOL
47
+ end
48
+ end
49
+ end
50
+
51
+ def button_helper(name, options = {})
52
+ label = l(:"#{@controller.controller_name}_#{name}_button")
53
+ "#{self.send(:submit_tag, label, options)}"
54
+ end
55
+
56
+ def link_helper(name, options = {})
57
+ raise ArgumentError if name.nil?
58
+ label = l(:"#{@controller.controller_name}_#{name}_link")
59
+ "#{self.send(:link_to, label, options)}"
60
+ end
61
+
62
+ def title_helper
63
+ "#{@controller.controller_class_name} #{@controller.action_name}"
64
+ end
65
+
66
+ def head_helper(options = {})
67
+ label = l(:"#{@controller.controller_name}_#{@controller.action_name}_head")
68
+ notice = message = error = nil
69
+ opts = DEFAULT_HEAD_OPTIONS.dup
70
+ opts.update(options.symbolize_keys)
71
+ s = "<h3>#{label}</h3>"
72
+ if @flash['notice'] and not opts[:notice].nil? and opts[:notice]
73
+ notice = "<div><p>#{@flash['notice']}</p></div>"
74
+ s = s + notice
75
+ end
76
+ if @flash['message'] and not opts[:message].nil? and opts[:message]
77
+ message = "<div id=\"ErrorExplanation\"><p>#{@flash['message']}</p></div>"
78
+ s = s + message
79
+ end
80
+ if not opts[:error].nil? and opts[:error]
81
+ error = error_messages_for('<%= singular_name %>')
82
+ if not error.nil?
83
+ error = error + "<br/>"
84
+ s = s + error
85
+ end
86
+ end
87
+ return s
88
+ <<-EOL
89
+ <h3>#{label}</h3>
90
+ #{notice}
91
+ #{message}
92
+ #{error}
93
+ EOL
94
+ end
95
+
96
+ def message_helper(name)
97
+ l(:"#{@controller.controller_name}_#{name}_message")
98
+ end
99
+
100
+ def start_form_tag_helper(options = {})
101
+ url = url_for(:action => "#{@controller.action_name}")
102
+ "#{self.send(:start_form_tag, url, options)}"
103
+ end
104
+
105
+ def attributes(hash)
106
+ hash.keys.inject("") { |attrs, key| attrs + %{#{key}="#{h(hash[key])}" } }
107
+ end
108
+
109
+ def read_only_field(form_name, field_name, html_options)
110
+ "<span #{attributes(html_options)}>#{instance_variable_get('@' + form_name)[field_name]}</span>"
111
+ end
112
+
113
+ def submit_button(form_name, prompt, html_options)
114
+ %{<input name="submit" type="submit" value="#{prompt}" />}
115
+ end
116
+
117
+ def changeable(<%= singular_name %>, field)
118
+ if <%= singular_name %>.new_record? or <%= class_name %>System::CONFIG[:changeable_fields].include?(field)
119
+ :text_field
120
+ else
121
+ :read_only_field
122
+ end
123
+ end
2
124
  end
@@ -0,0 +1,32 @@
1
+ module <%= class_name %>System
2
+ CONFIG = {
3
+ # Source address for user emails
4
+ :email_from => '',
5
+
6
+ # Destination email for system errors
7
+ :admin_email => '',
8
+
9
+ # Sent in emails to users
10
+ :app_url => 'http://localhost:3000/',
11
+
12
+ # Sent in emails to users
13
+ :app_name => '',
14
+
15
+ # Email charset
16
+ :mail_charset => 'utf-8',
17
+
18
+ # Security token lifetime in hours
19
+ :security_token_life_hours => 24,
20
+
21
+ # Two column form input
22
+ :two_column_input => true,
23
+
24
+ # Add all changeable <%= singular_name %> fields to this array.
25
+ # They will then be able to be edited from the edit action. You
26
+ # should NOT include the email field in this array.
27
+ :changeable_fields => [ 'firstname', 'lastname' ],
28
+
29
+ # Server environment
30
+ :server_env => "#{RAILS_ENV}"
31
+ }
32
+ end
@@ -1,7 +1,5 @@
1
- require_dependency "user"
1
+ module <%= class_name %>System
2
2
 
3
- module LoginSystem
4
-
5
3
  protected
6
4
 
7
5
  # overwrite this if you want to restrict access to only a few actions
@@ -9,10 +7,10 @@ module LoginSystem
9
7
  # example:
10
8
  #
11
9
  # # only allow nonbobs
12
- # def authorize?(user)
13
- # user.login != "bob"
10
+ # def authorize?(<%= singular_name %>)
11
+ # <%= singular_name %>.login != "bob"
14
12
  # end
15
- def authorize?(user)
13
+ def authorize?(<%= singular_name %>)
16
14
  true
17
15
  end
18
16
 
@@ -38,7 +36,7 @@ module LoginSystem
38
36
  # if the controller should be under any rights management.
39
37
  # for finer access control you can overwrite
40
38
  #
41
- # def authorize?(user)
39
+ # def authorize?(<%= singular_name %>)
42
40
  #
43
41
  def login_required
44
42
 
@@ -46,7 +44,7 @@ module LoginSystem
46
44
  return true
47
45
  end
48
46
 
49
- if @session['user'] and authorize?(@session['user'])
47
+ if <%= singular_name %>? and authorize?(@session['<%= singular_name %>'])
50
48
  return true
51
49
  end
52
50
 
@@ -65,7 +63,7 @@ module LoginSystem
65
63
  # example use :
66
64
  # a popup window might just close itself for instance
67
65
  def access_denied
68
- redirect_to :controller=>"/<%= file_name %>", :action =>"login"
66
+ redirect_to :controller => "/<%= file_name %>", :action => "login"
69
67
  end
70
68
 
71
69
  # store current uri in the session.
@@ -84,4 +82,20 @@ module LoginSystem
84
82
  end
85
83
  end
86
84
 
87
- end
85
+ def <%= singular_name %>?
86
+ # First, is the user already authenticated?
87
+ return true if not @session['<%= singular_name %>'].nil?
88
+
89
+ # If not, is the user being authenticated by a token?
90
+ return false if not @params['<%= singular_name %>']
91
+ id = @params['<%= singular_name %>']['id']
92
+ key = @params['key']
93
+ if id and key
94
+ @session['<%= singular_name %>'] = <%= class_name %>.authenticate_by_token(id, key)
95
+ return true if not @session['<%= singular_name %>'].nil?
96
+ end
97
+
98
+ # Everything failed
99
+ return false
100
+ end
101
+ end
@@ -0,0 +1,18 @@
1
+ require 'models/<%= file_name %>_notify.rb'
2
+
3
+ ActionMailer::Base.class_eval {
4
+ @@inject_one_error = false
5
+ cattr_accessor :inject_one_error
6
+
7
+ class << self
8
+ private
9
+ def perform_delivery_test(mail)
10
+ if inject_one_error
11
+ ActionMailer::Base::inject_one_error = false
12
+ raise "Failed to send email" if raise_delivery_errors
13
+ else
14
+ deliveries << mail
15
+ end
16
+ end
17
+ end
18
+ }
@@ -0,0 +1,17 @@
1
+ require 'time'
2
+
3
+ Time.class_eval {
4
+ @@advance_one_day = false
5
+ cattr_accessor :advance_one_day
6
+
7
+ class << Time
8
+ alias now_old now
9
+ def now
10
+ if Time.advance_one_day
11
+ return Time.at(now_old.to_i + 60 * 60 * 24 + 1)
12
+ else
13
+ now_old
14
+ end
15
+ end
16
+ end
17
+ }