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:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
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 | 3 | class UsersController < ApplicationController |
|
2 | 4 | |
|
3 | 5 | before_filter :authenticate, :except => [:new, :register] |
@@ -28,4 +30,27 | |||
|
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 |
@@ -19,7 +19,13 | |||
|
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 | |
@@ -27,6 +33,10 | |||
|
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 |
@@ -84,6 +94,13 | |||
|
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? |
@@ -98,4 +115,10 | |||
|
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 |
@@ -30,7 +30,7 | |||
|
30 | 30 | |
|
31 | 31 | %br/ |
|
32 | 32 | |
|
33 |
- - |
|
|
33 | + - if Configuration['system.online_registration'] | |
|
34 | 34 | Want to participate? |
|
35 | 35 | %b |
|
36 | 36 | Please |
@@ -10,26 +10,26 | |||
|
10 | 10 | :hashed_password => User.encrypt(@password,@salt)) |
|
11 | 11 | end |
|
12 | 12 | |
|
13 |
- it "should authenticate activated |
|
|
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 |
|
|
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 |
|
|
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 |
@@ -38,16 +38,26 | |||
|
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