Skip to content

Commit

Permalink
Update application.rb for newer rails and implement openid login
Browse files Browse the repository at this point in the history
  • Loading branch information
courtenay committed Aug 4, 2009
1 parent 178c6f3 commit 226d57c
Show file tree
Hide file tree
Showing 43 changed files with 1,126 additions and 24 deletions.
File renamed without changes.
20 changes: 20 additions & 0 deletions app/controllers/monitorships_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,20 @@
class MonitorshipsController < ApplicationController
before_filter :login_required

def create
@monitorship = Monitorship.find_or_initialize_by_user_id_and_topic_id(current_user.id, params[:topic_id])
@monitorship.update_attribute :active, true
respond_to do |format|
format.html { redirect_to topic_path(params[:forum_id], params[:topic_id]) }
format.js
end
end

def destroy
Monitorship.update_all ['active = ?', false], ['user_id = ? and topic_id = ?', current_user.id, params[:topic_id]]
respond_to do |format|
format.html { redirect_to topic_path(params[:forum_id], params[:topic_id]) }
format.js
end
end
end
66 changes: 65 additions & 1 deletion app/controllers/sessions_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,8 +18,24 @@ def create
redirect_back_or_default('/') redirect_back_or_default('/')
flash[:notice] = "Logged in successfully" flash[:notice] = "Logged in successfully"
else else
if using_open_id?
cookies[:use_open_id] = {:value => '1', :expires => 1.year.from_now.utc}
open_id_authentication(params[:openid_url])
else
cookies[:use_open_id] = {:value => '0', :expires => 1.year.ago.utc}
password_authentication params[:login], params[:password]
end
end
end

def create
if using_open_id?
cookies[:use_open_id] = {:value => '1', :expires => 1.year.from_now.utc}
open_id_authentication(params[:openid_url])
else
cookies[:use_open_id] = {:value => '0', :expires => 1.year.ago.utc}
password_authentication params[:login], params[:password]
flash[:error] = "Invalid login" flash[:error] = "Invalid login"
render :action => 'new'
end end
end end


Expand All @@ -30,4 +46,52 @@ def destroy
flash[:notice] = "You have been logged out." flash[:notice] = "You have been logged out."
redirect_back_or_default('/') redirect_back_or_default('/')
end end

protected

def password_authentication(name, password)
if @current_user = current_site.users.authenticate(params[:name], params[:password])
successful_login
else
failed_login "Sorry, that username/password doesn't work"
end
end

def open_id_authentication(openid_url)
authenticate_with_open_id(openid_url, :required => [:nickname, :email]) do |result, openid_url, registration|
if result.successful?
@user = User.find_or_initialize_by_openid_url(openid_url)
@current_user = @user
if @current_user
if @user.new_record?
@user.login = openid_url
@user.email = registration['email']
@user.password = 123456
@user.site = Site.find(:first)
@user.display_name = registration['nickname']
@user.save(false)
end
successful_login
else
failed_login "Sorry, no user by the identity URL {openid_url} exists"[:openid_no_user_message, openid_url.inspect]
end
else
failed_login result.message
end
end
end


private
def successful_login
flash[:notice] = 'You are now logged in! Welcome.'
session[:user_id] = @current_user.id
redirect_back_or_default('/')
end

def failed_login(message)
flash[:error] = message
redirect_to(new_session_url)
end

end end
6 changes: 4 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ def make_admin
protected protected
def find_user def find_user
@user = if admin? @user = if admin?
current_site.all_users.find_by_permalink(params[:id]) current_site.all_users.find params[:id]
elsif params[:id] == current_user.id?
current_user
else else
current_site.users.find_by_permalink(params[:id]) current_site.users.find params[:id]
end or raise ActiveRecord::RecordNotFound end or raise ActiveRecord::RecordNotFound
end end


Expand Down
9 changes: 9 additions & 0 deletions app/models/monitorships_sweeper.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
class MonitorshipsSweeper < ActionController::Caching::Sweeper
observe Monitorship

def after_save(monitorship)
FileUtils.rm_rf File.join(RAILS_ROOT, 'public', 'users', monitorship.user_id.to_s)
end

alias_method :after_destroy, :after_save
end
12 changes: 12 additions & 0 deletions app/models/posts_sweeper.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,12 @@
class PostsSweeper < ActionController::Caching::Sweeper
observe Post

