Skip to content

Commit 6b7495f

Browse files
committed
Drop support for CanCan, use its successor CanCanCan
1 parent 25ae06a commit 6b7495f

12 files changed

Lines changed: 47 additions & 481 deletions

File tree

.travis.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ matrix:
6868
- rvm: 2.6.3
6969
env: CI_ORM=active_record CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres
7070
gemfile: gemfiles/rails_6.0.gemfile
71-
- rvm: 2.6.3
72-
env: CI_ORM=active_record CI_DB_ADAPTER=sqlite3
73-
gemfile: gemfiles/cancan.gemfile
7471
- rvm: ruby-head
7572
env: CI_ORM=mongoid
7673
gemfile: gemfiles/rails_5.2.gemfile

Appraisals

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,3 @@ appraise "rails-6.0" do
107107
gem 'paper_trail', '>= 5.0'
108108
end
109109
end
110-
111-
appraise "cancan" do
112-
gem 'rails', '~> 5.1.0'
113-
gem 'sassc-rails', '~> 2.1'
114-
gem 'devise', '~> 4.0'
115-
116-
group :test do
117-
gem 'cancan', '>= 1.6'
118-
end
119-
end

gemfiles/cancan.gemfile

Lines changed: 0 additions & 51 deletions
This file was deleted.

lib/generators/rails_admin/templates/initializer.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ RailsAdmin.config do |config|
88
# end
99
# config.current_user_method(&:current_user)
1010

11-
## == Cancan ==
12-
# config.authorize_with :cancan
11+
## == CancanCan ==
12+
# config.authorize_with :cancancan
1313

1414
## == Pundit ==
1515
# config.authorize_with :pundit

lib/rails_admin.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require 'rails_admin/abstract_model'
33
require 'rails_admin/config'
44
require 'rails_admin/extension'
5-
require 'rails_admin/extensions/cancan'
65
require 'rails_admin/extensions/cancancan'
76
require 'rails_admin/extensions/pundit'
87
require 'rails_admin/extensions/paper_trail'

lib/rails_admin/config.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ def audit_with(*args, &block)
141141
# end
142142
#
143143
# To use an authorization adapter, pass the name of the adapter. For example,
144-
# to use with CanCan[https://github.com/ryanb/cancan], pass it like this.
144+
# to use with CanCanCan[https://github.com/CanCanCommunity/cancancan/], pass it like this.
145145
#
146-
# @example CanCan
146+
# @example CanCanCan
147147
# RailsAdmin.config do |config|
148-
# config.authorize_with :cancan
148+
# config.authorize_with :cancancan
149149
# end
150150
#
151151
# See the wiki[https://github.com/sferik/rails_admin/wiki] for more on authorization.

lib/rails_admin/config/actions/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Base
9999
key.to_sym
100100
end
101101

102-
# For Cancan and the like
102+
# For CanCanCan and the like
103103
register_instance_option :authorization_key do
104104
key.to_sym
105105
end

lib/rails_admin/extensions/cancan.rb

Lines changed: 0 additions & 3 deletions
This file was deleted.

lib/rails_admin/extensions/cancan/authorization_adapter.rb

Lines changed: 0 additions & 57 deletions
This file was deleted.

lib/rails_admin/extensions/cancancan/authorization_adapter.rb

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)