Class: AuthenticatedGenerator

Inherits:
Rails::Generator::NamedBase
  • Object
show all
Defined in:
lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime_args, runtime_options = {}) ⇒ AuthenticatedGenerator

Returns a new instance of AuthenticatedGenerator.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 36

def initialize(runtime_args, runtime_options = {})
  super

  @rspec = has_rspec?

  @controller_name = (args.shift || 'sessions').pluralize
  @model_controller_name = @name.pluralize

  # sessions controller
  base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
  @controller_class_name_without_nesting, @controller_file_name, @controller_plural_name = inflect_names(base_name)
  @controller_singular_name = @controller_file_name.singularize
  if @controller_class_nesting.empty?
    @controller_class_name = @controller_class_name_without_nesting
  else
    @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
  end
  @controller_routing_name  = @controller_singular_name
  @controller_routing_path  = @controller_file_path.singularize
  @controller_controller_name = @controller_plural_name

  # model controller
  base_name, @model_controller_class_path, @model_controller_file_path, @model_controller_class_nesting, @model_controller_class_nesting_depth = extract_modules(@model_controller_name)
  @model_controller_class_name_without_nesting, @model_controller_singular_name, @model_controller_plural_name = inflect_names(base_name)

  if @model_controller_class_nesting.empty?
    @model_controller_class_name = @model_controller_class_name_without_nesting
  else
    @model_controller_class_name = "#{@model_controller_class_nesting}::#{@model_controller_class_name_without_nesting}"
  end
  @model_controller_routing_name    = @table_name
  @model_controller_routing_path    = @model_controller_file_path
  @model_controller_controller_name = @model_controller_plural_name

  load_or_initialize_site_keys()

  if options[:dump_generator_attribute_names]
    dump_generator_attribute_names
  end
end

Instance Attribute Details

#controller_class_nameObject (readonly)

Returns the value of attribute controller_class_name.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_class_name
  @controller_class_name
end

#controller_class_nestingObject (readonly)

Returns the value of attribute controller_class_nesting.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_class_nesting
  @controller_class_nesting
end

#controller_class_nesting_depthObject (readonly)

Returns the value of attribute controller_class_nesting_depth.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_class_nesting_depth
  @controller_class_nesting_depth
end

#controller_class_pathObject (readonly)

Returns the value of attribute controller_class_path.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_class_path
  @controller_class_path
end

#controller_controller_nameObject (readonly)

Returns the value of attribute controller_controller_name.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_controller_name
  @controller_controller_name
end

#controller_file_nameObject (readonly)

Returns the value of attribute controller_file_name.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_file_name
  @controller_file_name
end

#controller_file_pathObject (readonly)

Returns the value of attribute controller_file_path.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_file_path
  @controller_file_path
end

#controller_nameObject (readonly)

Returns the value of attribute controller_name.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_name
  @controller_name
end

#controller_plural_nameObject (readonly) Also known as: controller_table_name

Returns the value of attribute controller_plural_name.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_plural_name
  @controller_plural_name
end

#controller_routing_nameObject (readonly)

Returns the value of attribute controller_routing_name.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_routing_name
  @controller_routing_name
end

#controller_routing_pathObject (readonly)

Returns the value of attribute controller_routing_path.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_routing_path
  @controller_routing_path
end

#controller_singular_nameObject (readonly)

Returns the value of attribute controller_singular_name.



9
10
11
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 9

def controller_singular_name
  @controller_singular_name
end

#model_controller_class_nameObject (readonly)

Returns the value of attribute model_controller_class_name.



22
23
24
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 22

def model_controller_class_name
  @model_controller_class_name
end

#model_controller_class_nestingObject (readonly)

Returns the value of attribute model_controller_class_nesting.



22
23
24
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 22

def model_controller_class_nesting
  @model_controller_class_nesting
end

#model_controller_class_nesting_depthObject (readonly)

Returns the value of attribute model_controller_class_nesting_depth.



22
23
24
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 22

def model_controller_class_nesting_depth
  @model_controller_class_nesting_depth
end

#model_controller_class_pathObject (readonly)

Returns the value of attribute model_controller_class_path.



22
23
24
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 22

def model_controller_class_path
  @model_controller_class_path
end

#model_controller_controller_nameObject (readonly)

Returns the value of attribute model_controller_controller_name.



22
23
24
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 22

def model_controller_controller_name
  @model_controller_controller_name
end

#model_controller_file_pathObject (readonly)

Returns the value of attribute model_controller_file_path.



22
23
24
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 22

