Description:
merge from algo-bm
Commit status:
[Not Reviewed]
References:
merge java
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r405:f3f0ea35cff3 - - 1 file changed: 43 inserted, 4 deleted

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