We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Fork of DocSavage/rails-authorization-plugin
Description: This plugin provides a flexible way to add authorization to Rails.
Homepage: http://www.writertopia.com/developers/authorization
Clone URL: git://github.com/bwyrosdick/rails-authorization-plugin.git
[PATCH] Allow different redirection locations for permission denied or 
login required.

Replaced DEFAULT_REDIRECTION_HASH with LOGIN_REQUIRED_REDIRECTION and
PERMISSION_DENIED_REDIRECTION to allow different redirection locations
for the following two cases:

  * User is not logged in -> LOGIN_REQUIRED_REDIRECTION
  * User is logged in, but not authorized to do something ->
    PERMISSION_DENIED_REDIRECTION

It is also possible to define different flash messages for the two cases.

Patch provided by Thomas Weibel.
grempe (author)
Wed Feb 27 15:51:07 -0800 2008
commit  b7005dc0b7986a4eeded0892a11d5a7eb8e7e6ec
tree    db0fc3c0729ce533a65fb3dec91580811190c9bd
parent  f3728184efc8caa9dd51c28f472abd6509d754b7
...
42
43
44
45
 
46
47
48
...
138
139
140
141
 
142
143
 
144
145
 
 
 
146
147
148
...
245
246
247
248
249
 
 
250
251
252
...
42
43
44
 
45
46
47
48
...
138
139
140
 
141
142
 
143
144
 
145
146
147
148
149
150
...
247
248
249
 
 
250
251
252
253
254
0
@@ -42,7 +42,7 @@ Manual Install:
0
 == Steps in using the plugin
0
 
0
 1. At the top of your config/environment.rb create an AUTHORIZATION_MIXIN constant and set it to "object roles" or "hardwired". (See init.rb in this plugin for how the role support is mixed in.)
0
-2. Make sure your application provides a current_user method or something that returns the current user object. Add the constants in environment.rb to set your authentication systemís login page (DEFAULT_REDIRECTION_HASH) and method for storing the current URL for return after authentication (STORE_LOCATION_METHOD). (See authorization.rb in the plugin's /lib directory for the default values of DEFAULT_REDIRECTION_HASH and STORE_LOCATION_METHOD.)
0
+2. Make sure your application provides a current_user method or something that returns the current user object. Add the constants in environment.rb to set your authentication system's login page (LOGIN_REQUIRED_REDIRECTION), permission denied page (PERMISSION_DENIED_REDIRECTION) and method for storing the current URL for return after authentication (STORE_LOCATION_METHOD). (See authorization.rb in the plugin's /lib directory for the default values of LOGIN_REQUIRED_REDIRECTION, PERMISSION_DENIED_REDIRECTION and STORE_LOCATION_METHOD.)
0
 3. If you use the "hardwired" mixin, no database use is required. Otherwise, you'll have to generate a role.rb model (and its associated join table with User) by running "script/generate role_model Role" and doing "rake migrate".
0
 4. Add <tt>acts_as_authorized_user</tt> to your user class.
0
 5. Add <tt>acts_as_authorizable</tt> to the models you want to query for roles.
0
@@ -138,11 +138,13 @@ Parentheses should be used to clarify permissions. Note that you may prefix the
0
 
0
 <tt>:redirect => bool</tt>. default is <tt>true</tt>. If <tt>false</tt>, permit will not redirect to denied page.
0
 
0
-<tt>:redirect_controller => controller</tt> that handles authorization failure (default is 'account')
0
+<tt>:login_required_redirection => path or hash</tt> where user will be redirected if not logged in (default is "{ :controller => 'session', :action => 'new' }")
0
 
0
-<tt>:redirect_action => action</tt> that handles authorization failure (default is 'login')
0
+<tt>:login_required_message => 'my message'</tt> (default is 'Login is required to access the requested page.')
0
 
0
-<tt>:redirect_message => 'my message'</tt>. (default is 'Login is required')
0
+<tt>:permission_denied_redirection => path or hash</tt> where user will be redirected if logged in but not authorized (default is '')
0
+
0
+<tt>:permission_denied_message => 'my message</tt> (default is 'Permission denied. You cannot access the requested page.')
0
 