def after_save(post)
FileUtils.rm_rf File.join(RAILS_ROOT, 'public', 'forums', post.forum_id.to_s, 'posts.rss')
FileUtils.rm_rf File.join(RAILS_ROOT, 'public', 'forums', post.forum_id.to_s, 'topics', "#{post.topic_id}.rss")
FileUtils.rm_rf File.join(RAILS_ROOT, 'public', 'users')
FileUtils.rm_rf File.join(RAILS_ROOT, 'public', 'posts.rss')
end

alias_method :after_destroy, :after_save
end
17 changes: 16 additions & 1 deletion app/models/user.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ def seen!
end end


def to_param def to_param
permalink id.to_s # permalink || login
end end

def openid_url=(value)
write_attribute :openid_url, value.blank? ? nil : OpenIdAuthentication.normalize_identifier(value)
end

def using_openid
self.openid_url.blank? ? false : true
end

def to_xml(options = {})
options[:except] ||= []
options[:except] << :email << :login_key << :login_key_expires_at << :password_hash << :openid_url << :activated << :admin
super
end

end end
4 changes: 2 additions & 2 deletions app/models/user/states.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def do_activation
self.deleted_at = nil self.deleted_at = nil
self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ) self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )


UserMailer.deliver_signup_notification(self) UserMailer.deliver_signup_notification(self) unless using_openid
end end


protected protected
Expand All @@ -52,7 +52,7 @@ def do_activate
self.deleted_at = nil self.deleted_at = nil
self.activation_code = "" self.activation_code = ""


UserMailer.deliver_activation(self) UserMailer.deliver_activation(self) unless using_openid
end end


def remove_moderatorships def remove_moderatorships
Expand Down
9 changes: 8 additions & 1 deletion app/models/user/validation.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class User
before_save :downcase_email_and_login before_save :downcase_email_and_login
before_save :encrypt_password before_save :encrypt_password
before_create :set_first_user_as_admin before_create :set_first_user_as_admin
# validates_email_format_of :email, :message=>"is invalid"
validates_uniqueness_of :openid_url, :case_sensitive => false, :allow_nil => true


# prevents a user from submitting a crafted form that bypasses activation # prevents a user from submitting a crafted form that bypasses activation
# anything else you want your user to change should be added here. # anything else you want your user to change should be added here.
Expand Down Expand Up @@ -42,7 +44,12 @@ def encrypt_password
self.crypted_password = encrypt(password) self.crypted_password = encrypt(password)
end end


def password_required? def using_openid
self.openid_url.blank? ? false : true
end

def password_required?
return false if using_openid
crypted_password.blank? || !password.blank? crypted_password.blank? || !password.blank?
end end


Expand Down
2 changes: 1 addition & 1 deletion app/views/forums/index.html.erb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<th class="la" width="30%" colspan="1"><%= 'Last Post'[:last_post] %></th> <th class="la" width="30%" colspan="1"><%= 'Last Post'[:last_post] %></th>
</tr> </tr>
<% for forum in @forums do %> <% for forum in @forums do %>
<tr> <tr class="forum" id="forum_<%= forum.id %>_row">
<td class="vat c1"> <td class="vat c1">


<% if recent_forum_activity(forum) %> <% if recent_forum_activity(forum) %>
Expand Down
5 changes: 2 additions & 3 deletions app/views/layouts/_head.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -1,11 +1,10 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><%= "#{h @page_title} - " if @page_title %> <%=h @current_site && current_site.name %></title> <title><%= "#{h @page_title} - " if @page_title %> <%=h @current_site && current_site.name %></title>
<%= stylesheet_link_tag 'display' %> <%= stylesheet_link_tag 'display' %>
<%= javascript_include_tag "prototype", "effects", "lowpro", "time", "application", :cache => 'beast' %> <%= javascript_include_tag "prototype", "effects", "lowpro", "time", "application", :cache => "beast" %>
<% unless @feed_icons.blank? -%> <% unless @feed_icons.blank? -%>
<% @feed_icons.each do |feed| -%> <% @feed_icons.each do |feed| -%>
<%= auto_discovery_link_tag :atom, feed[:url], :title => "Subscribe to '#{feed[:title]}'" %> <%= auto_discovery_link_tag :atom, feed[:url], :title => "Subscribe to '#{feed[:title]}'" %>
Expand All @@ -27,7 +26,7 @@
<li><%= link_to_function 'Search'[:search_title], "#", :href => root_path, :id => 'search-link' %></li> <li><%= link_to_function 'Search'[:search_title], "#", :href => root_path, :id => 'search-link' %></li>


