Description:
Merge pull request #26 from cafe-grader-team/master
merge edit from upstream
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r777:e61e522c673f - - 8 files changed: 82 inserted, 8 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 | class LoginController < ApplicationController |
|
1 | class LoginController < ApplicationController |
|
2 |
|
2 | ||
|
|
3 | + @@authenticators = [] | ||
|
|
4 | + | ||
|
3 | def index |
|
5 | def index |
|
4 | # show login screen |
|
6 | # show login screen |
|
5 | reset_session |
|
7 | reset_session |
@@ -7,7 +9,7 | |||||
|
7 | end |
|
9 | end |
|
8 |
|
10 | ||
|
9 | def login |
|
11 | def login |
|
10 |
- user = |
|
12 | + user = get_authenticated_user(params[:login], params[:password]) |
|
11 | unless user |
|
13 | unless user |
|
12 | flash[:notice] = 'Wrong password' |
|
14 | flash[:notice] = 'Wrong password' |
|
13 | redirect_to :controller => 'main', :action => 'login' |
|
15 | redirect_to :controller => 'main', :action => 'login' |
@@ -60,4 +62,24 | |||||
|
60 | end |
|
62 | end |
|
61 | end |
|
63 | end |
|
62 |
|
64 | ||
|
|
65 | + def self.add_authenticator(authenticator) | ||
|
|
66 | + @@authenticators << authenticator | ||
|
|
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 = User.authenticate(login, password) | ||
|
|
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 | + | ||
|
63 | end |
|
85 | end |
@@ -29,7 +29,7 | |||||
|
29 |
|
29 | ||
|
30 | def create |
|
30 | def create |
|
31 | @problem = Problem.new(problem_params) |
|
31 | @problem = Problem.new(problem_params) |
|
32 |
- @description = Description.new( |
|
32 | + @description = Description.new(description_params) |
|
33 | if @description.body!='' |
|
33 | if @description.body!='' |
|
34 | if !@description.save |
|
34 | if !@description.save |
|
35 | render :action => new and return |
|
35 | render :action => new and return |
@@ -252,6 +252,10 | |||||
|
252 | ################################## |
|
252 | ################################## |
|
253 | protected |
|
253 | protected |
|
254 |
|
254 | ||
|
|
255 | + def description_params | ||
|
|
256 | + params.require(:description).permit(:body, :markdowned) | ||
|
|
257 | + end | ||
|
|
258 | + | ||
|
255 | def allow_test_pair_import? |
|
259 | def allow_test_pair_import? |
|
256 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
260 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
257 | return ALLOW_TEST_PAIR_IMPORT |
|
261 | return ALLOW_TEST_PAIR_IMPORT |
@@ -127,8 +127,10 | |||||
|
127 | end |
|
127 | end |
|
128 |
|
128 | ||
|
129 | def assign_language |
|
129 | def assign_language |
|
130 | - self.language = Submission.find_language_in_source(self.source, |
|
130 | + if self.language == nil |
|
131 | - self.source_filename) |
|
131 | + self.language = Submission.find_language_in_source(self.source, |
|
|
132 | + self.source_filename) | ||
|
|
133 | + end | ||
|
132 | end |
|
134 | end |
|
133 |
|
135 | ||
|
134 | # validation codes |
|
136 | # validation codes |
@@ -138,8 +140,8 | |||||
|
138 | # for output_only tasks |
|
140 | # for output_only tasks |
|
139 | return if self.problem!=nil and self.problem.output_only |
|
141 | return if self.problem!=nil and self.problem.output_only |
|
140 |
|
142 | ||
|
141 | - if self.language==nil |
|
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 | end |
|
145 | end |
|
144 | end |
|
146 | end |
|
145 |
|
147 |
@@ -40,7 +40,7 | |||||
|
40 | validates_length_of :full_name, :minimum => 1 |
|
40 | validates_length_of :full_name, :minimum => 1 |
|
41 |
|
41 | ||
|
42 | validates_presence_of :password, :if => :password_required? |
|
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 | validates_confirmation_of :password, :if => :password_required? |
|
44 | validates_confirmation_of :password, :if => :password_required? |
|
45 |
|
45 | ||
|
46 | validates_format_of :email, |
|
46 | validates_format_of :email, |
@@ -59,7 +59,9 | |||||
|
59 | %span{aria: {hidden: 'true'}, data: {dismiss: 'modal'}} × |
|
59 | %span{aria: {hidden: 'true'}, data: {dismiss: 'modal'}} × |
|
60 | %h4 Compiler message |
|
60 | %h4 Compiler message |
|
61 | .modal-body |
|
61 | .modal-body |
|
62 | - %pre#compiler_msg= @submission.compiler_message |
|
62 | + %pre#compiler_msg |
|
|
63 | + - if @submission | ||
|
|
64 | + = @submission.compiler_message | ||
|
63 | .modal-footer |
|
65 | .modal-footer |
|
64 | %button.btn.btn-default{type: 'button', data: {dismiss: 'modal'}} Close |
|
66 | %button.btn.btn-default{type: 'button', data: {dismiss: 'modal'}} Close |
|
65 |
|
67 |
@@ -28,3 +28,6 | |||||
|
28 |
|
28 | ||
|
29 | # Uncomment so that configuration is read only once when the server is loaded |
|
29 | # Uncomment so that configuration is read only once when the server is loaded |
|
30 | # CONFIGURATION_CACHE_ENABLED = true |
|
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 | :default_value => 'false', |
|
102 | :default_value => 'false', |
|
103 | :description => 'When true, any user can view/download test data' |
|
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 | # If Configuration['system.online_registration'] is true, the |
|
113 | # If Configuration['system.online_registration'] is true, the |
|
106 | # system allows online registration, and will use these |
|
114 | # system allows online registration, and will use these |
|
107 | # information for sending confirmation emails. |
|
115 | # information for sending confirmation emails. |
You need to be logged in to leave comments.
Login now