Skip to content

Commit

Permalink
finish merging authenticated_mailer generator with authenticated gene…
Browse files Browse the repository at this point in the history
…rator [Rogelio Samour]

git-svn-id: http://svn.techno-weenie.net/projects/plugins/restful_authentication@2523 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Dec 5, 2006
1 parent cb26463 commit de2cb3d
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This is a basic restful authentication generator for rails, taken from acts as a

To use:

./script/generate authenticated user sessions
./script/generate authenticated user sessions --include-activation

The first parameter specifies the model that gets created in signup (typically a user or account model). A model with migration is created,
as well as a basic controller with the create method.
Expand Down
78 changes: 63 additions & 15 deletions generators/authenticated/authenticated_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def manifest
"#{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}"
m.class_collisions class_path, "#{class_name}", "#{class_name}Notifier", "#{class_name}NotifierTest", "#{class_name}Observer"
m.class_collisions [], 'AuthenticatedSystem', 'AuthenticatedTestHelper'

# Controller, helper, views, and test directories.
Expand All @@ -75,6 +75,14 @@ def manifest
class_path,
"#{file_name}.rb")

if options[:include_activation]
%w( notifier 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,
Expand Down Expand Up @@ -116,6 +124,10 @@ def manifest
class_path,
"#{file_name}_test.rb")

if options[:include_activation]
m.template 'notifier_test.rb', File.join('test/unit', class_path, "#{file_name}_notifier_test.rb")
end

m.template 'fixtures.yml',
File.join('test/fixtures',
"#{table_name}.yml")
Expand All @@ -124,27 +136,61 @@ def manifest
m.template 'login.rhtml', File.join('app/views', controller_class_path, controller_file_name, "new.rhtml")
m.template 'signup.rhtml', File.join('app/views', model_controller_class_path, model_controller_file_name, "new.rhtml")

if options[:include_activation]
# Mailer templates
%w( activation signup_notification ).each do |action|
m.template "#{action}.rhtml",
File.join('app/views', "#{file_name}_notifier", "#{action}.rhtml")
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
end

puts
puts ("-" * 70)
puts "Don't forget the restful routes in config.routes.rb"
puts
puts " map.resources :#{model_controller_file_name}, :#{controller_file_name}"
puts
puts "Try these for some familiar login URLs if you like:"
puts
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'"
puts
puts ("-" * 70)
puts
action = nil
action = $0.split("/")[1]
case action
when "generate"
puts
puts ("-" * 70)
puts "Don't forget to:"
puts
puts " - add restful routes in config.routes.rb"
puts " map.resources :#{model_controller_file_name}, :#{controller_file_name}"
puts " map.activate '/activate/:activation_code', :controller => '#{model_controller_file_name}', :action => 'activate'"
if options[:include_activation]
puts
puts " - add an observer to environment.rb"
puts " config.active_record.observers = :#{file_name}_observer"
end
puts
puts "Try these for some familiar login URLs if you like:"
puts
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'"
puts
puts ("-" * 70)
puts
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. It may not even be there)"
puts " # config.active_record.observers = :#{file_name}_observer"
puts
puts ("-" * 70)
puts
else
puts
end

