OSDN Git Service

t#30558:fix auth filter
[pettanr/pettanr.git] / app / controllers / application_controller.rb
index af9271d..ddc723e 100644 (file)
@@ -1,9 +1,29 @@
 class ApplicationController < ActionController::Base
   protect_from_forgery
+  layout :devise_layout if MagicNumber['test_layout']
   before_filter :bf
   
+  def devise_layout
+    if devise_controller?
+      case resource_name
+      when :admin
+        'guest'
+      when :user
+        'guest'
+      when :demand_user
+        'guest'
+      end
+    else
+      'application'
+    end
+  end
+  
   def bf
-    if Admin.count.to_i == 0 # or License.count.to_i == 0
+    @server_result = {
+      :location => {:controller => params[:controller], :action => params[:action]}
+    }
+    @server_result[:location][:id] = params[:id] if params[:id]
+    if Admin.count.to_i == 0 or License.count.to_i == 0
       if params[:controller] == 'system' and params[:action] == 'start'
       else
         redirect_to :controller => '/system', :action => 'start'
@@ -15,28 +35,137 @@ class ApplicationController < ActionController::Base
         @artist = if @author and @author.artist?
           @author.artist
         else
-          Artist.new author_id: @author.id, email: @user.email, name: @author.name, default_license_id: 1
+          nil
         end
       end
+      @admin = if admin_signed_in?
+        current_admin
+      else
+        nil
+      end
+      @demand_user = if demand_user_signed_in?
+        current_demand_user
+      else
+        nil
+      end
     end
   end
   
-=begin
-  rescue_from ActiveRecord::RecordNotFound, :with => :render_404\r
-\r
-  private\r
-  def render_404(exception = nil)\r
-    if exception\r
-        logger.info "Rendering 404: #{exception.message}"\r
-    end\r
-    respond_to do |format|
-          format.html { \r
-    render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false\r
-}
-          format.json { 
-    render :text => "404 found", :status => 404\r
-}
-  end\r
-  end\r
-=end  \r
+  def authenticate_reader
+    authenticate_user! unless (@user || @admin)
+  end
+  
+  def authenticate_user
+    authenticate_user! unless (@user)
+  end
+  
+  def authenticate_resource_reader
+    authenticate_user! unless (@user || @admin || @demand_user)
+  end
+  
+  def authenticate_author
+    if @author
+      true
+    else
+      respond_to do |format|
+        format.html { redirect_to main_app.new_author_path, :status => :found }
+        format.js { render "authors/new" }
+        format.json { 
+          raise ActiveRecord::Forbidden
+        }
+      end
+      false
+    end
+  end
+      
+  def authenticate_artist
+    if @artist
+      true
+    else
+      respond_to do |format|
+        format.html { redirect_to main_app.new_artist_path, :status => :found }
+        format.js { render "artists/new" }
+        format.json { 
+          raise ActiveRecord::Forbidden
+        }
+      end
+      false
+    end
+  end
+  
+  def set_image(file)
+    if file.respond_to?(:read)
+      file.read
+    else
+      Base64.decode64(file.to_s.gsub(' ', '+'))
+    end
+  end
+  
+  def ymd_to_time ymd_str
+    return nil if ymd_str.blank?
+    date = nil
+    begin
+      date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
+    rescue
+      date = nil
+    end
+    date
+  end
+  
+  def export_url demander_url, action, token, date
+    u = demander_url + (demander_url[-1] == '/' ? '' : '/')
+    u = URI.join(u, action + '.json?auth_token=' + token)
+    u = URI.join(u, '&date=' + date) unless date.blank?
+    u.to_s
+  end
+  
+  def export_from_provider url
+    res = nil
+    begin
+      json = RestClient.get url
+      res = JSON.parse json
+    rescue
+    end
+    res
+  end
+  
+  def export_by action, provider_status, ymd
+    t = ymd_to_time ymd
+    url = export_url provider_status.provider.demander_url, action, provider_status.token, t
+    export_from_provider(url)
+  end
+  
+  if Rails.env == 'production'
+    rescue_from ActiveRecord::RecordNotFound, :with => :render_404
+    rescue_from ActiveRecord::Forbidden, :with => :render_403
+    
+    private
+    def render_404(exception = nil)
+      if exception
+        logger.info "Rendering 404: #{exception.message}"
+      end
+      respond_to do |format|
+        format.html { 
+          render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
+        }
+        format.json { 
+          render :text => "404 Not found", :status => 404
+        }
+      end
+    end
+    
+    def render_403(exception = nil)
+      if exception
+        logger.info "Rendering 403: #{exception.message}"
+      end
+      respond_to do |format|
+        format.html { 
+          render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
+        }
+        format.json { 
+          render :text => "403 Forbidden", :status => 403
+        }
+      end
+    end
+  end
 end