Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 5 fixes #227

Open
wants to merge 74 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
8040968
Do not put a space between a method name and the opening parenthesis
annaswims May 25, 2016
b29c1e2
Don't explicity support rails < 4.0. or ruby <2.0
annaswims May 25, 2016
89380cb
Merge pull request #5 from mobi/update_gemspec_travis
May 25, 2016
5a36ddc
Don't inspect user; too much sensitve data in log
annaswims Mar 15, 2016
5e18e98
Merge pull request #6 from mobi/bugfix_dont_inspect_user_for_security…
May 25, 2016
fd2c478
trim trailing whitespace
annaswims May 25, 2016
0b19325
tests pass with ruby 2.2
annaswims May 25, 2016
a6114f2
Corrected rdoc inline code formatting
J3RN Jul 21, 2016
414112f
Implement support for polymorphic associations in read scopes.
chris-baynes Dec 6, 2016
1ed351f
Do not try to generate table aliases for empty paths.
chris-baynes Dec 8, 2016
85f32d7
Join conditions on concrete polymorphisms with an OR.
chris-baynes Dec 9, 2016
e98d9db
Simplify recursive methods, remove rails 2 support, make tests pass.
chris-baynes Dec 13, 2016
1035df3
Add readme specific for this branch.
chris-baynes Dec 14, 2016
1eef29f
Merge pull request #1 from Contiamo/support_polymorphism
chris-baynes Dec 14, 2016
843ffd0
pickle test
Jan 26, 2017
beaafa9
BUGFIX: Account for no implicit join references in Rails 4.1
Jan 26, 2017
6cd1874
Merge pull request #9 from mobi/bugfix_implicit_joins_4_1
Jan 26, 2017
6f49f5e
Ensure no read-only records are returned when involving DA scopes.
Feb 3, 2017
84c65d9
Merge pull request #2 from Contiamo/readonly_fix
danielbecker Feb 3, 2017
90202de
Fix return of readonly records for rails 4.
May 11, 2017
761b1dd
Fix handling of activerecord relations.
May 11, 2017
97fc010
Fix scoped query for rails 4.1.
May 15, 2017
f040137
Merge pull request #7 from mobi/trim_trailing_whitespace
Jul 7, 2017
c1576ec
Merge pull request #8 from J3RN/update_rdoc_formatting
Jul 7, 2017
13f0ee2
allow for testsing rails 4.2
annaswims Jul 13, 2016
747e960
Added Testing for Rails 4.2 removed testing for Rails <4.1, ruby <2.2
annaswims Jul 7, 2017
c745c29
Merge pull request #10 from mobi/test_with_rails_4_2
Jul 20, 2017
47a726a
Remove code related to rails <=4.0
annaswims Jul 20, 2017
7f69131
Add rails 5 compatibility.
Jul 26, 2017
b136b26
before_filter is called before_action now.
Jul 31, 2017
53ece5e
Merge pull request #11 from mobi/remove_deprecated_version_code
Aug 11, 2017
beb1727
Remove deprecated minitest syntax
annaswims Jul 20, 2017
7f7929c
test for rails 5.0
annaswims May 1, 2018
8c87b60
Rails 5 fixes
annaswims May 1, 2018
48fda84
Merge pull request #12 from mobi/fix_intermittent_test_failures
annaswims May 3, 2018
9fb63d3
check Rails 5, Ruby 2.4 on travis
May 16, 2018
f8fed7f
clear current_user in test teardown
May 16, 2018
56fbb34
Merge fixes
Xymist May 18, 2018
c6c80c1
Rubocop
Xymist May 18, 2018
52e6877
More test failures
Xymist May 18, 2018
09619b2
Up dependency to Rails 5.2
Xymist May 18, 2018
1471ca1
Test syntax no longer accepts positional arguments
Xymist May 18, 2018
b6bf0a9
Permit more recent ruby_parser
Xymist May 18, 2018
ef88fa0
Fix recursive call
Xymist May 18, 2018
b222212
lock minitest to 5.10.3
May 23, 2018
077ecac
ActiveRecord::Reflection issue fix
satyendra-cis May 24, 2018
c57af89
Merge pull request #1 from Xymist/r5_test_fix
Xymist May 24, 2018
50e43b1
before_filter to before_action; syntax fixes
May 30, 2018
2b36a09
remove assigns from controller tests
May 30, 2018
c6d6ac4
fix call to ActiveRecord::Relation initializer
Jun 1, 2018
78ea514
fix more deprecations
Jun 1, 2018
d884680
remove hidden_actions
Jun 1, 2018
d10b850
remove rails 4.2 from test matrix
Jun 1, 2018
9b7986b
Fixed failing test case
satyendra-cis Jun 7, 2018
6ee4d49
Merge pull request #2 from Xymist/FIXED-FAILING-TEST
Xymist Jun 7, 2018
e074849
Update .travis.yml
Xymist Jun 7, 2018
b472f41
Version bump
Xymist Jun 7, 2018
8b7c231
Randomise test order
Xymist Jun 7, 2018
ea4f1d4
Fix various warnings
Xymist Jun 7, 2018
dafccb7
Explicit require of rails-controller-testing
Xymist Jun 7, 2018
941319e
Add Ruby 2.2 and 2.4 to test matrix
Xymist Jun 7, 2018
6dbd2d1
Cache bundle after first build
Xymist Jun 7, 2018
c2948bd
Flag unused variables
Xymist Jun 7, 2018
8bde2df
Update Readme
Xymist Jun 7, 2018
2b64e0e
Merge pull request #3 from Xymist/update_travis
satyendra-cis Jun 8, 2018
614fee8
Added fix for sexp integer type errors
satyendra-cis Jun 8, 2018
c2aea5f
Merge pull request #4 from Xymist/ERRORS-FIX
Xymist Jun 8, 2018
2fcc92a
Merge branch 'mobi_actual_master' of https://github.com/Xymist/declar…
Xymist Jun 21, 2018
388a97e
Merge in Mobi base
Xymist Jun 21, 2018
81c0a00
Add Rails 4.2 back in to matrix
Xymist Jun 21, 2018
043759f
Allow Rails 5.2.+
niciliketo Nov 28, 2018
3ec49fa
Merge pull request #1 from niciliketo/allow_rails_5.2
Xymist Nov 29, 2018
223e664
Fixes request_with to use Hash
Apr 14, 2019
3cd7934
Merge pull request #2 from robvandijk/fork_allow_rails_5
Xymist Oct 22, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ rdoc
gemfiles/*.lock
log/*
*.sublime*


.ruby-version
.ruby-gemset
.idea
Gemfile
Gemfile.lock
24 changes: 8 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
language: ruby
script: bundle exec rake test
cache: bundler
rvm:
- 1.8.7
- 1.9.3
- 2.2
- 2.4
- 2.5
gemfile:
- gemfiles/2.3.gemfile
- gemfiles/3.0.gemfile
- gemfiles/3.1.gemfile
- gemfiles/3.2.gemfile
- gemfiles/4.0.gemfile
- gemfiles/4.1.gemfile
matrix:
exclude:
- rvm: 1.8.7
gemfile: gemfiles/4.0.gemfile
- rvm: 1.8.7
gemfile: gemfiles/4.1.gemfile
- rvm: 1.9.3
gemfile: gemfiles/2.3.gemfile
- gemfiles/4.2.gemfile
- gemfiles/5.0.gemfile
- gemfiles/5.2.gemfile

111 changes: 48 additions & 63 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= Declarative Authorization

The declarative authorization plugin offers an authorization mechanism inspired
The declarative authorization plugin offers an authorization mechanism inspired
by _RBAC_. The most notable distinction to other authorization plugins is the
declarative approach. That is, authorization rules are not defined
programmatically in between business logic but in an authorization configuration.
Expand All @@ -21,11 +21,11 @@ Plugin features
* Authorize CRUD (Create, Read, Update, Delete) activities
* Query rewriting to automatically only fetch authorized records
* DSL for specifying Authorization rules in an authorization configuration
* Support for Rails 4, with backwards compatibility through Rails 2
* Support for Rails 4.1-2


Requirements
* An authentication mechanism
* An authentication mechanism
* User object in Controller#current_user
* (For model security) Setting Authorization.current_user
* User objects need to respond to a method :role_symbols that returns an
Expand Down Expand Up @@ -55,14 +55,14 @@ Next, bundle and install.

This installer will create a Role model, an admin and a user role, and set a
has_and_belongs_to_many relationship between the User model and the Role model.
It will also add a `role_symbols` method to the user model to meet
It will also add a +role_symbols+ method to the user model to meet
declarative_authorization's requirements. The default User model is User. You can override this by simply typing the name of a model as above.

You can create the model with the fields provided by using the `--create-user` option.
You can create the model with the fields provided by using the +--create-user+ option.

The `--commit` option will run `rake db:migrate` and `rake db:seed`.
The +--commit+ option will run +rake db:migrate+ and +rake db:seed+.

The `--user-belongs-to-role` option will set up a one-to-many relationship between Users and Roles.
The +--user-belongs-to-role+ option will set up a one-to-many relationship between Users and Roles.
That is, each user has a role_id column and can only have one role. Role inheritance can be used
in authorization rules.

Expand All @@ -74,7 +74,7 @@ To copy a default set of authorization rules which includes CRUD priveleges, run

$ rails g authorization:rules

This command will copy the following to `config/authorization_rules.rb`. Remember
This command will copy the following to +config/authorization_rules.rb+. Remember
to implement the requirements of this gem as described in the Installation section
at the end of this README if you do not use the above installer.

Expand All @@ -83,7 +83,7 @@ at the end of this README if you do not use the above installer.
# add permissions for guests here, e.g.
# has_permission_on :conferences, :to => :read
end

# permissions on other roles, such as
# role :admin do
# has_permission_on :conferences, :to => :manage
Expand All @@ -108,14 +108,14 @@ at the end of this README if you do not use the above installer.

=== Controller Authorization

For RESTful controllers, add `filter_resource_access`:
For RESTful controllers, add +filter_resource_access+:

class MyRestfulController < ApplicationController
filter_resource_access
...
end

For a non-RESTful controller, you can use `filter_access_to`:
For a non-RESTful controller, you can use +filter_access_to+:

class MyOtherController < ApplicationController
filter_access_to :all
Expand All @@ -125,7 +125,7 @@ For a non-RESTful controller, you can use `filter_access_to`:

=== View Authorization

Declarative Authorization will use `current_user` to check authorization.
Declarative Authorization will use +current_user+ to check authorization.

<%= link_to 'Edit Post', edit_post_path(@post) if permitted_to? :update, @post %>

Expand All @@ -147,12 +147,12 @@ Declarative Authorization will use `current_user` to check authorization.
| Privilege | | Context | | Attribute |
'-----------' '---------' '-----------'

In the application domain, each *User* may be assigned to *Roles* that should
define the users' job in the application, such as _Administrator_. On the
right-hand side of this diagram, application developers specify which *Permissions*
In the application domain, each *User* may be assigned to *Roles* that should
define the users' job in the application, such as _Administrator_. On the
right-hand side of this diagram, application developers specify which *Permissions*
are necessary for users to perform activities, such as calling a controller action,
viewing parts of a View or acting on records in the database. Note that
Permissions consist of an *Privilege* that is to be performed, such as _read_,
Permissions consist of an *Privilege* that is to be performed, such as _read_,
and a *Context* in that the Operation takes place, such as _companies_.

In the authorization configuration, Permissions are assigned to Roles and Role
Expand Down Expand Up @@ -186,14 +186,14 @@ filter_access_to with the appropriate parameters to protect the CRUD methods.
See Authorization::AuthorizationInController::ClassMethods for options on
nested resources and custom member and collection actions.

By default, declarative_authorization will enable filter_resource_access compatibility with strong_parameters in Rails 4. If you want to disable this behavior, you can use the `:strong_parameters` option.
By default, declarative_authorization will enable filter_resource_access compatibility with strong_parameters in Rails 4. If you want to disable this behavior, you can use the +:strong_parameters+ option.

class EmployeesController < ApplicationController
filter_resource_access :strong_parameters => false
...
end

Simalarly, you can use `:strong_parameters => true` if you are using strong_parameters in Rails 3.
Simalarly, you can use +:strong_parameters => true+ if you are using strong_parameters in Rails 3.

If you prefer less magic or your controller has no resemblance with the resource
controllers, directly calling filter_access_to may be the better option. Examples
Expand Down Expand Up @@ -226,7 +226,7 @@ filter_access_to call may become more verbose:
end

For some actions it might be necessary to check certain attributes of the
object the action is to be acting on. Then, the object needs to be loaded
object the action is to be acting on. Then, the object needs to be loaded
before the action's access control is evaluated. On the other hand, some actions
might prefer the authorization to ignore specific attribute checks as the object is
unknown at checking time, so attribute checks and thus automatic loading of
Expand All @@ -239,13 +239,13 @@ objects needs to be enabled explicitly.
end
end

You can provide the needed object through before_filters. This way, you have
You can provide the needed object through before_actions. This way, you have
full control over the object that the conditions are checked against. Just make
sure, your before_filters occur before any of the filter_access_to calls.
sure, your before_actions occur before any of the filter_access_to calls.

class EmployeesController < ApplicationController
before_filter :new_employee_from_params, :only => :create
before_filter :new_employee, :only => [:index, :new]
before_action :new_employee_from_params, :only => :create
before_action :new_employee, :only => [:index, :new]
filter_access_to :all, :attribute_check => true

def create
Expand All @@ -260,8 +260,8 @@ sure, your before_filters occur before any of the filter_access_to calls.

If the access is denied, a +permission_denied+ method is called on the
current_controller, if defined, and the issue is logged.
For further customization of the filters and object loading, have a look at
the complete API documentation of filter_access_to in
For further customization of the filters and object loading, have a look at
the complete API documentation of filter_access_to in
Authorization::AuthorizationInController::ClassMethods.


Expand Down Expand Up @@ -313,8 +313,8 @@ model that model security should be enforced on, i.e.
Thus,
Employee.create(...)
fails, if the current user is not allowed to :create :employees according
to the authorization rules. For the application to find out about what
happened if an operation is denied, the filters throw
to the authorization rules. For the application to find out about what
happened if an operation is denied, the filters throw
Authorization::NotAuthorized exceptions.

As access control on read are costly, with possibly lots of objects being
Expand All @@ -336,8 +336,8 @@ the usual find method:

Employee.with_permissions_to(:read).find(:all, :conditions => ...)

If the current user is completely missing the permissions, an
Authorization::NotAuthorized exception is raised. Through
If the current user is completely missing the permissions, an
Authorization::NotAuthorized exception is raised. Through
Model.obligation_conditions, application developers may retrieve
the conditions for manual rewrites.

Expand Down Expand Up @@ -385,9 +385,9 @@ Privilege hierarchies may be context-specific, e.g. applicable to :employees.
privilege :manage, :employees, :includes => :increase_salary
end

For more complex use cases, authorizations need to be based on attributes. Note
For more complex use cases, authorizations need to be based on attributes. Note
that you then also need to set :attribute_check => true in controllers for filter_access_to.
E.g. if a branch admin should manage only employees of his branch (see
E.g. if a branch admin should manage only employees of his branch (see
Authorization::Reader in the API docs for a full list of available operators):

authorization do
Expand Down Expand Up @@ -454,14 +454,12 @@ setup and assume certain identities for tests:
without_access_control do
Employee.create(...)
end
assert_nothing_raised do
with_user(admin) do
Employee.find(:first)
end
with_user(admin) do
Employee.find(:first)
end
end
end

Or, with RSpec, it would work like this:

describe Employee do
Expand All @@ -484,29 +482,16 @@ See Authorization::TestHelper for more information.


= Installation of declarative_authorization

One of three options to install the plugin:
* Install by Gem: Add to your environment.rb in the initializer block:
config.gem "declarative_authorization"
Note: you need gemcutter support in place, i.e. call
gem install gemcutter
gem tumble
And call from your application's root directory
rake gems:install
* Alternativelyi, in Rails 2, to install from github, execute in your application's root directory
cd vendor/plugins && git clone git://github.com/stffn/declarative_authorization.git

Then,
* provide the requirements as noted below,
* create a basic config/authorization_rules.rb--you might want to take the
provided example authorization_rules.dist.rb in the plugin root as a starting
point,
* provide the requirements as noted below,
* create a basic config/authorization_rules.rb--you might want to take the
provided example authorization_rules.dist.rb in the plugin root as a starting
point,
* add +filter_access_to+, +permitted_to+? and model security as needed.

== Providing the Plugin's Requirements
The requirements are
* Rails >= 2.2, including 3 and Ruby >= 1.8.6, including 1.9
* An authentication mechanism
* Rails >= 4.1 Ruby >= 2.2
* An authentication mechanism
* A user object returned by Controller#current_user
* An array of role symbols returned by User#role_symbols
* (For model security) Setting Authorization.current_user to the request's user
Expand All @@ -519,18 +504,18 @@ restful_authentication.
cd ../.. && ruby script/generate authenticated user sessions
* Move "include AuthenticatedSystem" to ApplicationController
* Add +filter_access_to+ calls as described above.
* If you'd like to use model security, add a before_filter that sets the user
* If you'd like to use model security, add a before_action that sets the user
globally to your ApplicationController. This is thread-safe.
before_filter :set_current_user
before_action :set_current_user
protected
def set_current_user
Authorization.current_user = current_user
end

* Add roles field to the User model through a :+has_many+ association
(this is just one possible approach; you could just as easily use
(this is just one possible approach; you could just as easily use
:+has_many+ :+through+ or a serialized roles array):
* create a migration for table roles
* create a migration for table roles
class CreateRoles < ActiveRecord::Migration
def self.up
create_table "roles" do |t|
Expand All @@ -549,7 +534,7 @@ restful_authentication.
belongs_to :user
end

* add +has_many+ :+roles+ to the User model and a roles method that returns the roles
* add +has_many+ :+roles+ to the User model and a roles method that returns the roles
as an Array of Symbols, e.g.
class User < ActiveRecord::Base
has_many :roles
Expand Down Expand Up @@ -591,8 +576,8 @@ the following lines to your authorization_rules.rb for the appropriate role:
Then, point your browser to
http://localhost/authorization_rules

The browser needs Rails 2.3 (for Engine support). The graphical view requires
Graphviz (which e.g. can be installed through the graphviz package under Debian
The browser needs Rails 2.3 (for Engine support). The graphical view requires
Graphviz (which e.g. can be installed through the graphviz package under Debian
and Ubuntu) and has only been tested under Linux. Note: for Change Support
you'll need to have a User#login method that returns a non-ambiguous user
name for identification.
Expand All @@ -601,7 +586,7 @@ name for identification.
= Help and Contact

We have an issue tracker[http://github.com/stffn/declarative_authorization/issues]
for bugs and feature requests as well as a
for bugs and feature requests as well as a
Google Group[http://groups.google.com/group/declarative_authorization] for
discussions on the usage of the plugin. You are very welcome to contribute.
Just fork the git repository and create a new issue, send a pull request or
Expand Down
8 changes: 4 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ require 'rake/testtask'
require 'rdoc/task'

desc 'Default: run unit tests against all versions.'
task :default => 'bundles:test'
task default: 'bundles:test'

def run_for_bundles cmd
def run_for_bundles(cmd)
Dir['gemfiles/*.gemfile'].each do |gemfile|
puts "\n#{gemfile}: #{cmd}"
ENV['BUNDLE_GEMFILE'] = gemfile
Expand Down Expand Up @@ -47,7 +47,7 @@ if File.directory?(File.join(File.dirname(__FILE__), 'garlic'))
require File.join(File.dirname(__FILE__), 'garlic')
end

desc "clone the garlic repo (for running ci tasks)"
desc 'clone the garlic repo (for running ci tasks)'
task :get_garlic do
sh "git clone git://github.com/ianwhite/garlic.git garlic"
sh 'git clone git://github.com/ianwhite/garlic.git garlic'
end
16 changes: 16 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Original docs: [https://github.com/stffn/declarative_authorization/blob/master/README.rdoc](https://github.com/stffn/declarative_authorization/blob/master/README.rdoc)

Extends Declarative Authorization with Rails 5.2 and Ruby 2.5 support, aiming for backwards compatibility as far as Rails 4.2 and Ruby 2.2 on a best-effort basis.

Branch r5: [![Build Status](https://travis-ci.org/Xymist/declarative_authorization.svg?branch=r5)](https://travis-ci.org/Xymist/declarative_authorization)

### Running tests for DA

```
cp gemfiles/{RAILS_VERSION}.gemfile Gemfile
bundle

bundle exec rake test
```


Loading