parlement 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/CHANGES +709 -0
  2. data/COPYING +223 -0
  3. data/README +20 -0
  4. data/Rakefile +136 -0
  5. data/app/controllers/account_controller.rb +181 -0
  6. data/app/controllers/application.rb +30 -0
  7. data/app/controllers/elt_controller.rb +83 -0
  8. data/app/helpers/account_helper.rb +2 -0
  9. data/app/helpers/application_helper.rb +4 -0
  10. data/app/helpers/elt_helper.rb +37 -0
  11. data/app/helpers/live_tree.rb +238 -0
  12. data/app/helpers/mailman.rb +96 -0
  13. data/app/models/attachment.rb +4 -0
  14. data/app/models/elt.rb +17 -0
  15. data/app/models/mail.rb +4 -0
  16. data/app/models/notifier.rb +13 -0
  17. data/app/models/person.rb +9 -0
  18. data/app/models/user.rb +7 -0
  19. data/app/models/user_notify.rb +75 -0
  20. data/app/views/account/_help.rhtml +23 -0
  21. data/app/views/account/_login.rhtml +57 -0
  22. data/app/views/account/_show.rhtml +31 -0
  23. data/app/views/account/logout.rhtml +10 -0
  24. data/app/views/account/signup.rhtml +17 -0
  25. data/app/views/account/welcome.rhtml +13 -0
  26. data/app/views/elt/_elt.rhtml +105 -0
  27. data/app/views/elt/_form.rhtml +31 -0
  28. data/app/views/elt/_list.rhtml +28 -0
  29. data/app/views/elt/new.rhtml +102 -0
  30. data/app/views/elt/rss.rxml +31 -0
  31. data/app/views/elt/show.rhtml +46 -0
  32. data/app/views/elt/show_tree.rhtml +8 -0
  33. data/app/views/layouts/scaffold.rhtml +13 -0
  34. data/app/views/layouts/top.rhtml +45 -0
  35. data/app/views/notifier/changeEmail.rhtml +10 -0
  36. data/config/boot.rb +17 -0
  37. data/config/database.yml +82 -0
  38. data/config/environment.rb +92 -0
  39. data/config/environments/development.rb +17 -0
  40. data/config/environments/production.rb +17 -0
  41. data/config/environments/test.rb +17 -0
  42. data/config/environments/user_environment.rb +1 -0
  43. data/config/routes.rb +28 -0
  44. data/db/ROOT/CV.txt +166 -0
  45. data/db/ROOT/IP.txt +3 -0
  46. data/db/ROOT/parleR.txt +3 -0
  47. data/db/ROOT/parlement/security.txt +34 -0
  48. data/db/ROOT/parlement/test.txt +4 -0
  49. data/db/ROOT/parlement.txt +51 -0
  50. data/db/ROOT/perso.txt +215 -0
  51. data/db/schema.sql +127 -0
  52. data/lib/data_import.rb +54 -0
  53. data/lib/file_column.rb +263 -0
  54. data/lib/file_column_helper.rb +45 -0
  55. data/lib/localization.rb +88 -0
  56. data/lib/localizer.rb +88 -0
  57. data/lib/login_system.rb +87 -0
  58. data/lib/rails_file_column.rb +19 -0
  59. data/lib/user_system.rb +101 -0
  60. data/public/404.html +8 -0
  61. data/public/500.html +8 -0
  62. data/public/dispatch.cgi +10 -0
  63. data/public/dispatch.fcgi +24 -0
  64. data/public/dispatch.rb +10 -0
  65. data/public/engine_files/README +5 -0
  66. data/public/engine_files/login_engine/stylesheets/login_engine.css +81 -0
  67. data/public/favicon.ico +0 -0
  68. data/public/favicon.png +0 -0
  69. data/public/images/live_tree_branch_collapsed_icon.gif +0 -0
  70. data/public/images/live_tree_branch_expanded_icon.gif +0 -0
  71. data/public/images/live_tree_leaf_icon.gif +0 -0
  72. data/public/images/live_tree_loading_spinner.gif +0 -0
  73. data/public/images/webfeed.gif +0 -0
  74. data/public/javascripts/controls.js +721 -0
  75. data/public/javascripts/dragdrop.js +519 -0
  76. data/public/javascripts/effects.js +992 -0
  77. data/public/javascripts/live_tree.js +749 -0
  78. data/public/javascripts/prototype.js +1726 -0
  79. data/public/javascripts/scriptaculous.js +47 -0
  80. data/public/javascripts/slider.js +258 -0
  81. data/public/oldREADME +190 -0
  82. data/public/oldindex.html +78 -0
  83. data/public/robots.txt +1 -0
  84. data/public/stylesheets/default.css +238 -0
  85. data/public/stylesheets/live_tree.css +62 -0
  86. data/public/stylesheets/scaffold.css +74 -0
  87. data/script/about +3 -0
  88. data/script/benchmarker +19 -0
  89. data/script/breakpointer +3 -0
  90. data/script/console +3 -0
  91. data/script/create_db +7 -0
  92. data/script/destroy +3 -0
  93. data/script/generate +3 -0
  94. data/script/performance/benchmarker +3 -0
  95. data/script/performance/profiler +3 -0
  96. data/script/plugin +3 -0
  97. data/script/process/reaper +3 -0
  98. data/script/process/spawner +3 -0
  99. data/script/process/spinner +3 -0
  100. data/script/profiler +34 -0
  101. data/script/runner +3 -0
  102. data/script/server +3 -0
  103. data/test/fixtures/attachments.yml +10 -0
  104. data/test/fixtures/elts.yml +15 -0
  105. data/test/fixtures/mails.yml +7 -0
  106. data/test/fixtures/people.yml +49 -0
  107. data/test/fixtures/users.yml +41 -0
  108. data/test/functional/account_controller_test.rb +239 -0
  109. data/test/functional/elt_controller_test.rb +18 -0
  110. data/test/mocks/test/time.rb +17 -0
  111. data/test/mocks/test/user_notify.rb +16 -0
  112. data/test/test_helper.rb +28 -0
  113. data/test/unit/attachment_test.rb +14 -0
  114. data/test/unit/elt_test.rb +14 -0
  115. data/test/unit/mail_test.rb +14 -0
  116. data/test/unit/notifier_test.rb +31 -0
  117. data/test/unit/person_test.rb +24 -0
  118. data/test/unit/user_test.rb +94 -0
  119. data/vendor/plugins/engines/CHANGELOG +7 -0
  120. data/vendor/plugins/engines/README +128 -0
  121. data/vendor/plugins/engines/init.rb +33 -0
  122. data/vendor/plugins/engines/lib/action_mailer_extensions.rb +160 -0
  123. data/vendor/plugins/engines/lib/action_view_extensions.rb +130 -0
  124. data/vendor/plugins/engines/lib/dependencies_extensions.rb +56 -0
  125. data/vendor/plugins/engines/lib/engines.rb +292 -0
  126. data/vendor/plugins/engines/lib/ruby_extensions.rb +127 -0
  127. data/vendor/plugins/engines/lib/testing_extensions.rb +33 -0
  128. data/vendor/plugins/engines/test/ruby_extensions_test.rb +94 -0
  129. data/vendor/plugins/login_engine/README +258 -0
  130. data/vendor/plugins/login_engine/app/controllers/user_controller.rb +248 -0
  131. data/vendor/plugins/login_engine/app/helpers/user_helper.rb +88 -0
  132. data/vendor/plugins/login_engine/app/models/user.rb +7 -0
  133. data/vendor/plugins/login_engine/app/models/user_notify.rb +75 -0
  134. data/vendor/plugins/login_engine/app/views/user/_edit.rhtml +11 -0
  135. data/vendor/plugins/login_engine/app/views/user/_password.rhtml +9 -0
  136. data/vendor/plugins/login_engine/app/views/user/change_password.rhtml +17 -0
  137. data/vendor/plugins/login_engine/app/views/user/edit.rhtml +23 -0
  138. data/vendor/plugins/login_engine/app/views/user/forgot_password.rhtml +18 -0
  139. data/vendor/plugins/login_engine/app/views/user/home.rhtml +7 -0
  140. data/vendor/plugins/login_engine/app/views/user/login.rhtml +17 -0
  141. data/vendor/plugins/login_engine/app/views/user/logout.rhtml +8 -0
  142. data/vendor/plugins/login_engine/app/views/user/signup.rhtml +17 -0
  143. data/vendor/plugins/login_engine/app/views/user_notify/change_password.rhtml +10 -0
  144. data/vendor/plugins/login_engine/app/views/user_notify/delete.rhtml +5 -0
  145. data/vendor/plugins/login_engine/app/views/user_notify/forgot_password.rhtml +11 -0
  146. data/vendor/plugins/login_engine/app/views/user_notify/pending_delete.rhtml +9 -0
  147. data/vendor/plugins/login_engine/app/views/user_notify/signup.rhtml +12 -0
  148. data/vendor/plugins/login_engine/db/schema.rb +25 -0
  149. data/vendor/plugins/login_engine/init_engine.rb +10 -0
  150. data/vendor/plugins/login_engine/lib/login_engine/authenticated_system.rb +107 -0
  151. data/vendor/plugins/login_engine/lib/login_engine/authenticated_user.rb +149 -0
  152. data/vendor/plugins/login_engine/lib/login_engine.rb +58 -0
  153. data/vendor/plugins/login_engine/public/stylesheets/login_engine.css +81 -0
  154. data/vendor/plugins/login_engine/tasks/tasks.rake +4 -0
  155. data/vendor/plugins/login_engine/test/fixtures/templates/users.yml +41 -0
  156. data/vendor/plugins/login_engine/test/fixtures/users.yml +41 -0
  157. data/vendor/plugins/login_engine/test/functional/user_controller_test.rb +533 -0
  158. data/vendor/plugins/login_engine/test/mocks/mail.rb +14 -0
  159. data/vendor/plugins/login_engine/test/mocks/time.rb +19 -0
  160. data/vendor/plugins/login_engine/test/test_helper.rb +15 -0
  161. data/vendor/plugins/login_engine/test/unit/user_test.rb +94 -0
  162. metadata +276 -0