def model_controller_file_path
  @model_controller_file_path
end

#model_controller_nameObject (readonly)

Returns the value of attribute model_controller_name.



22
23
24
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 22

def model_controller_name
  @model_controller_name
end

#model_controller_plural_nameObject (readonly) Also known as: model_controller_table_name

Returns the value of attribute model_controller_plural_name.



22
23
24
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 22

def model_controller_plural_name
  @model_controller_plural_name
end

#model_controller_routing_nameObject (readonly)

Returns the value of attribute model_controller_routing_name.



22
23
24
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 22

def model_controller_routing_name
  @model_controller_routing_name
end

#model_controller_routing_pathObject (readonly)

Returns the value of attribute model_controller_routing_path.



22
23
24
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 22

def model_controller_routing_path
  @model_controller_routing_path
end

#model_controller_singular_nameObject (readonly) Also known as: model_controller_file_name

Returns the value of attribute model_controller_singular_name.



22
23
24
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 22

def model_controller_singular_name
  @model_controller_singular_name
end

Instance Method Details

#has_rspec?Boolean

Returns:

  • (Boolean)


329
330
331
332
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 329

def has_rspec?
  spec_dir = File.join(RAILS_ROOT, 'spec')
  options[:rspec] ||= (File.exist?(spec_dir) && File.directory?(spec_dir)) unless (options[:rspec] == false)
end

#load_or_initialize_site_keysObject

Try to be idempotent: pull in the existing site key if any, seed it with reasonable defaults otherwise



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 356

def load_or_initialize_site_keys
  case
  when defined? REST_AUTH_SITE_KEY
    if (options[:old_passwords]) && ((! REST_AUTH_SITE_KEY.blank?) || (REST_AUTH_DIGEST_STRETCHES != 1))
      raise "You have a site key, but --old-passwords will overwrite it.  If this is really what you want, move the file #{site_keys_file} and re-run."
    end
    $rest_auth_site_key_from_generator         = REST_AUTH_SITE_KEY
    $rest_auth_digest_stretches_from_generator = REST_AUTH_DIGEST_STRETCHES
  when options[:old_passwords]
    $rest_auth_site_key_from_generator         = nil
    $rest_auth_digest_stretches_from_generator = 1
    $rest_auth_keys_are_new                    = true
  else
    $rest_auth_site_key_from_generator         = make_token
    $rest_auth_digest_stretches_from_generator = 10
    $rest_auth_keys_are_new                    = true
  end
end

#make_tokenObject



340
341
342
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 340

def make_token
  secure_digest(Time.now, (1..10).map{ rand.to_s })
end

#manifestObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 77

