Description:
[web] improved log-in & roles efficiency git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@196 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r104:7a05c208f2ec - - 6 files changed: 23 inserted, 10 deleted

@@ -1,28 +1,27
1 1 class AnnouncementsController < ApplicationController
2 2
3 - before_filter :authenticate
4 - before_filter { |controller| controller.authorization_by_roles(['admin'])}
3 + before_filter :admin_authorization
5 4
6 5 in_place_edit_for :announcement, :published
7 6
8 7 # GET /announcements
9 8 # GET /announcements.xml
10 9 def index
11 10 @announcements = Announcement.find(:all,
12 11 :order => "created_at DESC")
13 12
14 13 respond_to do |format|
15 14 format.html # index.html.erb
16 15 format.xml { render :xml => @announcements }
17 16 end
18 17 end
19 18
20 19 # GET /announcements/1
21 20 # GET /announcements/1.xml
22 21 def show
23 22 @announcement = Announcement.find(params[:id])
24 23
25 24 respond_to do |format|
26 25 format.html # show.html.erb
27 26 format.xml { render :xml => @announcement }
28 27 end
@@ -1,51 +1,58
1 1 # Filters added to this controller apply to all controllers in the application.
2 2 # Likewise, all the methods added will be available for all controllers.
3 3
4 4 class ApplicationController < ActionController::Base
5 5 # Pick a unique cookie name to distinguish our session data from others'
6 6 session :session_key => '_grader_session_id'
7 7
8 8 SINGLE_USER_MODE_CONF_KEY = 'system.single_user_mode'
9 9
10 + def admin_authorization
11 + return false unless authenticate
12 + user = User.find(session[:user_id], :include => ['roles'])
13 + redirect_to :controller => 'main', :action => 'login' unless user.admin?
14 + end
15 +
10 16 def authorization_by_roles(allowed_roles)
11 17 return false unless authenticate
12 18 user = User.find(session[:user_id])
13 19 unless user.roles.detect { |role| allowed_roles.member?(role.name) }
14 20 flash[:notice] = 'You are not authorized to view the page you requested'
15 21 redirect_to :controller => 'main', :action => 'login'
16 22 return false
17 23 end
18 24 end
19 25
20 26 protected
27 +
21 28 def authenticate
22 29 unless session[:user_id]
23 30 redirect_to :controller => 'main', :action => 'login'
24 31 return false
25 32 end
26 33
27 - Configuration.reload
34 + #Configuration.reload
28 35 # check if run in single user mode
29 36 if (Configuration[SINGLE_USER_MODE_CONF_KEY])
30 37 user = User.find(session[:user_id])
31 38 if user==nil or user.login != 'root'
32 39 redirect_to :controller => 'main', :action => 'login'
33 40 return false
34 41 end
35 42 end
36 43
37 44 return true
38 45 end
39 46
40 47 def authorization
41 48 return false unless authenticate
42 49 user = User.find(session[:user_id])
43 50 unless user.roles.detect { |role|
44 51 role.rights.detect{ |right|
45 52 right.controller == self.class.controller_name and
46 53 (right.action == 'all' or right.action == action_name)
47 54 }
48 55 }
49 56 flash[:notice] = 'You are not authorized to view the page you requested'
50 57 #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login')
51 58 redirect_to :controller => 'main', :action => 'login'
@@ -1,19 +1,24
1 1 class LoginController < ApplicationController
2 2
3 3 def index
4 4 # show login screen
5 5 reset_session
6 6 redirect_to :controller => 'main', :action => 'login'
7 7 end
8 8
9 9 def login
10 10 if user = User.authenticate(params[:login], params[:password])
11 11 session[:user_id] = user.id
12 12 redirect_to :controller => 'main', :action => 'list'
13 + if user.admin?
14 + session[:admin] = true
15 + else
16 + session[:admin] = false
17 + end
13 18 else
14 19 flash[:notice] = 'Wrong password'
15 20 redirect_to :controller => 'main', :action => 'login'
16 21 end
17 22 end
18 23
19 24 end
@@ -1,34 +1,32
1 1 class MessagesController < ApplicationController
2 2
3 3 before_filter :authenticate
4 4
5 5 verify :method => :post, :only => ['create'],
6 6 :redirect_to => { :action => 'list' }
7 7
8 - before_filter :only => ['console','show'] do |controller|
9 - controller.authorization_by_roles(['admin'])
10 - end
8 + before_filter :admin_authorization, :only => ['console','show','reply']
11 9
12 10 def list
13 11 @user = User.find(session[:user_id])
14 12 @messages = Message.find_all_sent_by_user(@user)
15 13 end
16 14
17 15 def console
18 16 @user = User.find(session[:user_id])
19 17 @messages = Message.find_all_system_unreplied_messages
20 18 end
21 19
22 20 def show
23 21 @message = Message.find(params[:id])
24 22 end
25 23
26 24 def create
27 25 user = User.find(session[:user_id])
28 26 @message = Message.new(params[:message])
29 27 @message.sender = user
30 28 if !@message.save
31 29 render :action => 'list' and return
32 30 else
33 31 flash[:notice] = 'New message posted'
34 32 redirect_to :action => 'list'
@@ -1,32 +1,32
1 1 # Methods added to this helper will be available to all templates in the application.
2 2 module ApplicationHelper
3 3
4 4 def user_header
5 5 menu_items = ''
6 6 user = User.find(session[:user_id])
7 7
8 - if (user!=nil) and (user.admin?)
8 + if (user!=nil) and (session[:admin])
9 9 # admin menu
10 10 menu_items << "<b>Administrative task:</b> "
11 11 append_to menu_items, '[Announcements]', 'announcements', 'index'
12 12 append_to menu_items, '[Msg console]', 'messages', 'console'
13 13 append_to menu_items, '[Problem admin]', 'problems', 'index'
14 14 append_to menu_items, '[User admin]', 'user_admin', 'index'
15 15 append_to menu_items, '[User stat]', 'user_admin', 'user_stat'
16 16 #append_to menu_items, '[Graders]', 'graders', 'list'
17 17 append_to menu_items, '[Site config]', 'configurations', 'index'
18 18 menu_items << "<br/>"
19 19 end
20 20
21 21 # main page
22 22 append_to menu_items, '[Main]', 'main', 'list'
23 23 append_to menu_items, '[Messages]', 'messages', 'list'
24 24 append_to menu_items, '[Tasks]', 'tasks', 'list'
25 25 append_to menu_items, '[Submissions]', 'main', 'submission'
26 26 append_to menu_items, '[Test]', 'test', 'index'
27 27 append_to menu_items, '[Settings]', 'users', 'index'
28 28 append_to menu_items, '[Log out]', 'main', 'login'
29 29
30 30 menu_items
31 31 end
32 32
@@ -1,26 +1,30
1 1 - content_for :head do
2 2 = javascript_include_tag :defaults
3 3
4 4 %h1 Grader configuration
5 5
6 6 %table.info
7 7 %tr.info-head
8 8 %th Key
9 9 %th Type
10 10 %th Value
11 11
12 12 - @configurations.each do |conf|
13 13 - @configuration = conf
14 14 %tr
15 15 %td
16 16 = in_place_editor_field :configuration, :key, {}, :rows=>1
17 17 %td
18 18 = in_place_editor_field :configuration, :value_type, {}, :rows=>1
19 19 %td
20 20 = in_place_editor_field :configuration, :value, {}, :rows=>1
21 21
22 22 %br/
23 - = link_to '[Reload configuration]', :action => 'reload'
23 + Your config is saved, but it does not automatically take effect.
24 24 %br/
25 - Your config is saved, but it does not automatically take effect.
26 - You must reload.
25 + If you have one mongrel process running, you can
26 + = link_to '[click]', :action => 'reload'
27 + here to reload.
28 + %br/
29 + If you have more than one process running, you should restart
30 + them manually.
You need to be logged in to leave comments. Login now