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

r395:52cd3037bb5e - - 1 file changed: 2 inserted, 1 deleted

@@ -18,98 +18,99
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 && user.authenticated_by_pop3?(password)
67 user.password = password
67 user.password = password
68 + user.save
68 return user
69 return user
69 end
70 end
70 end
71 end
71
72
72 def authenticated?(password)
73 def authenticated?(password)
73 if self.activated
74 if self.activated
74 hashed_password == User.encrypt(password,self.salt)
75 hashed_password == User.encrypt(password,self.salt)
75 else
76 else
76 false
77 false
77 end
78 end
78 end
79 end
79
80
80 def authenticated_by_pop3?(password)
81 def authenticated_by_pop3?(password)
81 Net::POP3.enable_ssl
82 Net::POP3.enable_ssl
82 pop = Net::POP3.new('pops.it.chula.ac.th')
83 pop = Net::POP3.new('pops.it.chula.ac.th')
83 authen = true
84 authen = true
84 begin
85 begin
85 pop.start(login, password) # (1)
86 pop.start(login, password) # (1)
86 pop.finish
87 pop.finish
87 return true
88 return true
88 rescue
89 rescue
89 return false
90 return false
90 end
91 end
91 end
92 end
92
93
93 def admin?
94 def admin?
94 self.roles.detect {|r| r.name == 'admin' }
95 self.roles.detect {|r| r.name == 'admin' }
95 end
96 end
96
97
97 def email_for_editing
98 def email_for_editing
98 if self.email==nil
99 if self.email==nil
99 "(unknown)"
100 "(unknown)"
100 elsif self.email==''
101 elsif self.email==''
101 "(blank)"
102 "(blank)"
102 else
103 else
103 self.email
104 self.email
104 end
105 end
105 end
106 end
106
107
107 def email_for_editing=(e)
108 def email_for_editing=(e)
108 self.email=e
109 self.email=e
109 end
110 end
110
111
111 def alias_for_editing
112 def alias_for_editing
112 if self.alias==nil
113 if self.alias==nil
113 "(unknown)"
114 "(unknown)"
114 elsif self.alias==''
115 elsif self.alias==''
115 "(blank)"
116 "(blank)"
You need to be logged in to leave comments. Login now