Skip to content

Commit c0a22d5

Browse files
committed
second commit :)
1 parent 353297c commit c0a22d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+9057
-0
lines changed

Rakefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
4+
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
5+
6+
require 'rake'
7+
require 'rake/testtask'
8+
require 'rake/rdoctask'
9+
10+
require 'tasks/rails'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Filters added to this controller apply to all controllers in the application.
2+
# Likewise, all the methods added will be available for all controllers.
3+
4+
class ApplicationController < ActionController::Base
5+
helper :all # include all helpers, all the time
6+
protect_from_forgery # See ActionController::RequestForgeryProtection for details
7+
8+
layout 'general'
9+
10+
# Scrub sensitive parameters from your log
11+
# filter_parameter_logging :password
12+
end
13+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This controller handles the login/logout function of the site.
2+
class SessionsController < ApplicationController
3+
# Be sure to include AuthenticationSystem in Application Controller instead
4+
include AuthenticatedSystem
5+
6+
# render new.rhtml
7+
def new
8+
end
9+
10+
def create
11+
logout_keeping_session!
12+
user = User.authenticate(params[:login], params[:password])
13+
if user
14+
# Protects against session fixation attacks, causes request forgery
15+
# protection if user resubmits an earlier form using back
16+
# button. Uncomment if you understand the tradeoffs.
17+
# reset_session
18+
self.current_user = user
19+
new_cookie_flag = (params[:remember_me] == "1")
20+
handle_remember_cookie! new_cookie_flag
21+
redirect_back_or_default('/')
22+
flash[:notice] = "Logged in successfully"
23+
else
24+
note_failed_signin
25+
@login = params[:login]
26+
@remember_me = params[:remember_me]
27+
render :action => 'new'
28+
end
29+
end
30+
31+
def destroy
32+
logout_killing_session!
33+
flash[:notice] = "You have been logged out."
34+
redirect_back_or_default('/')
35+
end
36+
37+
protected
38+
# Track failed login attempts
39+
def note_failed_signin
40+
flash[:error] = "Couldn't log you in as '#{params[:login]}'"
41+
logger.warn "Failed login for '#{params[:login]}' from #{request.remote_ip} at #{Time.now.utc}"
42+
end
43+
end

app/controllers/users_controller.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class UsersController < ApplicationController
2+
# Be sure to include AuthenticationSystem in Application Controller instead
3+
include AuthenticatedSystem
4+
5+
6+
# render new.rhtml
7+
def new
8+
@user = User.new
9+
end
10+
11+
def create
12+
logout_keeping_session!
13+
@user = User.new(params[:user])
14+
success = @user && @user.save
15+
if success && @user.errors.empty?
16+
# Protects against session fixation attacks, causes request forgery
17+
# protection if visitor resubmits an earlier form using back
18+
# button. Uncomment if you understand the tradeoffs.
19+
# reset session
20+
self.current_user = @user # !! now logged in
21+
redirect_back_or_default('/')
22+
flash[:notice] = "Thanks for signing up! We're sending you an email with your activation code."
23+
else
24+
flash[:error] = "We couldn't set up that account, sorry. Please try again, or contact an admin (link is above)."
25+
render :action => 'new'
26+
end
27+
end
28+
end

app/controllers/welcome_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class WelcomeController < ApplicationController
2+
end

app/helpers/application_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Methods added to this helper will be available to all templates in the application.
2+
module ApplicationHelper
3+
end

app/helpers/sessions_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module SessionsHelper
2+
end

