Description:
more work on registration git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@296 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r157:1ed23f0780ca - - 6 files changed: 106 inserted, 8 deleted

@@ -0,0 +1,29
1 + %h1 New user registration
2 +
3 + = error_messages_for :user, :header_message => 'Errors occured during registration'
4 +
5 + %table
6 + - form_for :user, @user, :url => { :action => 'register' } do |f|
7 + %tr
8 + %td Login:
9 + %td= f.text_field :login
10 + %tr
11 + %td
12 + %td
13 + %small Only a-z, A-Z, 0-9 and _
14 + %tr
15 + %td Full name:
16 + %td= f.text_field :full_name
17 + %tr
18 + %td E-mail:
19 + %td= f.text_field :email
20 + %tr
21 + %td
22 + %td
23 + %small
24 + Please make sure that your e-mail is correct.
25 + %br/
26 + You'll need to verify your account by email.
27 + %tr
28 + %td{:colspan => 2}= submit_tag "Register"
29 +
@@ -0,0 +1,11
1 + %h1 Registration successful
2 +
3 + We have sent a confimation message to your e-mail.
4 + %br/
5 + Please check at
6 + = "#{@user.email}."
7 + %br/
8 + %br/
9 +
10 + Go back to
11 + = link_to 'login page.', :controller => 'main', :action => 'login'
@@ -1,6 +1,8
1 + require 'pony'
2 +
1 3 class UsersController < ApplicationController
2 4
3 5 before_filter :authenticate, :except => [:new, :register]
4 6
5 7 verify :method => :post, :only => [:chg_passwd],
6 8 :redirect_to => { :action => :index }
@@ -25,7 +27,30
25 27 else
26 28 flash[:notice] = 'Error: password changing failed'
27 29 end
28 30 redirect_to :action => 'index'
29 31 end
30 32
33 + def new
34 + @user = User.new
35 + render :action => 'new', :layout => 'empty'
36 + end
37 +
38 + def register
39 + @user = User.new(params[:user])
40 + @user.password_confirmation = @user.password = User.random_password
41 + @user.activated = false
42 + if (@user.valid?) and (@user.save)
43 + send_confirmation_email(@user)
44 + render :action => 'new_splash', :layout => 'empty'
45 + else
46 + @user.errors.add_to_base("Email cannot be blank") if @user.email==''
47 + render :action => 'new', :layout => 'empty'
48 + end
49 + end
50 +
51 + protected
52 +
53 + def send_confirmation_email(user)
54 + end
55 +
31 56 end
@@ -16,20 +16,30
16 16 :foreign_key => "receiver_id",
17 17 :order => 'created_at DESC'
18 18
19 19 belongs_to :site
20 20 belongs_to :country
21 21
22 + named_scope :activated, :conditions => {:activated => true}
23 +
22 24 validates_presence_of :login
25 + validates_uniqueness_of :login
26 + validates_format_of :login, :with => /^[\_a-z0-9]+$/
27 + validates_length_of :login, :within => 3..10
28 +
23 29 validates_presence_of :full_name
24 30 validates_length_of :full_name, :minimum => 1
25 31
26 32 validates_presence_of :password, :if => :password_required?
27 33 validates_length_of :password, :within => 4..20, :if => :password_required?
28 34 validates_confirmation_of :password, :if => :password_required?
29 35
36 + validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :allow_blank => true
37 +
38 + validate :uniqueness_of_email_from_activated_users
39 +
30 40 attr_accessor :password
31 41
32 42 before_save :encrypt_new_password
33 43
34 44 def self.authenticate(login, password)
35 45 user = find_by_login(login)
@@ -81,12 +91,19
81 91 end
82 92
83 93 def verify_activation_key(key)
84 94 key == activation_key
85 95 end
86 96
97 + def self.random_password(length=5)
98 + chars = 'abcdefghjkmnopqrstuvwxyz'
99 + password = ''
100 + length.times { password << chars[rand(chars.length - 1)] }
101 + password
102 + end
103 +
87 104 protected
88 105 def encrypt_new_password
89 106 return if password.blank?
90 107 self.salt = (10+rand(90)).to_s
91 108 self.hashed_password = User.encrypt(self.password,self.salt)
92 109 end
@@ -95,7 +112,13
95 112 self.hashed_password.blank? || !self.password.blank?
96 113 end
97 114
98 115 def self.encrypt(string,salt)
99 116 Digest::SHA1.hexdigest(salt + string)
100 117 end
118 +
119 + def uniqueness_of_email_from_activated_users
120 + if User.activated.find_by_email(self.email)!=nil
121 + self.errors.add_to_base("Email has already been taken")
122 + end
123 + end
101 124 end
@@ -27,13 +27,13
27 27 %td{:align => "right"} Password:
28 28 %td= password_field_tag
29 29 = submit_tag 'Login'
30 30
31 31 %br/
32 32
33 - -# if Configuration['system.online_registration']
33 + - if Configuration['system.online_registration']
34 34 Want to participate?
35 35 %b
36 36 Please
37 37 = link_to 'register.', :controller => :users, :action => :new
38 38 %br/
39 39
@@ -7,47 +7,57
7 7 @password = "hello"
8 8 @salt = "123"
9 9 @john = stub_model(User, :salt => @salt,
10 10 :hashed_password => User.encrypt(@password,@salt))
11 11 end
12 12
13 - it "should authenticate activated user" do
13 + it "should be authenticated if activated" do
14 14 @john.should_receive(:activated).and_return(true)
15 15 @john.authenticated?(@password).should == true
16 16 end
17 17
18 - it "should not authenticate inactivated user" do
18 + it "should not be authenticated if inactivated" do
19 19 @john.should_receive(:activated).and_return(false)
20 20 @john.authenticated?(@password).should == false
21 21 end
22 22
23 - it "should not authenticate user with incorrect password" do
23 + it "should not be authenticated if incorrect password is provided" do
24 24 @john.should_receive(:activated).and_return(true)
25 25 @john.should_receive(:hashed_password).and_return("byebye")
26 26 @john.authenticated?(@password).should == false
27 27 end
28 -
28 +
29 29 end
30 30
31 31 describe User, "during registration" do
32 -
32 +
33 33 class User
34 34 public :encrypt_new_password
35 35 end
36 36
37 37 before(:each) do
38 38 @john = User.new(:login => 'john', :password => 'hello')
39 39 @john.encrypt_new_password
40 40 end
41 -
41 +
42 42 it "should produce and accept activation key" do
43 43 activation_key = @john.activation_key
44 44
45 45 @john.verify_activation_key(activation_key).should == true
46 46 end
47 -
47 +
48 48 it "should not accept invalid activation key" do
49 49 @john.verify_activation_key("12345").should == false
50 50 end
51 +
52 + end
51 53
54 + describe User, "as a class" do
52 55
56 + it "should be able to generate random password" do
57 + password1 = User.random_password
58 + password2 = User.random_password
59 +
60 + password1.should_not == password2
61 + end
62 +
53 63 end
You need to be logged in to leave comments. Login now