1
0
Fork 0
mirror of https://github.com/varvet/pundit.git synced 2022-11-09 12:30:11 -05:00
Commit graph

20 commits

Author SHA1 Message Date
Jonas Nicklas
753bb0a2b6 Revert namespaces 2014-08-22 11:19:36 +02:00
Jonas Nicklas
9d578a0a2d Make sure Pundit doesn’t generate any deprecation warnings with any version of RSpec 2014-07-18 16:20:48 +02:00
Jonas Nicklas
1942db052b Merge pull request #168 from elabs/headless-policies
Enable headless policies
2014-07-17 11:31:37 +02:00
Unknown_Guy
bf29cae128 find correct policy when model is namespaced 2014-07-15 13:08:08 +03:00
Thomas Klemm
b18d16ca10 Enable headless policies
Enables `policy(:dashboard) # => DashboardPolicy`.

Policies without a matching model can come in handy when a controller
isn't modeled alongside a resource, e.g. a `DashboardsController`.

The policy lookup by symbol also helps with strong parameters,
since I prefer `policy(:post)` or `policy(@post || :post)` over `policy(@post || Post)`.
2014-07-13 12:48:45 +02:00
Thomas Klemm
edf0e6ef76 Policy namespacing: Add spec ensuring fallback on non-namespaced policy class 2014-07-13 11:16:45 +02:00
Jay Hayes
2f20c58484 Raise more description exception for verify_policy_scoped
* The AuthorizationNotPerformedError is very descriptive of the
  situation when authorization is forgotten. In the case of no
  scoping, it can be a head scratcher.
* This new error type is implemented as a subclass of the current error
  type to prevent regressions in existing code. While this is not ideal,
  it is the simplest solution I could come up with for compatibility.
2014-07-08 06:53:52 -05:00
Eduardo Gutierrez
4c7d3c4dd6 Lookup policies in the current namespace
Addresses #12. If the policy is not defined in `namespace`, `const_get`
will search through the inheritance change of `namespace` to find the
policy.
2014-05-22 12:55:34 -04:00
Dillon Benson
30b7861431 moved class definitions from pundit_spec.rb to spec_helper.rb 2014-04-23 22:58:38 -04:00
Jonas Nicklas
8155ba5d79 Raise a different error when authorization is not performed
closes #108
2014-04-04 16:30:41 +02:00
Ulysse Carion
d766d9e792 Add #query, #record, and #policy properties to Pundit::NotAuthorizedError.
Exceptions raised by #authorize now provide the query (e.g. 'create?') and
record (e.g. an instance of 'Post') that caused the exception to be raised, as
well as the relevant policy (e.g. an instance of 'PostPolicy').

NotAuthorizedError is modified to continue to inherit from StandardError, but
now also has attr_accessor values for :query, :record, and :policy.
2014-03-05 00:12:44 -08:00
Chris Legault
0e85e0cdb7 - Updated gemspec to use RSpec 3
- Updated tests to match new expect syntax
2014-02-08 08:19:24 -04:00
Tim Cooper
26e3706719 Allow policies and scopes to be injected into controllers.
In controller specs instead of relying on Pundit to instantiate the correct
policy object allow it to be injected into the controller. More often than not
a double is used in controller specs which means the policy cannot be
inferred. This also allows us to double the policy to ensure that on a unit
level the rights methods are being called on callaborators.

class PostsController < ApplicationController
  attr_writer :post
  helper_method :post

  def create
    authorize post

    post.save
    respond_with post
  end

  private

  def post
    @post ||= Post.new post_attributes
  end
end

describe PagesController do
  let(:policy) { double 'SomePolicy', create?: true }

  before do
    controller.policy = policy
  end

  it 'delegates authorization to policy' do
    expect(policy).to have_received(:create?)
  end
end

Add spec for injecting policy.

Use `or` instead of ternary operator.

Allow policy_scope to be injected for controller tests.
2013-09-02 11:32:30 +02:00
Thomas Klemm
91b3681900 Use RSpec double syntax over stub to fix deprecation warning 2013-08-26 10:04:15 +02:00
Philip Vieira
d7ef22bb97 Custom pundit user 2013-07-13 05:42:34 +02:00
Thomas Klemm
e918f7c3c4 Add ActiveSupport dependency in specs for Rails 4 to make them green again 2013-06-18 01:13:07 +02:00
Brendon Murphy
7be0a890a6 Add #verify_policy_scoped for controller usage.
See the readme changes for an example.  In short, this behaves
like verify_authorized but is useful for actions that find a
collection (like index) and don't authorize instances.
2013-04-17 22:05:24 -07:00
Jason Daly
e65159f26b Improvements on .policy_class support
The `BlogPolicy -> "BlogPolicy" -> "Blog" -> "BlogPolicy" -> BlogPolicy`
issue @jnicklas pointed out has been resolved. For example, given

```ruby
class BlogPolicy < Struct.new(:user, :blog); end
class Blog; end
class ArtificialBlog < Blog
  def self.policy_class
    BlogPolicy
  end
end
```

The above string manipulation/casting is prevented; the `BlogPolicy`
class will be immediately returned to `policy` and on to be evaluated.

Anonymous classes are now supported too. For example, given

```ruby
class BlogPolicy < Struct.new(:user, :blog); end
class Blog; end
class ArtificialBlog < Blog
  def self.policy_class
    Struct.new(:user, :blog) do
      def create?
        true
      end
    end
  end
end
```

The `Struct` will be returned and evaluated as any other policy.
2012-12-13 18:20:12 -05:00
Jason Daly
4fc13620ee Adds support for policy_class model instance/class for custom Policy 2012-11-30 10:21:46 -05:00
Jonas Nicklas
b8fd37110a Add specs and dependencies 2012-11-19 13:02:42 +01:00