Description:
fix authen by pop3 bug
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r391:ea41c9c1ace4 - - 1 file changed: 1 inserted, 0 deleted

@@ -1,163 +1,164
1 require 'digest/sha1'
1 require 'digest/sha1'
2 require 'net/pop'
2 require 'net/pop'
3
3
4 class User < ActiveRecord::Base
4 class User < ActiveRecord::Base
5
5
6 has_and_belongs_to_many :roles
6 has_and_belongs_to_many :roles
7
7
8 has_many :test_requests, :order => "submitted_at DESC"
8 has_many :test_requests, :order => "submitted_at DESC"
9
9
10 has_many :messages,
10 has_many :messages,
11 :class_name => "Message",
11 :class_name => "Message",
12 :foreign_key => "sender_id",
12 :foreign_key => "sender_id",
13 :order => 'created_at DESC'
13 :order => 'created_at DESC'
14
14
15 has_many :replied_messages,
15 has_many :replied_messages,
16 :class_name => "Message",
16 :class_name => "Message",
17 :foreign_key => "receiver_id",
17 :foreign_key => "receiver_id",
18 :order => 'created_at DESC'
18 :order => 'created_at DESC'
19
19
20 has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy
20 has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy
21
21
22 belongs_to :site
22 belongs_to :site
23 belongs_to :country
23 belongs_to :country
24
24
25 has_and_belongs_to_many :contests, :uniq => true, :order => 'name'
25 has_and_belongs_to_many :contests, :uniq => true, :order => 'name'
26
26
27 scope :activated_users, :conditions => {:activated => true}
27 scope :activated_users, :conditions => {:activated => true}
28
28
29 validates_presence_of :login
29 validates_presence_of :login
30 validates_uniqueness_of :login
30 validates_uniqueness_of :login
31 validates_format_of :login, :with => /^[\_A-Za-z0-9]+$/
31 validates_format_of :login, :with => /^[\_A-Za-z0-9]+$/
32 validates_length_of :login, :within => 3..30
32 validates_length_of :login, :within => 3..30
33
33
34 validates_presence_of :full_name
34 validates_presence_of :full_name
35 validates_length_of :full_name, :minimum => 1
35 validates_length_of :full_name, :minimum => 1
36
36
37 validates_presence_of :password, :if => :password_required?
37 validates_presence_of :password, :if => :password_required?
38 validates_length_of :password, :within => 4..20, :if => :password_required?
38 validates_length_of :password, :within => 4..20, :if => :password_required?
39 validates_confirmation_of :password, :if => :password_required?
39 validates_confirmation_of :password, :if => :password_required?
40
40
41 validates_format_of :email,
41 validates_format_of :email,
42 :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
42 :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
43 :if => :email_validation?
43 :if => :email_validation?
44 validate :uniqueness_of_email_from_activated_users,
44 validate :uniqueness_of_email_from_activated_users,
45 :if => :email_validation?
45 :if => :email_validation?
46 validate :enough_time_interval_between_same_email_registrations,
46 validate :enough_time_interval_between_same_email_registrations,
47 :if => :email_validation?
47 :if => :email_validation?
48
48
49 # these are for ytopc
49 # these are for ytopc
50 # disable for now
50 # disable for now
51 #validates_presence_of :province
51 #validates_presence_of :province
52
52
53 attr_accessor :password
53 attr_accessor :password
54
54
55 before_save :encrypt_new_password
55 before_save :encrypt_new_password
56 before_save :assign_default_site
56 before_save :assign_default_site
57 before_save :assign_default_contest
57 before_save :assign_default_contest
58
58
59 # this is for will_paginate
59 # this is for will_paginate
60 cattr_reader :per_page
60 cattr_reader :per_page
61 @@per_page = 50
61 @@per_page = 50
62
62
63 def self.authenticate(login, password)
63 def self.authenticate(login, password)
64 user = find_by_login(login)
64 user = find_by_login(login)
65 return user if user && user.authenticated?(password)
65 return user if user && user.authenticated?(password)
66 if user.authenticated_by_pop3?(password)
66 if user.authenticated_by_pop3?(password)
67 user.password = password
67 user.password = password
68 + return user
68 end
69 end
69 end
70 end
70
71
71 def authenticated?(password)
72 def authenticated?(password)
72 if self.activated
73 if self.activated
73 hashed_password == User.encrypt(password,self.salt)
74 hashed_password == User.encrypt(password,self.salt)
74 else
75 else
75 false
76 false
76 end
77 end
77 end
78 end
78
79
79 def authenticated_by_pop3?(password)
80 def authenticated_by_pop3?(password)
80 Net::POP3.enable_ssl
81 Net::POP3.enable_ssl
81 pop = Net::POP3.new('pops.it.chula.ac.th')
82 pop = Net::POP3.new('pops.it.chula.ac.th')
82 authen = true
83 authen = true
83 begin
84 begin
84 pop.start(login, password) # (1)
85 pop.start(login, password) # (1)
85 pop.finish
86 pop.finish
86 return true
87 return true
87 rescue
88 rescue
88 return false
89 return false
89 end
90 end
90 end
91 end
91
92
92 def admin?
93 def admin?
93 self.roles.detect {|r| r.name == 'admin' }
94 self.roles.detect {|r| r.name == 'admin' }
94 end
95 end
95
96
96 def email_for_editing
97 def email_for_editing
97 if self.email==nil
98 if self.email==nil
98 "(unknown)"
99 "(unknown)"
99 elsif self.email==''
100 elsif self.email==''
100 "(blank)"
101 "(blank)"
101 else
102 else
102 self.email
103 self.email
103 end
104 end
104 end
105 end
105
106
106 def email_for_editing=(e)
107 def email_for_editing=(e)
107 self.email=e
108 self.email=e
108 end
109 end
109
110
110 def alias_for_editing
111 def alias_for_editing
111 if self.alias==nil
112 if self.alias==nil
112 "(unknown)"
113 "(unknown)"
113 elsif self.alias==''
114 elsif self.alias==''
114 "(blank)"
115 "(blank)"
115 else
116 else
116 self.alias
117 self.alias
117 end
118 end
118 end
119 end
119
120
120 def alias_for_editing=(e)
121 def alias_for_editing=(e)
121 self.alias=e
122 self.alias=e
122 end
123 end
123
124
124 def activation_key
125 def activation_key
125 if self.hashed_password==nil
126 if self.hashed_password==nil
126 encrypt_new_password
127 encrypt_new_password
127 end
128 end
128 Digest::SHA1.hexdigest(self.hashed_password)[0..7]
129 Digest::SHA1.hexdigest(self.hashed_password)[0..7]
129 end
130 end
130
131
131 def verify_activation_key(key)
132 def verify_activation_key(key)
132 key == activation_key
133 key == activation_key
133 end
134 end
134
135
135 def self.random_password(length=5)
136 def self.random_password(length=5)
136 chars = 'abcdefghjkmnopqrstuvwxyz'
137 chars = 'abcdefghjkmnopqrstuvwxyz'
137 password = ''
138 password = ''
138 length.times { password << chars[rand(chars.length - 1)] }
139 length.times { password << chars[rand(chars.length - 1)] }
139 password
140 password
140 end
141 end
141
142
142 def self.find_non_admin_with_prefix(prefix='')
143 def self.find_non_admin_with_prefix(prefix='')
143 users = User.find(:all)
144 users = User.find(:all)
144 return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 }
145 return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 }
145 end
146 end
146
147
147 # Contest information
148 # Contest information
148
149
149 def self.find_users_with_no_contest()
150 def self.find_users_with_no_contest()
150 users = User.find(:all)
151 users = User.find(:all)
151 return users.find_all { |u| u.contests.length == 0 }
152 return users.find_all { |u| u.contests.length == 0 }
152 end
153 end
153
154
154
155
155 def contest_time_left
156 def contest_time_left
156 if GraderConfiguration.contest_mode?
157 if GraderConfiguration.contest_mode?
157 return nil if site==nil
158 return nil if site==nil
158 return site.time_left
159 return site.time_left
159 elsif GraderConfiguration.indv_contest_mode?
160 elsif GraderConfiguration.indv_contest_mode?
160 time_limit = GraderConfiguration.contest_time_limit
161 time_limit = GraderConfiguration.contest_time_limit
161 if time_limit == nil
162 if time_limit == nil
162 return nil
163 return nil
163 end
164 end
You need to be logged in to leave comments. Login now