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,3 +1,5
1 + require 'pony'
2 +
1 class UsersController < ApplicationController
3 class UsersController < ApplicationController
2
4
3 before_filter :authenticate, :except => [:new, :register]
5 before_filter :authenticate, :except => [:new, :register]
@@ -28,4 +30,27
28 redirect_to :action => 'index'
30 redirect_to :action => 'index'
29 end
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 end
56 end
@@ -19,7 +19,13
19 belongs_to :site
19 belongs_to :site
20 belongs_to :country
20 belongs_to :country
21
21
22 + named_scope :activated, :conditions => {:activated => true}
23 +
22 validates_presence_of :login
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 validates_presence_of :full_name
29 validates_presence_of :full_name
24 validates_length_of :full_name, :minimum => 1
30 validates_length_of :full_name, :minimum => 1
25
31
@@ -27,6 +33,10
27 validates_length_of :password, :within => 4..20, :if => :password_required?
33 validates_length_of :password, :within => 4..20, :if => :password_required?
28 validates_confirmation_of :password, :if => :password_required?
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 attr_accessor :password
40 attr_accessor :password
31
41
32 before_save :encrypt_new_password
42 before_save :encrypt_new_password
@@ -84,6 +94,13
84 key == activation_key
94 key == activation_key
85 end
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 protected
104 protected
88 def encrypt_new_password
105 def encrypt_new_password
89 return if password.blank?
106 return if password.blank?
@@ -98,4 +115,10
98 def self.encrypt(string,salt)
115 def self.encrypt(string,salt)
99 Digest::SHA1.hexdigest(salt + string)
116 Digest::SHA1.hexdigest(salt + string)
100 end
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 end
124 end
@@ -30,7 +30,7
30
30
31 %br/
31 %br/
32
32
33 - -# if Configuration['system.online_registration']
33 + - if Configuration['system.online_registration']
34 Want to participate?
34 Want to participate?
35 %b
35 %b
36 Please
36 Please
@@ -10,26 +10,26
10 :hashed_password => User.encrypt(@password,@salt))
10 :hashed_password => User.encrypt(@password,@salt))
11 end
11 end
12
12
13 - it "should authenticate activated user" do
13 + it "should be authenticated if activated" do
14 @john.should_receive(:activated).and_return(true)
14 @john.should_receive(:activated).and_return(true)
15 @john.authenticated?(@password).should == true
15 @john.authenticated?(@password).should == true
16 end
16 end
17
17
18 - it "should not authenticate inactivated user" do
18 + it "should not be authenticated if inactivated" do
19 @john.should_receive(:activated).and_return(false)
19 @john.should_receive(:activated).and_return(false)
20 @john.authenticated?(@password).should == false
20 @john.authenticated?(@password).should == false
21 end
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 @john.should_receive(:activated).and_return(true)
24 @john.should_receive(:activated).and_return(true)
25 @john.should_receive(:hashed_password).and_return("byebye")
25 @john.should_receive(:hashed_password).and_return("byebye")
26 @john.authenticated?(@password).should == false
26 @john.authenticated?(@password).should == false
27 end
27 end
28 -
28 +
29 end
29 end
30
30
31 describe User, "during registration" do
31 describe User, "during registration" do
32 -
32 +
33 class User
33 class User
34 public :encrypt_new_password
34 public :encrypt_new_password
35 end
35 end
@@ -38,16 +38,26
38 @john = User.new(:login => 'john', :password => 'hello')
38 @john = User.new(:login => 'john', :password => 'hello')
39 @john.encrypt_new_password
39 @john.encrypt_new_password
40 end
40 end
41 -
41 +
42 it "should produce and accept activation key" do
42 it "should produce and accept activation key" do
43 activation_key = @john.activation_key
43 activation_key = @john.activation_key
44
44
45 @john.verify_activation_key(activation_key).should == true
45 @john.verify_activation_key(activation_key).should == true
46 end
46 end
47 -
47 +
48 it "should not accept invalid activation key" do
48 it "should not accept invalid activation key" do
49 @john.verify_activation_key("12345").should == false
49 @john.verify_activation_key("12345").should == false
50 end
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 end
63 end
You need to be logged in to leave comments. Login now