From d4c6c5edbc77f24a4cec08a82d5768f205ecd344 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Fri, 5 Jul 2013 20:08:09 +0400 Subject: [PATCH 01/10] #82: added Contents controller, logical model, views --- app/controllers/api/v1/base_controller.rb | 10 +-- .../platforms/contents_controller.rb | 16 ++++ app/helpers/paginate_helper.rb | 13 +++ app/models/platform_content.rb | 82 +++++++++++++++++++ app/views/platforms/base/_sidebar.html.haml | 2 + app/views/platforms/contents/index.html.haml | 30 +++++++ app/views/platforms/platforms/show.html.haml | 4 +- config/routes.rb | 3 + 8 files changed, 150 insertions(+), 10 deletions(-) create mode 100644 app/controllers/platforms/contents_controller.rb create mode 100644 app/helpers/paginate_helper.rb create mode 100644 app/models/platform_content.rb create mode 100644 app/views/platforms/contents/index.html.haml diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 0d108ba31..b6f1114bd 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -1,5 +1,6 @@ # -*- encoding : utf-8 -*- class Api::V1::BaseController < ApplicationController + include PaginateHelper #respond_to :json helper_method :member_path @@ -72,15 +73,6 @@ class Api::V1::BaseController < ApplicationController end end - def paginate_params - per_page = params[:per_page].to_i - per_page = 20 if per_page < 1 - per_page = 100 if per_page >100 - page = params[:page].to_i - page = nil if page == 0 - {:page => page, :per_page => per_page} - end - def render_json_response(subject, message, status = 200) id = status != 200 ? nil : subject.id diff --git a/app/controllers/platforms/contents_controller.rb b/app/controllers/platforms/contents_controller.rb new file mode 100644 index 000000000..d98c64324 --- /dev/null +++ b/app/controllers/platforms/contents_controller.rb @@ -0,0 +1,16 @@ +class Platforms::ContentsController < Platforms::BaseController + include PaginateHelper + + before_filter :authenticate_user! + skip_before_filter :authenticate_user! if APP_CONFIG['anonymous_access'] + + load_and_authorize_resource :platform + + def index + @path = '/' << params[:path].to_s + @term = params[:term] + @contents = PlatformContent.find_by_platform(@platform, @path, @term) + .paginate(paginate_params) + end + +end diff --git a/app/helpers/paginate_helper.rb b/app/helpers/paginate_helper.rb new file mode 100644 index 000000000..aa5d0e26f --- /dev/null +++ b/app/helpers/paginate_helper.rb @@ -0,0 +1,13 @@ +# -*- encoding : utf-8 -*- +module PaginateHelper + + def paginate_params + per_page = params[:per_page].to_i + per_page = 20 if per_page < 1 + per_page = 100 if per_page >100 + page = params[:page].to_i + page = nil if page == 0 + {:page => page, :per_page => per_page} + end + +end diff --git a/app/models/platform_content.rb b/app/models/platform_content.rb new file mode 100644 index 000000000..f5dbca177 --- /dev/null +++ b/app/models/platform_content.rb @@ -0,0 +1,82 @@ +class PlatformContent + + # ------------------ + # *** ATTRIBUTES *** + # ------------------ + + attr_reader :path + + # --------------- + # *** METHODS *** + # --------------- + + def initialize(platform, path) + @platform, @path = platform, path + end + + def build_list + return @build_list if !!@build_list + return nil if path !~ /\/(release|updates)+\/\w/ + return nil unless repository_name = path.match(/\/[\w]+\/(release|updates)\//) + repository_name = repository_name[0].gsub(/\/(release|updates)\/$/, '').gsub('/', '') + + repository = @platform.repositories.where(:name => repository_name).first + return nil unless repository + + if @platform.main? + build_for_platform = @platform + else + bfp_name = path.match(/\/#{@platform.name}\/repository\/[\w]+\//) + return nil unless bfp_name + bfp_name = bfp_name[0].gsub(/\/#{@platform.name}\/repository\//, '').gsub('/', '') + build_for_platform = Platform.main.find_by_name bfp_name + return nil unless build_for_platform + end + + @build_list = BuildList.for_status(BuildList::BUILD_PUBLISHED) + .for_platform(build_for_platform) + .scoped_to_save_platform(@platform) + .where(:save_to_repository_id => repository) + .where(:build_list_packages => {:fullname => name, :actual => true}) + .joins(:packages) + .last + + return @build_list + end + + def name + @name ||= @path.gsub(/.*#{File::SEPARATOR}/, '') + end + + def size + @size ||= File.size(@path) + end + + def is_folder? + @is_folder.nil? ? (@is_folder = File.directory?(path)) : @is_folder + end + + def download_url + suffix = path.gsub(/^#{@platform.path}/, '') + "#{APP_CONFIG['downloads_url']}/#{@platform.name}#{suffix}" + end + + # --------------------- + # *** CLASS METHODS *** + # --------------------- + + def self.find_by_platform(platform, path, term) + term = (term.present? && term =~ /\w/) ? term : '' + path = path.split(File::SEPARATOR) + .select{ |p| p.present? && p =~ /\w/ } + .join(File::SEPARATOR) + results = Dir.glob(File.join(platform.path, path, "*#{term}*")) + if term + results = results.sort_by(&:length) + else + results = results.sort + end + results.map{ |p| PlatformContent.new(platform, p) } + end + +end \ No newline at end of file diff --git a/app/views/platforms/base/_sidebar.html.haml b/app/views/platforms/base/_sidebar.html.haml index 5775c9314..827837b02 100644 --- a/app/views/platforms/base/_sidebar.html.haml +++ b/app/views/platforms/base/_sidebar.html.haml @@ -10,6 +10,8 @@ = link_to t("layout.platforms.about"), platform_path(@platform) %li{:class => (contr == :repositories) ? 'active' : ''} = link_to t("layout.repositories.list_header"), platform_repositories_path(@platform) + %li{:class => (contr == :contents) ? 'active' : ''} + = link_to t('layout.platforms.contents'), platform_contents_path(@platform) - if can? :show, @platform %li{:class => (act == :index && contr == :maintainers) ? 'active' : nil} = link_to t("layout.platforms.maintainers"), platform_maintainers_path(@platform) diff --git a/app/views/platforms/contents/index.html.haml b/app/views/platforms/contents/index.html.haml new file mode 100644 index 000000000..40dfa4d48 --- /dev/null +++ b/app/views/platforms/contents/index.html.haml @@ -0,0 +1,30 @@ +- set_meta_tags :title => [title_object(@platform), t('layout.platforms.contents')] += render 'platforms/base/submenu' += render 'platforms/base/sidebar' + +%div + = "#{@platform.name}: #{@path}" + + +%table.tablesorter.project{:cellpadding => "0", :cellspacing => "0"} + %tbody + + - if @path != '/' + %tr + %td= link_to '../', "#{platform_contents_path(@platform)}#{@path}/../" + %td + %td + + - (@contents.select(&:is_folder?) | @contents).each do |content| + %tr + %td + - if content.is_folder? + - pic = 'folder.png' + - path = "#{platform_contents_path(@platform)}#{@path}/#{content.name}" + .pic= image_tag pic || 'code.png' + .name= link_to(content.name, path || content.download_url, :class => 'files-see') + %td= link_to t('activerecord.models.build_list'), content.build_list if content.build_list + %td= number_to_human_size(content.size) unless content.is_folder? +.both + += will_paginate @contents \ No newline at end of file diff --git a/app/views/platforms/platforms/show.html.haml b/app/views/platforms/platforms/show.html.haml index 0461c86d6..14aa01776 100644 --- a/app/views/platforms/platforms/show.html.haml +++ b/app/views/platforms/platforms/show.html.haml @@ -2,7 +2,9 @@ = render 'submenu' = render 'sidebar' -%h3.fix= "#{t("layout.platforms.about")} #{@platform.name}" +%h3.fix + = t 'layout.platforms.about' + = link_to @platform.name, platform_contents_path(@platform) %p= @platform.description diff --git a/config/routes.rb b/config/routes.rb index 5c5710fbf..1e3d9a442 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -161,6 +161,9 @@ Rosa::Application.routes.draw do get :advisories end + resources :contents, :only => [:index] + match '/contents/*path' => 'contents#index', :format => false + resources :mass_builds, :only => [:create, :new, :index] do member do post :cancel From df405209463b4149ec37b73272dd8b6ce98caef1 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Jul 2013 14:39:44 +0400 Subject: [PATCH 02/10] #82: added search form to Contents page --- app/assets/stylesheets/design/custom.scss | 23 +++++++++++++ .../platforms/contents_controller.rb | 3 +- app/helpers/contents_helper.rb | 26 ++++++++++++++ .../platforms/contents/_contents.html.haml | 34 +++++++++++++++++++ app/views/platforms/contents/index.html.haml | 32 +++++------------ app/views/platforms/contents/index.js.haml | 2 ++ 6 files changed, 95 insertions(+), 25 deletions(-) create mode 100644 app/helpers/contents_helper.rb create mode 100644 app/views/platforms/contents/_contents.html.haml create mode 100644 app/views/platforms/contents/index.js.haml diff --git a/app/assets/stylesheets/design/custom.scss b/app/assets/stylesheets/design/custom.scss index 1a28daf0a..f8ac92919 100644 --- a/app/assets/stylesheets/design/custom.scss +++ b/app/assets/stylesheets/design/custom.scss @@ -2036,3 +2036,26 @@ a.button.reject_publish, a.button.create_container { height: 16px; padding: 3px 20px 5px 20px; } + +#contents { + .path { + a { + margin-right: 20px; + } + .text { + padding: 5px 10px; + background-color: #dcecfa; + float: left; + height: 14px; + } + .arrow-right { + content: ''; + width: 0; + height: 0; + border: 12px solid transparent; + border-left: 12px solid #dcecfa; + float: left; + } + } + +} diff --git a/app/controllers/platforms/contents_controller.rb b/app/controllers/platforms/contents_controller.rb index d98c64324..62c9d566a 100644 --- a/app/controllers/platforms/contents_controller.rb +++ b/app/controllers/platforms/contents_controller.rb @@ -7,10 +7,11 @@ class Platforms::ContentsController < Platforms::BaseController load_and_authorize_resource :platform def index - @path = '/' << params[:path].to_s + @path = params[:path].to_s @term = params[:term] @contents = PlatformContent.find_by_platform(@platform, @path, @term) .paginate(paginate_params) + end end diff --git a/app/helpers/contents_helper.rb b/app/helpers/contents_helper.rb new file mode 100644 index 000000000..3ac256546 --- /dev/null +++ b/app/helpers/contents_helper.rb @@ -0,0 +1,26 @@ +# -*- encoding : utf-8 -*- +module ContentsHelper + + def build_content_paths(platform, path) + paths = ['/'] + paths |= path.split('/').select(&:present?) + paths.uniq! + + compound_path = '' + paths.map do |p| + compound_path << p << '/' if p != '/' + link_to(platform_content_path(platform, compound_path), {:remote => true}) do + content_tag(:span, p, {:class => 'text'}) + + content_tag(:span, '', {:class => 'arrow-right'}) + end + end.join.html_safe + end + + def platform_content_path(platform, path, name = nil) + full_path = platform_contents_path(platform) + full_path << '/' << path if path.present? + full_path << ('/' << name) if name.present? + full_path + end + +end diff --git a/app/views/platforms/contents/_contents.html.haml b/app/views/platforms/contents/_contents.html.haml new file mode 100644 index 000000000..9b05c93d6 --- /dev/null +++ b/app/views/platforms/contents/_contents.html.haml @@ -0,0 +1,34 @@ +#contents + + = form_for @platform, :url => platform_content_path(@platform, @path), :html => { :class => :form, :remote => true, :method => :get } do |f| + = tracker_search_field(:term, t('layout.issues.search_user')) + = f.submit t('layout.search.header') + %br + + .path= build_content_paths(@platform, @path) + .both + + %table.tablesorter.project{:cellpadding => "0", :cellspacing => "0"} + %tbody + + - if @path.present? + %tr + %td= link_to '../', platform_content_path(@platform, @path, '../'), {:remote => true} + %td + %td + + - (@contents.select(&:is_folder?) | @contents).each do |content| + %tr + %td + - options = {:class => 'files-see'} + - if content.is_folder? + - pic = 'folder.png' + - path = platform_content_path(@platform, @path, content.name) + - options[:remote] = true + .pic= image_tag pic || 'code.png' + .name= link_to(content.name, path || content.download_url, options) + %td= link_to t('activerecord.models.build_list'), content.build_list if content.build_list + %td= number_to_human_size(content.size) unless content.is_folder? + .both + + = will_paginate @contents, {:remote => true} \ No newline at end of file diff --git a/app/views/platforms/contents/index.html.haml b/app/views/platforms/contents/index.html.haml index 40dfa4d48..84898f087 100644 --- a/app/views/platforms/contents/index.html.haml +++ b/app/views/platforms/contents/index.html.haml @@ -2,29 +2,13 @@ = render 'platforms/base/submenu' = render 'platforms/base/sidebar' -%div - = "#{@platform.name}: #{@path}" +%h3 + = t('layout.platforms.contents_of') + = @platform.name += render 'contents' -%table.tablesorter.project{:cellpadding => "0", :cellspacing => "0"} - %tbody - - - if @path != '/' - %tr - %td= link_to '../', "#{platform_contents_path(@platform)}#{@path}/../" - %td - %td - - - (@contents.select(&:is_folder?) | @contents).each do |content| - %tr - %td - - if content.is_folder? - - pic = 'folder.png' - - path = "#{platform_contents_path(@platform)}#{@path}/#{content.name}" - .pic= image_tag pic || 'code.png' - .name= link_to(content.name, path || content.download_url, :class => 'files-see') - %td= link_to t('activerecord.models.build_list'), content.build_list if content.build_list - %td= number_to_human_size(content.size) unless content.is_folder? -.both - -= will_paginate @contents \ No newline at end of file +:javascript + $(function(){ + $('.pagination a').attr('data-remote', 'true'); + }); \ No newline at end of file diff --git a/app/views/platforms/contents/index.js.haml b/app/views/platforms/contents/index.js.haml new file mode 100644 index 000000000..2612b957a --- /dev/null +++ b/app/views/platforms/contents/index.js.haml @@ -0,0 +1,2 @@ +$('#contents').html("#{escape_javascript(render 'contents')}"); +$('.pagination a').attr('data-remote', 'true'); \ No newline at end of file From e18516533dfbf315a9b1163676b7ad89c365a5bd Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Jul 2013 14:47:17 +0400 Subject: [PATCH 03/10] #82: updated locales --- app/views/platforms/contents/_contents.html.haml | 2 +- config/locales/models/platform.en.yml | 3 +++ config/locales/models/platform.ru.yml | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/views/platforms/contents/_contents.html.haml b/app/views/platforms/contents/_contents.html.haml index 9b05c93d6..b2ef13345 100644 --- a/app/views/platforms/contents/_contents.html.haml +++ b/app/views/platforms/contents/_contents.html.haml @@ -1,7 +1,7 @@ #contents = form_for @platform, :url => platform_content_path(@platform, @path), :html => { :class => :form, :remote => true, :method => :get } do |f| - = tracker_search_field(:term, t('layout.issues.search_user')) + = tracker_search_field(:term, @term.present? ? @term : t('layout.platforms.search_contents')) = f.submit t('layout.search.header') %br diff --git a/config/locales/models/platform.en.yml b/config/locales/models/platform.en.yml index 4ef57f072..b41511fa9 100644 --- a/config/locales/models/platform.en.yml +++ b/config/locales/models/platform.en.yml @@ -1,6 +1,9 @@ en: layout: platforms: + contents: Contents + contents_of: Contents of + search_contents: Search name of file/folder... admin_id: Owner build_all: Build all list: List diff --git a/config/locales/models/platform.ru.yml b/config/locales/models/platform.ru.yml index 8d918053c..08cd6ce72 100644 --- a/config/locales/models/platform.ru.yml +++ b/config/locales/models/platform.ru.yml @@ -1,6 +1,9 @@ ru: layout: platforms: + contents: Содержимое + contents_of: Содержимое + search_contents: Найти имя файла/папки... admin_id: Владелец build_all: Собрать все list: Список From 280b54ca38af71e508279057414e36054b3a2070 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Jul 2013 16:08:53 +0400 Subject: [PATCH 04/10] #82: added specs of access rights for controller --- .../platforms/contents_controller_spec.rb | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 spec/controllers/platforms/contents_controller_spec.rb diff --git a/spec/controllers/platforms/contents_controller_spec.rb b/spec/controllers/platforms/contents_controller_spec.rb new file mode 100644 index 000000000..e5cca7814 --- /dev/null +++ b/spec/controllers/platforms/contents_controller_spec.rb @@ -0,0 +1,98 @@ +# -*- encoding : utf-8 -*- +require 'spec_helper' + +shared_examples_for 'content platform user without show rights for hidden platform' do + it 'should not be able to perform index action' do + @platform.update_column(:visibility, 'hidden') + get :index, :platform_id => @platform + response.should_not be_success + end +end + +shared_examples_for 'content platform user with show rights for hidden platform' do + it 'should be able to perform index action' do + @platform.update_column(:visibility, 'hidden') + get :index, :platform_id => @platform + response.should be_success + end +end + +shared_examples_for 'content platform user with show rights' do + it 'should be able to perform index action for main platform' do + get :index, :platform_id => @platform + response.should be_success + end + + it 'should be able to perform index action for personal platform' do + get :index, :platform_id => @personal_platform + response.should be_success + end +end + +describe Platforms::ContentsController do + before do + stub_symlink_methods + + @platform = FactoryGirl.create(:platform) + @personal_platform = FactoryGirl.create(:platform, :platform_type => 'personal') + + @user = FactoryGirl.create(:user) + end + + context 'for guest' do + + it 'should not be able to perform index action for main platform', :anonymous_access => false do + get :index, :platform_id => @platform + response.should_not be_success + end + + it 'should not be able to perform index action for personal platform', :anonymous_access => false do + get :index, :platform_id => @personal_platform + response.should_not be_success + end + + it_should_behave_like 'content platform user with show rights' if APP_CONFIG['anonymous_access'] + it_should_behave_like 'content platform user without show rights for hidden platform' + end + + context 'for global admin' do + before do + http_login(FactoryGirl.create(:admin)) + end + + it_should_behave_like 'content platform user with show rights' + it_should_behave_like 'content platform user with show rights for hidden platform' + end + + context 'for owner user' do + before do + http_login(@user) + @platform.owner = @user; @platform.save + @platform.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'admin') + end + + it_should_behave_like 'content platform user with show rights' + it_should_behave_like 'content platform user with show rights for hidden platform' + end + + context 'for member of platform' do + before do + http_login(@user) + @platform.add_member(@user) + @personal_platform.add_member(@user) + end + + it_should_behave_like 'content platform user with show rights' + it_should_behave_like 'content platform user with show rights for hidden platform' + end + + context 'for simple user' do + before do + http_login(@user) + end + + it_should_behave_like 'content platform user with show rights' + it_should_behave_like 'content platform user without show rights for hidden platform' + end + +end From ecfde4ad21fd333ad99a7e21a8dea6613040019b Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Jul 2013 17:50:24 +0400 Subject: [PATCH 05/10] #82: added specs for PlatformContent --- app/models/platform_content.rb | 2 +- spec/models/platform_content.rb | 71 +++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 spec/models/platform_content.rb diff --git a/app/models/platform_content.rb b/app/models/platform_content.rb index f5dbca177..299575434 100644 --- a/app/models/platform_content.rb +++ b/app/models/platform_content.rb @@ -16,7 +16,7 @@ class PlatformContent def build_list return @build_list if !!@build_list - return nil if path !~ /\/(release|updates)+\/\w/ + return nil if path !~ /\/(release|updates)+\/[\w\-\.]+$/ return nil unless repository_name = path.match(/\/[\w]+\/(release|updates)\//) repository_name = repository_name[0].gsub(/\/(release|updates)\/$/, '').gsub('/', '') diff --git a/spec/models/platform_content.rb b/spec/models/platform_content.rb new file mode 100644 index 000000000..1edb31e8f --- /dev/null +++ b/spec/models/platform_content.rb @@ -0,0 +1,71 @@ +# -*- encoding : utf-8 -*- +require 'spec_helper' + +describe PlatformContent do + subject { PlatformContent } + + before { stub_symlink_methods } + let!(:platform) { FactoryGirl.create(:platform) } + + context '#find_by_platform' do + before do + File.open(File.join(platform.path, 'test001'), "w") + File.open(File.join(platform.path, 'test002'), "w") + end + + it 'ensures that finds files' do + # + /repository folder + subject.find_by_platform(platform, '', '').should have(3).items + end + + context 'ensures that finds files by name' do + it { subject.find_by_platform(platform, '', 'test').should have(2).items } + it { subject.find_by_platform(platform, '', 'test001').should have(1).item } + it { subject.find_by_platform(platform, 'repository', 'test').should have(:no).items } + end + + end + + context '#is_folder?' do + it 'ensures that returns true for folder' do + subject.find_by_platform(platform, '', 'repository').first.is_folder? + .should be_true + end + + it 'ensures that returns false for file' do + File.open(File.join(platform.path, 'test001'), "w") + subject.find_by_platform(platform, '', 'test').first.is_folder? + .should be_false + end + end + + context '#build_list' do + let!(:package) { FactoryGirl.create(:build_list_package, :actual => true) } + let(:platform) { package.build_list.save_to_platform } + let(:repository) { platform.repositories.first } + + before do + File.open(File.join(platform.path, 'test001'), "w") + + package.build_list.update_column(:status, BuildList::BUILD_PUBLISHED) + path = File.join platform.path, 'repository', 'SRPMS', repository.name, 'release' + FileUtils.mkdir_p path + File.open(File.join(path, package.fullname), "w") + + path = File.join path, 'repodata' + FileUtils.mkdir_p path + File.open(File.join(path, package.fullname), "w") + end + + context 'ensures that returns nil for simple file' do + it { subject.find_by_platform(platform, '', 'test').first.build_list.should be_nil } + it { subject.find_by_platform(platform, "repository/SRPMS/#{repository.name}/release/repodata", '').first.build_list.should be_nil } + end + + it 'ensures that returns build_list for package' do + subject.find_by_platform(platform, "repository/SRPMS/#{repository.name}/release", package.fullname) + .first.build_list.should == package.build_list + end + end + +end From 9eaf62666b760f869a67e59f7e630cdc3ace68eb Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Jul 2013 18:15:54 +0400 Subject: [PATCH 06/10] #82: updated regexp --- app/models/platform_content.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/platform_content.rb b/app/models/platform_content.rb index 299575434..a72960105 100644 --- a/app/models/platform_content.rb +++ b/app/models/platform_content.rb @@ -66,9 +66,9 @@ class PlatformContent # --------------------- def self.find_by_platform(platform, path, term) - term = (term.present? && term =~ /\w/) ? term : '' + term = (term.present? && term =~ /^[\w]+$/) ? term : '' path = path.split(File::SEPARATOR) - .select{ |p| p.present? && p =~ /\w/ } + .select{ |p| p.present? && p =~ /^[\w]+$/ } .join(File::SEPARATOR) results = Dir.glob(File.join(platform.path, path, "*#{term}*")) if term From b3740de03029fe0ddd635964c8a8487920303c3a Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Jul 2013 18:20:06 +0400 Subject: [PATCH 07/10] #82: updated regexp for term --- app/models/platform_content.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/platform_content.rb b/app/models/platform_content.rb index a72960105..614431575 100644 --- a/app/models/platform_content.rb +++ b/app/models/platform_content.rb @@ -66,7 +66,7 @@ class PlatformContent # --------------------- def self.find_by_platform(platform, path, term) - term = (term.present? && term =~ /^[\w]+$/) ? term : '' + term = (term.present? && term =~ /^[\w\-\.]+$/) ? term : '' path = path.split(File::SEPARATOR) .select{ |p| p.present? && p =~ /^[\w]+$/ } .join(File::SEPARATOR) From f94cff88f4b4b069c199b026c97409688885af12 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Jul 2013 18:44:07 +0400 Subject: [PATCH 08/10] #82: Strip out the non-ascii character --- app/models/platform.rb | 2 +- app/models/platform_content.rb | 6 +++--- app/models/project.rb | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/platform.rb b/app/models/platform.rb index 71186790f..154821aba 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -1,7 +1,7 @@ # -*- encoding : utf-8 -*- class Platform < ActiveRecord::Base VISIBILITIES = %w(open hidden) - NAME_PATTERN = /[a-zA-Z0-9_\-\.]+/ + NAME_PATTERN = /[\w\-\.]+/ belongs_to :parent, :class_name => 'Platform', :foreign_key => 'parent_platform_id' belongs_to :owner, :polymorphic => true diff --git a/app/models/platform_content.rb b/app/models/platform_content.rb index 614431575..8e1987ccd 100644 --- a/app/models/platform_content.rb +++ b/app/models/platform_content.rb @@ -66,9 +66,9 @@ class PlatformContent # --------------------- def self.find_by_platform(platform, path, term) - term = (term.present? && term =~ /^[\w\-\.]+$/) ? term : '' - path = path.split(File::SEPARATOR) - .select{ |p| p.present? && p =~ /^[\w]+$/ } + term = (term.present? && term =~ /\A#{Project::NAME_REGEXP}\z/) ? term : '' + path = path.split(File::SEPARATOR).select(&:present?) + .map{ |p| p.gsub(/[^\w\-\.]/, '_') } # Strip out the non-ascii character .join(File::SEPARATOR) results = Dir.glob(File.join(platform.path, path, "*#{term}*")) if term diff --git a/app/models/project.rb b/app/models/project.rb index ac2ccafae..9c4ff4c63 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -2,7 +2,7 @@ class Project < ActiveRecord::Base VISIBILITIES = ['open', 'hidden'] MAX_OWN_PROJECTS = 32000 - NAME_REGEXP = /[a-zA-Z0-9_\-\+\.]+/ + NAME_REGEXP = /[\w\-\+\.]+/ belongs_to :owner, :polymorphic => true, :counter_cache => :own_projects_count belongs_to :maintainer, :class_name => "User" From 446a87da046ae24954a17a75288155e10c93d46f Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Jul 2013 19:08:55 +0400 Subject: [PATCH 09/10] #82: sanitize path and term --- app/models/platform_content.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/platform_content.rb b/app/models/platform_content.rb index 8e1987ccd..d433b1cc3 100644 --- a/app/models/platform_content.rb +++ b/app/models/platform_content.rb @@ -66,10 +66,19 @@ class PlatformContent # --------------------- def self.find_by_platform(platform, path, term) - term = (term.present? && term =~ /\A#{Project::NAME_REGEXP}\z/) ? term : '' + # Strip out the non-ascii character + term = (term || '').strip.gsub(/[\\\/]+/, '') + .gsub(/[^\w\-\+\.]/, '_') + path = path.split(File::SEPARATOR).select(&:present?) - .map{ |p| p.gsub(/[^\w\-\.]/, '_') } # Strip out the non-ascii character + .map{ |p| + # Strip out the non-ascii character + p.strip.gsub(/[\\\/]+/, '') + .gsub(/^[\.]+/, '') + .gsub(/[^\w\-\.]/, '_') + } .join(File::SEPARATOR) + puts path.inspect results = Dir.glob(File.join(platform.path, path, "*#{term}*")) if term results = results.sort_by(&:length) From a590444730e34c8bcde1241c09e76e2668db696d Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 8 Jul 2013 19:12:33 +0400 Subject: [PATCH 10/10] #82: small refactoring --- app/models/platform_content.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/models/platform_content.rb b/app/models/platform_content.rb index d433b1cc3..fc98013ba 100644 --- a/app/models/platform_content.rb +++ b/app/models/platform_content.rb @@ -68,17 +68,16 @@ class PlatformContent def self.find_by_platform(platform, path, term) # Strip out the non-ascii character term = (term || '').strip.gsub(/[\\\/]+/, '') - .gsub(/[^\w\-\+\.]/, '_') + .gsub(/[^\w\-\+\.]/, '_') - path = path.split(File::SEPARATOR).select(&:present?) + path = path.split(File::SEPARATOR).map(&:strip).select(&:present?) .map{ |p| # Strip out the non-ascii character - p.strip.gsub(/[\\\/]+/, '') + p.gsub(/[\\\/]+/, '') .gsub(/^[\.]+/, '') .gsub(/[^\w\-\.]/, '_') } .join(File::SEPARATOR) - puts path.inspect results = Dir.glob(File.join(platform.path, path, "*#{term}*")) if term results = results.sort_by(&:length)