From c1f6690387fcdf6759998cf39ace5482470f4b65 Mon Sep 17 00:00:00 2001 From: Bryan W Clark Date: Thu, 14 Feb 2008 15:28:43 -0500 Subject: [PATCH] account creation on the fly! now when a new user emails the issue address their issue and account will be created on the fly. Future issues are somewhat obvious with this, we need to require account activation before issues can be public to prevent spam. in the process of creating accounts via email the activation code was also heavily touched and should work much better now. The previous issue that it wasn't sending activation mails was due to the nobody@ email wasn't set. --- app/controllers/application.rb | 6 ++++++ app/controllers/users_controller.rb | 8 +++++--- app/models/kwestie_mailer.rb | 10 +++++----- app/models/user.rb | 16 +++++++++++++++- app/models/user_mailer.rb | 12 ++++++++---- app/views/user_mailer/activation.rhtml | 6 ++++-- app/views/users/_show.rhtml | 6 +++--- config/routes.rb | 2 ++ public/stylesheets/default.css | 3 ++- 9 files changed, 50 insertions(+), 19 deletions(-) diff --git a/app/controllers/application.rb b/app/controllers/application.rb index f6fa19c..a765eff 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -22,4 +22,10 @@ class ApplicationController < ActionController::Base def set_current_user User.current_user = current_user end + + def redirect_back_or(path) + redirect_to :back + rescue ActionController::RedirectBackError + redirect_to path + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index b553a06..1c5aeb2 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -22,7 +22,7 @@ class UsersController < ApplicationController @user = User.new(params[:user]) @user.save! self.current_user = @user - redirect_back_or_default('/') + redirect_back_or('/') flash[:notice] = "Thanks for signing up!" rescue ActiveRecord::RecordInvalid render :action => 'new' @@ -32,9 +32,11 @@ class UsersController < ApplicationController self.current_user = params[:activation_code].blank? ? :false : User.find_by_activation_code(params[:activation_code]) if logged_in? && !current_user.activated? current_user.activate - flash[:notice] = "Signup complete!" + flash[:notice] = "Your account has been activated!! You should check out your page" + else + flash[:notice] = "I don't recognize that activation code, perhaps you should try again" end - redirect_back_or_default('/') + redirect_to index_url end end diff --git a/app/models/kwestie_mailer.rb b/app/models/kwestie_mailer.rb index 31c1d9c..9b050ee 100644 --- a/app/models/kwestie_mailer.rb +++ b/app/models/kwestie_mailer.rb @@ -51,10 +51,10 @@ class KwestieMailer < ActionMailer::Base private - #FIXME: require use of the UserMailer for creating a new user on the fly without a password + #FIXME: this creates a user on the fly but needs a little more love to be ok def find_user_from_email(email) - for mail in email.from - user = User.find_by_email(mail) + for mail in email.from_addrs + user = User.find_or_create_by_mail(mail) return user if ! user.nil? end return nil @@ -62,8 +62,8 @@ class KwestieMailer < ActionMailer::Base # FIXME: doesn't handle the CC address def issue_id_from_email(email) - for mail in email.to - id = mail.split('@')[0].split('+')[1] + for mail in email.to_addrs + id = mail.spec.split('@')[0].split('+')[1] return id if ! id.nil? end return nil diff --git a/app/models/user.rb b/app/models/user.rb index 730ab13..66749eb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,7 +17,8 @@ class User < ActiveRecord::Base validates_uniqueness_of :login, :email, :case_sensitive => false, :allow_nil => true before_save :encrypt_password - before_create :make_activation_code + before_create :make_activation_code + # prevents a user from submitting a crafted form that bypasses activation # anything else you want your user to change should be added here. attr_accessible :display_name, :login, :email, :password, :password_confirmation, :identity_url @@ -28,6 +29,17 @@ class User < ActiveRecord::Base has_many :recent_issues, :class_name => "Issue", :foreign_key => "created_by", :order => "created_at DESC", :limit => 5 has_many :recent_comments, :class_name => "Comment", :foreign_key => "created_by", :order => "created_at DESC", :group => "issue_id", :limit => 5 + def self.find_or_create_by_mail(mail) + user = find_by_email(mail.spec) + return user if ! user.nil? + + d_name = (mail.name.nil?)? mail.spec.split('@')[0] : mail.name + p_word = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )[0,7] + # a full email is probably a better chance for a unique login than cutting it up + create({ :email => mail.spec, :login => mail.spec, :display_name => d_name, + :password => p_word, :password_confirmation => p_word }) + end + # Activates the user in the database. def activate @activated = true @@ -110,4 +122,6 @@ class User < ActiveRecord::Base def not_openid? identity_url.blank? end + + end diff --git a/app/models/user_mailer.rb b/app/models/user_mailer.rb index b105a43..7eb955c 100644 --- a/app/models/user_mailer.rb +++ b/app/models/user_mailer.rb @@ -1,23 +1,27 @@ class UserMailer < ActionMailer::Base + + default_url_options[:host] = "0.0.0.0:3000" + def signup_notification(user) setup_email(user) @subject += 'Please activate your new account' - @body[:url] = "http://YOURSITE/activate/#{user.activation_code}" + #"http://#{default_url_options[:host]}/users/activate/#{user.activation_code}" + @body[:url] = url_for :controller => 'users', :action => 'activate', :activation_code => user.activation_code end def activation(user) setup_email(user) @subject += 'Your account has been activated!' - @body[:url] = "http://YOURSITE/" + @body[:url] = user_url(user) end protected def setup_email(user) @recipients = "#{user.email}" - @from = "ADMINEMAIL" - @subject = "[YOURSITE] " + @from = "nobody@localhost.localdomain" + @subject = "[KWESTIE] " @sent_on = Time.now @body[:user] = user end diff --git a/app/views/user_mailer/activation.rhtml b/app/views/user_mailer/activation.rhtml index 3e9c253..65ca2fa 100644 --- a/app/views/user_mailer/activation.rhtml +++ b/app/views/user_mailer/activation.rhtml @@ -1,3 +1,5 @@ -<%= @user.login %>, your account has been activated. You may now start adding your plugins: +<%= @user.display_name %>, your account has been activated! - <%= @url %> \ No newline at end of file +Here's your new home page: + + <%= @url %> diff --git a/app/views/users/_show.rhtml b/app/views/users/_show.rhtml index ce8493a..b5763ca 100644 --- a/app/views/users/_show.rhtml +++ b/app/views/users/_show.rhtml @@ -1,6 +1,6 @@
- -
<%= @user.display_name %>
- +
name: <%= @user.display_name %>
+ +
Member for <%= distance_of_time_in_words_to_now(@user.created_at) %>, since <%= @user.created_at.to_s %>
diff --git a/config/routes.rb b/config/routes.rb index dbdf1b4..ffd1864 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,6 +24,8 @@ ActionController::Routing::Routes.draw do |map| map.open_id_complete 'session', :controller => "sessions", :action => "create", :requirements => { :method => :get } map.resource :session + map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate' + map.signup '/signup', :controller => 'users', :action => 'new' map.login '/login', :controller => 'sessions', :action => 'new' map.logout '/logout', :controller => 'sessions', :action => 'destroy' diff --git a/public/stylesheets/default.css b/public/stylesheets/default.css index 0beda71..81479a2 100644 --- a/public/stylesheets/default.css +++ b/public/stylesheets/default.css @@ -18,6 +18,7 @@ span.created_on_full { color: #888; font-size: small; } /* ========= FLASH ========== */ div.flash { text-align: center; } span.flash { padding: 0.2em 2em; -moz-border-radius: 0.2em; font-size: 11pt; font-weight: bold; color: #000; } +span.flash a { color: #fff; } #notice { background-color: #084fa1;; color: #fff; } /* friendly, you've logged in! */ #warning { background-color: #cc0000; color: #fff; } /* error, your login failed! */ #message { background-color: #fce94f; } /* normal, updates were saved */ @@ -103,7 +104,7 @@ div.issue-comments { width: 30em; } div.issue div.entry-title { font-size: x-large; font-weight: bold; margin: 1em 0pt; } div.issue div.entry-title a { font-size: x-small; } div.issue div.issue-meta { font-size: small; } -div.issue div.entry-content { margin: 1em 0pt; } +div.issue div.entry-content { margin: 1em 0pt; white-space: pre; } div.issue .created_by { } div.issue .created_at { float: right; } div.issue .updated_at { float: right; } -- 2.11.4.GIT