From 38e1a32782d0a2f5d66a936b707f307ceb13288f Mon Sep 17 00:00:00 2001 From: David Carlos Date: Thu, 12 Mar 2015 14:59:38 -0300 Subject: [PATCH] Use ~ as placeholder for current user in URLs --- app/controllers/application_controller.rb | 12 ++++++++++++ config/routes.rb | 40 ++++++++++++++++++++-------------------- lib/noosfero.rb | 6 ++++++ test/functional/application_controller_test.rb | 18 ++++++++++++++++++ test/integration/routing_test.rb | 4 ++++ 5 files changed, 60 insertions(+), 20 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2bb0835..75fb3fd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base before_filter :allow_cross_domain_access before_filter :login_required, :if => :private_environment? before_filter :verify_members_whitelist, :if => [:private_environment?, :user] + before_filter :redirect_to_current_user def verify_members_whitelist render_access_denied unless user.is_admin? || environment.in_whitelist?(user) @@ -191,4 +192,15 @@ class ApplicationController < ActionController::Base def private_environment? @environment.enabled?(:restrict_to_members) end + + def redirect_to_current_user + if params[:profile] == '~' + if logged_in? + redirect_to params.merge(:profile => user.identifier) + else + render_not_found + end + end + end + end diff --git a/config/routes.rb b/config/routes.rb index f370954..a54ea19 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -57,37 +57,37 @@ Noosfero::Application.routes.draw do match 'search(/:action(/*category_path))', :controller => 'search' # events - match 'profile/:profile/events_by_day', :controller => 'events', :action => 'events_by_day', :profile => /#{Noosfero.identifier_format}/ - match 'profile/:profile/events_by_month', :controller => 'events', :action => 'events_by_month', :profile => /#{Noosfero.identifier_format}/ - match 'profile/:profile/events/:year/:month/:day', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :day => /\d*/, :profile => /#{Noosfero.identifier_format}/ - match 'profile/:profile/events/:year/:month', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :profile => /#{Noosfero.identifier_format}/ - match 'profile/:profile/events', :controller => 'events', :action => 'events', :profile => /#{Noosfero.identifier_format}/ + match 'profile/:profile/events_by_day', :controller => 'events', :action => 'events_by_day', :profile => /#{Noosfero.identifier_format_in_url}/ + match 'profile/:profile/events_by_month', :controller => 'events', :action => 'events_by_month', :profile => /#{Noosfero.identifier_format_in_url}/ + match 'profile/:profile/events/:year/:month/:day', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :day => /\d*/, :profile => /#{Noosfero.identifier_format_in_url}/ + match 'profile/:profile/events/:year/:month', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :profile => /#{Noosfero.identifier_format_in_url}/ + match 'profile/:profile/events', :controller => 'events', :action => 'events', :profile => /#{Noosfero.identifier_format_in_url}/ # catalog - match 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format}/, :as => :catalog + match 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/, :as => :catalog # invite - match 'profile/:profile/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => /#{Noosfero.identifier_format}/ - match 'profile/:profile/invite/:action', :controller => 'invite', :profile => /#{Noosfero.identifier_format}/ + match 'profile/:profile/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => /#{Noosfero.identifier_format_in_url}/ + match 'profile/:profile/invite/:action', :controller => 'invite', :profile => /#{Noosfero.identifier_format_in_url}/ # feeds per tag - match 'profile/:profile/tags/:id/feed', :controller => 'profile', :action =>'tag_feed', :id => /.+/, :profile => /#{Noosfero.identifier_format}/, :as => :tag_feed + match 'profile/:profile/tags/:id/feed', :controller => 'profile', :action =>'tag_feed', :id => /.+/, :profile => /#{Noosfero.identifier_format_in_url}/, :as => :tag_feed # profile tags - match 'profile/:profile/tags/:id', :controller => 'profile', :action => 'content_tagged', :id => /.+/, :profile => /#{Noosfero.identifier_format}/ - match 'profile/:profile/tags(/:id)', :controller => 'profile', :action => 'tags', :profile => /#{Noosfero.identifier_format}/ + match 'profile/:profile/tags/:id', :controller => 'profile', :action => 'content_tagged', :id => /.+/, :profile => /#{Noosfero.identifier_format_in_url}/ + match 'profile/:profile/tags(/:id)', :controller => 'profile', :action => 'tags', :profile => /#{Noosfero.identifier_format_in_url}/ # profile search - match 'profile/:profile/search', :controller => 'profile_search', :action => 'index', :profile => /#{Noosfero.identifier_format}/ + match 'profile/:profile/search', :controller => 'profile_search', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/ # comments - match 'profile/:profile/comment/:action/:id', :controller => 'comment', :profile => /#{Noosfero.identifier_format}/ + match 'profile/:profile/comment/:action/:id', :controller => 'comment', :profile => /#{Noosfero.identifier_format_in_url}/ # public profile information - match 'profile/:profile(/:action(/:id))', :controller => 'profile', :action => 'index', :id => /[^\/]*/, :profile => /#{Noosfero.identifier_format}/, :as => :profile + match 'profile/:profile(/:action(/:id))', :controller => 'profile', :action => 'index', :id => /[^\/]*/, :profile => /#{Noosfero.identifier_format_in_url}/, :as => :profile # contact - match 'contact/:profile/:action(/:id)', :controller => 'contact', :action => 'index', :id => /.*/, :profile => /#{Noosfero.identifier_format}/ + match 'contact/:profile/:action(/:id)', :controller => 'contact', :action => 'index', :id => /.*/, :profile => /#{Noosfero.identifier_format_in_url}/ # map balloon match 'map_balloon/:action/:id', :controller => 'map_balloon', :id => /.*/ @@ -99,8 +99,8 @@ Noosfero::Application.routes.draw do ## Controllers that are profile-specific (for profile admins ) ###################################################### # profile customization - "My profile" - match 'myprofile/:profile', :controller => 'profile_editor', :action => 'index', :profile => /#{Noosfero.identifier_format}/ - match 'myprofile/:profile/:controller(/:action(/:id))', :controller => Noosfero.pattern_for_controllers_in_directory('my_profile'), :profile => /#{Noosfero.identifier_format}/, :as => :myprofile + match 'myprofile/:profile', :controller => 'profile_editor', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/ + match 'myprofile/:profile/:controller(/:action(/:id))', :controller => Noosfero.pattern_for_controllers_in_directory('my_profile'), :profile => /#{Noosfero.identifier_format_in_url}/, :as => :myprofile ###################################################### @@ -128,14 +128,14 @@ Noosfero::Application.routes.draw do # cache stuff - hack match 'public/:action/:id', :controller => 'public' - match ':profile/*page/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => /#{Noosfero.identifier_format}/, :constraints => EnvironmentDomainConstraint.new + match ':profile/*page/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new match '*page/versions', :controller => 'content_viewer', :action => 'article_versions' - match ':profile/*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff', :profile => /#{Noosfero.identifier_format}/, :constraints => EnvironmentDomainConstraint.new + match ':profile/*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new match '*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff' # match requests for profiles that don't have a custom domain - match ':profile(/*page)', :controller => 'content_viewer', :action => 'view_page', :profile => /#{Noosfero.identifier_format}/, :constraints => EnvironmentDomainConstraint.new + match ':profile(/*page)', :controller => 'content_viewer', :action => 'view_page', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new # match requests for content in domains hosted for profiles match '/(*page)', :controller => 'content_viewer', :action => 'view_page' diff --git a/lib/noosfero.rb b/lib/noosfero.rb index d7ec786..b1ae492 100644 --- a/lib/noosfero.rb +++ b/lib/noosfero.rb @@ -57,6 +57,12 @@ module Noosfero '[a-z0-9][a-z0-9~.]*([_\-][a-z0-9~.]+)*' end + # All valid identifiers, plus ~ meaning "the current user". See + # ApplicationController#redirect_to_current_user + def self.identifier_format_in_url + "(#{identifier_format}|~)" + end + def self.default_hostname Environment.table_exists? && Environment.default ? Environment.default.default_hostname : 'localhost' end diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index 1c4ca48..1a99752 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -578,4 +578,22 @@ class ApplicationControllerTest < ActionController::TestCase assert_response :success end + should "redirect to 404 if profile is '~' and user is not logged in" do + get :index, :profile => '~' + assert_response :missing + end + + should "redirect to action when profile is '~' " do + login_as('ze') + get :index, :profile => '~' + assert_response 302 + end + + should "substitute '~' by current user and redirect properly " do + login_as('ze') + profile = Profile.where(:identifier => 'ze').first + get :index, :profile => '~' + assert_redirected_to :controller => 'test', :action => 'index', :profile => profile.identifier + end + end diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index 5a77eff..1c57323 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -272,4 +272,8 @@ class RoutingTest < ActionController::IntegrationTest assert_routing('/embed/block/12345', :controller => 'embed', :action => 'block', :id => '12345') end + should 'accept ~ as placeholder for current user' do + assert_routing('/profile/~', :controller => 'profile', :profile => '~', :action => 'index') + end + end -- libgit2 0.21.2