Skip to content

Commit

Permalink
More mopping up: rename routes for sessions, add utility methods from h…
Browse files Browse the repository at this point in the history
…ttp://github.com/binarylogic/authlogic_example . More specs passing, though not all yet. [#19]
  • Loading branch information
marnen committed Oct 13, 2009
1 parent d80d351 commit b9cf03a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
22 changes: 22 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -68,4 +68,26 @@ def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end

def require_user
unless current_user
store_location
flash[:notice] = _("Please log in to view this page.")
redirect_to new_user_session_url
return false
end
end

def require_no_user
if current_user
store_location
flash[:notice] = _("Please log out to view this page.")
redirect_to account_url
return false
end
end

def store_location
session[:return_to] = request.request_uri
end
end
2 changes: 1 addition & 1 deletion app/controllers/permissions_controller.rb
@@ -1,7 +1,7 @@
class PermissionsController < ApplicationController
@@nonadmin = :index, :subscribe, :destroy
before_filter :check_admin, :except => @@nonadmin
before_filter :login_required, :only => @@nonadmin
before_filter :require_user, :only => @@nonadmin
layout 'standard'

class NotDeletableError < RuntimeError
Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Expand Up @@ -26,11 +26,11 @@

# For restful_authentication:
map.resources :users
map.resource :session
map.resource :user_session

# And some prettification...
map.login 'login', :controller => 'sessions', :action => 'new'
map.logout 'logout', :controller => 'sessions', :action => 'destroy'
map.login 'login', :controller => 'user_sessions', :action => 'new'
map.logout 'logout', :controller => 'user_sessions', :action => 'destroy'
map.register 'register', :controller => 'users', :action => 'new'
map.reset_password 'reset', :controller => 'users', :action => 'reset'
map.regenerate_key 'regenerate_key', :controller => 'users', :action => 'regenerate_key'
Expand Down

0 comments on commit b9cf03a

Please sign in to comment.