Description:
merge with upstream
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r779:df983f8fc960 - - 8 files changed: 73 inserted, 10 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,5 +1,7 | |||
|
1 | 1 | class LoginController < ApplicationController |
|
2 | 2 | |
|
3 | + @@authenticators = [] | |
|
4 | + | |
|
3 | 5 | def index |
|
4 | 6 | # show login screen |
|
5 | 7 | reset_session |
@@ -7,7 +9,7 | |||
|
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' |
@@ -64,4 +66,24 | |||
|
64 | 66 | redirect_to root_path |
|
65 | 67 | end |
|
66 | 68 | |
|
69 | + def self.add_authenticator(authenticator) | |
|
70 | + @@authenticators << authenticator | |
|
67 | 71 | end |
|
72 | + | |
|
73 | + protected | |
|
74 | + | |
|
75 | + def get_authenticated_user(login, password) | |
|
76 | + if @@authenticators.empty? | |
|
77 | + return User.authenticate(login, password) | |
|
78 | + else | |
|
79 | + user = User.authenticate(login, password) | |
|
80 | + @@authenticators.each do |authenticator| | |
|
81 | + if not user | |
|
82 | + user = authenticator.authenticate(login, password) | |
|
83 | + end | |
|
84 | + end | |
|
85 | + return user | |
|
86 | + end | |
|
87 | + end | |
|
88 | + | |
|
89 | + end |
@@ -2,10 +2,6 | |||
|
2 | 2 | |
|
3 | 3 | before_action :admin_authorization |
|
4 | 4 | |
|
5 | - #NOTE: ghost from the past? | |
|
6 | - #before_action :testcase_authorization, only: [:show_testcase] | |
|
7 | - | |
|
8 | - | |
|
9 | 5 | in_place_edit_for :problem, :name |
|
10 | 6 | in_place_edit_for :problem, :full_name |
|
11 | 7 | in_place_edit_for :problem, :full_score |
@@ -26,7 +22,7 | |||
|
26 | 22 | |
|
27 | 23 | def create |
|
28 | 24 | @problem = Problem.new(problem_params) |
|
29 |
- @description = Description.new( |
|
|
25 | + @description = Description.new(description_params) | |
|
30 | 26 | if @description.body!='' |
|
31 | 27 | if !@description.save |
|
32 | 28 | render :action => new and return |
@@ -305,7 +301,7 | |||
|
305 | 301 | end |
|
306 | 302 | |
|
307 | 303 | def description_params |
|
308 | - params.require(:description).permit(:body, :markdown) | |
|
304 | + params.require(:description).permit(:body, :markdowned) | |
|
309 | 305 | end |
|
310 | 306 | |
|
311 | 307 | end |
@@ -127,9 +127,11 | |||
|
127 | 127 | end |
|
128 | 128 | |
|
129 | 129 | def assign_language |
|
130 | + if self.language == nil | |
|
130 | 131 | self.language = Submission.find_language_in_source(self.source, |
|
131 | 132 | self.source_filename) |
|
132 | 133 | end |
|
134 | + end | |
|
133 | 135 | |
|
134 | 136 | # validation codes |
|
135 | 137 | def must_specify_language |
@@ -139,7 +141,7 | |||
|
139 | 141 | return if self.problem!=nil and self.problem.output_only |
|
140 | 142 | |
|
141 | 143 | if self.language==nil |
|
142 |
- errors.add('source',"Cannot detect language. Did you submit a correct source file?") |
|
|
144 | + errors.add('source',"Cannot detect language. Did you submit a correct source file?") | |
|
143 | 145 | end |
|
144 | 146 | end |
|
145 | 147 |
@@ -40,7 +40,7 | |||
|
40 | 40 | validates_length_of :full_name, :minimum => 1 |
|
41 | 41 | |
|
42 | 42 | validates_presence_of :password, :if => :password_required? |
|
43 |
- validates_length_of :password, :within => 4.. |
|
|
43 | + validates_length_of :password, :within => 4..50, :if => :password_required? | |
|
44 | 44 | validates_confirmation_of :password, :if => :password_required? |
|
45 | 45 | |
|
46 | 46 | validates_format_of :email, |
@@ -28,3 +28,6 | |||
|
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) |
@@ -102,6 +102,14 | |||
|
102 | 102 | :default_value => 'false', |
|
103 | 103 | :description => 'When true, any user can view/download test data' |
|
104 | 104 | }, |
|
105 | + | |
|
106 | + { | |
|
107 | + :key => 'system.online_registration', | |
|
108 | + :value_type => 'boolean', | |
|
109 | + :default_value => 'false', | |
|
110 | + :description => 'This option enables online registration.' | |
|
111 | + }, | |
|
112 | + | |
|
105 | 113 | # If Configuration['system.online_registration'] is true, the |
|
106 | 114 | # system allows online registration, and will use these |
|
107 | 115 | # information for sending confirmation emails. |
You need to be logged in to leave comments.
Login now