aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/application_controller.rb
blob: 2cb218bbe3fe6a24ce85d7c903bdd99938dbb447 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de).
# 
# This file is part of ECS.
# 
# ECS is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# 
# ECS is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public
# License along with ECS. If not, see <http://www.gnu.org/licenses/>.


# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.


class ApplicationController < ActionController::Base

  require 'exceptions'
  require 'pp'
  require 'json/add/rails'

  #rescue_from Exception, :with => :rescue_body_500
  rescue_from Ecs::InvalidMessageException, :with => :rescue_body_400
  rescue_from Ecs::MissingReceiverHeaderException, :with => :rescue_body_400
  rescue_from Ecs::AuthenticationException, :with => :rescue_body_401
  rescue_from Ecs::AuthorizationException, :with => :rescue_body_403
  rescue_from Ecs::InvalidRessourceUriException, :with => :rescue_body_404
  rescue_from ActiveRecord::RecordNotFound, :with => :rescue_body_404
  rescue_from ActionController::RoutingError , :with => :rescue_body_404
  rescue_from Ecs::NoReceiverOfMessageException, :with => :rescue_body_404
  rescue_from Ecs::OuttimedAuthsException, :with => :rescue_body_409
  rescue_from ActiveRecord::StaleObjectError, :with => :rescue_body_409
  rescue_from ActiveRecord::StatementInvalid, :with => :rescue_body_415
  rescue_from Ecs::InvalidMimetypeException, :with => :rescue_body_415

  #helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details
  # session :on

  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password
  #

  def initialize
    super
    #@ar_model_name = nil
    #@ar_model = nil
    @list = nil
    @record = nil
    @app_namespace = nil
    @ressource_name = nil
    @ressource_path = nil
    @body = ""
    @participant = nil
    @memberships = nil
    @cookie = nil
    @outdated_auth_token = nil
  end

protected

  def authentication
    logger.info "X-EcsAuthId: #{request.headers['X-EcsAuthId']}" unless request.headers['X-EcsAuthId'].blank?
    if ECS_CONFIG["participants"]["allow_anonymous"]
      # new anonymous participant
      if request.headers["X-EcsAuthId"].blank? and request.headers["Cookie"].blank?
        @participant, @cookie = Participant.generate_anonymous_participant
        return @participant
      end
      # anonymous participants
      if !(@cookie = cookies[:ecs_anonymous]).blank?
        if (identity = Identity.find_by_name(@cookie)).blank?
          raise Ecs::AuthenticationException, "No valid identity found for cookie: #{@cookie}"
        elsif (participant = identity.participant).blank?
          raise Ecs::AuthenticationException, "Cookie: #{@cookie}\" is not assigned any participant"
        else
          return @participant = participant
        end
      end
    end
    # authenticated participants
    if (auth_id = request.headers["X-EcsAuthId"]).blank?
      raise Ecs::AuthenticationException, "No \"X-EcsAuthId\" http header"
    elsif (identity = Identity.find_by_name(auth_id)).blank?
      raise Ecs::AuthenticationException, "No \"X-EcsAuthId: #{auth_id}\" identity found"
    elsif (participant = identity.participant).blank?
      raise Ecs::AuthenticationException, "\"X-EcsAuthId: #{auth_id}\" is not assigned any participant"
    else
      return @participant = participant
    end
  end

  # set the cookie header
  def add_cookie_header
    cookies[:ecs_anonymous] = \
      {
        :value => @cookie, 
        :path => "/", 
        :expires => Participant::TTL.seconds.from_now
      } unless @cookie.blank?
  end

  def touch_participant_ttl
    Participant.touch_ttl(@participant) if @participant.anonymous
  end



  # error pages
  def rescue_body_401
    @http_error= $!
    logger.error $!.to_s
    render :text => "#{$!.to_s}\n", :layout => false, :status => 401
  end

  def rescue_body_500
    @http_error= $!
    logger.error $!.to_s
    render :text => "#{$!.to_s}\n", :layout => false, :status => 500
  end

  def rescue_body_400
    @http_error= $!
    logger.error $!.to_s
    render :text => "#{$!.to_s}\n" , :layout => false, :status => 400
  end
  
  def rescue_body_403
    @http_error= $!
    logger.error $!.to_s
    render :text => "#{$!.to_s}\n" , :layout => false, :status => 403
  end

  def rescue_body_404
    @http_error= $!
    logger.error $!.to_s
    if $!.to_s.blank?
      render :text => "The server does not know the ressource\nor the message queue in question is empty.\n" , :layout => false, :status => 404
    else
      render :text => "#{$!.to_s}\n" , :layout => false, :status => 404
    end
  end

  def rescue_body_409
    @http_error= $!
    logger.error $!.to_s
    render :text => "#{$!.to_s}\n" , :layout => false, :status => 409
  end
  
  def rescue_body_415(controller_binding)
    @http_error= $!
    logger.error $!.to_s
    if $!.to_s.blank?
      render :text => "The format of the client data is not supported by the server.\nIf your format is right please doublecheck the encoding !\nIt has to be UTF8 !\n", :layout => false, :status => 415
    else
      render :text => "#{$!.to_s}\n" , :layout => false, :status => 415
    end

  end
end