Description:
authenticates through programming.in.th
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r770:572d003cd44c - - 3 files changed: 59 inserted, 1 deleted
@@ -0,0 +1,33 | |||
|
1 | + # Authentication and user imports through programming.in.th web request | |
|
2 | + require 'net/http' | |
|
3 | + require 'uri' | |
|
4 | + require 'json' | |
|
5 | + | |
|
6 | + class ProgrammingAuthenticator | |
|
7 | + PROGRAMMING_AUTHEN_URL = "https://programming.in.th/authen.php" | |
|
8 | + | |
|
9 | + def find_or_create_user(result) | |
|
10 | + user = User.find_by(login: result['username']) | |
|
11 | + if not user | |
|
12 | + user = User.new(login: result['username'], | |
|
13 | + full_name: result['firstname'] + ' ' + result['surname'], | |
|
14 | + alias: result['display'], | |
|
15 | + email: result['email']) | |
|
16 | + user.password = User.random_password | |
|
17 | + user.save | |
|
18 | + end | |
|
19 | + return user | |
|
20 | + end | |
|
21 | + | |
|
22 | + def authenticate(login, password) | |
|
23 | + uri = URI(PROGRAMMING_AUTHEN_URL) | |
|
24 | + result = Net::HTTP.post_form(uri, 'username' => login, 'password' => password) | |
|
25 | + request_result = JSON.parse(result.body) | |
|
26 | + | |
|
27 | + if request_result.fetch('status', 'incorrect') == 'OK' | |
|
28 | + return find_or_create_user(request_result) | |
|
29 | + else | |
|
30 | + return nil | |
|
31 | + end | |
|
32 | + end | |
|
33 | + end |
@@ -1,63 +1,85 | |||
|
1 | 1 | class LoginController < ApplicationController |
|
2 | 2 | |
|
3 | + @@authenticators = [] | |
|
4 | + | |
|
3 | 5 | def index |
|
4 | 6 | # show login screen |
|
5 | 7 | reset_session |
|
6 | 8 | redirect_to :controller => 'main', :action => 'login' |
|
7 | 9 | end |
|
8 | 10 | |
|
9 | 11 | def login |
|
10 |
- user = |
|
|
12 | + user = get_authenticated_user(params[:login], params[:password]) | |
|
11 | 13 | unless user |
|
12 | 14 | flash[:notice] = 'Wrong password' |
|
13 | 15 | redirect_to :controller => 'main', :action => 'login' |
|
14 | 16 | return |
|
15 | 17 | end |
|
16 | 18 | |
|
17 | 19 | if (!GraderConfiguration['right.bypass_agreement']) and (!params[:accept_agree]) and !user.admin? |
|
18 | 20 | flash[:notice] = 'You must accept the agreement before logging in' |
|
19 | 21 | redirect_to :controller => 'main', :action => 'login' |
|
20 | 22 | return |
|
21 | 23 | end |
|
22 | 24 | |
|
23 | 25 | #process logging in |
|
24 | 26 | session[:user_id] = user.id |
|
25 | 27 | session[:admin] = user.admin? |
|
26 | 28 | |
|
27 | 29 | # clear forced logout flag for multicontests contest change |
|
28 | 30 | if GraderConfiguration.multicontests? |
|
29 | 31 | contest_stat = user.contest_stat |
|
30 | 32 | if contest_stat.respond_to? :forced_logout |
|
31 | 33 | if contest_stat.forced_logout |
|
32 | 34 | contest_stat.forced_logout = false |
|
33 | 35 | contest_stat.save |
|
34 | 36 | end |
|
35 | 37 | end |
|
36 | 38 | end |
|
37 | 39 | |
|
38 | 40 | #save login information |
|
39 | 41 | Login.create(user_id: user.id, ip_address: request.remote_ip) |
|
40 | 42 | |
|
41 | 43 | redirect_to :controller => 'main', :action => 'list' |
|
42 | 44 | end |
|
43 | 45 | |
|
44 | 46 | def site_login |
|
45 | 47 | begin |
|
46 | 48 | site = Site.find(params[:login][:site_id]) |
|
47 | 49 | rescue ActiveRecord::RecordNotFound |
|
48 | 50 | site = nil |
|
49 | 51 | end |
|
50 | 52 | if site==nil |
|
51 | 53 | flash[:notice] = 'Wrong site' |
|
52 | 54 | redirect_to :controller => 'main', :action => 'login' and return |
|
53 | 55 | end |
|
54 | 56 | if (site.password) and (site.password == params[:login][:password]) |
|
55 | 57 | session[:site_id] = site.id |
|
56 | 58 | redirect_to :controller => 'site', :action => 'index' |
|
57 | 59 | else |
|
58 | 60 | flash[:notice] = 'Wrong site password' |
|
59 | 61 | redirect_to :controller => 'site', :action => 'login' |
|
60 | 62 | end |
|
61 | 63 | end |
|
62 | 64 | |
|
65 | + def self.add_authenticator(authenticator) | |
|
66 | + @@authenticators << authenticator | |
|
63 | 67 | end |
|
68 | + | |
|
69 | + protected | |
|
70 | + | |
|
71 | + def get_authenticated_user(login, password) | |
|
72 | + if @@authenticators.empty? | |
|
73 | + return User.authenticate(login, password) | |
|
74 | + else | |
|
75 | + user = nil | |
|
76 | + @@authenticators.each do |authenticator| | |
|
77 | + if not user | |
|
78 | + user = authenticator.authenticate(login, password) | |
|
79 | + end | |
|
80 | + end | |
|
81 | + return user | |
|
82 | + end | |
|
83 | + end | |
|
84 | + | |
|
85 | + end |
@@ -1,30 +1,33 | |||
|
1 | 1 | # If you want to manage graders through web interface, set the path to |
|
2 | 2 | # the grader directory below. This dir is where raw, ev, ev-exam, |
|
3 | 3 | # scripts reside. All grader scripts will be in |
|
4 | 4 | # #{GRADER_ROOT_DIR}/scripts. |
|
5 | 5 | GRADER_ROOT_DIR = '' |
|
6 | 6 | |
|
7 | 7 | # These are where inputs and outputs of test requests are stored |
|
8 | 8 | TEST_REQUEST_INPUT_FILE_DIR = (Rails.root + 'data/test_request/input').to_s |
|
9 | 9 | TEST_REQUEST_OUTPUT_FILE_DIR = (Rails.root + 'data/test_request/output').to_s |
|
10 | 10 | |
|
11 | 11 | # To use ANALYSIS MODE, provide the testcases/testruns breakdown, |
|
12 | 12 | # and the directory of the grading result (usually in judge's dir). |
|
13 | 13 | TASK_GRADING_INFO_FILENAME = Rails.root + 'config/tasks.yml' |
|
14 | 14 | |
|
15 | 15 | # TODO: change this to where results are kept. |
|
16 | 16 | GRADING_RESULT_DIR = 'RESULT-DIR' |
|
17 | 17 | |
|
18 | 18 | # Change this to allow importing testdata into database as test-pairs. |
|
19 | 19 | # This is mainly for Code Jom contest. |
|
20 | 20 | ALLOW_TEST_PAIR_IMPORT = false |
|
21 | 21 | |
|
22 | 22 | # Uncomment so that the system validates user e-mails |
|
23 | 23 | # VALIDATE_USER_EMAILS = true |
|
24 | 24 | |
|
25 | 25 | # Uncomment so that Apache X-Sendfile is used when delivering files |
|
26 | 26 | # (e.g., in /tasks/view). |
|
27 | 27 | # USE_APACHE_XSENDFILE = true |
|
28 | 28 | |
|
29 | 29 | # Uncomment so that configuration is read only once when the server is loaded |
|
30 | 30 | # CONFIGURATION_CACHE_ENABLED = true |
|
31 | + | |
|
32 | + # Uncomment to allow authentication and user import from programming.in.th | |
|
33 | + # LoginController.add_authenticator(ProgrammingAuthenticator.new) |
You need to be logged in to leave comments.
Login now