X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fcontrollers%2Fapplication_controller.rb;h=be0c189277666059b968a9b69c57e7976c261ae6;hb=d7c8065be895b67dc453b0e11ad0f259f1ca6706;hp=bd48f0f8fbadbfb440260e3ab7b953038e951100;hpb=339d45c101d93bf611299e3c90f3883f28937c2b;p=pettanr%2Fpettanr.git diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index bd48f0f8..be0c1892 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,15 +1,19 @@ class ApplicationController < ActionController::Base protect_from_forgery - layout :devise_layout if MagicNumber['test_layout'] - before_filter :bf + protect_from_forgery with: :null_session, if: Proc.new {|c| c.request.format == 'application/json'} + layout :devise_layout + before_action :bf + before_action :authenticate_user_from_token!, if: -> {params[:email].present? or request.headers[:email]} def devise_layout if devise_controller? case resource_name when :admin - 'test' + 'guest' when :user - 'test' + 'guest' + when :demand_user + 'guest' end else 'application' @@ -21,41 +25,64 @@ class ApplicationController < ActionController::Base :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 + user = if user_signed_in? + current_user else - if user_signed_in? - @user = current_user - @author = @user.author - @artist = if @author and @author.artist? - @author.artist - else - 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 + 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 + + def authenticate_user_from_token! + user = User.find_by(email: (params[:email] or request.headers[:email])) + if Devise.secure_compare(user.try(:authentication_token), (params[:token] or request.headers[:token])) + sign_in user, store: false + self.bf 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_admin + authenticate_admin! unless @operators.admin? + end + def authenticate_author - if @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 } @@ -63,14 +90,13 @@ class ApplicationController < ActionController::Base false end end - + def authenticate_artist - if @author.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 } @@ -79,6 +105,358 @@ class ApplicationController < ActionController::Base end end + def self.controller + Manifest.manifest.controllers[self.model.item_name] + end + + def self.profiler_manager + Manifest.manifest.profiler_managers[self.model.item_name] + end + + def set_model + @my_controller = Manifest.manifest.controllers[params[:controller].to_s] + @my_action = @my_controller.actions[params[:action].to_s] + @my_model = Manifest.manifest.models[@my_action.item_name] + @my_model_class = @my_model.classify + end + + def set_list options = {} + set_model + # params merge to options + options[:page] ||= params[:page] + options[:page_size] ||= params[:page_size] + options[:order] ||= params[:order] + options[:direction] ||= params[:direction] + @finder = @my_action.find options + end + + def filer_list options = {} + set_list options + respond_to do |format| + format.html { + @filer = Locmare::Filer.new @my_action.return_item_name, @finder, @finder, @operators + render @filer.template_name, :locals => { + :filer => @filer + } + } + list_json_format @finder, format + format.atom + format.rss + end + end + + def list_json_format list, format + format.json { + res = { + :page_status => { + :type => :default, :total => @finder.total_count, :total_page => @finder.total_pages, + :page => @finder.current_page, :page_size => @finder.limit_value, + :item_name => @my_action.return_item_name + }, + # rails3.2 has problem + # y method defined as private + # attribute y conflict at PanelPicture, balloon ..etc + # use i.attributes[name] + :list => list.map{|i| i.attributes} + # :list => @finder.to_json + } + render json: res.to_json + } + end + + def set_play + set_model + @item = @my_model_class.show(params[:id], @operators) + options = if @item.own?(@operators) + {finder: :find_private_play, param: [params[:id], @operators]} + else + {finder: :find_play, param: params[:id]} + end + set_list options + end + + def play_list + @items = @finder #.map {|sp| sp.root } + @count = @finder.total_count + @pager = @finder + end + + def set_show + set_model + @item = @my_model_class.show(params[:id], @operators) + end + + def show_prof_format format + format.prof { + self.formats = [:html] + @profiler = Locmare::Profiler.new @my_model_class.item_name, @item, @operators + render @profiler.template_name, :locals => { + :profiler => @profiler + } + } + end + + def show_json_format format + format.json { render json: @item.to_json(@my_model_class.show_json_opt) } + end + + def show_json_format_for_root format + format.json { render json: @item.to_json(@my_model_class.show_json_opt_for_root) } + end + + def set_new + set_model + @item = @my_model_class.new + @item.boosts 'post' + @item.supply_default + end + + def set_edit + set_model + @item = @my_model_class.edit(params[:id], @operators) + @item.boosts 'post' + end + + def render_form + respond_to do |format| + format.html { + @form = Locmare::Bucket.factory @item.item_name, 'default', @item, true, true, @operators + render @form.template_name, :locals => { + :form => @form + } + } + format.json { render json: @item.to_json } + end + end + + def form_new + set_new + render_form + end + + def form_edit + set_edit + render_form + end + + def created_html_format format, redirect_url = nil + format.html { + flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human) + redirect_to (redirect_url ? redirect_url : @item) + } + end + + def created_json_format format + format.json { + render json: @item.to_json(@my_model_class.show_json_opt), status: :created, location: @item + } + end + + def not_created_html_format format + format.html { + flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human) + render_form + } + end + + def not_created_json_format format + format.json { + render json: @item.errors, status: :unprocessable_entity + } + end + + def render_create redirect_url = nil + if @item.save + respond_to do |format| + created_html_format format, redirect_url + created_json_format format + end + else + respond_to do |format| + not_created_html_format format + not_created_json_format format + end + end + end + + def leaf_created_html_format format, redirect_url + format.html { + flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human) + redirect_to redirect_url + } + end + + def leaf_not_created_html_format format, redirect_url + format.html { + flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human) + redirect_to redirect_url + } + end + + def leaf_render_create redirect_url + if @item.store @operators + respond_to do |format| + leaf_created_html_format format, redirect_url + created_json_format format + end + else + respond_to do |format| + leaf_not_created_html_format format, redirect_url + not_created_json_format format + end + end + end + + def updated_html_format format, redirect_url = nil + format.html { + flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human) + redirect_to (redirect_url ? redirect_url : @item) + } + end + + def updated_json_format format + format.json { + render json: '{}', status: :ok + } + end + + def not_updated_html_format format + format.html { + flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human) + render_form + } + end + + def not_updated_json_format format + format.json { + render json: @item.errors, status: :unprocessable_entity + } + end + + def render_update redirect_url = nil + if @item.save + respond_to do |format| + updated_html_format format, redirect_url + updated_json_format format + end + else + respond_to do |format| + not_updated_html_format format + not_updated_json_format format + end + end + end + + def leaf_updated_html_format format, redirect_url + format.html { + flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human) + redirect_to redirect_url + } + end + + def leaf_not_updated_html_format format, redirect_url + format.html { + flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human) + redirect_to redirect_url + } + end + + def leaf_render_update ot, redirect_url + if @item.store @operators, ot + respond_to do |format| + leaf_updated_html_format format, redirect_url + updated_json_format format + end + else + respond_to do |format| + leaf_not_updated_html_format format, redirect_url + not_updated_json_format format + end + end + end + + def destroyed_html_format format, redirect_url + format.html { + flash[:notice] = I18n.t('flash.notice.destroyed', :model => @my_model_class.model_name.human) + redirect_to redirect_url + } + end + + def destroyed_json_format format + format.json { + render json: '{}', status: :ok + } + end + + def not_destroyed_html_format format + format.html { + flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => @my_model_class.model_name.human) + redirect_to @item + } + end + + def not_destroyed_json_format format + format.json { + render json: @item.errors, status: :unprocessable_entity + } + end + + def render_destroy redirect_url + if @item.destroy + respond_to do |format| + destroyed_html_format format, redirect_url + destroyed_json_format format + end + else + respond_to do |format| + not_destroyed_html_format format + not_destroyed_json_format format + end + end + end + + def render_destroy_by destroy_method_name, redirect_url = nil + if @item.__send__(destroy_method_name) + respond_to do |format| + destroyed_html_format format, redirect_url + destroyed_json_format format + end + else + respond_to do |format| + not_destroyed_html_format format + not_destroyed_json_format format + end + end + 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 format_prof format + format.prof { + @profiler = self.class.profiler_manager.open(@item, @operators) + render :template => 'system/prof', :locals => { + :profiler => @profiler + } + } + end + + def assist_items controller_name, action_name, options = {} + controller = Manifest.manifest.controllers[controller_name] + action = controller.actions[action_name] + options[:page] = 1 + options[:page_size] = 5 + finder = action.find options + finder + end + def set_image(file) if file.respond_to?(:read) file.read @@ -87,22 +465,87 @@ class ApplicationController < ActionController::Base end end -=begin - rescue_from ActiveRecord::RecordNotFound, :with => :render_404 - - private - def render_404(exception = nil) - if exception - logger.info "Rendering 404: #{exception.message}" + 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 - 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 -} + 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 end