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,22 +1,24 | |||
|
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 | |
@@ -51,13 +53,33 | |||
|
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 | |
|
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 | 85 | end |
@@ -20,25 +20,25 | |||
|
20 | 20 | |
|
21 | 21 | def show |
|
22 | 22 | @problem = Problem.find(params[:id]) |
|
23 | 23 | end |
|
24 | 24 | |
|
25 | 25 | def new |
|
26 | 26 | @problem = Problem.new |
|
27 | 27 | @description = nil |
|
28 | 28 | end |
|
29 | 29 | |
|
30 | 30 | def create |
|
31 | 31 | @problem = Problem.new(problem_params) |
|
32 |
- @description = Description.new( |
|
|
32 | + @description = Description.new(description_params) | |
|
33 | 33 | if @description.body!='' |
|
34 | 34 | if !@description.save |
|
35 | 35 | render :action => new and return |
|
36 | 36 | end |
|
37 | 37 | else |
|
38 | 38 | @description = nil |
|
39 | 39 | end |
|
40 | 40 | @problem.description = @description |
|
41 | 41 | if @problem.save |
|
42 | 42 | flash[:notice] = 'Problem was successfully created.' |
|
43 | 43 | redirect_to action: :index |
|
44 | 44 | else |
@@ -243,24 +243,28 | |||
|
243 | 243 | def remove_contest |
|
244 | 244 | problem = Problem.find(params[:id]) |
|
245 | 245 | contest = Contest.find(params[:contest_id]) |
|
246 | 246 | if problem!=nil and contest!=nil |
|
247 | 247 | problem.contests.delete(contest) |
|
248 | 248 | end |
|
249 | 249 | redirect_to :action => 'manage' |
|
250 | 250 | end |
|
251 | 251 | |
|
252 | 252 | ################################## |
|
253 | 253 | protected |
|
254 | 254 | |
|
255 | + def description_params | |
|
256 | + params.require(:description).permit(:body, :markdowned) | |
|
257 | + end | |
|
258 | + | |
|
255 | 259 | def allow_test_pair_import? |
|
256 | 260 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
257 | 261 | return ALLOW_TEST_PAIR_IMPORT |
|
258 | 262 | else |
|
259 | 263 | return false |
|
260 | 264 | end |
|
261 | 265 | end |
|
262 | 266 | |
|
263 | 267 | def change_date_added |
|
264 | 268 | problems = get_problems_from_params |
|
265 | 269 | date = Date.parse(params[:date_added]) |
|
266 | 270 | problems.each do |p| |
@@ -118,37 +118,39 | |||
|
118 | 118 | begin |
|
119 | 119 | self.problem = Problem.find(self.problem_id) |
|
120 | 120 | rescue ActiveRecord::RecordNotFound |
|
121 | 121 | self.problem = nil |
|
122 | 122 | end |
|
123 | 123 | else |
|
124 | 124 | self.problem = Submission.find_problem_in_source(self.source, |
|
125 | 125 | self.source_filename) |
|
126 | 126 | end |
|
127 | 127 | end |
|
128 | 128 | |
|
129 | 129 | def assign_language |
|
130 | - self.language = Submission.find_language_in_source(self.source, | |
|
131 | - self.source_filename) | |
|
130 | + if self.language == nil | |
|
131 | + self.language = Submission.find_language_in_source(self.source, | |
|
132 | + self.source_filename) | |
|
133 | + end | |
|
132 | 134 | end |
|
133 | 135 | |
|
134 | 136 | # validation codes |
|
135 | 137 | def must_specify_language |
|
136 | 138 | return if self.source==nil |
|
137 | 139 | |
|
138 | 140 | # for output_only tasks |
|
139 | 141 | return if self.problem!=nil and self.problem.output_only |
|
140 | 142 | |
|
141 | - if self.language==nil | |
|
142 |
- errors.add('source',"Cannot detect language. Did you submit a correct source file?") |
|
|
143 | + if self.language == nil | |
|
144 | + errors.add('source',"Cannot detect language. Did you submit a correct source file?") | |
|
143 | 145 | end |
|
144 | 146 | end |
|
145 | 147 | |
|
146 | 148 | def must_have_valid_problem |
|
147 | 149 | return if self.source==nil |
|
148 | 150 | if self.problem==nil |
|
149 | 151 | errors.add('problem',"must be specified.") |
|
150 | 152 | else |
|
151 | 153 | #admin always have right |
|
152 | 154 | return if self.user.admin? |
|
153 | 155 | |
|
154 | 156 | #check if user has the right to submit the problem |
@@ -31,25 +31,25 | |||
|
31 | 31 | |
|
32 | 32 | scope :activated_users, -> {where activated: true} |
|
33 | 33 | |
|
34 | 34 | validates_presence_of :login |
|
35 | 35 | validates_uniqueness_of :login |
|
36 | 36 | validates_format_of :login, :with => /\A[\_A-Za-z0-9]+\z/ |
|
37 | 37 | validates_length_of :login, :within => 3..30 |
|
38 | 38 | |
|
39 | 39 | validates_presence_of :full_name |
|
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, |
|
47 | 47 | :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, |
|
48 | 48 | :if => :email_validation? |
|
49 | 49 | validate :uniqueness_of_email_from_activated_users, |
|
50 | 50 | :if => :email_validation? |
|
51 | 51 | validate :enough_time_interval_between_same_email_registrations, |
|
52 | 52 | :if => :email_validation? |
|
53 | 53 | |
|
54 | 54 | # these are for ytopc |
|
55 | 55 | # disable for now |
@@ -50,25 +50,27 | |||
|
50 | 50 | .col-md-12 |
|
51 | 51 | %h2 Console |
|
52 | 52 | %textarea#console{style: 'height: 100%; width: 100%;background-color:#000;color:#fff;font-family: consolas, monaco, "Droid Sans Mono";',rows: 20} |
|
53 | 53 | |
|
54 | 54 | .modal.fade#compiler{tabindex: -1,role: 'dialog'} |
|
55 | 55 | .modal-dialog.modal-lg{role:'document'} |
|
56 | 56 | .modal-content |
|
57 | 57 | .modal-header |
|
58 | 58 | %button.close{type: 'button', data: {dismissed: :modal}, aria: {label: 'close'}} |
|
59 | 59 | %span{aria: {hidden: 'true'}, data: {dismiss: 'modal'}} × |
|
60 | 60 | %h4 Compiler message |
|
61 | 61 | .modal-body |
|
62 | - %pre#compiler_msg= @submission.compiler_message | |
|
62 | + %pre#compiler_msg | |
|
63 | + - if @submission | |
|
64 | + = @submission.compiler_message | |
|
63 | 65 | .modal-footer |
|
64 | 66 | %button.btn.btn-default{type: 'button', data: {dismiss: 'modal'}} Close |
|
65 | 67 | |
|
66 | 68 | :javascript |
|
67 | 69 | $(document).ready(function() { |
|
68 | 70 | e = ace.edit("editor") |
|
69 | 71 | e.setValue($("#text_sourcecode").val()); |
|
70 | 72 | e.gotoLine(1); |
|
71 | 73 | $("#language_id").trigger('change'); |
|
72 | 74 | |
|
73 | 75 | $("#load_file").on('change',function(evt) { |
|
74 | 76 | var file = evt.target.files[0]; |
@@ -19,12 +19,15 | |||
|
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) |
@@ -93,24 +93,32 | |||
|
93 | 93 | :key => 'right.heartbeat_response_full', |
|
94 | 94 | :value_type => 'string', |
|
95 | 95 | :default_value => 'OK', |
|
96 | 96 | :description => 'Heart beat response text when user got full score (set this value to the empty string to disable this feature)' |
|
97 | 97 | }, |
|
98 | 98 | |
|
99 | 99 | { |
|
100 | 100 | :key => 'right.view_testcase', |
|
101 | 101 | :value_type => 'boolean', |
|
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. |
|
108 | 116 | { |
|
109 | 117 | :key => 'system.online_registration.smtp', |
|
110 | 118 | :value_type => 'string', |
|
111 | 119 | :default_value => 'smtp.somehost.com' |
|
112 | 120 | }, |
|
113 | 121 | |
|
114 | 122 | { |
|
115 | 123 | :key => 'system.online_registration.from', |
|
116 | 124 | :value_type => 'string', |
You need to be logged in to leave comments.
Login now