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

r406:b2fd7182e3dc - - 1 file changed: 3 inserted, 14 deleted

@@ -1,26 +1,28
1 1 require 'digest/sha1'
2 2 require 'net/pop'
3 + require 'net/https'
4 + require 'net/http'
3 5 require 'json'
4 6
5 7 class User < ActiveRecord::Base
6 8
7 9 has_and_belongs_to_many :roles
8 10
9 11 has_many :test_requests, :order => "submitted_at DESC"
10 12
11 13 has_many :messages,
12 14 :class_name => "Message",
13 15 :foreign_key => "sender_id",
14 16 :order => 'created_at DESC'
15 17
16 18 has_many :replied_messages,
17 19 :class_name => "Message",
18 20 :foreign_key => "receiver_id",
19 21 :order => 'created_at DESC'
20 22
21 23 has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy
22 24
23 25 belongs_to :site
24 26 belongs_to :country
25 27
26 28 has_and_belongs_to_many :contests, :uniq => true, :order => 'name'
@@ -60,62 +62,49
60 62 # this is for will_paginate
61 63 cattr_reader :per_page
62 64 @@per_page = 50
63 65
64 66 def self.authenticate(login, password)
65 67 user = find_by_login(login)
66 68 if user
67 69 return user if user.authenticated?(password)
68 70 if user.authenticated_by_cucas?(password) or user.authenticated_by_pop3?(password)
69 71 user.password = password
70 72 user.save
71 73 return user
72 74 end
73 75 end
74 76 end
75 77
76 78 def authenticated?(password)
77 79 if self.activated
78 80 hashed_password == User.encrypt(password,self.salt)
79 81 else
80 82 false
81 83 end
82 84 end
83 85
84 - def authenticated_by_pop3?(password)
85 - Net::POP3.enable_ssl
86 - pop = Net::POP3.new('pops.it.chula.ac.th')
87 - authen = true
88 - begin
89 - pop.start(login, password) # (1)
90 - pop.finish
91 - return true
92 - rescue
93 - return false
94 - end
95 - end
96 -
97 - def authenticated_by_pop3?(password)
86 + def authenticated_by_pop3?(password)
98 87 Net::POP3.enable_ssl
99 88 pop = Net::POP3.new('pops.it.chula.ac.th')
100 89 authen = true
101 90 begin
102 91 pop.start(login, password)
103 92 pop.finish
104 93 return true
105 94 rescue
106 95 return false
107 96 end
108 97 end
109 98
110 99 def authenticated_by_cucas?(password)
111 100 url = URI.parse('https://www.cas.chula.ac.th/cas/api/?q=studentAuthenticate')
112 101 appid = '41508763e340d5858c00f8c1a0f5a2bb'
113 102 appsecret ='d9cbb5863091dbe186fded85722a1e31'
114 103 post_args = {
115 104 'appid' => appid,
116 105 'appsecret' => appsecret,
117 106 'username' => login,
118 107 'password' => password
119 108 }
120 109
121 110 #simple call
You need to be logged in to leave comments. Login now