merged initial codebase
[smr.git] / gui / app / controllers / application_controller.rb
blob00a98332bef41731cafad70da02a7bfea03f38cf
2 # This file is part of SMR.
4 # SMR is free software: you can redistribute it and/or modify it under the
5 # terms of the GNU General Public License as published by the Free Software
6 # Foundation, either version 3 of the License, or (at your option) any later
7 # version.
9 # SMR is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # SMR.  If not, see <http://www.gnu.org/licenses/>.
16 class ApplicationController < ActionController::Base
17     before_filter :force_login
18     helper_method :current_user
20     private
21     
22     # redirect to login unless we have a authenticated current_user
23     def force_login 
24       unless current_user or params[:controller].eql?'sessions'
25         redirect_to :controller=>'sessions', :action=>'new'
26       end
27     end
28     
29     # find current_user by :auth_token cookie
30     def current_user
31       @current_user ||= User.find_by_password_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
32     end
34     # print whatever is given as parameter to the servers output 
35     def debug(*parameters)
36       parameters.each { |p| print 'DEBUG> ', p.inspect }
37     end
41     public   
43     # set session parameters that are of global use
44     # - these can be modified through POST from anywhere
45     # 
46     # you may POST
47     #
48     # * smr_browse_date = {"day"=>"23","month"=>"5","year"=>"2010"}
49     #
50     def set_session_params
52         if params[:smr_browse_date] then
53             d=params[:smr_browse_date]
54             session[:smr_browse_date] = Time.new(d['year'],d['month'],d['day']).end_of_day.to_i
55         end
57         redirect_to :back
58     end
60     # return date the user is currently browsing, as Time
61     def smr_browse_date
62         if session[:smr_browse_date].nil? then Time.new
63         else Time.at(session[:smr_browse_date]) end
64     end
66 end