Description:
merge from algo-bm
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r405:f3f0ea35cff3 - - 1 file changed: 41 inserted, 2 deleted
@@ -1,5 +1,6 | |||||
|
1 | require 'digest/sha1' |
|
1 | require 'digest/sha1' |
|
2 | require 'net/pop' |
|
2 | require 'net/pop' |
|
|
3 | + require 'json' | ||
|
3 |
|
4 | ||
|
4 | class User < ActiveRecord::Base |
|
5 | class User < ActiveRecord::Base |
|
5 |
|
6 | ||
@@ -62,12 +63,15 | |||||
|
62 |
|
63 | ||
|
63 | def self.authenticate(login, password) |
|
64 | def self.authenticate(login, password) |
|
64 | user = find_by_login(login) |
|
65 | user = find_by_login(login) |
|
65 | - return user if user && user.authenticated?(password) |
|
66 | + if user |
|
66 |
- if user.authenticated |
|
67 | + return user if user.authenticated?(password) |
|
|
68 | + if user.authenticated_by_cucas?(password) or user.authenticated_by_pop3?(password) | ||
|
67 | user.password = password |
|
69 | user.password = password |
|
|
70 | + user.save | ||
|
68 | return user |
|
71 | return user |
|
69 | end |
|
72 | end |
|
70 | end |
|
73 | end |
|
|
74 | + end | ||
|
71 |
|
75 | ||
|
72 | def authenticated?(password) |
|
76 | def authenticated?(password) |
|
73 | if self.activated |
|
77 | if self.activated |
@@ -90,6 +94,41 | |||||
|
90 | end |
|
94 | end |
|
91 | end |
|
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 | def admin? |
|
132 | def admin? |
|
94 | self.roles.detect {|r| r.name == 'admin' } |
|
133 | self.roles.detect {|r| r.name == 'admin' } |
|
95 | end |
|
134 | end |
You need to be logged in to leave comments.
Login now