<% if @current_site and logged_in? -%> <% if @current_site and logged_in? -%>
<li class="login"><%= link_to current_user.login, user_path(current_user) %></li> <li class="login"><%= link_to current_user.display_name, user_path(current_user) %></li>
<li class="logout"><%= link_to 'Settings'[:settings_title], settings_path %></li> <li class="logout"><%= link_to 'Settings'[:settings_title], settings_path %></li>
<li class="logout"><%= link_to 'Logout'[:logout_title], logout_path(:to => CGI.escape(request.request_uri)) %></li> <li class="logout"><%= link_to 'Logout'[:logout_title], logout_path(:to => CGI.escape(request.request_uri)) %></li>
<% else -%> <% else -%>
Expand Down
4 changes: 4 additions & 0 deletions app/views/monitorships/create.rjs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
#page["monitorship-icon-topics-#{params[:topic_id]}"].remove_class_name(:grey)
#page["monitorship-icon-topics-#{params[:topic_id]}"].add_class_name(:green)

page[:monitor_label].innerHTML = "Monitoring topic"[]
4 changes: 4 additions & 0 deletions app/views/monitorships/destroy.rjs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
#page["monitorship-icon-topics-#{params[:topic_id]}"].remove_class_name(:green)
#page["monitorship-icon-topics-#{params[:topic_id]}"].add_class_name(:darkgrey)

page[:monitor_label].innerHTML = "Monitor topic"[]
34 changes: 30 additions & 4 deletions app/views/sessions/new.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -1,16 +1,42 @@
<h1>Log In</h1> <h1>Log In</h1>


<% form_tag session_path do -%> <% form_tag(session_path) do -%>
<div id="openid_fields" style="display:none">
<p>
<label for="openid_url">Login with OpenID</label>
<br />
<%= text_field_tag 'openid_url', params[:openid_url], :class => "openid_url" %>
</p>
<p>or login with <a href="#" onclick="LoginForm.setToPassword()">username/password</a></p>
</div>

<div id="password_fields">
<p><%= label_tag 'login' %><br /> <p><%= label_tag 'login' %><br />
<%= text_field_tag 'login', @login %></p> <%= text_field_tag 'login', @login %></p>


<p><%= label_tag 'password' %><br/> <p><%= label_tag 'password' %><br/>
<%= password_field_tag 'password', nil %></p> <%= password_field_tag 'password', nil %></p>


<!-- Uncomment this if you want this functionality
<p><%= label_tag 'remember_me', 'Remember me' %> <p><%= label_tag 'remember_me', 'Remember me' %>
<%= check_box_tag 'remember_me', '1', @remember_me %></p> <%= check_box_tag 'remember_me', '1', @remember_me %></p>
-->


<p><%= submit_tag 'Log in' %></p> <p>or login with <a href="#" onclick="LoginForm.setToOpenID()">OpenID</a></p>
</div>

<p><%= submit_tag 'Log in' %> or <%= link_to_function('reset password'[], "$('reset-password').toggle();") %></p>

<% end -%> <% end -%>
<% form_tag users_path, :id => 'reset-password', :style => 'display:none' do -%>

<hr />
<h5><%= 'Reset Password'[] %></h5>

<p><%= 'Enter your email, and a brand new login key will be sent to you. Click the link in the email to log in, and then change your password.'[:email_directions] %></p>
<p><%= text_field_tag :email, "", :size => 30 %></p>
<p><%= submit_tag 'E-mail me the link'[:email_submit] %>
or <%= link_to_function('cancel'[], "$('reset-password').hide()") %></p>

<% end -%>
<%= javascript_tag "$('openid_input').focus();"%>
2 changes: 1 addition & 1 deletion app/views/topics/show.html.erb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


