authority 2.5.0 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0486fc9d457c9b5624e3f424edfae6f6cf95d97f
4
+ data.tar.gz: 1d0e25a2ae7c561ab1552f5eb4a0bbf4b0f530e6
5
+ SHA512:
6
+ metadata.gz: a6d5fe92ed2f6a0291e8d8837ac4d846effede53fca5303369294e3e6aa20d167539cf4d9bfb27a6c5e0b8e44058e3fee3d5bd26ac71b87741e7f1797948deb1
7
+ data.tar.gz: 897deb92bd285807303b6b67511c46dbca0e4ebc3937a0b980d9b7283f8c478402bf803584048e8b80dafa9e6060123d98bb295a605cded512991680a04350b3
data/.gitignore CHANGED
@@ -17,3 +17,5 @@ test/version_tmp
17
17
  tmp
18
18
  *.swp
19
19
  *.swo
20
+ .ruby-version
21
+ .ruby-gemset
data/.travis.yml CHANGED
@@ -13,4 +13,16 @@ gemfile:
13
13
  - gemfiles/3.0.gemfile
14
14
  - gemfiles/3.1.gemfile
15
15
  - gemfiles/3.2.gemfile
16
+ - gemfiles/4.0.gemfile
16
17
  - Gemfile
18
+
19
+ matrix:
20
+ exclude:
21
+ - rvm: 1.8.7
22
+ gemfile: gemfiles/4.0.gemfile
23
+ - rvm: 1.9.2
24
+ gemfile: gemfiles/4.0.gemfile
25
+ - rvm: jruby-18mode
26
+ gemfile: gemfiles/4.0.gemfile
27
+ - rvm: rbx-18mode
28
+ gemfile: gemfiles/4.0.gemfile
data/CHANGELOG.markdown CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  Authority does its best to use [semantic versioning](http://semver.org).
4
4
 
5
+ ## v2.6.0
6
+
7
+ - Now dependent on ActiveSupport, not all of Rails, as a step toward easier use with other frameworks
8
+ - Testing with Rails 4.0
9
+ - Clearer backtraces in certain situations
10
+
5
11
  ## v2.5.0
6
12
 
7
13
  Models whose `authorizer_name` is not specified will now check for an authorizer with their own name before falling back to `ApplicationAuthorizer`. Eg, `Comment` will look for `CommentAuthorizer`. Namespacing is respected.
data/README.markdown CHANGED
@@ -1,10 +1,10 @@
1
1
  # Authority
2
2
 
3
- Authority helps you authorize actions in your Rails app. It's **ORM-neutral** and has very little fancy syntax; just group your models under one or more Authorizer classes and write plain Ruby methods on them.
3
+ Authority helps you authorize actions in your Ruby app. It's **ORM-neutral** and has very little fancy syntax; just group your models under one or more Authorizer classes and write plain Ruby methods on them.
4
4
 
5
5
  Authority will work fine with a standalone app or a single sign-on system. You can check roles in a database or permissions in a YAML file. It doesn't care! What it **does** do is give you an easy way to organize your logic and handle unauthorized actions.
6
6
 
7
- It requires that you already have some kind of user object in your application, accessible from all controllers and views via a method like `current_user` (configurable).
7
+ If you're using it with Rails controllers, it requires that you already have some kind of user object in your application, accessible via a method like `current_user` (configurable).
8
8
 
9
9
  [![Build Status](https://secure.travis-ci.org/nathanl/authority.png?branch=master)](http://travis-ci.org/nathanl/authority)
10
10
  [![Code Climate](https://codeclimate.com/github/nathanl/authority.png)](https://codeclimate.com/github/nathanl/authority)
@@ -40,10 +40,10 @@ It requires that you already have some kind of user object in your application,
40
40
 
41
41
  Using Authority, you have:
42
42
 
43
- - Broad, **class-level** rules. Examples:
43
+ - Broad, **class-level** rules. Examples:
44
44
  - "Basic users cannot delete any Widget."
45
45
  - "Only admin users can create Offices."
46
- - Fine-grained, **instance-level** rules. Examples:
46
+ - Fine-grained, **instance-level** rules. Examples:
47
47
  - "Management users can only edit schedules with date ranges in the future."
48
48
  - "Users can't create playlists more than 20 songs long unless they've paid."
49
49
  - A clear syntax for permissions-based views. Examples:
@@ -54,7 +54,7 @@ Using Authority, you have:
54
54
 
55
55
  Most importantly, you have **total flexibility**: Authority does not constrain you into using a particular scheme of roles and/or permissions.
56
56
 
57
- Authority lets you control access based on:
57
+ Authority lets you control access based on:
58
58
 
59
59
  - Roles in your app's database ([rolify](http://github.com/EppO/rolify) makes this easy)
60
60
  - Roles in a separate, single-sign-on app
@@ -62,7 +62,7 @@ Authority lets you control access based on:
62
62
  - Time and date
63
63
  - Weather, stock prices, vowels in the user's name, or **anything else you can check with Ruby**
64
64
 
65
- All you have to do is define the methods you need on your authorizers. You have all the flexibility of normal Ruby classes.
65
+ All you have to do is define the methods you need on your authorizers. You have all the flexibility of normal Ruby classes.
66
66
 
67
67
  **You** make the rules; Authority enforces them.
68
68
 
@@ -80,7 +80,7 @@ You can specify a model's authorizer using the class method `authorizer_name=`.
80
80
 
81
81
  Some example groupings:
82
82
 
83
- Simplest case Logical groups Most granular
83
+ Simplest case Logical groups Most granular
84
84
 
85
85
  ApplicationAuthorizer ApplicationAuthorizer ApplicationAuthorizer
86
86
  + + +
@@ -106,8 +106,8 @@ The authorization process generally flows like this:
106
106
  + # If you don't, the inherited one
107
107
  | # calls `default`...
108
108
  v
109
- AdminAuthorizer.default(:creatable, current_user) # *You define this method.*
110
- # If you don't, it will use the one
109
+ AdminAuthorizer.default(:creatable, current_user) # *You define this method.*
110
+ # If you don't, it will use the one
111
111
  # inherited from ApplicationAuthorizer.
112
112
  # (Its parent, Authority::Authorizer,
113
113
  # defines the method as `return false`.)
@@ -119,7 +119,9 @@ If the answer is `false` and the original caller was a controller, this is treat
119
119
  <a name="installation">
120
120
  ## Installation
121
121
 
122
- Starting from a clean commit status, add `authority` to your Gemfile, `bundle`, then `rails g authority:install`.
122
+ Starting from a clean commit status, add `authority` to your Gemfile, then `bundle`.
123
+
124
+ If you're using Rails, run `rails g authority:install`. Otherwise, pass a block to `Authority.configure` with [configuration options](https://github.com/nathanl/authority/blob/master/lib/generators/templates/authority_initializer.rb) somewhere when your application boots up.
123
125
 
124
126
  <a name="defining_your_abilities">
125
127
  ## Defining Your Abilities
@@ -145,7 +147,7 @@ This option determines what methods are added to your users, models and authoriz
145
147
 
146
148
  ```ruby
147
149
  # Whatever class represents a logged-in user in your app
148
- class User
150
+ class User
149
151
  # Adds `can_create?(resource)`, etc
150
152
  include Authority::UserAbilities
151
153
  ...
@@ -195,13 +197,13 @@ class ScheduleAuthorizer < ApplicationAuthorizer
195
197
  end
196
198
 
197
199
  # undefined; calls `ScheduleAuthorizer.default(:updatable, user)`
198
- ScheduleAuthorizer.updatable_by?(user)
200
+ ScheduleAuthorizer.updatable_by?(user)
199
201
  ```
200
202
 
201
203
  As you can see, you can specify different logic for every method on every model, if necessary. On the other extreme, you could simply supply a [default method](#default_methods) that covers all your use cases.
202
204
 
203
205
  <a name="passing_options">
204
- #### Passing Options
206
+ #### Passing Options
205
207
 
206
208
  Any options you pass when checking permissions will be passed right up the chain. One use case for this would be if you needed an associated instance in order to do a class-level check. For example:
207
209
 
@@ -238,7 +240,7 @@ class ApplicationAuthorizer < Authority::Authorizer
238
240
  def self.default(able, user)
239
241
  has_role_granting?(user, able) || user.admin?
240
242
  end
241
-
243
+
242
244
  protected
243
245
 
244
246
  def has_role_granting(user, able)
@@ -264,7 +266,7 @@ One nice thing about putting your authorization logic in authorizers is the ease
264
266
  # An authorizer shared by several admin-only models
265
267
  describe AdminAuthorizer do
266
268
 
267
- before :each do
269
+ before :each do
268
270
  @user = FactoryGirl.build(:user)
269
271
  @admin = FactoryGirl.build(:admin)
270
272
  end
@@ -302,6 +304,8 @@ end
302
304
  <a name="controllers">
303
305
  ### Controllers
304
306
 
307
+ If you're using Rails, ActionController support will be loaded in through a Railtie. Otherwise, you'll want to integrate it into your framework yourself. [Authority's controller](https://github.com/nathanl/authority/blob/master/lib/authority/controller.rb) is an excellent starting point.
308
+
305
309
  Anytime a controller finds a user attempting something they're not authorized to do, a [Security Violation](#security_violations_and_logging) will result. Controllers get two ways to check authorization:
306
310
 
307
311
  - `authorize_actions_for Llama` protects multiple controller actions with a `before_filter`, which performs a **class-level** check. If the current user is never allowed to delete a `Llama`, they'll never even get to the controller's `destroy` method.
@@ -329,7 +333,7 @@ class LlamasController < ApplicationController
329
333
  # Check class-level authorizations before all actions except :create
330
334
  # Also, to authorize this controller's 'neuter' action, ask whether `current_user.can_update?(Llama)`
331
335
  authorize_actions_for Llama, :except => :create, :actions => {:neuter => :update},
332
-
336
+
333
337
  # To authorize this controller's 'breed' action, ask whether `current_user.can_create?(Llama)`
334
338
  # To authorize its 'vaporize' action, ask whether `current_user.can_delete?(Llama)`
335
339
  authority_actions :breed => 'create', :vaporize => 'delete'
@@ -338,7 +342,7 @@ class LlamasController < ApplicationController
338
342
 
339
343
  def edit
340
344
  @llama = Llama.find(params[:id])
341
- authorize_action_for(@llama) # Check to see if you're allowed to edit this llama. failure == SecurityViolation
345
+ authorize_action_for(@llama) # Check to see if you're allowed to edit this llama. failure == SecurityViolation
342
346
  end
343
347
 
344
348
  def update
@@ -418,7 +422,9 @@ Use this very sparingly, and consider it a [code smell](http://en.wikipedia.org/
418
422
  <a name="security_violations_and_logging">
419
423
  ## Security Violations & Logging
420
424
 
421
- If you're using Authority's view helpers, users should only see links for actions they're authorized to take. If a user deliberately tries to access a restricted resource (for instance, by typing the URL directly), Authority raises and rescues an `Authority::SecurityViolation`.
425
+ If you're using Authority's `ActiveController` integration or have used it as a template for your own, your application will handle unauthorized requests with `403 Forbidden` automatically.
426
+
427
+ If you use Authority to [conditionally render links](#security_violations_and_logging), users will only see links for actions they're authorized to take. If a user deliberately tries to access a restricted resource (for instance, by typing the URL directly), Authority raises and rescues an `Authority::SecurityViolation`.
422
428
 
423
429
  When it rescues the exception, Authority calls whatever controller method is specified by your `security_violation_handler` option, handing it the exception. The default handler is `authority_forbidden`, which Authority mixes in to your `ApplicationController`. It does the following:
424
430
 
data/authority.gemspec CHANGED
@@ -8,7 +8,8 @@ Gem::Specification.new do |gem|
8
8
  gem.description = %q{Authority helps you authorize actions in your Rails app. It's ORM-neutral and has very little fancy syntax; just group your models under one or more Authorizer classes and write plain Ruby methods on them.}
9
9
  gem.homepage = "https://github.com/nathanl/authority"
10
10
 
11
- gem.add_dependency "rails", ">= 3.0.0"
11
+ gem.add_dependency "activesupport", ">= 3.0.0"
12
+ gem.add_dependency "rake", ">= 0.8.7"
12
13
 
13
14
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
15
  gem.files = `git ls-files`.split("\n")
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", ">= 4.0.0.rc", "< 4.1"
4
+ gem 'rspec', '>= 2.8.0'
5
+
6
+ gemspec :path=>"../"
data/lib/authority.rb CHANGED
@@ -2,6 +2,7 @@ require 'active_support/concern'
2
2
  require 'active_support/core_ext/class/attribute'
3
3
  require 'active_support/core_ext/hash/keys'
4
4
  require 'active_support/core_ext/string/inflections'
5
+ require 'active_support/rescuable'
5
6
  require 'forwardable'
6
7
  require 'logger'
7
8
  require 'authority/security_violation'
@@ -33,7 +34,7 @@ module Authority
33
34
  # @return [Model] resource instance
34
35
  def self.enforce(action, resource, user, options = {})
35
36
  unless action_authorized?(action, resource, user, options)
36
- raise SecurityViolation.new(user, action, resource)
37
+ raise SecurityViolation.new(user, action, resource)
37
38
  end
38
39
  resource
39
40
  end
@@ -9,7 +9,6 @@ module Authority
9
9
 
10
10
  module Abilities
11
11
  extend ActiveSupport::Concern
12
- extend Forwardable
13
12
 
14
13
  included do |base|
15
14
  class_attribute :authorizer_name
@@ -28,18 +27,18 @@ module Authority
28
27
  self.class.authorizer.new(self) # instantiate on every check, in case model has changed
29
28
  end
30
29
 
31
- # Send all calls like `editable_by?` to an authorizer instance
32
- Authority.adjectives.each do |adjective|
33
- def_delegators :authorizer, :"#{adjective}_by?"
30
+ module Definitions
31
+ # Send all calls like `editable_by?` to an authorizer instance
32
+ # Not using Forwardable because it makes it harder for users to track an ArgumentError
33
+ # back to their authorizer
34
+ Authority.adjectives.each do |adjective|
35
+ define_method("#{adjective}_by?") { |*args| authorizer.send("#{adjective}_by?", *args) }
36
+ end
34
37
  end
38
+ include Definitions
35
39
 
36
40
  module ClassMethods
37
- extend Forwardable
38
-
39
- # Send all calls like `editable_by?` to the authorizer class
40
- Authority.adjectives.each do |adjective|
41
- def_delegators :authorizer, :"#{adjective}_by?"
42
- end
41
+ include Definitions
43
42
 
44
43
  # @return [Class] of the designated authorizer
45
44
  def authorizer
@@ -3,6 +3,7 @@ module Authority
3
3
  module Controller
4
4
 
5
5
  extend ActiveSupport::Concern
6
+ include ActiveSupport::Rescuable unless defined?(Rails)
6
7
 
7
8
  def self.security_violation_callback
8
9
  Proc.new do |exception|
@@ -1,3 +1,3 @@
1
1
  module Authority
2
- VERSION = "2.5.0"
2
+ VERSION = "2.6.0"
3
3
  end
@@ -5,7 +5,7 @@ module Authority
5
5
  class InstallGenerator < Rails::Generators::Base
6
6
 
7
7
  source_root File.expand_path("../../templates", __FILE__)
8
- desc "Creates an Authority initializer for your application."
8
+ desc "Creates an Authority initializer for your application."
9
9
 
10
10
  def do_all
11
11
  create_authorizers_directory
@@ -19,14 +19,14 @@ module Authority
19
19
 
20
20
  RUBY
21
21
  puts message.strip_heredoc
22
-
22
+
23
23
  end
24
24
 
25
25
  private
26
26
 
27
27
  def create_authorizers_directory
28
28
  # Creates empty directory if none; doesn't empty the directory
29
- empty_directory "app/authorizers"
29
+ empty_directory "app/authorizers"
30
30
  end
31
31
 
32
32
  def copy_application_authorizer
@@ -2,7 +2,7 @@
2
2
  class ApplicationAuthorizer < Authority::Authorizer
3
3
 
4
4
  # Any class method from Authority::Authorizer that isn't overridden
5
- # will call its authorizer's default method.
5
+ # will call its authorizer's default method.
6
6
  #
7
7
  # @param [Symbol] adjective; example: `:creatable`
8
8
  # @param [Object] user - whatever represents the current user in your app
@@ -9,7 +9,7 @@ Authority.configure do |config|
9
9
  # Default is:
10
10
  #
11
11
  # config.user_method = :current_user
12
-
12
+
13
13
  # CONTROLLER_ACTION_MAP
14
14
  # =====================
15
15
  # For a given controller method, what verb must a user be able to do?
@@ -33,7 +33,7 @@ Authority.configure do |config|
33
33
  # ABILITIES
34
34
  # =========
35
35
  # Teach Authority how to understand the verbs and adjectives in your system. Perhaps you
36
- # need {:microwave => 'microwavable'}. I'm not saying you do, of course. Stop looking at
36
+ # need {:microwave => 'microwavable'}. I'm not saying you do, of course. Stop looking at
37
37
  # me like that.
38
38
  #
39
39
  # Defaults are as follows:
@@ -48,7 +48,7 @@ Authority.configure do |config|
48
48
  # LOGGER
49
49
  # ======
50
50
  # If a user tries to perform an unauthorized action, where should we log that fact?
51
- # Provide a logger object which responds to `.warn(message)`, unless your
51
+ # Provide a logger object which responds to `.warn(message)`, unless your
52
52
  # security_violation_handler calls a different method.
53
53
  #
54
54
  # Default is:
@@ -167,9 +167,9 @@ describe Authority::Controller do
167
167
  end
168
168
 
169
169
  let(:controller_instance) do
170
- controller_class.new.tap do |cc|
170
+ controller_class.new.tap do |cc|
171
171
  cc.stub(Authority.configuration.user_method).and_return(user)
172
- end
172
+ end
173
173
  end
174
174
 
175
175
  let(:user) { ExampleUser.new }
@@ -275,7 +275,7 @@ describe Authority::Controller do
275
275
  end
276
276
 
277
277
  end
278
-
278
+
279
279
  end
280
280
 
281
281
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authority
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
5
- prerelease:
4
+ version: 2.6.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Nathan Long
@@ -10,19 +9,36 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2013-03-21 00:00:00.000000000 Z
12
+ date: 2013-06-24 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
- name: rails
17
- requirement: &74282880 !ruby/object:Gem::Requirement
18
- none: false
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
19
17
  requirements:
20
- - - ! '>='
18
+ - - '>='
21
19
  - !ruby/object:Gem::Version
22
20
  version: 3.0.0
23
21
  type: :runtime
24
22
  prerelease: false
25
- version_requirements: *74282880
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: 3.0.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: 0.8.7
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: 0.8.7
26
42
  description: Authority helps you authorize actions in your Rails app. It's ORM-neutral
27
43
  and has very little fancy syntax; just group your models under one or more Authorizer
28
44
  classes and write plain Ruby methods on them.
@@ -35,7 +51,6 @@ extra_rdoc_files: []
35
51
  files:
36
52
  - .gitignore
37
53
  - .rspec
38
- - .rvmrc
39
54
  - .travis.yml
40
55
  - CHANGELOG.markdown
41
56
  - Gemfile
@@ -47,6 +62,7 @@ files:
47
62
  - gemfiles/3.0.gemfile
48
63
  - gemfiles/3.1.gemfile
49
64
  - gemfiles/3.2.gemfile
65
+ - gemfiles/4.0.gemfile
50
66
  - lib/authority.rb
51
67
  - lib/authority/abilities.rb
52
68
  - lib/authority/authorizer.rb
@@ -72,27 +88,36 @@ files:
72
88
  - spec/support/mock_rails.rb
73
89
  homepage: https://github.com/nathanl/authority
74
90
  licenses: []
91
+ metadata: {}
75
92
  post_install_message:
76
93
  rdoc_options: []
77
94
  require_paths:
78
95
  - lib
79
96
  required_ruby_version: !ruby/object:Gem::Requirement
80
- none: false
81
97
  requirements:
82
- - - ! '>='
98
+ - - '>='
83
99
  - !ruby/object:Gem::Version
84
100
  version: '0'
85
101
  required_rubygems_version: !ruby/object:Gem::Requirement
86
- none: false
87
102
  requirements:
88
- - - ! '>='
103
+ - - '>='
89
104
  - !ruby/object:Gem::Version
90
105
  version: '0'
91
106
  requirements: []
92
107
  rubyforge_project:
93
- rubygems_version: 1.8.10
108
+ rubygems_version: 2.0.3
94
109
  signing_key:
95
- specification_version: 3
110
+ specification_version: 4
96
111
  summary: Authority helps you authorize actions in your Rails app using plain Ruby
97
112
  methods on Authorizer classes.
98
- test_files: []
113
+ test_files:
114
+ - spec/authority/abilities_spec.rb
115
+ - spec/authority/authorizer_spec.rb
116
+ - spec/authority/configuration_spec.rb
117
+ - spec/authority/controller_spec.rb
118
+ - spec/authority/integration_spec.rb
119
+ - spec/authority/user_abilities_spec.rb
120
+ - spec/authority_spec.rb
121
+ - spec/spec_helper.rb
122
+ - spec/support/example_classes.rb
123
+ - spec/support/mock_rails.rb
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm use --create default@authority