@@ -2,7 +2,28 @@ module RailsAdmin
22 module Extensions
33 module CanCanCan
44 # This adapter is for the CanCanCan[https://github.com/CanCanCommunity/cancancan] authorization library.
5- class AuthorizationAdapter < RailsAdmin ::Extensions ::CanCan ::AuthorizationAdapter
5+ class AuthorizationAdapter
6+ module ControllerExtension
7+ def current_ability
8+ # use _current_user instead of default current_user so it works with
9+ # whatever current user method is defined with RailsAdmin
10+ @current_ability ||= @ability . new ( _current_user )
11+ end
12+ end
13+
14+ # See the +authorize_with+ config method for where the initialization happens.
15+ def initialize ( controller , ability = ::Ability )
16+ @controller = controller
17+ @controller . instance_variable_set '@ability' , ability
18+ @controller . extend ControllerExtension
19+ @controller . current_ability . authorize! :access , :rails_admin
20+ end
21+
22+ # This method is called in every controller action and should raise an exception
23+ # when the authorization fails. The first argument is the name of the controller
24+ # action as a symbol (:create, :bulk_delete, etc.). The second argument is the
25+ # AbstractModel instance that applies. The third argument is the actual model
26+ # instance if it is available.
627 def authorize ( action , abstract_model = nil , model_object = nil )
728 return unless action
829 subject = model_object || abstract_model && abstract_model . model
@@ -13,13 +34,31 @@ def authorize(action, abstract_model = nil, model_object = nil)
1334 end
1435 end
1536
37+ # This method is called primarily from the view to determine whether the given user
38+ # has access to perform the action on a given model. It should return true when authorized.
39+ # This takes the same arguments as +authorize+. The difference is that this will
40+ # return a boolean whereas +authorize+ will raise an exception when not authorized.
1641 def authorized? ( action , abstract_model = nil , model_object = nil )
1742 return unless action
1843 subject = model_object || abstract_model && abstract_model . model
1944 authorized_for_dashboard_in_legacy_way? ( action , true ) ||
2045 @controller . current_ability . can? ( *resolve_with_compatibility ( action , subject ) )
2146 end
2247
48+ # This is called when needing to scope a database query. It is called within the list
49+ # and bulk_delete/destroy actions and should return a scope which limits the records
50+ # to those which the user can perform the given action on.
51+ def query ( action , abstract_model )
52+ abstract_model . model . accessible_by ( @controller . current_ability , action )
53+ end
54+
55+ # This is called in the new/create actions to determine the initial attributes for new
56+ # records. It should return a hash of attributes which match what the user
57+ # is authorized to create.
58+ def attributes_for ( action , abstract_model )
59+ @controller . current_ability . attributes_for ( action , abstract_model && abstract_model . model )
60+ end
61+
2362 private
2463
2564 def authorized_for_dashboard_in_legacy_way? ( action , silent = false )
0 commit comments