Description:
fixed user confirmation bug
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@305 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
r160:0138848b0c5b - - 5 files changed: 23 inserted, 12 deleted
@@ -1,126 +1,134 | |||||
|
1 | class MainController < ApplicationController |
|
1 | class MainController < ApplicationController |
|
2 |
|
2 | ||
|
3 | SYSTEM_MODE_CONF_KEY = 'system.mode' |
|
3 | SYSTEM_MODE_CONF_KEY = 'system.mode' |
|
4 |
|
4 | ||
|
5 | before_filter :authenticate, :except => [:index, :login] |
|
5 | before_filter :authenticate, :except => [:index, :login] |
|
6 | before_filter :check_viewability, :except => [:index, :login] |
|
6 | before_filter :check_viewability, :except => [:index, :login] |
|
7 |
|
7 | ||
|
8 | - # |
|
8 | + # COMMENTED OUT: filter in each action instead |
|
9 | - # COMMENT OUT: filter in each action instead |
|
||
|
10 | - # |
|
||
|
11 |
|
|
9 | # before_filter :verify_time_limit, :only => [:submit] |
|
12 |
|
10 | ||
|
13 | verify :method => :post, :only => [:submit], |
|
11 | verify :method => :post, :only => [:submit], |
|
14 | :redirect_to => { :action => :index } |
|
12 | :redirect_to => { :action => :index } |
|
15 |
|
13 | ||
|
16 |
- |
|
14 | + # COMMENT OUT: only need when having high load |
|
17 |
|
|
15 | # caches_action :index, :login |
|
18 |
|
16 | ||
|
|
17 | + # NOTE: This method is not actually needed, 'config/routes.rb' has | ||
|
|
18 | + # assigned action login as a default action. | ||
|
19 | def index |
|
19 | def index |
|
20 | redirect_to :action => 'login' |
|
20 | redirect_to :action => 'login' |
|
21 | end |
|
21 | end |
|
22 |
|
22 | ||
|
23 | def login |
|
23 | def login |
|
24 | saved_notice = flash[:notice] |
|
24 | saved_notice = flash[:notice] |
|
25 | reset_session |
|
25 | reset_session |
|
26 | flash[:notice] = saved_notice |
|
26 | flash[:notice] = saved_notice |
|
27 |
|
27 | ||
|
|
28 | + # EXPERIMENT: | ||
|
|
29 | + # Hide login if in single user mode and the url does not | ||
|
|
30 | + # explicitly specify /login | ||
|
28 | # |
|
31 | # |
|
29 | - # These are for site administrator login |
|
32 | + # logger.info "PATH: #{request.path}" |
|
30 | - # |
|
33 | + # if Configuration['system.single_user_mode'] and |
|
|
34 | + # request.path!='/main/login' | ||
|
|
35 | + # @hidelogin = true | ||
|
|
36 | + # end | ||
|
|
37 | + | ||
|
|
38 | + # Site administrator login | ||
|
31 | @countries = Country.find(:all, :include => :sites) |
|
39 | @countries = Country.find(:all, :include => :sites) |
|
32 | @country_select = @countries.collect { |c| [c.name, c.id] } |
|
40 | @country_select = @countries.collect { |c| [c.name, c.id] } |
|
33 |
|
41 | ||
|
34 | @country_select_with_all = [['Any',0]] |
|
42 | @country_select_with_all = [['Any',0]] |
|
35 | @countries.each do |country| |
|
43 | @countries.each do |country| |
|
36 | @country_select_with_all << [country.name, country.id] |
|
44 | @country_select_with_all << [country.name, country.id] |
|
37 | end |
|
45 | end |
|
38 |
|
46 | ||
|
39 | @site_select = [] |
|
47 | @site_select = [] |
|
40 | @countries.each do |country| |
|
48 | @countries.each do |country| |
|
41 | country.sites.each do |site| |
|
49 | country.sites.each do |site| |
|
42 | @site_select << ["#{site.name}, #{country.name}", site.id] |
|
50 | @site_select << ["#{site.name}, #{country.name}", site.id] |
|
43 | end |
|
51 | end |
|
44 | end |
|
52 | end |
|
45 |
|
53 | ||
|
46 | @announcements = Announcement.find_for_frontpage |
|
54 | @announcements = Announcement.find_for_frontpage |
|
47 | render :action => 'login', :layout => 'empty' |
|
55 | render :action => 'login', :layout => 'empty' |
|
48 | end |
|
56 | end |
|
49 |
|
57 | ||
|
50 | def list |
|
58 | def list |
|
51 | prepare_list_information |
|
59 | prepare_list_information |
|
52 | end |
|
60 | end |
|
53 |
|
61 | ||
|
54 | def help |
|
62 | def help |
|
55 | @user = User.find(session[:user_id]) |
|
63 | @user = User.find(session[:user_id]) |
|
56 | end |
|
64 | end |
|
57 |
|
65 | ||
|
58 | def submit |
|
66 | def submit |
|
59 | user = User.find(session[:user_id]) |
|
67 | user = User.find(session[:user_id]) |
|
60 |
|
68 | ||
|
61 | @submission = Submission.new(params[:submission]) |
|
69 | @submission = Submission.new(params[:submission]) |
|
62 | @submission.user = user |
|
70 | @submission.user = user |
|
63 | @submission.language_id = 0 |
|
71 | @submission.language_id = 0 |
|
64 | if params['file']!='' |
|
72 | if params['file']!='' |
|
65 | @submission.source = params['file'].read |
|
73 | @submission.source = params['file'].read |
|
66 | @submission.source_filename = params['file'].original_filename |
|
74 | @submission.source_filename = params['file'].original_filename |
|
67 | end |
|
75 | end |
|
68 | @submission.submitted_at = Time.new.gmtime |
|
76 | @submission.submitted_at = Time.new.gmtime |
|
69 |
|
77 | ||
|
70 | if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and |
|
78 | if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and |
|
71 | user.site!=nil and user.site.finished? |
|
79 | user.site!=nil and user.site.finished? |
|
72 | @submission.errors.add_to_base "The contest is over." |
|
80 | @submission.errors.add_to_base "The contest is over." |
|
73 | prepare_list_information |
|
81 | prepare_list_information |
|
74 | render :action => 'list' and return |
|
82 | render :action => 'list' and return |
|
75 | end |
|
83 | end |
|
76 |
|
84 | ||
|
77 | if @submission.valid? |
|
85 | if @submission.valid? |
|
78 | if @submission.save == false |
|
86 | if @submission.save == false |
|
79 | flash[:notice] = 'Error saving your submission' |
|
87 | flash[:notice] = 'Error saving your submission' |
|
80 | elsif Task.create(:submission_id => @submission.id, |
|
88 | elsif Task.create(:submission_id => @submission.id, |
|
81 | :status => Task::STATUS_INQUEUE) == false |
|
89 | :status => Task::STATUS_INQUEUE) == false |
|
82 | flash[:notice] = 'Error adding your submission to task queue' |
|
90 | flash[:notice] = 'Error adding your submission to task queue' |
|
83 | end |
|
91 | end |
|
84 | else |
|
92 | else |
|
85 | prepare_list_information |
|
93 | prepare_list_information |
|
86 | render :action => 'list' and return |
|
94 | render :action => 'list' and return |
|
87 | end |
|
95 | end |
|
88 | redirect_to :action => 'list' |
|
96 | redirect_to :action => 'list' |
|
89 | end |
|
97 | end |
|
90 |
|
98 | ||
|
91 | def source |
|
99 | def source |
|
92 | submission = Submission.find(params[:id]) |
|
100 | submission = Submission.find(params[:id]) |
|
93 | if submission.user_id == session[:user_id] |
|
101 | if submission.user_id == session[:user_id] |
|
94 | if submission.problem.output_only |
|
102 | if submission.problem.output_only |
|
95 | fname = submission.source_filename |
|
103 | fname = submission.source_filename |
|
96 | else |
|
104 | else |
|
97 | fname = submission.problem.name + '.' + submission.language.ext |
|
105 | fname = submission.problem.name + '.' + submission.language.ext |
|
98 | end |
|
106 | end |
|
99 | send_data(submission.source, |
|
107 | send_data(submission.source, |
|
100 | {:filename => fname, |
|
108 | {:filename => fname, |
|
101 | :type => 'text/plain'}) |
|
109 | :type => 'text/plain'}) |
|
102 | else |
|
110 | else |
|
103 | flash[:notice] = 'Error viewing source' |
|
111 | flash[:notice] = 'Error viewing source' |
|
104 | redirect_to :action => 'list' |
|
112 | redirect_to :action => 'list' |
|
105 | end |
|
113 | end |
|
106 | end |
|
114 | end |
|
107 |
|
115 | ||
|
108 | def compiler_msg |
|
116 | def compiler_msg |
|
109 | @submission = Submission.find(params[:id]) |
|
117 | @submission = Submission.find(params[:id]) |
|
110 | if @submission.user_id == session[:user_id] |
|
118 | if @submission.user_id == session[:user_id] |
|
111 | render :action => 'compiler_msg', :layout => 'empty' |
|
119 | render :action => 'compiler_msg', :layout => 'empty' |
|
112 | else |
|
120 | else |
|
113 | flash[:notice] = 'Error viewing source' |
|
121 | flash[:notice] = 'Error viewing source' |
|
114 | redirect_to :action => 'list' |
|
122 | redirect_to :action => 'list' |
|
115 | end |
|
123 | end |
|
116 | end |
|
124 | end |
|
117 |
|
125 | ||
|
118 | def submission |
|
126 | def submission |
|
119 | @user = User.find(session[:user_id]) |
|
127 | @user = User.find(session[:user_id]) |
|
120 | @problems = Problem.find_available_problems |
|
128 | @problems = Problem.find_available_problems |
|
121 | if params[:id]==nil |
|
129 | if params[:id]==nil |
|
122 | @problem = nil |
|
130 | @problem = nil |
|
123 | @submissions = nil |
|
131 | @submissions = nil |
|
124 | else |
|
132 | else |
|
125 | @problem = Problem.find_by_name(params[:id]) |
|
133 | @problem = Problem.find_by_name(params[:id]) |
|
126 | if not @problem.available |
|
134 | if not @problem.available |
@@ -1,116 +1,116 | |||||
|
1 | require 'tmail' |
|
1 | require 'tmail' |
|
2 | require 'net/smtp' |
|
2 | require 'net/smtp' |
|
3 |
|
3 | ||
|
4 | class UsersController < ApplicationController |
|
4 | class UsersController < ApplicationController |
|
5 |
|
5 | ||
|
6 | before_filter :authenticate, :except => [:new, :register, :confirm] |
|
6 | before_filter :authenticate, :except => [:new, :register, :confirm] |
|
7 |
|
7 | ||
|
8 | verify :method => :post, :only => [:chg_passwd], |
|
8 | verify :method => :post, :only => [:chg_passwd], |
|
9 | :redirect_to => { :action => :index } |
|
9 | :redirect_to => { :action => :index } |
|
10 |
|
10 | ||
|
11 | in_place_edit_for :user, :alias_for_editing |
|
11 | in_place_edit_for :user, :alias_for_editing |
|
12 | in_place_edit_for :user, :email_for_editing |
|
12 | in_place_edit_for :user, :email_for_editing |
|
13 |
|
13 | ||
|
14 | def index |
|
14 | def index |
|
15 | if !Configuration['system.user_setting_enabled'] |
|
15 | if !Configuration['system.user_setting_enabled'] |
|
16 | redirect_to :controller => 'main', :action => 'list' |
|
16 | redirect_to :controller => 'main', :action => 'list' |
|
17 | else |
|
17 | else |
|
18 | @user = User.find(session[:user_id]) |
|
18 | @user = User.find(session[:user_id]) |
|
19 | end |
|
19 | end |
|
20 | end |
|
20 | end |
|
21 |
|
21 | ||
|
22 | def chg_passwd |
|
22 | def chg_passwd |
|
23 | user = User.find(session[:user_id]) |
|
23 | user = User.find(session[:user_id]) |
|
24 | user.password = params[:passwd] |
|
24 | user.password = params[:passwd] |
|
25 | user.password_confirmation = params[:passwd_verify] |
|
25 | user.password_confirmation = params[:passwd_verify] |
|
26 | if user.save |
|
26 | if user.save |
|
27 | flash[:notice] = 'password changed' |
|
27 | flash[:notice] = 'password changed' |
|
28 | else |
|
28 | else |
|
29 | flash[:notice] = 'Error: password changing failed' |
|
29 | flash[:notice] = 'Error: password changing failed' |
|
30 | end |
|
30 | end |
|
31 | redirect_to :action => 'index' |
|
31 | redirect_to :action => 'index' |
|
32 | end |
|
32 | end |
|
33 |
|
33 | ||
|
34 | def new |
|
34 | def new |
|
35 | @user = User.new |
|
35 | @user = User.new |
|
36 | render :action => 'new', :layout => 'empty' |
|
36 | render :action => 'new', :layout => 'empty' |
|
37 | end |
|
37 | end |
|
38 |
|
38 | ||
|
39 | def register |
|
39 | def register |
|
40 | @user = User.new(params[:user]) |
|
40 | @user = User.new(params[:user]) |
|
41 | @user.password_confirmation = @user.password = User.random_password |
|
41 | @user.password_confirmation = @user.password = User.random_password |
|
42 | @user.activated = false |
|
42 | @user.activated = false |
|
43 | if (@user.valid?) and (@user.save) |
|
43 | if (@user.valid?) and (@user.save) |
|
44 | if send_confirmation_email(@user) |
|
44 | if send_confirmation_email(@user) |
|
45 | render :action => 'new_splash', :layout => 'empty' |
|
45 | render :action => 'new_splash', :layout => 'empty' |
|
46 | else |
|
46 | else |
|
47 | render :action => 'email_error', :layout => 'empty' |
|
47 | render :action => 'email_error', :layout => 'empty' |
|
48 | end |
|
48 | end |
|
49 | else |
|
49 | else |
|
50 | @user.errors.add_to_base("Email cannot be blank") if @user.email=='' |
|
50 | @user.errors.add_to_base("Email cannot be blank") if @user.email=='' |
|
51 | render :action => 'new', :layout => 'empty' |
|
51 | render :action => 'new', :layout => 'empty' |
|
52 | end |
|
52 | end |
|
53 | end |
|
53 | end |
|
54 |
|
54 | ||
|
55 | def confirm |
|
55 | def confirm |
|
56 | login = params[:login] |
|
56 | login = params[:login] |
|
57 | key = params[:activation] |
|
57 | key = params[:activation] |
|
58 | - user = User.find_by_login(login) |
|
58 | + @user = User.find_by_login(login) |
|
59 | - if (user) and (user.verify_activation_key(key)) |
|
59 | + if (@user) and (@user.verify_activation_key(key)) |
|
60 | - if user.valid? # check uniquenss of email |
|
60 | + if @user.valid? # check uniquenss of email |
|
61 | - user.activated = true |
|
61 | + @user.activated = true |
|
62 | - user.save |
|
62 | + @user.save |
|
63 | @result = :successful |
|
63 | @result = :successful |
|
64 | else |
|
64 | else |
|
65 | @result = :email_used |
|
65 | @result = :email_used |
|
66 | end |
|
66 | end |
|
67 | else |
|
67 | else |
|
68 | @result = :failed |
|
68 | @result = :failed |
|
69 | end |
|
69 | end |
|
70 | render :action => 'confirm', :layout => 'empty' |
|
70 | render :action => 'confirm', :layout => 'empty' |
|
71 | end |
|
71 | end |
|
72 |
|
72 | ||
|
73 | protected |
|
73 | protected |
|
74 |
|
74 | ||
|
75 | def send_confirmation_email(user) |
|
75 | def send_confirmation_email(user) |
|
76 | contest_name = Configuration['contest.name'] |
|
76 | contest_name = Configuration['contest.name'] |
|
77 | activation_url = url_for(:action => 'confirm', |
|
77 | activation_url = url_for(:action => 'confirm', |
|
78 | :login => user.login, |
|
78 | :login => user.login, |
|
79 | :activation => user.activation_key) |
|
79 | :activation => user.activation_key) |
|
80 | home_url = url_for(:controller => 'main', :action => 'index') |
|
80 | home_url = url_for(:controller => 'main', :action => 'index') |
|
81 | mail = TMail::Mail.new |
|
81 | mail = TMail::Mail.new |
|
82 | mail.to = user.email |
|
82 | mail.to = user.email |
|
83 | mail.from = Configuration['system.online_registration.from'] |
|
83 | mail.from = Configuration['system.online_registration.from'] |
|
84 | mail.subject = "[#{contest_name}] Confirmation" |
|
84 | mail.subject = "[#{contest_name}] Confirmation" |
|
85 | mail.body = <<-EOF |
|
85 | mail.body = <<-EOF |
|
86 | Hello #{user.full_name}, |
|
86 | Hello #{user.full_name}, |
|
87 |
|
87 | ||
|
88 | You have registered for #{contest_name} (#{home_url}). |
|
88 | You have registered for #{contest_name} (#{home_url}). |
|
89 |
|
89 | ||
|
90 | Your login is: #{user.login} |
|
90 | Your login is: #{user.login} |
|
91 | Your password is: #{user.password} |
|
91 | Your password is: #{user.password} |
|
92 |
|
92 | ||
|
93 | Please follow the link: |
|
93 | Please follow the link: |
|
94 | #{activation_url} |
|
94 | #{activation_url} |
|
95 | to activate your user account. |
|
95 | to activate your user account. |
|
96 |
|
96 | ||
|
97 | If you did not register, please ignore this e-mail. |
|
97 | If you did not register, please ignore this e-mail. |
|
98 |
|
98 | ||
|
99 | Thanks! |
|
99 | Thanks! |
|
100 | EOF |
|
100 | EOF |
|
101 |
|
101 | ||
|
102 | smtp_server = Configuration['system.online_registration.smtp'] |
|
102 | smtp_server = Configuration['system.online_registration.smtp'] |
|
103 |
|
103 | ||
|
104 | begin |
|
104 | begin |
|
105 | Net::SMTP.start(smtp_server) do |smtp| |
|
105 | Net::SMTP.start(smtp_server) do |smtp| |
|
106 | smtp.send_message(mail.to_s, mail.from, mail.to) |
|
106 | smtp.send_message(mail.to_s, mail.from, mail.to) |
|
107 | end |
|
107 | end |
|
108 | result = true |
|
108 | result = true |
|
109 | rescue |
|
109 | rescue |
|
110 | result = false |
|
110 | result = false |
|
111 | end |
|
111 | end |
|
112 |
|
112 | ||
|
113 | return result |
|
113 | return result |
|
114 | end |
|
114 | end |
|
115 |
|
115 | ||
|
116 | end |
|
116 | end |
@@ -35,104 +35,105 | |||||
|
35 |
|
35 | ||
|
36 | validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :allow_blank => true |
|
36 | validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :allow_blank => true |
|
37 |
|
37 | ||
|
38 | validate :uniqueness_of_email_from_activated_users |
|
38 | validate :uniqueness_of_email_from_activated_users |
|
39 | validate :enough_time_interval_between_same_email_registrations |
|
39 | validate :enough_time_interval_between_same_email_registrations |
|
40 |
|
40 | ||
|
41 | attr_accessor :password |
|
41 | attr_accessor :password |
|
42 |
|
42 | ||
|
43 | before_save :encrypt_new_password |
|
43 | before_save :encrypt_new_password |
|
44 |
|
44 | ||
|
45 | def self.authenticate(login, password) |
|
45 | def self.authenticate(login, password) |
|
46 | user = find_by_login(login) |
|
46 | user = find_by_login(login) |
|
47 | return user if user && user.authenticated?(password) |
|
47 | return user if user && user.authenticated?(password) |
|
48 | end |
|
48 | end |
|
49 |
|
49 | ||
|
50 | def authenticated?(password) |
|
50 | def authenticated?(password) |
|
51 | if self.activated |
|
51 | if self.activated |
|
52 | hashed_password == User.encrypt(password,self.salt) |
|
52 | hashed_password == User.encrypt(password,self.salt) |
|
53 | else |
|
53 | else |
|
54 | false |
|
54 | false |
|
55 | end |
|
55 | end |
|
56 | end |
|
56 | end |
|
57 |
|
57 | ||
|
58 | def admin? |
|
58 | def admin? |
|
59 | self.roles.detect {|r| r.name == 'admin' } |
|
59 | self.roles.detect {|r| r.name == 'admin' } |
|
60 | end |
|
60 | end |
|
61 |
|
61 | ||
|
62 | def email_for_editing |
|
62 | def email_for_editing |
|
63 | if self.email==nil |
|
63 | if self.email==nil |
|
64 | "(unknown)" |
|
64 | "(unknown)" |
|
65 | elsif self.email=='' |
|
65 | elsif self.email=='' |
|
66 | "(blank)" |
|
66 | "(blank)" |
|
67 | else |
|
67 | else |
|
68 | self.email |
|
68 | self.email |
|
69 | end |
|
69 | end |
|
70 | end |
|
70 | end |
|
71 |
|
71 | ||
|
72 | def email_for_editing=(e) |
|
72 | def email_for_editing=(e) |
|
73 | self.email=e |
|
73 | self.email=e |
|
74 | end |
|
74 | end |
|
75 |
|
75 | ||
|
76 | def alias_for_editing |
|
76 | def alias_for_editing |
|
77 | if self.alias==nil |
|
77 | if self.alias==nil |
|
78 | "(unknown)" |
|
78 | "(unknown)" |
|
79 | elsif self.alias=='' |
|
79 | elsif self.alias=='' |
|
80 | "(blank)" |
|
80 | "(blank)" |
|
81 | else |
|
81 | else |
|
82 | self.alias |
|
82 | self.alias |
|
83 | end |
|
83 | end |
|
84 | end |
|
84 | end |
|
85 |
|
85 | ||
|
86 | def alias_for_editing=(e) |
|
86 | def alias_for_editing=(e) |
|
87 | self.alias=e |
|
87 | self.alias=e |
|
88 | end |
|
88 | end |
|
89 |
|
89 | ||
|
90 | def activation_key |
|
90 | def activation_key |
|
91 | if self.hashed_password==nil |
|
91 | if self.hashed_password==nil |
|
92 | encrypt_new_password |
|
92 | encrypt_new_password |
|
93 | end |
|
93 | end |
|
94 | Digest::SHA1.hexdigest(self.hashed_password)[0..7] |
|
94 | Digest::SHA1.hexdigest(self.hashed_password)[0..7] |
|
95 | end |
|
95 | end |
|
96 |
|
96 | ||
|
97 | def verify_activation_key(key) |
|
97 | def verify_activation_key(key) |
|
98 | key == activation_key |
|
98 | key == activation_key |
|
99 | end |
|
99 | end |
|
100 |
|
100 | ||
|
101 | def self.random_password(length=5) |
|
101 | def self.random_password(length=5) |
|
102 | chars = 'abcdefghjkmnopqrstuvwxyz' |
|
102 | chars = 'abcdefghjkmnopqrstuvwxyz' |
|
103 | password = '' |
|
103 | password = '' |
|
104 | length.times { password << chars[rand(chars.length - 1)] } |
|
104 | length.times { password << chars[rand(chars.length - 1)] } |
|
105 | password |
|
105 | password |
|
106 | end |
|
106 | end |
|
107 |
|
107 | ||
|
108 | protected |
|
108 | protected |
|
109 | def encrypt_new_password |
|
109 | def encrypt_new_password |
|
110 | return if password.blank? |
|
110 | return if password.blank? |
|
111 | self.salt = (10+rand(90)).to_s |
|
111 | self.salt = (10+rand(90)).to_s |
|
112 | self.hashed_password = User.encrypt(self.password,self.salt) |
|
112 | self.hashed_password = User.encrypt(self.password,self.salt) |
|
113 | end |
|
113 | end |
|
114 |
|
114 | ||
|
115 | def password_required? |
|
115 | def password_required? |
|
116 | self.hashed_password.blank? || !self.password.blank? |
|
116 | self.hashed_password.blank? || !self.password.blank? |
|
117 | end |
|
117 | end |
|
118 |
|
118 | ||
|
119 | def self.encrypt(string,salt) |
|
119 | def self.encrypt(string,salt) |
|
120 | Digest::SHA1.hexdigest(salt + string) |
|
120 | Digest::SHA1.hexdigest(salt + string) |
|
121 | end |
|
121 | end |
|
122 |
|
122 | ||
|
123 | def uniqueness_of_email_from_activated_users |
|
123 | def uniqueness_of_email_from_activated_users |
|
124 | user = User.activated_users.find_by_email(self.email) |
|
124 | user = User.activated_users.find_by_email(self.email) |
|
125 | if user and (user.login != self.login) |
|
125 | if user and (user.login != self.login) |
|
126 | self.errors.add_to_base("Email has already been taken") |
|
126 | self.errors.add_to_base("Email has already been taken") |
|
127 | end |
|
127 | end |
|
128 | end |
|
128 | end |
|
129 |
|
129 | ||
|
130 | def enough_time_interval_between_same_email_registrations |
|
130 | def enough_time_interval_between_same_email_registrations |
|
|
131 | + return if !self.new_record? | ||
|
131 | open_user = User.find_by_email(self.email, |
|
132 | open_user = User.find_by_email(self.email, |
|
132 | :order => 'created_at DESC') |
|
133 | :order => 'created_at DESC') |
|
133 | if open_user and open_user.created_at and |
|
134 | if open_user and open_user.created_at and |
|
134 | (open_user.created_at > Time.now.gmtime - 5.minutes) |
|
135 | (open_user.created_at > Time.now.gmtime - 5.minutes) |
|
135 | self.errors.add_to_base("There are already unactivated registrations with this e-mail address (please wait for 5 minutes)") |
|
136 | self.errors.add_to_base("There are already unactivated registrations with this e-mail address (please wait for 5 minutes)") |
|
136 | end |
|
137 | end |
|
137 | end |
|
138 | end |
|
138 | end |
|
139 | end |
@@ -1,69 +1,70 | |||||
|
1 | %h1= Configuration['ui.front.title'] |
|
1 | %h1= Configuration['ui.front.title'] |
|
2 |
|
2 | ||
|
3 | - if @announcements.length!=0 |
|
3 | - if @announcements.length!=0 |
|
4 | .announcementbox |
|
4 | .announcementbox |
|
5 | %span{:class => 'title'} |
|
5 | %span{:class => 'title'} |
|
6 | Announcements |
|
6 | Announcements |
|
7 | = render :partial => 'announcement', :collection => @announcements |
|
7 | = render :partial => 'announcement', :collection => @announcements |
|
8 |
|
8 | ||
|
9 | %b= Configuration['ui.front.welcome_message'] |
|
9 | %b= Configuration['ui.front.welcome_message'] |
|
10 | %br/ |
|
10 | %br/ |
|
|
11 | + | ||
|
|
12 | + - if !@hidelogin | ||
|
11 | Please login to see the problem list. |
|
13 | Please login to see the problem list. |
|
12 | %br/ |
|
14 | %br/ |
|
13 | %br/ |
|
15 | %br/ |
|
14 |
|
16 | ||
|
15 | - if flash[:notice] |
|
17 | - if flash[:notice] |
|
16 | %hr/ |
|
18 | %hr/ |
|
17 | %b= flash[:notice] |
|
19 | %b= flash[:notice] |
|
18 | %hr/ |
|
20 | %hr/ |
|
19 |
|
21 | ||
|
20 | %div{ :style => "border: solid 1px gray; padding: 2px; background: #f0f0f0;"} |
|
22 | %div{ :style => "border: solid 1px gray; padding: 2px; background: #f0f0f0;"} |
|
21 | - form_tag :controller => 'login', :action => 'login' do |
|
23 | - form_tag :controller => 'login', :action => 'login' do |
|
22 | %table |
|
24 | %table |
|
23 | %tr |
|
25 | %tr |
|
24 | %td{:align => "right"} Login: |
|
26 | %td{:align => "right"} Login: |
|
25 | %td= text_field_tag 'login' |
|
27 | %td= text_field_tag 'login' |
|
26 | %tr |
|
28 | %tr |
|
27 | %td{:align => "right"} Password: |
|
29 | %td{:align => "right"} Password: |
|
28 | %td= password_field_tag |
|
30 | %td= password_field_tag |
|
29 | = submit_tag 'Login' |
|
31 | = submit_tag 'Login' |
|
30 | - |
|
||
|
31 | %br/ |
|
32 | %br/ |
|
32 |
|
33 | ||
|
33 | - if Configuration['system.online_registration'] |
|
34 | - if Configuration['system.online_registration'] |
|
34 | Want to participate? |
|
35 | Want to participate? |
|
35 | %b |
|
36 | %b |
|
36 | Please |
|
37 | Please |
|
37 | = link_to 'register.', :controller => :users, :action => :new |
|
38 | = link_to 'register.', :controller => :users, :action => :new |
|
38 | %br/ |
|
39 | %br/ |
|
39 |
|
40 | ||
|
40 | - if (Configuration['system.mode']=='contest') and (Configuration['contest.multisites']) |
|
41 | - if (Configuration['system.mode']=='contest') and (Configuration['contest.multisites']) |
|
41 | %script{:type => 'text/javascript'} |
|
42 | %script{:type => 'text/javascript'} |
|
42 | var siteList = new Array(); |
|
43 | var siteList = new Array(); |
|
43 | - @countries.each do |country| |
|
44 | - @countries.each do |country| |
|
44 | = "siteList[#{country.id}] = new Array();" |
|
45 | = "siteList[#{country.id}] = new Array();" |
|
45 | - country.sites.each do |site| |
|
46 | - country.sites.each do |site| |
|
46 | = "siteList[#{country.id}][#{site.id}] = \"#{site.name}\";" |
|
47 | = "siteList[#{country.id}][#{site.id}] = \"#{site.name}\";" |
|
47 |
|
48 | ||
|
48 | var allSiteList = new Array(); |
|
49 | var allSiteList = new Array(); |
|
49 | - @site_select.each do |sel| |
|
50 | - @site_select.each do |sel| |
|
50 | = "allSiteList[#{sel[1]}]=\"#{sel[0]}\";" |
|
51 | = "allSiteList[#{sel[1]}]=\"#{sel[0]}\";" |
|
51 |
|
52 | ||
|
52 | %script{:type => 'text/javascript', :src => '/javascripts/site_update.js'} |
|
53 | %script{:type => 'text/javascript', :src => '/javascripts/site_update.js'} |
|
53 |
|
54 | ||
|
54 | %div{ :style => "border: solid 1px gray; padding: 2px; background: #f0f0f0;"} |
|
55 | %div{ :style => "border: solid 1px gray; padding: 2px; background: #f0f0f0;"} |
|
55 | %b For Site Administrator. |
|
56 | %b For Site Administrator. |
|
56 | %br/ |
|
57 | %br/ |
|
57 | Please select your country and site and login. |
|
58 | Please select your country and site and login. |
|
58 | - form_for :login, nil, :url => {:controller => 'login', :action => 'site_login'} do |f| |
|
59 | - form_for :login, nil, :url => {:controller => 'login', :action => 'site_login'} do |f| |
|
59 | Country: |
|
60 | Country: |
|
60 | = select :site_country, :id, @country_select_with_all, {}, {:onchange => "updateSiteList();", :onclick => "updateSiteList();" } |
|
61 | = select :site_country, :id, @country_select_with_all, {}, {:onchange => "updateSiteList();", :onclick => "updateSiteList();" } |
|
61 | Site: |
|
62 | Site: |
|
62 | = select :login, :site_id, @site_select |
|
63 | = select :login, :site_id, @site_select |
|
63 | %br/ |
|
64 | %br/ |
|
64 | Password: |
|
65 | Password: |
|
65 | = f.password_field :password |
|
66 | = f.password_field :password |
|
66 | = submit_tag "Site Administrator Login" |
|
67 | = submit_tag "Site Administrator Login" |
|
67 |
|
68 | ||
|
68 | %script{:type => 'text/javascript'} |
|
69 | %script{:type => 'text/javascript'} |
|
69 | updateSiteList(); |
|
70 | updateSiteList(); |
@@ -1,95 +1,96 | |||||
|
1 |
|
1 | ||
|
2 | require File.dirname(__FILE__) + '/../spec_helper' |
|
2 | require File.dirname(__FILE__) + '/../spec_helper' |
|
3 |
|
3 | ||
|
4 | describe UsersController, "when a new user registers" do |
|
4 | describe UsersController, "when a new user registers" do |
|
5 |
|
5 | ||
|
6 | before(:each) do |
|
6 | before(:each) do |
|
7 | # create john |
|
7 | # create john |
|
8 |
|
8 | ||
|
9 | @john_info = {:login => 'john', |
|
9 | @john_info = {:login => 'john', |
|
10 | :full_name => 'John John', |
|
10 | :full_name => 'John John', |
|
11 | :email => 'john@space.com'} |
|
11 | :email => 'john@space.com'} |
|
12 | @john = User.new(@john_info) |
|
12 | @john = User.new(@john_info) |
|
13 |
|
13 | ||
|
14 | @john_activation_key = "123456" |
|
14 | @john_activation_key = "123456" |
|
15 |
|
15 | ||
|
16 | @john.should_receive(:activation_key). |
|
16 | @john.should_receive(:activation_key). |
|
17 | any_number_of_times. |
|
17 | any_number_of_times. |
|
18 | and_return(@john_activation_key) |
|
18 | and_return(@john_activation_key) |
|
19 |
|
19 | ||
|
20 | get :new |
|
20 | get :new |
|
21 | response.should render_template('users/new') |
|
21 | response.should render_template('users/new') |
|
22 | end |
|
22 | end |
|
23 |
|
23 | ||
|
24 | it "should show the new form again when user information is invalid" do |
|
24 | it "should show the new form again when user information is invalid" do |
|
25 | User.should_receive(:new).with(any_args()).and_return(@john) |
|
25 | User.should_receive(:new).with(any_args()).and_return(@john) |
|
26 | @john.should_receive(:activated=).with(false) |
|
26 | @john.should_receive(:activated=).with(false) |
|
27 | @john.should_receive(:valid?).and_return(false) |
|
27 | @john.should_receive(:valid?).and_return(false) |
|
28 | @john.should_not_receive(:save) |
|
28 | @john.should_not_receive(:save) |
|
29 |
|
29 | ||
|
30 | post :register, :login => @john_info[:login], |
|
30 | post :register, :login => @john_info[:login], |
|
31 | :full_name => @john_info[:full_name], |
|
31 | :full_name => @john_info[:full_name], |
|
32 | :email => @john_info[:email] |
|
32 | :email => @john_info[:email] |
|
33 |
|
33 | ||
|
34 | response.should render_template('users/new') |
|
34 | response.should render_template('users/new') |
|
35 | end |
|
35 | end |
|
36 |
|
36 | ||
|
37 | it "should create unactivated user and send e-mail with activation key" do |
|
37 | it "should create unactivated user and send e-mail with activation key" do |
|
38 | User.should_receive(:new).with(any_args()).and_return(@john) |
|
38 | User.should_receive(:new).with(any_args()).and_return(@john) |
|
39 | @john.should_receive(:activated=).with(false) |
|
39 | @john.should_receive(:activated=).with(false) |
|
40 | @john.should_receive(:valid?).and_return(true) |
|
40 | @john.should_receive(:valid?).and_return(true) |
|
41 | @john.should_receive(:save).and_return(true) |
|
41 | @john.should_receive(:save).and_return(true) |
|
42 |
|
42 | ||
|
43 | smtp_mock = mock("smtp") |
|
43 | smtp_mock = mock("smtp") |
|
44 | smtp_mock.should_receive(:send_message) do |msg,fr,to| |
|
44 | smtp_mock.should_receive(:send_message) do |msg,fr,to| |
|
45 | to.should == [@john_info[:email]] |
|
45 | to.should == [@john_info[:email]] |
|
46 | msg.index(@john_activation_key).should_not be_nil |
|
46 | msg.index(@john_activation_key).should_not be_nil |
|
47 | end |
|
47 | end |
|
48 |
|
48 | ||
|
49 | Net::SMTP.should_receive(:start). |
|
49 | Net::SMTP.should_receive(:start). |
|
50 | with(any_args()). |
|
50 | with(any_args()). |
|
51 | and_yield(smtp_mock) |
|
51 | and_yield(smtp_mock) |
|
52 |
|
52 | ||
|
53 | post :register, :login => @john_info[:login], |
|
53 | post :register, :login => @john_info[:login], |
|
54 | :full_name => @john_info[:full_name], |
|
54 | :full_name => @john_info[:full_name], |
|
55 | :email => @john_info[:email] |
|
55 | :email => @john_info[:email] |
|
56 |
|
56 | ||
|
57 | response.should render_template('users/new_splash') |
|
57 | response.should render_template('users/new_splash') |
|
58 | end |
|
58 | end |
|
59 |
|
59 | ||
|
60 | it "should create unactivated user and return error page when e-mail sending error" do |
|
60 | it "should create unactivated user and return error page when e-mail sending error" do |
|
61 | User.should_receive(:new).with(any_args()).and_return(@john) |
|
61 | User.should_receive(:new).with(any_args()).and_return(@john) |
|
62 | @john.should_receive(:activated=).with(false) |
|
62 | @john.should_receive(:activated=).with(false) |
|
63 | @john.should_receive(:valid?).and_return(true) |
|
63 | @john.should_receive(:valid?).and_return(true) |
|
64 | @john.should_receive(:save).and_return(true) |
|
64 | @john.should_receive(:save).and_return(true) |
|
65 |
|
65 | ||
|
66 | smtp_mock = mock("smtp") |
|
66 | smtp_mock = mock("smtp") |
|
67 | smtp_mock.should_receive(:send_message). |
|
67 | smtp_mock.should_receive(:send_message). |
|
68 | and_throw(:error) |
|
68 | and_throw(:error) |
|
69 |
|
69 | ||
|
70 | Net::SMTP.should_receive(:start). |
|
70 | Net::SMTP.should_receive(:start). |
|
71 | with(any_args()). |
|
71 | with(any_args()). |
|
72 | and_yield(smtp_mock) |
|
72 | and_yield(smtp_mock) |
|
73 |
|
73 | ||
|
74 | post :register, :login => @john_info[:login], |
|
74 | post :register, :login => @john_info[:login], |
|
75 | :full_name => @john_info[:full_name], |
|
75 | :full_name => @john_info[:full_name], |
|
76 | :email => @john_info[:email] |
|
76 | :email => @john_info[:email] |
|
77 |
|
77 | ||
|
78 | response.should render_template('users/email_error') |
|
78 | response.should render_template('users/email_error') |
|
79 | end |
|
79 | end |
|
80 |
|
80 | ||
|
81 | it "should activate user with valid activation key" do |
|
81 | it "should activate user with valid activation key" do |
|
82 | login = @john_info[:login] |
|
82 | login = @john_info[:login] |
|
83 | User.should_receive(:find_by_login). |
|
83 | User.should_receive(:find_by_login). |
|
84 | with(login). |
|
84 | with(login). |
|
85 | and_return(@john) |
|
85 | and_return(@john) |
|
|
86 | + User.should_not_receive(:find_by_email) | ||
|
86 | @john.should_receive(:valid?).and_return(true) |
|
87 | @john.should_receive(:valid?).and_return(true) |
|
87 | @john.should_receive(:activated=).with(true) |
|
88 | @john.should_receive(:activated=).with(true) |
|
88 | @john.should_receive(:save).and_return(true) |
|
89 | @john.should_receive(:save).and_return(true) |
|
89 |
|
90 | ||
|
90 | get :confirm, :login => login, :activation => @john_activation_key |
|
91 | get :confirm, :login => login, :activation => @john_activation_key |
|
91 |
|
92 | ||
|
92 | response.should render_template('users/confirm') |
|
93 | response.should render_template('users/confirm') |
|
93 | end |
|
94 | end |
|
94 |
|
95 | ||
|
95 | end |
|
96 | end |
You need to be logged in to leave comments.
Login now