app/helpers/users_helper.rb

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
module UsersHelper
2+
3+
#
4+
# Use this to wrap view elements that the user can't access.
5+
# !! Note: this is an *interface*, not *security* feature !!
6+
# You need to do all access control at the controller level.
7+
#
8+
# Example:
9+
# <%= if_authorized?(:index, User) do link_to('List all users', users_path) end %> |
10+
# <%= if_authorized?(:edit, @user) do link_to('Edit this user', edit_user_path) end %> |
11+
# <%= if_authorized?(:destroy, @user) do link_to 'Destroy', @user, :confirm => 'Are you sure?', :method => :delete end %>
12+
#
13+
#
14+
def if_authorized?(action, resource, &block)
15+
if authorized?(action, resource)
16+
yield action, resource
17+
end
18+
end
19+
20+
#
21+
# Link to user's page ('users/1')
22+
#
23+
# By default, their login is used as link text and link title (tooltip)
24+
#
25+
# Takes options
26+
# * :content_text => 'Content text in place of user.login', escaped with
27+
# the standard h() function.
28+
# * :content_method => :user_instance_method_to_call_for_content_text
29+
# * :title_method => :user_instance_method_to_call_for_title_attribute
30+
# * as well as link_to()'s standard options
31+
#
32+
# Examples:
33+
# link_to_user @user
34+
# # => <a href="/users/3" title="barmy">barmy</a>
35+
#
36+
# # if you've added a .name attribute:
37+
# content_tag :span, :class => :vcard do
38+
# (link_to_user user, :class => 'fn n', :title_method => :login, :content_method => :name) +
39+
# ': ' + (content_tag :span, user.email, :class => 'email')
40+
# end
41+
# # => <span class="vcard"><a href="/users/3" title="barmy" class="fn n">Cyril Fotheringay-Phipps</a>: <span class="email">[email protected]</span></span>
42+
#
43+
# link_to_user @user, :content_text => 'Your user page'
44+
# # => <a href="/users/3" title="barmy" class="nickname">Your user page</a>
45+
#
46+
def link_to_user(user, options={})
47+
raise "Invalid user" unless user
48+
options.reverse_merge! :content_method => :login, :title_method => :login, :class => :nickname
49+
content_text = options.delete(:content_text)
50+
content_text ||= user.send(options.delete(:content_method))
51+
options[:title] ||= user.send(options.delete(:title_method))
52+
link_to h(content_text), user_path(user), options
53+
end
54+
55+
#
56+
# Link to login page using remote ip address as link content
57+
#
58+
# The :title (and thus, tooltip) is set to the IP address
59+
#
60+
# Examples:
61+
# link_to_login_with_IP
62+
# # => <a href="/login" title="169.69.69.69">169.69.69.69</a>
63+
#
64+
# link_to_login_with_IP :content_text => 'not signed in'
65+
# # => <a href="/login" title="169.69.69.69">not signed in</a>
66+
#
67+
def link_to_login_with_IP content_text=nil, options={}
68+
ip_addr = request.remote_ip
69+
content_text ||= ip_addr
70+
options.reverse_merge! :title => ip_addr
71+
if tag = options.delete(:tag)
72+
content_tag tag, h(content_text), options
73+
else
74+
link_to h(content_text), login_path, options
75+
end
76+
end
77+
78+
#
79+
# Link to the current user's page (using link_to_user) or to the login page
80+
# (using link_to_login_with_IP).
81+
#
82+
def link_to_current_user(options={})
83+
if current_user
84+
link_to_user current_user, options
85+
else
86+
content_text = options.delete(:content_text) || 'not signed in'
87+
# kill ignored options from link_to_user
88+
[:content_method, :title_method].each{|opt| options.delete(opt)}
89+
link_to_login_with_IP content_text, options
90+
end
91+
end
92+
93+
end

app/helpers/welcome_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module WelcomeHelper
2+
end

app/models/user.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'digest/sha1'
2+
3+
class User < ActiveRecord::Base
4+
include Authentication
5+
include Authentication::ByPassword
6+
include Authentication::ByCookieToken
7+
8+
validates_presence_of :login
9+
validates_length_of :login, :within => 3..40
10+
validates_uniqueness_of :login
11+
validates_format_of :login, :with => Authentication.login_regex, :message => Authentication.bad_login_message
12+
13+
validates_format_of :name, :with => Authentication.name_regex, :message => Authentication.bad_name_message, :allow_nil => true
14+
validates_length_of :name, :maximum => 100
15+
16+
validates_presence_of :email
17+
validates_length_of :email, :within => 6..100 #[email protected]
18+
validates_uniqueness_of :email
19+
validates_format_of :email, :with => Authentication.email_regex, :message => Authentication.bad_email_message
20+
21+
22+
23+
# HACK HACK HACK -- how to do attr_accessible from here?
24+
# prevents a user from submitting a crafted form that bypasses activation
25+
# anything else you want your user to change should be added here.
26+
attr_accessible :login, :email, :name, :password, :password_confirmation
27+
28+
29+
30+
# Authenticates a user by their login name and unencrypted password. Returns the user or nil.
31+
#
32+
# uff. this is really an authorization, not authentication routine.
33+
# We really need a Dispatch Chain here or something.
34+
# This will also let us return a human error message.
35+
#
36+
def self.authenticate(login, password)
37+
return nil if login.blank? || password.blank?
38+
u = find_by_login(login.downcase) # need to get the salt
39+
u && u.authenticated?(password) ? u : nil
40+
end
41+
42+
def login=(value)
43+
write_attribute :login, (value ? value.downcase : nil)
44+
end
45+
46+
def email=(value)
47+
write_attribute :email, (value ? value.downcase : nil)
48+
end
49+
50+
protected
51+
52+
53+
54+
end

0 commit comments

Comments
 (0)