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