+ around_filter :thread_with_auth_info, :except => [:render_error, :render_not_found]
+
+ before_filter :remote_ip
+ before_filter :login_required, :except => :render_not_found
+ before_filter :catch_redirect_hint
+
+ before_filter :find_objects_for_index, :only => :index
+ before_filter :find_object_by_uuid, :except => [:index, :create]
+
+ attr_accessor :resource_attrs
+
+ def catch_redirect_hint
+ if !current_user
+ if params.has_key?('redirect_to') then
+ session[:redirect_to] = params[:redirect_to]
+ end
+ end
+ end
+
+ unless Rails.application.config.consider_all_requests_local
+ rescue_from Exception,
+ :with => :render_error
+ rescue_from ActiveRecord::RecordNotFound,
+ :with => :render_not_found
+ rescue_from ActionController::RoutingError,
+ :with => :render_not_found
+ rescue_from ActionController::UnknownController,
+ :with => :render_not_found
+ rescue_from ActionController::UnknownAction,
+ :with => :render_not_found
+ end
+
+ def render_error(e)
+ logger.error e.inspect
+ logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
+ if @object and @object.errors and @object.errors.full_messages
+ errors = @object.errors.full_messages
+ else
+ errors = [e.inspect]
+ end
+ render json: { errors: errors }, status: 422
+ end
+
+ def render_not_found(e=ActionController::RoutingError.new("Path not found"))
+ logger.error e.inspect
+ render json: { errors: ["Path not found"] }, status: 404
+ end
+
+ def find_objects_for_index
+ uuid_list = [current_user.uuid, *current_user.groups_i_can(:read)]
+ sanitized_uuid_list = uuid_list.
+ collect { |uuid| model_class.sanitize(uuid) }.join(', ')
+ @objects ||= model_class.
+ joins("LEFT JOIN links permissions ON permissions.head_uuid=#{table_name}.owner AND permissions.tail_uuid in (#{sanitized_uuid_list}) AND permissions.link_class='permission'").
+ where("?=? OR #{table_name}.owner in (?) OR #{table_name}.uuid=? OR permissions.head_uuid IS NOT NULL",
+ true, current_user.is_admin,
+ uuid_list,
+ current_user.uuid)
+ @where = params[:where] || {}
+ @where = Oj.load(@where) if @where.is_a?(String)
+ if params[:where]
+ conditions = ['1=1']
+ @where.each do |attr,value|
+ if (!value.nil? and
+ attr.to_s.match(/^[a-z][_a-z0-9]+$/) and
+ model_class.columns.collect(&:name).index(attr))
+ if value.is_a? Array
+ conditions[0] << " and #{table_name}.#{attr} in (?)"
+ conditions << value
+ elsif value.is_a? String or value.is_a? Fixnum or value == true or value == false
+ conditions[0] << " and #{table_name}.#{attr}=?"
+ conditions << value
+ end
+ elsif (!value.nil? and attr == 'any' and
+ value.is_a?(Array) and value[0] == 'contains' and
+ model_class.columns.collect(&:name).index('name')) then
+ conditions[0] << " and #{table_name}.name ilike ?"
+ conditions << "%#{value[1]}%"
+ end
+ end
+ if conditions.length > 1
+ conditions[0].sub!(/^1=1 and /, '')
+ @objects = @objects.
+ where(*conditions)
+ end
+ end
+ if params[:limit]
+ begin
+ @objects = @objects.limit(params[:limit].to_i)
+ rescue
+ raise "invalid argument (limit)"
+ end
+ else
+ @objects = @objects.limit(100)
+ end
+ @objects = @objects.order("#{table_name}.modified_at desc")
+ end