X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fcontrollers%2Fapplication_controller.rb;h=aa4f3a6a89963e77b7e482b24fa6bbf02d09411f;hb=02ea4fc4bbe7a54dd576c1cd700aa9e32710abf8;hp=79a5b24e43005300f62d30d4810956e0f21a1e92;hpb=f8cd0347c685363a6ab296ca5bcf42c158e5e25f;p=pettanr%2Fpettanr.git diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 79a5b24e..aa4f3a6a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,42 +1,236 @@ 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 + @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' end else - if user_signed_in? - @user = current_user - @author = @user.author - @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 - end - end - end - end - -=begin - rescue_from ActiveRecord::RecordNotFound, :with => :render_404 - - 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 found", :status => 404 -} - end - end -=end + user = if user_signed_in? + current_user + else + nil + end + author = if user + user.author + else + nil + end + artist = if user + user.artist + else + nil + end + admin = if admin_signed_in? + current_admin + else + nil + end + demand_user = if demand_user_signed_in? + current_demand_user + else + nil + end + @operators = Operator.new [user, author, artist, admin, demand_user] + end + end + + def authenticate_reader + authenticate_user! unless @operators.reader? + end + + def authenticate_user + authenticate_user! unless @operators.user? + end + + def authenticate_resource_reader + authenticate_user! unless @operators.resource_reader? + end + + def authenticate_author + if @operators.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 @operators.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_filer + @page = @@model.page_number params[:page] + @page_size = @@model.page_size params[:page_size] + list_method = 'list' || configurations['models'][@@model.to_s.underscore]['select']['method'] + @items = @@model.__send__(list_method, @page, @page_size) + end + + def format_filer format + format.html { + @paginate = @@model.list_paginate(@page, @page_size) + render :template => 'system/filer', :locals => { + :items => @items, :model => @@model, + :operators => @operators, :pager => @paginate + } + } + end + + def set_filer_by_anybody + @page = @@model.page_number params[:page] + @page_size = @@model.page_size params[:page_size] + list_method = 'himlist' || configurations['models'][@@model.to_s.underscore]['select']['method'] + @items = @@model.__send__(list_method, @operators, @page, @page_size) + end + + def format_filer_by_anybody format + format.html { + @paginate = @@model.himlist_paginate(@operators, @page, @page_size) + render :template => 'system/filer', :locals => { + :items => @items, :model => @@model, + :operators => @operators, :pager => @paginate + } + } + end + + def format_prof format + format.prof { + render :template => 'system/prof', :locals => { + :item => @item, :operators => @operators + } + } + 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] == '/' ? '' : '/') + prm = '?auth_token=' + token + prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank? + u = URI.join(u, action + '.json' + prm) + 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 + + rescue_from Pettanr::NotWork, :with => :render_not_work + def render_not_work(exception = nil) + if exception + logger.info "Rendering , :: #{exception.message}" + end + respond_to do |format| + format.html { + render :file => "#{Rails.root}/public/not_work.html", :layout => false + } + format.json { + render :text => "400 Not work", :status => 400 + } + end + 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