0
 === Setting and getting the roles
0
 
0
@@ -245,8 +247,8 @@ the login action. This allows the application to return to the desired page afte
0
 If the application doesn't provide this method, the method will not be called.
0
 
0
 The name of the method for storing a location can be modified by changing the constant
0
-STORE_LOCATION_METHOD in environment.rb. Also, the default login page is defined by the
0
-constant DEFAULT_REDIRECTION_HASH in authorization.rb and can be overriden in your environment.rb.
0
+STORE_LOCATION_METHOD in environment.rb. Also, the default login and permission denied pages are defined by the
0
+constants LOGIN_REQUIRED_REDIRECTION and PERMISSION_DENIED_REDIRECTION in authorization.rb and can be overriden in your environment.rb.
0
 
0
 === Conventions
0
 
...
4
5
6
7
8
9
 
 
 
 
 
 
 
 
 
 
10
11
12
...
83
84
85
86
87
88
89
90
 
 
91
92
93
 
 
 
94
95
 
 
96
97
98
99
100
...
4
5
6
 
 
 
7
8
9
10
11
12
13
14
15
16
17
18
19
...
90
91
92
 
 
 
93
 
94
95
96
 
 
97
98
99
100
 
101
102
103
 
104
105
106
0
@@ -4,9 +4,16 @@ require File.dirname(__FILE__) + '/publishare/parser'
0
 module Authorization
0
   module Base
0
 
0
- # Modify these constants in your environment.rb to tailor the plugin to your authentication system
0
- if not Object.constants.include? "DEFAULT_REDIRECTION_HASH"
0
- DEFAULT_REDIRECTION_HASH = { :controller => 'account', :action => 'login' }
0
+ # Modify these constants in your environment.rb to tailor the plugin to
0
+ # your authentication system
0
+ if not Object.constants.include? "LOGIN_REQUIRED_REDIRECTION"
0
+ LOGIN_REQUIRED_REDIRECTION = {
0
+ :controller => 'session',
0
+ :action => 'new'
0
+ }
0
+ end
0
+ if not Object.constants.include? "PERMISSION_DENIED_REDIRECTION"
0
+ PERMISSION_DENIED_REDIRECTION = ''
0
     end
0
     if not Object.constants.include? "STORE_LOCATION_METHOD"
0
       STORE_LOCATION_METHOD = :store_location
0
@@ -83,18 +90,17 @@ module Authorization
0
       # Handle redirection within permit if authorization is denied.
0
       def handle_redirection
0
         return if not self.respond_to?( :redirect_to )
0
- redirection = DEFAULT_REDIRECTION_HASH
0
- redirection[:controller] = @options[:redirect_controller] if @options[:redirect_controller]
0
- redirection[:action] = @options[:redirect_action] if @options[:redirect_action]
0
 
0
- # Store url in session for return if this is available from authentication
0
+ # Store url in session for return if this is available from
0
+ # authentication
0
         send( STORE_LOCATION_METHOD ) if respond_to? STORE_LOCATION_METHOD
0
- if @current_user
0
- flash[:notice] = "Permission denied. Your account cannot access the requested page."
0
+ if @current_user && !@current_user.nil? && @current_user != :false
0
+ flash[:notice] = @options[:permission_denied_message] || "Permission denied. You cannot access the requested page."
0
+ redirect_to @options[:permission_denied_redirection] || PERMISSION_DENIED_REDIRECTION
0
         else
0
- flash[:notice] = @options[:redirect_message] ? @options[:redirect_message] : "Login is required"
0
+ flash[:notice] = @options[:login_required_message] || "Login is required to access the requested page."
0
+ redirect_to @options[:login_required_redirection] || LOGIN_REQUIRED_REDIRECTION
0
         end
0
- redirect_to redirection
0
         false # Want to short-circuit the filters
0
       end
0
 

Comments

    No one has commented yet.