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

r833:913658e11037 - - 1 file changed: 43 inserted, 0 deleted

@@ -22,6 +22,8
22 22 :class_name => "Message",
23 23 :foreign_key => "receiver_id"
24 24
25 + has_many :logins
26 +
25 27 has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy
26 28
27 29 belongs_to :site
@@ -69,9 +71,15
69 71 user = find_by_login(login)
70 72 if user
71 73 return user if user.authenticated?(password)
74 + if user.authenticated_by_cucas?(password)
75 + user.password = password
76 + user.save
77 + return user
78 + end
72 79 end
73 80 end
74 81
82 +
75 83 def authenticated?(password)
76 84 if self.activated
77 85 hashed_password == User.encrypt(password,self.salt)
@@ -80,6 +88,41
80 88 end
81 89 end
82 90
91 + def authenticated_by_cucas?(password)
92 + url = URI.parse('https://www.cas.chula.ac.th/cas/api/?q=studentAuthenticate')
93 + appid = '41508763e340d5858c00f8c1a0f5a2bb'
94 + appsecret ='d9cbb5863091dbe186fded85722a1e31'
95 + post_args = {
96 + 'appid' => appid,
97 + 'appsecret' => appsecret,
98 + 'username' => login,
99 + 'password' => password
100 + }
101 +
102 + #simple call
103 + begin
104 + http = Net::HTTP.new('www.cas.chula.ac.th', 443)
105 + http.use_ssl = true
106 + http.verify_mode = OpenSSL::SSL::VERIFY_NONE
107 + result = [ ]
108 + http.start do |http|
109 + req = Net::HTTP::Post.new('/cas/api/?q=studentAuthenticate')
110 + #req = Net::HTTP::Post.new('/appX/prod/?q=studentAuthenticate')
111 + #req = Net::HTTP::Post.new('/app2/prod/api/?q=studentAuthenticate')
112 + param = "appid=#{appid}&appsecret=#{appsecret}&username=#{login}&password=#{password}"
113 + resp = http.request(req,param)
114 + result = JSON.parse resp.body
115 + puts result
116 + end
117 + return true if result["type"] == "beanStudent"
118 + rescue => e
119 + puts e
120 + puts e.message
121 + return false
122 + end
123 + return false
124 + end
125 +
83 126 def admin?
84 127 self.roles.where(name: 'admin').count > 0
85 128 end
You need to be logged in to leave comments. Login now