recorded_session
end
Expand All @@ -160,5 +206,7 @@ def add_options!(opt)
opt.separator 'Options:'
opt.on("--skip-migration",
"Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
opt.on("--include-activation",
"Generate signup 'activation code' confirmation via email") { |v| options[:include_activation] = v }
end
end
3 changes: 3 additions & 0 deletions generators/authenticated/templates/activation.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%%= @<%= file_name %>.login %>, your account has been activated. You may now start adding your plugins:

<%%= @url %>
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ def authenticate
def check
@controller.assert_response 401
end
end
end
5 changes: 4 additions & 1 deletion generators/authenticated/templates/fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ quentin:
salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
created_at: <%%= 5.days.ago.to_s :db %>
<% if options[:include_activation] %> activation_code: 8f24789ae988411ccf33ab0c30fe9106fab32e9b <% end %>
<% if options[:include_activation] %> activated_at: <%%= 5.days.ago.to_s :db %> <% end %>
aaron:
id: 2
login: aaron
email: aaron@example.com
salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
created_at: <%%= 1.days.ago.to_s :db %>
created_at: <%%= 1.days.ago.to_s :db %>
<% if options[:include_activation] %> activation_code: 8f24789ae988411ccf33ab0c30fe9106fab32e9b <% end %>
2 changes: 1 addition & 1 deletion generators/authenticated/templates/functional_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_should_login_with_cookie
def test_should_fail_expired_cookie_login
<%= table_name %>(:quentin).remember_me
users(:quentin).update_attribute :remember_token_expires_at, 5.minutes.ago
<%= table_name %>(:quentin).update_attribute :remember_token_expires_at, 5.minutes.ago
@request.cookies["auth_token"] = cookie_for(:quentin)
get :new
assert !@controller.send(:logged_in?)
Expand Down
3 changes: 3 additions & 0 deletions generators/authenticated/templates/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ def self.up
t.column :updated_at, :datetime
t.column :remember_token, :string
t.column :remember_token_expires_at, :datetime
<% if options[:include_activation] %>
t.column :activation_code, :string, :limit => 40
t.column :activated_at, :datetime<% end %>
end
end
Expand Down
20 changes: 20 additions & 0 deletions generators/authenticated/templates/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,25 @@ class <%= class_name %> < ActiveRecord::Base
validates_length_of :email, :within => 3..100
validates_uniqueness_of :login, :email, :case_sensitive => false
before_save :encrypt_password
<% if options[:include_activation] %> before_create :make_activation_code <% end %>
<% if options[:include_activation] %>
# Activates the user in the database.
def activate
@activated = true
update_attributes(:activated_at => Time.now.utc, :activation_code => nil)
end

# Returns true if the user has just been activated.
def recently_activated?
@activated
end <% end %>
# Authenticates a user by their login name and unencrypted password. Returns the user or nil.
def self.authenticate(login, password)
<% if options[:include_activation] %>
u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL', login]
<% else %>
u = find_by_login(login) # need to get the salt
<% end %>
u && u.authenticated?(password) ? u : nil
end

Expand Down Expand Up @@ -69,4 +84,9 @@ def encrypt_password
def password_required?
crypted_password.blank? || !password.blank?
end

<% if options[:include_activation] %>
def make_activation_code
self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
end <% end %>
end
9 changes: 9 additions & 0 deletions generators/authenticated/templates/model_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ def create
rescue ActiveRecord::RecordInvalid
render :action => 'new'
end

def activate
self.current_<%= file_name %> = <%= class_name %>.find_by_activation_code(params[:activation])
if logged_in? && !current_<%= file_name %>.activated?
current_<%= file_name %>.activate
flash[:notice] = "Signup complete!"
end
redirect_back_or_default('/')
end
end
9 changes: 9 additions & 0 deletions generators/authenticated/templates/model_functional_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def test_should_require_email_on_signup
assert_response :success
end
end
<% if options[:include_activation] %>
def test_should_activate_user
assert_nil <%= class_name %>.authenticate('aaron', 'test')
#get :activate, :id => <%= table_name %>(:aaron).activation_code
# We cheat here since there isn't an activate action in the controller
# Please do add one in your own code :-) -rjs-
<%= table_name %>(:aaron).activate
assert_equal <%= table_name %>(:aaron), <%= class_name %>.authenticate('aaron', 'test')
end <% end %>
protected
def create_<%= file_name %>(options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def signup_notification(<%= file_name %>)
setup_email(<%= file_name %>)
@subject += 'Please activate your new account'
<% if options[:include_activation] %>
@body[:url] = "http://YOURSITE/account/activate/#{<%= file_name %>.activation_code}"
@body[:url] = "http://YOURSITE/activate/#{<%= file_name %>.activation_code}"
<% else %>
@body[:url] = "http://YOURSITE/login/" <% end %>
end
Expand All @@ -15,11 +15,11 @@ def activation(<%= file_name %>)
end
protected
def setup_email(<%= file_name %>)
@recipients = "#{<%= file_name %>.email}"
@from = "ADMINEMAIL"
@subject = "[YOURSITE] "
@sent_on = Time.now
@body[:<%= file_name %>] = <%= file_name %>
end
def setup_email(<%= file_name %>)
@recipients = "#{<%= file_name %>.email}"
@from = "ADMINEMAIL"
@subject = "[YOURSITE] "
@sent_on = Time.now
@body[:<%= file_name %>] = <%= file_name %>
end
end

0 comments on commit de2cb3d

Please sign in to comment.