def manifest
  recorded_session = record do |m|
    # Check for class naming collisions.
    m.class_collisions controller_class_path,       "#{controller_class_name}Controller", # Sessions Controller
                                                    "#{controller_class_name}Helper"
    m.class_collisions model_controller_class_path, "#{model_controller_class_name}Controller", # Model Controller
                                                    "#{model_controller_class_name}Helper"
    m.class_collisions class_path,                  "#{class_name}", "#{class_name}Mailer", "#{class_name}MailerTest", "#{class_name}Observer"
    m.class_collisions [], 'AuthenticatedSystem', 'AuthenticatedTestHelper'

    # Controller, helper, views, and test directories.
    m.directory File.join('app/models', class_path)
    m.directory File.join('app/controllers', controller_class_path)
    m.directory File.join('app/controllers', model_controller_class_path)
    m.directory File.join('app/helpers', controller_class_path)
    m.directory File.join('app/views', controller_class_path, controller_file_name)
    m.directory File.join('app/views', class_path, "#{file_name}_mailer") if options[:include_activation]

    m.directory File.join('app/controllers', model_controller_class_path)
    m.directory File.join('app/helpers', model_controller_class_path)
    m.directory File.join('app/views', model_controller_class_path, model_controller_file_name)
    m.directory File.join('config/initializers')

    if @rspec
      m.directory File.join('spec/controllers', controller_class_path)
      m.directory File.join('spec/controllers', model_controller_class_path)
      m.directory File.join('spec/models', class_path)
      m.directory File.join('spec/helpers', model_controller_class_path)
      m.directory File.join('spec/fixtures', class_path)
      m.directory 'features'
      m.directory File.join('features', 'step_definitions')
    else
      m.directory File.join('test/functional', controller_class_path)
      m.directory File.join('test/functional', model_controller_class_path)
      m.directory File.join('test/unit', class_path)
      m.directory File.join('test/fixtures', class_path)
    end

    m.template 'model.rb',
                File.join('app/models',
                          class_path,
                          "#{file_name}.rb")

    if options[:include_activation]
      %w( mailer observer ).each do |model_type|
        m.template "#{model_type}.rb", File.join('app/models',
                                             class_path,
                                             "#{file_name}_#{model_type}.rb")
      end
    end

    m.template 'controller.rb',
                File.join('app/controllers',
                          controller_class_path,
                          "#{controller_file_name}_controller.rb")

    m.template 'model_controller.rb',
                File.join('app/controllers',
                          model_controller_class_path,
                          "#{model_controller_file_name}_controller.rb")

    m.template 'authenticated_system.rb',
                File.join('lib', 'authenticated_system.rb')

    m.template 'authenticated_test_helper.rb',
                File.join('lib', 'authenticated_test_helper.rb')

    m.template 'site_keys.rb', site_keys_file

    if @rspec
      # RSpec Specs
      m.template  'spec/controllers/users_controller_spec.rb',
                  File.join('spec/controllers',
                            model_controller_class_path,
                            "#{model_controller_file_name}_controller_spec.rb")
      m.template  'spec/controllers/sessions_controller_spec.rb',
                  File.join('spec/controllers',
                            controller_class_path,
                            "#{controller_file_name}_controller_spec.rb")
      m.template  'spec/controllers/access_control_spec.rb',
                  File.join('spec/controllers',
                            controller_class_path,
                            "access_control_spec.rb")
      m.template  'spec/controllers/authenticated_system_spec.rb',
                  File.join('spec/controllers',
                            controller_class_path,
                            "authenticated_system_spec.rb")
      m.template  'spec/helpers/users_helper_spec.rb',
                  File.join('spec/helpers',
                            model_controller_class_path,
                            "#{table_name}_helper_spec.rb")
      m.template  'spec/models/user_spec.rb',
                  File.join('spec/models',
                            class_path,
                            "#{file_name}_spec.rb")
      m.template 'spec/fixtures/users.yml',
                  File.join('spec/fixtures',
                             class_path,
                            "#{table_name}.yml")

      # Cucumber features
      m.template  'features/step_definitions/ra_navigation_steps.rb',
       File.join('features/step_definitions/ra_navigation_steps.rb')
      m.template  'features/step_definitions/ra_response_steps.rb',
       File.join('features/step_definitions/ra_response_steps.rb')
      m.template  'features/step_definitions/ra_resource_steps.rb',
       File.join('features/step_definitions/ra_resource_steps.rb')
      m.template  'features/step_definitions/user_steps.rb',
       File.join('features/step_definitions/', "#{file_name}_steps.rb")
      m.template  'features/accounts.feature',
       File.join('features', 'accounts.feature')
      m.template  'features/sessions.feature',
       File.join('features', 'sessions.feature')
      m.template  'features/step_definitions/rest_auth_features_helper.rb',
       File.join('features', 'step_definitions', 'rest_auth_features_helper.rb')
      m.template  'features/step_definitions/ra_env.rb',
       File.join('features', 'step_definitions', 'ra_env.rb')

    else
      m.template 'test/functional_test.rb',
                  File.join('test/functional',
                            controller_class_path,
                            "#{controller_file_name}_controller_test.rb")
      m.template 'test/model_functional_test.rb',
                  File.join('test/functional',
                            model_controller_class_path,
                            "#{model_controller_file_name}_controller_test.rb")
      m.template 'test/unit_test.rb',
                  File.join('test/unit',
                            class_path,
                            "#{file_name}_test.rb")
      if options[:include_activation]
        m.template 'test/mailer_test.rb', File.join('test/unit', class_path, "#{file_name}_mailer_test.rb")
      end
      m.template 'spec/fixtures/users.yml',
                  File.join('test/fixtures',
                            class_path,
                            "#{table_name}.yml")
    end

    m.template 'helper.rb',
                File.join('app/helpers',
                          controller_class_path,
                          "#{controller_file_name}_helper.rb")

    m.template 'model_helper.rb',
                File.join('app/helpers',
                          model_controller_class_path,
                          "#{model_controller_file_name}_helper.rb")


    # Controller templates
    m.template 'login.html.erb',  File.join('app/views', controller_class_path, controller_file_name, "new.html.erb")
    m.template 'signup.html.erb', File.join('app/views', model_controller_class_path, model_controller_file_name, "new.html.erb")
    m.template '_model_partial.html.erb', File.join('app/views', model_controller_class_path, model_controller_file_name, "_#{file_name}_bar.html.erb")

    if options[:include_activation]
      # Mailer templates
      %w( activation signup_notification ).each do |action|
        m.template "#{action}.erb",
                   File.join('app/views', "#{file_name}_mailer", "#{action}.erb")
      end
    end

    unless options[:skip_migration]
      m.migration_template 'migration.rb', 'db/migrate', :assigns => {
        :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
      }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
    end
    unless options[:skip_routes]
      # Note that this fails for nested classes -- you're on your own with setting up the routes.
      m.route_resource  controller_singular_name
      m.route_resources model_controller_plural_name
      m.route_name('signup',   '/signup',   {:controller => model_controller_plural_name, :action => 'new'})
      m.route_name('register', '/register', {:controller => model_controller_plural_name, :action => 'create'})
      m.route_name('login',    '/login',    {:controller => controller_controller_name, :action => 'new'})
      m.route_name('logout',   '/logout',   {:controller => controller_controller_name, :action => 'destroy'})
    end
  end

  #
  # Post-install notes
  #
  action = File.basename($0) # grok the action from './script/generate' or whatever
  case action
  when "generate"
    puts "Ready to generate."
    puts ("-" * 70)
    puts "Once finished, don't forget to:"
    puts
    if options[:include_activation]
      puts "- Add an observer to config/environment.rb"
      puts "    config.active_record.observers = :#{file_name}_observer"
    end
    if options[:aasm]
      puts "- Install the acts_as_state_machine gem:"
      puts "    sudo gem sources -a http://gems.github.com (If you haven't already)"
      puts "    sudo gem install rubyist-aasm"
    elsif options[:stateful]
      puts "- Install the acts_as_state_machine plugin:"
      puts "    svn export http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk vendor/plugins/acts_as_state_machine"
    end
    puts "- Add routes to these resources. In config/routes.rb, insert routes like:"
    puts %(    map.signup '/signup', :controller => '#{model_controller_file_name}', :action => 'new')
    puts %(    map.login  '/login',  :controller => '#{controller_file_name}', :action => 'new')
    puts %(    map.logout '/logout', :controller => '#{controller_file_name}', :action => 'destroy')
    if options[:include_activation]
      puts %(    map.activate '/activate/:activation_code', :controller => '#{model_controller_file_name}', :action => 'activate', :activation_code => nil)
    end
    if options[:stateful]
      puts  "  and modify the map.resources :#{model_controller_file_name} line to include these actions:"
      puts  "    map.resources :#{model_controller_file_name}, :member => { :suspend => :put, :unsuspend => :put, :purge => :delete }"
    end
    puts
    puts ("-" * 70)
    puts
    if $rest_auth_site_key_from_generator.blank?
      puts "You've set a nil site key. This preserves existing users' passwords,"
      puts "but allows dictionary attacks in the unlikely event your database is"
      puts "compromised and your site code is not.  See the README for more."
    elsif $rest_auth_keys_are_new
      puts "We've create a new site key in #{site_keys_file}.  If you have existing"
      puts "user accounts their passwords will no longer work (see README). As always,"
      puts "keep this file safe but don't post it in public."
    else
      puts "We've reused the existing site key in #{site_keys_file}.  As always,"
      puts "keep this file safe but don't post it in public."
    end
    puts
    puts ("-" * 70)
  when "destroy"
    puts
    puts ("-" * 70)
    puts
    puts "Thanks for using restful_authentication"
    puts
    puts "Don't forget to comment out the observer line in environment.rb"
    puts "  (This was optional so it may not even be there)"
    puts "  # config.active_record.observers = :#{file_name}_observer"
    puts
    puts ("-" * 70)
    puts
  else
    puts "Didn't understand the action '#{action}' -- you might have missed the 'after running me' instructions."
  end

  #
  # Do the thing
  #
  recorded_session
end

#password_digest(password, salt) ⇒ Object



343
344
345
346
347
348
349
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 343

def password_digest(password, salt)
  digest = $rest_auth_site_key_from_generator
  $rest_auth_digest_stretches_from_generator.times do
    digest = secure_digest(digest, salt, password, $rest_auth_site_key_from_generator)
  end
  digest
end

#secure_digest(*args) ⇒ Object

!! These must match the corresponding routines in by_password.rb !!



337
338
339
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 337

def secure_digest(*args)
  Digest::SHA1.hexdigest(args.flatten.join('--'))
end

#site_keys_fileObject



374
375
376
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb', line 374

def site_keys_file
  File.join("config", "initializers", "site_keys.rb")
end