@@ -0,0 +1,127 @@
1
+ #--
2
+ # Copyright (c) 2005 James Adam
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ #--
25
+ # Add these methods to the top-level module so that they are available in all
26
+ # modules, etc
27
+ #++
28
+ class ::Module
29
+ # Defines a constant within a module/class ONLY if that constant does
30
+ # not already exist.
31
+ #
32
+ # This can be used to implement defaults in plugins/engines/libraries, e.g.
33
+ # if a plugin module exists:
34
+ # module MyPlugin
35
+ # default_constant :MyDefault, "the_default_value"
36
+ # end
37
+ #
38
+ # then developers can override this default by defining that constant at
39
+ # some point *before* the module/plugin gets loaded (such as environment.rb)
40
+ def default_constant(name, value)
41
+ if !(name.is_a?(String) or name.is_a?(Symbol))
42
+ raise "Cannot use a #{name.class.name} ['#{name}'] object as a constant name"
43
+ end
44
+ if !self.const_defined?(name)
45
+ self.class_eval("#{name} = #{value.inspect}")
46
+ end
47
+ end
48
+
49
+ # A mechanism for defining configuration of Modules. With this
50
+ # mechanism, default values for configuration can be provided within shareable
51
+ # code, and the end user can customise the configuration without having to
52
+ # provide all values.
53
+ #
54
+ # Example:
55
+ #
56
+ # module MyModule
57
+ # config :param_one, "some value"
58
+ # config :param_two, 12345
59
+ # end
60
+ #
61
+ # Those values can now be accessed by the following method
62
+ #
63
+ # MyModule.config :param_one
64
+ # => "some value"
65
+ # MyModule.config :param_two
66
+ # => 12345
67
+ #
68
+ # ... or, if you have overrriden the method 'config'
69
+ #
70
+ # MyModule::CONFIG[:param_one]
71
+ # => "some value"
72
+ # MyModule::CONFIG[:param_two]
73
+ # => 12345
74
+ #
75
+ # Once a value is stored in the configuration, it will not be altered
76
+ # by subsequent assignments, unless a special flag is given:
77
+ #
78
+ # (later on in your code, most likely in another file)
79
+ # module MyModule
80
+ # config :param_one, "another value"
81
+ # config :param_two, 98765, :force
82
+ # end
83
+ #
84
+ # The configuration is now:
85
+ #
86
+ # MyModule.config :param_one
87
+ # => "some value" # not changed
88
+ # MyModule.config :param_two
89
+ # => 98765
90
+ #
91
+ def config(*args)
92
+
93
+ raise "config expects at least one argument" if args.empty?
94
+
95
+ # extract the arguments
96
+ if args[0].is_a?(Hash)
97
+ # we can't override when using hash'd arguments since
98
+ # if > 1 hash keys are given, it's impossible to tell which
99
+ # one is the name of the option, and which is the override flag.
100
+ args[0].each { |key, value| _handle_config(key, value)}
101
+ else
102
+ _handle_config(*args)
103
+ end
104
+ end
105
+
106
+ private
107
+ # Actually set the config values
108
+ def _handle_config(name, value=nil, override=false)
109
+ if !self.const_defined?("CONFIG")
110
+ self.class_eval("CONFIG = {}")
111
+ end
112
+
113
+ if value != nil
114
+ if override or self::CONFIG[name] == nil
115
+ self::CONFIG[name] = value
116
+ end
117
+ else
118
+ # if we pass an array of config keys to config(),
119
+ # get the array of values back
120
+ if name.is_a? Array
121
+ name.map { |c| self::CONFIG[c] }
122
+ else
123
+ self::CONFIG[name]
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,33 @@
1
+ require 'fileutils'
2
+
3
+ module Test
4
+ module Unit
5
+ class TestCase
6
+ # Create a fixtures file based on the template file
7
+ # (<fixture_path>/templates/<fixture_template_name>.yml), and create a suitable
8
+ # fixture file in the fixture_path directory to be loaded into the table given by
9
+ # table_name.
10
+ def self.set_fixtures_table(fixture_file_name, table_name)
11
+ # presume that the template files are in fixture_path + "/templates"
12
+ template_file = File.join(fixture_path, "templates", fixture_file_name.to_s + ".yml")
13
+ destination_file = File.join(fixture_path, table_name.to_s + ".yml")
14
+ if !File.exists?(template_file)
15
+ raise "Cannot find fixture template file '#{template_file}'!"
16
+ end
17
+ # Copy the file across, unless the destination is identical.
18
+ begin
19
+ unless File.exist?(destination_file) && FileUtils.identical?(template_file, destination_file)
20
+ FileUtils.cp(template_file, destination_file)
21
+ end
22
+ rescue Exception => e
23
+ raise "Couldn't create fixture file: " + e
24
+ end
25
+ end
26
+
27
+ # Returns any object from the given fixtures
28
+ def fixture_object(fixture_name, object_name)
29
+ send(fixture_name.to_sym, object_name)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,94 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require File.expand_path(File.dirname(__FILE__) + '/../../../../config/environment')
3
+ require 'test_help'
4
+
5
+ class EnginesTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ # create the module to be used for config testing
9
+ eval "module TestModule end"
10
+ end
11
+
12
+ def teardown
13
+ # remove the TestModule constant from our scope
14
+ self.class.class_eval { remove_const :TestModule }
15
+ end
16
+
17
+
18
+ #
19
+ # Module.config
20
+ #
21
+
22
+ def test_config_no_arguments
23
+ assert_raise(RuntimeError) { TestModule.config }
24
+ end
25
+
26
+ def test_config_array_arguments
27
+ TestModule.config :monkey, 123
28
+ assert_equal(123, TestModule.config(:monkey))
29
+ end
30
+
31
+ def test_config_hash_arguments
32
+ TestModule.config :monkey => 123, :donkey => 456
33
+ assert_equal(123, TestModule.config(:monkey))
34
+ assert_equal(456, TestModule.config(:donkey))
35
+ end
36
+
37
+ def test_config_cant_overwrite_existing_config_values
38
+ TestModule.config :monkey, 123
39
+ assert_equal(123, TestModule.config(:monkey))
40
+ TestModule.config :monkey, 456
41
+ assert_equal(123, TestModule.config(:monkey))
42
+
43
+ # in this case, the resulting Hash only has {:baboon => "goodbye!"} - that's Ruby, users beware.
44
+ TestModule.config :baboon => "hello", :baboon => "goodbye!"
45
+ assert_equal("goodbye!", TestModule.config(:baboon))
46
+ end
47
+
48
+ def test_config_force_new_value
49
+ TestModule.config :monkey, 123
50
+ assert_equal(123, TestModule.config(:monkey))
51
+ TestModule.config :monkey, 456, :force
52
+ assert_equal(456, TestModule.config(:monkey))
53
+ end
54
+
55
+ # this test is somewhat redundant, but it might be an idea to havbe it explictly anyway
56
+ def test_config_get_values
57
+ TestModule.config :monkey, 123
58
+ assert_equal(123, TestModule.config(:monkey))
59
+ end
60
+
61
+ def test_config_get_multiple_values
62
+ TestModule.config :monkey, 123
63
+ TestModule.config :donkey, 456
64
+ assert_equal([123, 456], TestModule.config([:monkey, :donkey]))
65
+ end
66
+
67
+
68
+ #
69
+ # Module.default_constant
70
+ #
71
+
72
+ def test_default_constant_set
73
+ TestModule.default_constant :Monkey, 123
74
+ assert_equal(123, TestModule::Monkey)
75
+ TestModule.default_constant "Hello", 456
76
+ assert_equal(456, TestModule::Hello)
77
+ end
78
+
79
+ def test_default_constant_cannot_set_again
80
+ TestModule.default_constant :Monkey, 789
81
+ assert_equal(789, TestModule::Monkey)
82
+ TestModule.default_constant :Monkey, 456
83
+ assert_equal(789, TestModule::Monkey)
84
+ end
85
+
86
+ def test_default_constant_bad_arguments
87
+ # constant names must be Captialized
88
+ assert_raise(NameError) { TestModule.default_constant :lowercase_name, 123 }
89
+
90
+ # constant names should be given as Strings or Symbols
91
+ assert_raise(RuntimeError) { TestModule.default_constant 123, 456 }
92
+ assert_raise(RuntimeError) { TestModule.default_constant Object.new, 456 }
93
+ end
94
+ end
@@ -0,0 +1,258 @@
1
+ = Before we start
2
+
3
+ This is a Rails Engine version of the Salted Login Generator, a most excellent login system which is sufficient for most simple cases. For the most part, this code has not been altered from its generator form, with the following notable exceptions
4
+
5
+ * Localization has been removed.
6
+ * The 'welcome' page has been changed to the 'home' page
7
+ * A few new functions have been thrown in
8
+ * It's... uh.... a Rails Engine now ;-)
9
+
10
+ However, what I'm trying to say is that 99.9999% of the credit for this should go to Tobias Luetke (xal) and the folks that worked on the original Salted Login generator code. I've just wrapped it into something runnable with the Rails Engine system.
11
+
12
+ Please also bear in mind that this is a work in progress, and things like testing are wildly up in the air... but they will fall into place very soon. And now, on with the show.
13
+
14
+
15
+ = Installation
16
+
17
+ Installing the Login Engine is fairly simple, but there are a few configuration steps that you'll need to take to get everything running smoothly. Listed below are the changes to your application you will need to make.
18
+
19
+ === Setup your Rails application
20
+
21
+ Edit your <tt>database.yml</tt>, most importantly! You might also want to move <tt>public/index.html</tt> out of the way, and set up some default routes in <tt>config/routes.rb</tt>.
22
+
23
+ === Add configuration and start engine
24
+
25
+ Add the following to the bottom of environment.rb:
26
+
27
+ module LoginEngine
28
+ config :salt, "your-salt-here"
29
+ end
30
+
31
+ Engines.start :login
32
+
33
+ You'll probably want to change the Salt value to something unique. You can also override any of the configuration values defined at the top of lib/user_system.rb in a similar way. Note that you don't need to start the engine with <tt>Engines.start :login_engine</tt> - instead, <tt>:login</tt> (or any name) is sufficient if the engine is a directory named <some-name>_engine.
34
+
35
+
36
+ === Add the filters
37
+
38
+ Next, edit your <tt>app/controllers/application.rb</tt> file. The beginning of your <tt>ApplicationController</tt> should look something like this:
39
+
40
+ require 'login_engine'
41
+
42
+ class ApplicationController < ActionController::Base
43
+ include LoginEngine
44
+ helper :user
45
+ model :user
46
+
47
+ before_filter :login_required
48
+
49
+ If you don't want ALL actions to require a login, you need to read further below to learn how to restrict only certain actions.
50
+
51
+ Add the following to your ApplicationHelper:
52
+
53
+ module ApplicationHelper
54
+ include LoginEngine
55
+ end
56
+
57
+ This ensures that the methods to work with users in your views are available
58
+
59
+ === Set up ActionMailer
60
+
61
+ If you want to disable email functions within the Login Engine, simple set the :use_email_notification config flag to false in your environment.rb file:
62
+
63
+ module LoginEngine
64
+
65
+ # ... other options...
66
+ config :use_email_notification, false
67
+
68
+ end
69
+
70
+ You should note that retrieving forgotten passwords automatically isn't possible when the email functions are disabled. Instead, the user is presented with a message instructing them to contact the system administrator
71
+
72
+ If you wish you use email notifications and account creation verification, you must properly configure ActionMailer for your mail settings. For example, you could add the following in config/environments/development.rb (for a .Mac account, and with your own username and password, obviously):
73
+
74
+ ActionMailer::Base.server_settings = {
75
+ :address => "smtp.mac.com",
76
+ :port => 25,
77
+ :domain => "smtp.mac.com",
78
+ :user_name => "<your user name here>",
79
+ :password => "<your password here>",
80
+ :authentication => :login
81
+ }
82
+
83
+ You'll need to configure it properly so that email can be sent. One of the easiest ways to test your configuration is to temporarily reraise exceptions from the signup method (so that you get the actual mailer exception string). In the rescue statement, put a single "raise" statement in. Once you've debugged any setting problems, remove that statement to get the proper flash error handling back.
84
+
85
+
86
+ === Create the DB schema
87
+
88
+ After you have done the modifications the the ApplicationController and its helper, you can import the user model into the database. An ActiveRecord schema.rb file is provided in login_engine/db/schema.rb. You should check that this file isn't going to interfere with anything in your application. You can set the table name used by adding
89
+
90
+ module LoginEngine
91
+
92
+ # ... other options...
93
+ config :user_table, "your_table_name"
94
+
95
+ end
96
+
97
+ To the LoginEngine configuration in <tt>environment.rb</tt>. Then run from the root of your project:
98
+
99
+ rake import_login_engine_schema
100
+
101
+ to import the schema into your database.
102
+
103
+
104
+ == Include stylesheets
105
+
106
+ If you want the default stylesheet, add the following line to your layout:
107
+
108
+ <%= engine_stylesheet 'login_engine' %>
109
+
110
+ ... somewhere in the <head> section of your HTML layout file.
111
+
112
+
113
+
114
+ = How to use the Login Engine
115
+
116
+ Now you can go around and happily add "before_filter :login_required" to the controllers which you would like to protect.
117
+
118
+ After integrating the login system with your rails application navigate to your new controller's signup method. There you can create a new account. After you are done you should have a look at your DB. Your freshly created user will be there but the password will be a sha1 hashed 40 digit mess. I find this should be the minimum of security which every page offering login & password should give its customers. Now you can move to one of those controllers which you protected with the before_filter :login_required snippet. You will automatically be re-directed to your freshly created login controller and you are asked for a password. After entering valid account data you will be taken back to the controller which you requested earlier. Simple huh?
119
+
120
+ === Protection using <tt>before_filter</tt>
121
+
122
+ Adding the line <tt>before_filter :login_required</tt> to your <tt>app/controllers/application.rb</tt> file will protect *all* of your applications methods, in every controller. If you only want to control access to specific controllers, remove this line from <tt>application.rb</tt> and add it to the controllers that you want to secure.
123
+
124
+ Within individual controllers you can restrict which methods the filter runs on in the usual way:
125
+
126
+ before_filter :login_required, :only => [:myaccount, :changepassword]
127
+ before_filter :login_required, :except => [:index]
128
+
129
+ === Protection using <tt>protect?()</tt>
130
+
131
+ Alternatively, you can leave the <tt>before_filter</tt> in the global <tt>application.rb</tt> file, and control which actions are restricted in individual controllers by defining a <tt>protect?()</tt> method in that controller.
132
+
133
+ For instance, in the <tt>UserController</tt> we want to allow everyone access to the 'login', 'signup' and 'forgot_password' methods (otherwise noone would be able to access our site!). So a <tt>protect?()</tt> method is defined in <tt>user_controller.rb</tt> as follows:
134
+
135
+ def protect?(action)
136
+ if ['login', 'signup', 'forgot_password'].include?(action)
137
+ return false
138
+ else
139
+ return true
140
+ end
141
+ end
142
+
143
+ Of course, you can override this Engine behaviour in your application - see below.
144
+
145
+ == Configuration
146
+
147
+ TODO: document the configuration options in the LoginEngine
148
+
149
+ == Overriding controllers and views
150
+
151
+ The standard home page is almost certainly not what you want to present to your users. Because this login system is a Rails Engine, overriding the default behaviour couldn't be simpler. To change the RHTML template shown for the <tt>home</tt> action, simple create a new file in <tt>RAILS_ROOT/app/views/user/home.rhtml</tt> (you'll probably need to create the directory <tt>user</tt> at the same time). This new view file will be used instead of the one provided in the Login Engine. Easy!
152
+
153
+
154
+ == Tips & Tricks
155
+
156
+ How do I...
157
+
158
+ ... access the user who is currently logged in
159
+
160
+ A: You can get the user object from the session using session[:user]
161
+ Example:
162
+ Welcome <%= session[:user].name %>
163
+
164
+ You can also use the 'current_user' method provided by UserHelper:
165
+ Example:
166
+ Welcome <%= current_user.name %>
167
+
168
+
169
+ ... restrict access to only a few methods?
170
+
171
+ A: Use before_filters build in scoping.
172
+ Example:
173
+ before_filter :login_required, :only => [:myaccount, :changepassword]
174
+ before_filter :login_required, :except => [:index]
175
+
176
+ ... check if a user is logged-in in my views?
177
+
178
+ A: session[:user] will tell you. Here is an example helper which you can use to make this more pretty:
179
+ Example:
180
+ def user?
181
+ !session[:user].nil?
182
+ end
183
+
184
+ ... return a user to the page they came from before logging in?
185
+
186
+ A: The user will be send back to the last url which called the method "store_location"
187
+ Example:
188
+ User was at /articles/show/1, wants to log in.
189
+ in articles_controller.rb, add store_location to the show function and
190
+ send the user to the login form.
191
+ After he logs in he will be send back to /articles/show/1
192
+
193
+ You can find more help at http://wiki.rubyonrails.com/rails/show/SaltedLoginGenerator
194
+
195
+ == Troubleshooting
196
+
197
+ One of the more common problems people have seen is that after verifying an account by following the emailed URL, they are unable to login via the normal login method since the verified field is not properly set in the user model's row in the DB.
198
+
199
+ The most common cause of this problem is that the DB and session get out of sync. In particular, it always happens for me after recreating the DB if I have run the server previously. To fix the problem, remove the /tmp/ruby* session files (from wherever they are for your installation) while the server is stopped, and then restart. This usually is the cause of the problem.
200
+
201
+
202
+
203
+ = Database Schema Details
204
+
205
+ You need a database table corresponding to the User model. This is provided as a Rails Schema file, but the schema is presented below for information. Note the table type for MySQL. Whatever DB you use, it must support transactions. If it does not, the functional tests will not work properly, nor will the application in the face of failures during certain DB creates and updates.
206
+
207
+ mysql syntax:
208
+ CREATE TABLE users (
209
+ id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
210
+ login VARCHAR(80) NOT NULL,
211
+ salted_password VARCHAR(40) NOT NULL,
212
+ email VARCHAR(60) NOT NULL,
213
+ firstname VARCHAR(40),
214
+ lastname VARCHAR(40),
215
+ salt CHAR(40) NOT NULL,
216
+ verified INT default 0,
217
+ role VARCHAR(40) default NULL,
218
+ security_token CHAR(40) default NULL,
219
+ token_expiry DATETIME default NULL,
220
+ deleted INT default 0,
221
+ delete_after DATETIME default NULL
222
+ ) TYPE=InnoDB DEFAULT CHARSET=utf8;
223
+
224
+ postgres:
225
+ CREATE TABLE "users" (
226
+ id SERIAL PRIMARY KEY
227
+ login VARCHAR(80) NOT NULL,
228
+ salted_password VARCHAR(40) NOT NULL,
229
+ email VARCHAR(60) NOT NULL,
230
+ firstname VARCHAR(40),
231
+ lastname VARCHAR(40),
232
+ salt CHAR(40) NOT NULL,
233
+ verified INT default 0,
234
+ role VARCHAR(40) default NULL,
235
+ security_token CHAR(40) default NULL,
236
+ token_expiry TIMESTAMP default NULL,
237
+ deleted INT default 0,
238
+ delete_after TIMESTAMP default NULL
239
+ ) WITH OIDS;
240
+
241
+ sqlite:
242
+ CREATE TABLE 'users' (
243
+ id INTEGER PRIMARY KEY,
244
+ login VARCHAR(80) NOT NULL,
245
+ salted_password VARCHAR(40) NOT NULL,
246
+ email VARCHAR(60) NOT NULL,
247
+ firstname VARCHAR(40),
248
+ lastname VARCHAR(40),
249
+ salt CHAR(40) NOT NULL,
250
+ verified INT default 0,
251
+ role VARCHAR(40) default NULL,
252
+ security_token CHAR(40) default NULL,
253
+ token_expiry DATETIME default NULL,
254
+ deleted INT default 0,
255
+ delete_after DATETIME default NULL
256
+ );
257
+
258
+ Of course your user model can have any amount of extra fields. This is just a starting point.