<% end # right content -%> <% end # right content -%>
<% if false#logged_in? %> <% if logged_in? %>
<% form_tag monitorship_path(@forum, @topic), :style => 'margin-top:0em; float:right;' do -%> <% form_tag monitorship_path(@forum, @topic), :style => 'margin-top:0em; float:right;' do -%>
<div> <div>
Expand Down
4 changes: 4 additions & 0 deletions app/views/users/new.rhtml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@
<p><label for="password_confirmation">Confirm Password</label><br/> <p><label for="password_confirmation">Confirm Password</label><br/>
<%= f.password_field :password_confirmation %></p> <%= f.password_field :password_confirmation %></p>


<p><label for="user_openid_url">OpenID URL</label><br />
<%= f.text_field :openid_url %>
</p>

<p><%= submit_tag 'Sign up' %></p> <p><%= submit_tag 'Sign up' %></p>
<% end -%> <% end -%>
4 changes: 3 additions & 1 deletion config/environment.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# ENV['RAILS_ENV'] ||= 'production' # ENV['RAILS_ENV'] ||= 'production'


# Specifies gem version of Rails to use when vendor/rails is not present # Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION


# Bootstrap the Rails environment, frameworks, and default configuration # Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot') require File.join(File.dirname(__FILE__), 'boot')
Expand All @@ -18,6 +18,8 @@


config.gem 'mislav-will_paginate', :lib => "will_paginate", config.gem 'mislav-will_paginate', :lib => "will_paginate",
:source => "http://gems.github.com" :source => "http://gems.github.com"

config.gem "bluecloth"


# Skip frameworks you're not going to use (only works if using vendor/rails). # Skip frameworks you're not going to use (only works if using vendor/rails).
# To use Rails without a database, you must remove the Active Record framework # To use Rails without a database, you must remove the Active Record framework
Expand Down
1 change: 1 addition & 0 deletions config/initializers/application.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
OpenIdAuthentication.store = :file
15 changes: 12 additions & 3 deletions config/routes.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,10 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.resource :session map.open_id_complete '/session',
:controller => "sessions", :action => "create",
:requirements => { :method => :get }


map.resources :sites, :moderatorships map.resources :sites, :moderatorships, :monitorship



map.resources :forums, :has_many => :posts do |forum| map.resources :forums, :has_many => :posts do |forum|
forum.resources :topics do |topic| forum.resources :topics do |topic|
Expand All @@ -11,8 +14,8 @@
forum.resources :posts forum.resources :posts
end end


map.user '/users/:id', :controller => "users", :action => "show"
map.resources :posts, :collection => {:search => :get} map.resources :posts, :collection => {:search => :get}

map.resources :users, :member => { :suspend => :put, map.resources :users, :member => { :suspend => :put,
:settings => :get, :settings => :get,
:make_admin => :put, :make_admin => :put,
Expand All @@ -26,5 +29,11 @@
map.logout '/logout', :controller => 'sessions', :action => 'destroy' map.logout '/logout', :controller => 'sessions', :action => 'destroy'
map.settings '/settings', :controller => 'users', :action => 'settings' map.settings '/settings', :controller => 'users', :action => 'settings'
map.resource :session map.resource :session

map.with_options :controller => 'posts', :action => 'monitored' do |map|
map.formatted_monitored_posts 'users/:user_id/monitored.:format'
map.monitored_posts 'users/:user_id/monitored'
end

map.root :controller => 'forums', :action => 'index' map.root :controller => 'forums', :action => 'index'
end end
28 changes: 28 additions & 0 deletions db/migrate/20090317123901_add_open_id_authentication_tables.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,28 @@
class AddOpenIdAuthenticationTables < ActiveRecord::Migration
def self.up
create_table "open_id_authentication_associations", :force => true do |t|
t.column "server_url", :binary
t.column "handle", :string
t.column "secret", :binary
t.column "issued", :integer
t.column "lifetime", :integer
t.column "assoc_type", :string
end

create_table "open_id_authentication_nonces", :force => true do |t|
t.column "nonce", :string
t.column "created", :integer
end

create_table "open_id_authentication_settings", :force => true do |t|
t.column "setting", :string
t.column "value", :binary
end
end

def self.down
drop_table "open_id_authentication_associations"
drop_table "open_id_authentication_nonces"
drop_table "open_id_authentication_settings"
end
end
Loading

0 comments on commit 226d57c

Please sign in to comment.