Commit b3632c6b6c4515003db31b6c394080c52210c26e
Exists in
master
and in
22 other branches
Merge branch 'alternative_redirect_behavior' into 'next'
Add alternative redirect's behavior when user is '~' This is a new feature to create generic URL's related to usernames in Noosfero. It is useful for internal links on articles and for general links on themes. Examples: - If you're logged in as "test" and you go to /~/blog it will redirect you to /test/blog - If you're logged in as "test" and you go to /myprofile/~ it will redirect you to /myprofile/test - If you're not logged in and you go to /profile/~ or any other link using "~" it will give you a not found page(404). See merge request !518
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,6 +9,7 @@ class ApplicationController < ActionController::Base | ||
9 | before_filter :allow_cross_domain_access | 9 | before_filter :allow_cross_domain_access |
10 | before_filter :login_required, :if => :private_environment? | 10 | before_filter :login_required, :if => :private_environment? |
11 | before_filter :verify_members_whitelist, :if => [:private_environment?, :user] | 11 | before_filter :verify_members_whitelist, :if => [:private_environment?, :user] |
12 | + before_filter :redirect_to_current_user | ||
12 | 13 | ||
13 | def verify_members_whitelist | 14 | def verify_members_whitelist |
14 | render_access_denied unless user.is_admin? || environment.in_whitelist?(user) | 15 | render_access_denied unless user.is_admin? || environment.in_whitelist?(user) |
@@ -192,4 +193,15 @@ class ApplicationController < ActionController::Base | @@ -192,4 +193,15 @@ class ApplicationController < ActionController::Base | ||
192 | def private_environment? | 193 | def private_environment? |
193 | @environment.enabled?(:restrict_to_members) | 194 | @environment.enabled?(:restrict_to_members) |
194 | end | 195 | end |
196 | + | ||
197 | + def redirect_to_current_user | ||
198 | + if params[:profile] == '~' | ||
199 | + if logged_in? | ||
200 | + redirect_to params.merge(:profile => user.identifier) | ||
201 | + else | ||
202 | + render_not_found | ||
203 | + end | ||
204 | + end | ||
205 | + end | ||
206 | + | ||
195 | end | 207 | end |
config/routes.rb
@@ -56,37 +56,37 @@ Noosfero::Application.routes.draw do | @@ -56,37 +56,37 @@ Noosfero::Application.routes.draw do | ||
56 | match 'search(/:action(/*category_path))', :controller => 'search' | 56 | match 'search(/:action(/*category_path))', :controller => 'search' |
57 | 57 | ||
58 | # events | 58 | # events |
59 | - match 'profile/:profile/events_by_day', :controller => 'events', :action => 'events_by_day', :profile => /#{Noosfero.identifier_format}/ | ||
60 | - match 'profile/:profile/events_by_month', :controller => 'events', :action => 'events_by_month', :profile => /#{Noosfero.identifier_format}/ | ||
61 | - match 'profile/:profile/events/:year/:month/:day', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :day => /\d*/, :profile => /#{Noosfero.identifier_format}/ | ||
62 | - match 'profile/:profile/events/:year/:month', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :profile => /#{Noosfero.identifier_format}/ | ||
63 | - match 'profile/:profile/events', :controller => 'events', :action => 'events', :profile => /#{Noosfero.identifier_format}/ | 59 | + match 'profile/:profile/events_by_day', :controller => 'events', :action => 'events_by_day', :profile => /#{Noosfero.identifier_format_in_url}/ |
60 | + match 'profile/:profile/events_by_month', :controller => 'events', :action => 'events_by_month', :profile => /#{Noosfero.identifier_format_in_url}/ | ||
61 | + match 'profile/:profile/events/:year/:month/:day', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :day => /\d*/, :profile => /#{Noosfero.identifier_format_in_url}/ | ||
62 | + match 'profile/:profile/events/:year/:month', :controller => 'events', :action => 'events', :year => /\d*/, :month => /\d*/, :profile => /#{Noosfero.identifier_format_in_url}/ | ||
63 | + match 'profile/:profile/events', :controller => 'events', :action => 'events', :profile => /#{Noosfero.identifier_format_in_url}/ | ||
64 | 64 | ||
65 | # catalog | 65 | # catalog |
66 | - match 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format}/, :as => :catalog | 66 | + match 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/, :as => :catalog |
67 | 67 | ||
68 | # invite | 68 | # invite |
69 | - match 'profile/:profile/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => /#{Noosfero.identifier_format}/ | ||
70 | - match 'profile/:profile/invite/:action', :controller => 'invite', :profile => /#{Noosfero.identifier_format}/ | 69 | + match 'profile/:profile/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => /#{Noosfero.identifier_format_in_url}/ |
70 | + match 'profile/:profile/invite/:action', :controller => 'invite', :profile => /#{Noosfero.identifier_format_in_url}/ | ||
71 | 71 | ||
72 | # feeds per tag | 72 | # feeds per tag |
73 | - match 'profile/:profile/tags/:id/feed', :controller => 'profile', :action =>'tag_feed', :id => /.+/, :profile => /#{Noosfero.identifier_format}/, :as => :tag_feed | 73 | + match 'profile/:profile/tags/:id/feed', :controller => 'profile', :action =>'tag_feed', :id => /.+/, :profile => /#{Noosfero.identifier_format_in_url}/, :as => :tag_feed |
74 | 74 | ||
75 | # profile tags | 75 | # profile tags |
76 | - match 'profile/:profile/tags/:id', :controller => 'profile', :action => 'content_tagged', :id => /.+/, :profile => /#{Noosfero.identifier_format}/ | ||
77 | - match 'profile/:profile/tags(/:id)', :controller => 'profile', :action => 'tags', :profile => /#{Noosfero.identifier_format}/ | 76 | + match 'profile/:profile/tags/:id', :controller => 'profile', :action => 'content_tagged', :id => /.+/, :profile => /#{Noosfero.identifier_format_in_url}/ |
77 | + match 'profile/:profile/tags(/:id)', :controller => 'profile', :action => 'tags', :profile => /#{Noosfero.identifier_format_in_url}/ | ||
78 | 78 | ||
79 | # profile search | 79 | # profile search |
80 | - match 'profile/:profile/search', :controller => 'profile_search', :action => 'index', :profile => /#{Noosfero.identifier_format}/ | 80 | + match 'profile/:profile/search', :controller => 'profile_search', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/ |
81 | 81 | ||
82 | # comments | 82 | # comments |
83 | - match 'profile/:profile/comment/:action/:id', :controller => 'comment', :profile => /#{Noosfero.identifier_format}/ | 83 | + match 'profile/:profile/comment/:action/:id', :controller => 'comment', :profile => /#{Noosfero.identifier_format_in_url}/ |
84 | 84 | ||
85 | # public profile information | 85 | # public profile information |
86 | - match 'profile/:profile(/:action(/:id))', :controller => 'profile', :action => 'index', :id => /[^\/]*/, :profile => /#{Noosfero.identifier_format}/, :as => :profile | 86 | + match 'profile/:profile(/:action(/:id))', :controller => 'profile', :action => 'index', :id => /[^\/]*/, :profile => /#{Noosfero.identifier_format_in_url}/, :as => :profile |
87 | 87 | ||
88 | # contact | 88 | # contact |
89 | - match 'contact/:profile/:action(/:id)', :controller => 'contact', :action => 'index', :id => /.*/, :profile => /#{Noosfero.identifier_format}/ | 89 | + match 'contact/:profile/:action(/:id)', :controller => 'contact', :action => 'index', :id => /.*/, :profile => /#{Noosfero.identifier_format_in_url}/ |
90 | 90 | ||
91 | # map balloon | 91 | # map balloon |
92 | match 'map_balloon/:action/:id', :controller => 'map_balloon', :id => /.*/ | 92 | match 'map_balloon/:action/:id', :controller => 'map_balloon', :id => /.*/ |
@@ -98,8 +98,8 @@ Noosfero::Application.routes.draw do | @@ -98,8 +98,8 @@ Noosfero::Application.routes.draw do | ||
98 | ## Controllers that are profile-specific (for profile admins ) | 98 | ## Controllers that are profile-specific (for profile admins ) |
99 | ###################################################### | 99 | ###################################################### |
100 | # profile customization - "My profile" | 100 | # profile customization - "My profile" |
101 | - match 'myprofile/:profile', :controller => 'profile_editor', :action => 'index', :profile => /#{Noosfero.identifier_format}/ | ||
102 | - match 'myprofile/:profile/:controller(/:action(/:id))', :controller => Noosfero.pattern_for_controllers_in_directory('my_profile'), :profile => /#{Noosfero.identifier_format}/, :as => :myprofile | 101 | + match 'myprofile/:profile', :controller => 'profile_editor', :action => 'index', :profile => /#{Noosfero.identifier_format_in_url}/ |
102 | + match 'myprofile/:profile/:controller(/:action(/:id))', :controller => Noosfero.pattern_for_controllers_in_directory('my_profile'), :profile => /#{Noosfero.identifier_format_in_url}/, :as => :myprofile | ||
103 | 103 | ||
104 | 104 | ||
105 | ###################################################### | 105 | ###################################################### |
@@ -127,14 +127,14 @@ Noosfero::Application.routes.draw do | @@ -127,14 +127,14 @@ Noosfero::Application.routes.draw do | ||
127 | # cache stuff - hack | 127 | # cache stuff - hack |
128 | match 'public/:action/:id', :controller => 'public' | 128 | match 'public/:action/:id', :controller => 'public' |
129 | 129 | ||
130 | - match ':profile/*page/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => /#{Noosfero.identifier_format}/, :constraints => EnvironmentDomainConstraint.new | 130 | + match ':profile/*page/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new |
131 | match '*page/versions', :controller => 'content_viewer', :action => 'article_versions' | 131 | match '*page/versions', :controller => 'content_viewer', :action => 'article_versions' |
132 | 132 | ||
133 | - match ':profile/*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff', :profile => /#{Noosfero.identifier_format}/, :constraints => EnvironmentDomainConstraint.new | 133 | + match ':profile/*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new |
134 | match '*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff' | 134 | match '*page/versions_diff', :controller => 'content_viewer', :action => 'versions_diff' |
135 | 135 | ||
136 | # match requests for profiles that don't have a custom domain | 136 | # match requests for profiles that don't have a custom domain |
137 | - match ':profile(/*page)', :controller => 'content_viewer', :action => 'view_page', :profile => /#{Noosfero.identifier_format}/, :constraints => EnvironmentDomainConstraint.new | 137 | + match ':profile(/*page)', :controller => 'content_viewer', :action => 'view_page', :profile => /#{Noosfero.identifier_format_in_url}/, :constraints => EnvironmentDomainConstraint.new |
138 | 138 | ||
139 | # match requests for content in domains hosted for profiles | 139 | # match requests for content in domains hosted for profiles |
140 | match '/(*page)', :controller => 'content_viewer', :action => 'view_page' | 140 | match '/(*page)', :controller => 'content_viewer', :action => 'view_page' |
lib/noosfero.rb
@@ -57,6 +57,12 @@ module Noosfero | @@ -57,6 +57,12 @@ module Noosfero | ||
57 | '[a-z0-9][a-z0-9~.]*([_\-][a-z0-9~.]+)*' | 57 | '[a-z0-9][a-z0-9~.]*([_\-][a-z0-9~.]+)*' |
58 | end | 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 | def self.default_hostname | 66 | def self.default_hostname |
61 | Environment.table_exists? && Environment.default ? Environment.default.default_hostname : 'localhost' | 67 | Environment.table_exists? && Environment.default ? Environment.default.default_hostname : 'localhost' |
62 | end | 68 | end |
test/functional/application_controller_test.rb
@@ -578,4 +578,22 @@ class ApplicationControllerTest < ActionController::TestCase | @@ -578,4 +578,22 @@ class ApplicationControllerTest < ActionController::TestCase | ||
578 | assert_response :success | 578 | assert_response :success |
579 | end | 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 | end | 599 | end |
test/integration/routing_test.rb
@@ -272,4 +272,8 @@ class RoutingTest < ActionController::IntegrationTest | @@ -272,4 +272,8 @@ class RoutingTest < ActionController::IntegrationTest | ||
272 | assert_routing('/embed/block/12345', :controller => 'embed', :action => 'block', :id => '12345') | 272 | assert_routing('/embed/block/12345', :controller => 'embed', :action => 'block', :id => '12345') |
273 | end | 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 | end | 279 | end |