Commit 38e1a32782d0a2f5d66a936b707f307ceb13288f
Committed by
Antonio Terceiro
1 parent
cb62ddad
Exists in
master
and in
22 other branches
Use ~ as placeholder for current user in URLs
When the :profile parameter is '~', replace it with the identifier of the currently logged-in user and redirect. This is useful for example for adding direct links to the control panel of the current user in documentation. Signed-off-by: Antonio Terceiro <terceiro@colivre.coop.br> Signed-off-by: Arthur Del Esposte <arthurmde@gmail.com> Signed-off-by: David Carlos <ddavidcarlos1392@gmail.com> Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
Showing
5 changed files
with
60 additions
and
20 deletions
Show diff stats
app/controllers/application_controller.rb
| ... | ... | @@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base |
| 9 | 9 | before_filter :allow_cross_domain_access |
| 10 | 10 | before_filter :login_required, :if => :private_environment? |
| 11 | 11 | before_filter :verify_members_whitelist, :if => [:private_environment?, :user] |
| 12 | + before_filter :redirect_to_current_user | |
| 12 | 13 | |
| 13 | 14 | def verify_members_whitelist |
| 14 | 15 | render_access_denied unless user.is_admin? || environment.in_whitelist?(user) |
| ... | ... | @@ -191,4 +192,15 @@ class ApplicationController < ActionController::Base |
| 191 | 192 | def private_environment? |
| 192 | 193 | @environment.enabled?(:restrict_to_members) |
| 193 | 194 | end |
| 195 | + | |
| 196 | + def redirect_to_current_user | |
| 197 | + if params[:profile] == '~' | |
| 198 | + if logged_in? | |
| 199 | + redirect_to params.merge(:profile => user.identifier) | |
| 200 | + else | |
| 201 | + render_not_found | |
| 202 | + end | |
| 203 | + end | |
| 204 | + end | |
| 205 | + | |
| 194 | 206 | end | ... | ... |
config/routes.rb
| ... | ... | @@ -57,37 +57,37 @@ Noosfero::Application.routes.draw do |
| 57 | 57 | match 'search(/:action(/*category_path))', :controller => 'search' |
| 58 | 58 | |
| 59 | 59 | # events |
| 60 | - match 'profile/:profile/events_by_day', :controller => 'events', :action => 'events_by_day', :profile => /#{Noosfero.identifier_format}/ | |
| 61 | - match 'profile/:profile/events_by_month', :controller => 'events', :action => 'events_by_month', :profile => /#{Noosfero.identifier_format}/ | |
| 62 | - match 'profile/:profile/events/:year/:month/:day', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :day => /\d*/, :profile => /#{Noosfero.identifier_format}/ | |
| 63 | - match 'profile/:profile/events/:year/:month', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :profile => /#{Noosfero.identifier_format}/ | |
| 64 | - match 'profile/:profile/events', :controller => 'events', :action => 'events', :profile => /#{Noosfero.identifier_format}/ | |
| 60 | + match 'profile/:profile/events_by_day', :controller => 'events', :action => 'events_by_day', :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 61 | + match 'profile/:profile/events_by_month', :controller => 'events', :action => 'events_by_month', :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 62 | + match 'profile/:profile/events/:year/:month/:day', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :day => /\d*/, :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 63 | + match 'profile/:profile/events/:year/:month', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 64 | + match 'profile/:profile/events', :controller => 'events', :action => 'events', :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 65 | 65 | |
| 66 | 66 | # catalog |
| 67 | - match 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format}/, :as => :catalog | |
| 67 | + match 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/, :as => :catalog | |
| 68 | 68 | |
| 69 | 69 | # invite |
| 70 | - match 'profile/:profile/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => /#{Noosfero.identifier_format}/ | |
| 71 | - match 'profile/:profile/invite/:action', :controller => 'invite', :profile => /#{Noosfero.identifier_format}/ | |
| 70 | + match 'profile/:profile/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 71 | + match 'profile/:profile/invite/:action', :controller => 'invite', :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 72 | 72 | |
| 73 | 73 | # feeds per tag |
| 74 | - match 'profile/:profile/tags/:id/feed', :controller => 'profile', :action =>'tag_feed', :id => /.+/, :profile => /#{Noosfero.identifier_format}/, :as => :tag_feed | |
| 74 | + match 'profile/:profile/tags/:id/feed', :controller => 'profile', :action =>'tag_feed', :id => /.+/, :profile => /#{Noosfero.identifier_format_in_url}/, :as => :tag_feed | |
| 75 | 75 | |
| 76 | 76 | # profile tags |
| 77 | - match 'profile/:profile/tags/:id', :controller => 'profile', :action => 'content_tagged', :id => /.+/, :profile => /#{Noosfero.identifier_format}/ | |
| 78 | - match 'profile/:profile/tags(/:id)', :controller => 'profile', :action => 'tags', :profile => /#{Noosfero.identifier_format}/ | |
| 77 | + match 'profile/:profile/tags/:id', :controller => 'profile', :action => 'content_tagged', :id => /.+/, :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 78 | + match 'profile/:profile/tags(/:id)', :controller => 'profile', :action => 'tags', :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 79 | 79 | |
| 80 | 80 | # profile search |
| 81 | - match 'profile/:profile/search', :controller => 'profile_search', :action => 'index', :profile => /#{Noosfero.identifier_format}/ | |
| 81 | + match 'profile/:profile/search', :controller => 'profile_search', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 82 | 82 | |
| 83 | 83 | # comments |
| 84 | - match 'profile/:profile/comment/:action/:id', :controller => 'comment', :profile => /#{Noosfero.identifier_format}/ | |
| 84 | + match 'profile/:profile/comment/:action/:id', :controller => 'comment', :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 85 | 85 | |
| 86 | 86 | # public profile information |
| 87 | - match 'profile/:profile(/:action(/:id))', :controller => 'profile', :action => 'index', :id => /[^\/]*/, :profile => /#{Noosfero.identifier_format}/, :as => :profile | |
| 87 | + match 'profile/:profile(/:action(/:id))', :controller => 'profile', :action => 'index', :id => /[^\/]*/, :profile => /#{Noosfero.identifier_format_in_url}/, :as => :profile | |
| 88 | 88 | |
| 89 | 89 | # contact |
| 90 | - match 'contact/:profile/:action(/:id)', :controller => 'contact', :action => 'index', :id => /.*/, :profile => /#{Noosfero.identifier_format}/ | |
| 90 | + match 'contact/:profile/:action(/:id)', :controller => 'contact', :action => 'index', :id => /.*/, :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 91 | 91 | |
| 92 | 92 | # map balloon |
| 93 | 93 | match 'map_balloon/:action/:id', :controller => 'map_balloon', :id => /.*/ |
| ... | ... | @@ -99,8 +99,8 @@ Noosfero::Application.routes.draw do |
| 99 | 99 | ## Controllers that are profile-specific (for profile admins ) |
| 100 | 100 | ###################################################### |
| 101 | 101 | # profile customization - "My profile" |
| 102 | - match 'myprofile/:profile', :controller => 'profile_editor', :action => 'index', :profile => /#{Noosfero.identifier_format}/ | |
| 103 | - match 'myprofile/:profile/:controller(/:action(/:id))', :controller => Noosfero.pattern_for_controllers_in_directory('my_profile'), :profile => /#{Noosfero.identifier_format}/, :as => :myprofile | |
| 102 | + match 'myprofile/:profile', :controller => 'profile_editor', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/ | |
| 103 | + match 'myprofile/:profile/:controller(/:action(/:id))', :controller => Noosfero.pattern_for_controllers_in_directory('my_profile'), :profile => /#{Noosfero.identifier_format_in_url}/, :as => :myprofile | |
| 104 | 104 | |
| 105 | 105 | |
| 106 | 106 | ###################################################### |
| ... | ... | @@ -128,14 +128,14 @@ Noosfero::Application.routes.draw do |
| 128 | 128 | # cache stuff - hack |
| 129 | 129 | match 'public/:action/:id', :controller => 'public' |
| 130 | 130 | |
| 131 | - match ':profile/*page/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => /#{Noosfero.identifier_format}/, :constraints => EnvironmentDomainConstraint.new | |
| 131 | + match ':profile/*page/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new | |
| 132 | 132 | match '*page/versions', :controller => 'content_viewer', :action => 'article_versions' |
| 133 | 133 | |
| 134 | - match ':profile/*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff', :profile => /#{Noosfero.identifier_format}/, :constraints => EnvironmentDomainConstraint.new | |
| 134 | + match ':profile/*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new | |
| 135 | 135 | match '*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff' |
| 136 | 136 | |
| 137 | 137 | # match requests for profiles that don't have a custom domain |
| 138 | - match ':profile(/*page)', :controller => 'content_viewer', :action => 'view_page', :profile => /#{Noosfero.identifier_format}/, :constraints => EnvironmentDomainConstraint.new | |
| 138 | + match ':profile(/*page)', :controller => 'content_viewer', :action => 'view_page', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new | |
| 139 | 139 | |
| 140 | 140 | # match requests for content in domains hosted for profiles |
| 141 | 141 | match '/(*page)', :controller => 'content_viewer', :action => 'view_page' | ... | ... |
lib/noosfero.rb
| ... | ... | @@ -57,6 +57,12 @@ module Noosfero |
| 57 | 57 | '[a-z0-9][a-z0-9~.]*([_\-][a-z0-9~.]+)*' |
| 58 | 58 | end |
| 59 | 59 | |
| 60 | + # All valid identifiers, plus ~ meaning "the current user". See | |
| 61 | + # ApplicationController#redirect_to_current_user | |
| 62 | + def self.identifier_format_in_url | |
| 63 | + "(#{identifier_format}|~)" | |
| 64 | + end | |
| 65 | + | |
| 60 | 66 | def self.default_hostname |
| 61 | 67 | Environment.table_exists? && Environment.default ? Environment.default.default_hostname : 'localhost' |
| 62 | 68 | end | ... | ... |
test/functional/application_controller_test.rb
| ... | ... | @@ -578,4 +578,22 @@ class ApplicationControllerTest < ActionController::TestCase |
| 578 | 578 | assert_response :success |
| 579 | 579 | end |
| 580 | 580 | |
| 581 | + should "redirect to 404 if profile is '~' and user is not logged in" do | |
| 582 | + get :index, :profile => '~' | |
| 583 | + assert_response :missing | |
| 584 | + end | |
| 585 | + | |
| 586 | + should "redirect to action when profile is '~' " do | |
| 587 | + login_as('ze') | |
| 588 | + get :index, :profile => '~' | |
| 589 | + assert_response 302 | |
| 590 | + end | |
| 591 | + | |
| 592 | + should "substitute '~' by current user and redirect properly " do | |
| 593 | + login_as('ze') | |
| 594 | + profile = Profile.where(:identifier => 'ze').first | |
| 595 | + get :index, :profile => '~' | |
| 596 | + assert_redirected_to :controller => 'test', :action => 'index', :profile => profile.identifier | |
| 597 | + end | |
| 598 | + | |
| 581 | 599 | end | ... | ... |
test/integration/routing_test.rb
| ... | ... | @@ -272,4 +272,8 @@ class RoutingTest < ActionController::IntegrationTest |
| 272 | 272 | assert_routing('/embed/block/12345', :controller => 'embed', :action => 'block', :id => '12345') |
| 273 | 273 | end |
| 274 | 274 | |
| 275 | + should 'accept ~ as placeholder for current user' do | |
| 276 | + assert_routing('/profile/~', :controller => 'profile', :profile => '~', :action => 'index') | |
| 277 | + end | |
| 278 | + | |
| 275 | 279 | end | ... | ... |