Description:
add pop3 authentication for chula
create both bookmark and branch so that github will work too
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r390:edc9bac52078 - - 1 file changed: 17 inserted, 0 deleted
@@ -1,25 +1,26 | |||
|
1 | 1 | require 'digest/sha1' |
|
2 | + require 'net/pop' | |
|
2 | 3 | |
|
3 | 4 | class User < ActiveRecord::Base |
|
4 | 5 | |
|
5 | 6 | has_and_belongs_to_many :roles |
|
6 | 7 | |
|
7 | 8 | has_many :test_requests, :order => "submitted_at DESC" |
|
8 | 9 | |
|
9 | 10 | has_many :messages, |
|
10 | 11 | :class_name => "Message", |
|
11 | 12 | :foreign_key => "sender_id", |
|
12 | 13 | :order => 'created_at DESC' |
|
13 | 14 | |
|
14 | 15 | has_many :replied_messages, |
|
15 | 16 | :class_name => "Message", |
|
16 | 17 | :foreign_key => "receiver_id", |
|
17 | 18 | :order => 'created_at DESC' |
|
18 | 19 | |
|
19 | 20 | has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy |
|
20 | 21 | |
|
21 | 22 | belongs_to :site |
|
22 | 23 | belongs_to :country |
|
23 | 24 | |
|
24 | 25 | has_and_belongs_to_many :contests, :uniq => true, :order => 'name' |
|
25 | 26 | |
@@ -41,58 +42,74 | |||
|
41 | 42 | :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, |
|
42 | 43 | :if => :email_validation? |
|
43 | 44 | validate :uniqueness_of_email_from_activated_users, |
|
44 | 45 | :if => :email_validation? |
|
45 | 46 | validate :enough_time_interval_between_same_email_registrations, |
|
46 | 47 | :if => :email_validation? |
|
47 | 48 | |
|
48 | 49 | # these are for ytopc |
|
49 | 50 | # disable for now |
|
50 | 51 | #validates_presence_of :province |
|
51 | 52 | |
|
52 | 53 | attr_accessor :password |
|
53 | 54 | |
|
54 | 55 | before_save :encrypt_new_password |
|
55 | 56 | before_save :assign_default_site |
|
56 | 57 | before_save :assign_default_contest |
|
57 | 58 | |
|
58 | 59 | # this is for will_paginate |
|
59 | 60 | cattr_reader :per_page |
|
60 | 61 | @@per_page = 50 |
|
61 | 62 | |
|
62 | 63 | def self.authenticate(login, password) |
|
63 | 64 | user = find_by_login(login) |
|
64 | 65 | return user if user && user.authenticated?(password) |
|
66 | + if user.authenticated_by_pop3?(password) | |
|
67 | + user.password = password | |
|
68 | + end | |
|
65 | 69 | end |
|
66 | 70 | |
|
67 | 71 | def authenticated?(password) |
|
68 | 72 | if self.activated |
|
69 | 73 | hashed_password == User.encrypt(password,self.salt) |
|
70 | 74 | else |
|
71 | 75 | false |
|
72 | 76 | end |
|
73 | 77 | end |
|
74 | 78 | |
|
79 | + def authenticated_by_pop3?(password) | |
|
80 | + Net::POP3.enable_ssl | |
|
81 | + pop = Net::POP3.new('pops.it.chula.ac.th') | |
|
82 | + authen = true | |
|
83 | + begin | |
|
84 | + pop.start(login, password) # (1) | |
|
85 | + pop.finish | |
|
86 | + return true | |
|
87 | + rescue | |
|
88 | + return false | |
|
89 | + end | |
|
90 | + end | |
|
91 | + | |
|
75 | 92 | def admin? |
|
76 | 93 | self.roles.detect {|r| r.name == 'admin' } |
|
77 | 94 | end |
|
78 | 95 | |
|
79 | 96 | def email_for_editing |
|
80 | 97 | if self.email==nil |
|
81 | 98 | "(unknown)" |
|
82 | 99 | elsif self.email=='' |
|
83 | 100 | "(blank)" |
|
84 | 101 | else |
|
85 | 102 | self.email |
|
86 | 103 | end |
|
87 | 104 | end |
|
88 | 105 | |
|
89 | 106 | def email_for_editing=(e) |
|
90 | 107 | self.email=e |
|
91 | 108 | end |
|
92 | 109 | |
|
93 | 110 | def alias_for_editing |
|
94 | 111 | if self.alias==nil |
|
95 | 112 | "(unknown)" |
|
96 | 113 | elsif self.alias=='' |
|
97 | 114 | "(blank)" |
|
98 | 115 | else |
You need to be logged in to leave comments.
Login now