allowy 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -31,7 +31,7 @@ So I decided to put up allowy to solve those issue for me.
31
31
  Add it to your Rails application's `Gemfile`:
32
32
 
33
33
  ```ruby
34
- gem 'alowy'
34
+ gem 'allowy'
35
35
  ```
36
36
 
37
37
  Then `bundle install`.
@@ -93,7 +93,7 @@ If you want to change the context in Rails then just override it in the controll
93
93
  ```ruby
94
94
  class PagesController < ApplicationController
95
95
  def allowy_context
96
- {realy: 'anything', can_be: 'here', event: params}
96
+ {realy: 'anything', can_be: 'here', even: params}
97
97
  end
98
98
  end
99
99
  ```
@@ -217,8 +217,28 @@ describe PagesController do
217
217
  post(:create).should be_success
218
218
  end
219
219
  end
220
+
221
+ ```
222
+
223
+ But if you don't want to stub the context because you access its `can?`, `cannot?` or `authorize!` methods
224
+ (allwing permission delegation) then you can simply mix the `Allowy::Context` in:
225
+
226
+ ```ruby
227
+ class ControllerLikeContext
228
+ include Alllowy::Context
229
+ attr_accessor :current_user
230
+
231
+ def initialize(user)
232
+ @current_user = user
233
+ end
234
+ end
235
+
236
+ # Then you can simply instantiate it to check the permissions:
237
+ ControllerLikeContext.new(that_user).should be_able_to :edit, Blog
238
+ ControllerLikeContext.new(this_user).should_not be_able_to :edit, Blog
220
239
  ```
221
240
 
241
+
222
242
  # Development
223
243
 
224
244
 
@@ -4,6 +4,7 @@ require 'active_support/inflector'
4
4
  require "allowy/version"
5
5
  require "allowy/access_control"
6
6
  require "allowy/registry"
7
+ require "allowy/context"
7
8
  require "allowy/controller_extensions"
8
9
 
9
10
  module Allowy
@@ -0,0 +1,48 @@
1
+ module Allowy
2
+
3
+ # This module provides the default and common context to for checking the permissions.
4
+ # It is mixed into the Controller in Rails by default and provides an wasy way to reuse it
5
+ # in other parts of the application (in RSpec or Cucumber) without needint a controller.
6
+ # For example, you can use this code in your Cucumber features:
7
+ #
8
+ # @example
9
+ # class CustomContext
10
+ # include Allowy::Context
11
+ # attr_accessor :current_user
12
+ #
13
+ # def initialize(user)
14
+ # @current_user = user
15
+ # end
16
+ #
17
+ # And the you can easily check the permissions simply using something like:
18
+ #
19
+ # @example
20
+ # CustomContext.new(that_user).should be_able_to :create, Blog
21
+ module Context
22
+ extend ActiveSupport::Concern
23
+
24
+ module InstanceMethods
25
+
26
+ def allowy_context
27
+ self
28
+ end
29
+
30
+ def current_allowy
31
+ @current_allowy ||= ::Allowy::Registry.new(allowy_context)
32
+ end
33
+
34
+ def can?(action, subject, *args)
35
+ current_allowy.access_control_for!(subject).can?(action, subject, *args)
36
+ end
37
+
38
+ def cannot?(*args)
39
+ current_allowy.access_control_for!(subject).cannot?(action, subject, *args)
40
+ end
41
+
42
+ def authorize!(action, subject, *args)
43
+ current_allowy.access_control_for!(subject).authorize!(action, subject, *args)
44
+ end
45
+ end
46
+ end
47
+
48
+ end
@@ -3,32 +3,9 @@ module Allowy
3
3
  module ControllerExtensions
4
4
  extend ActiveSupport::Concern
5
5
  included do
6
+ include ::Allowy::Context
6
7
  helper_method :can?, :cannot?
7
8
  end
8
-
9
- module InstanceMethods
10
-
11
- def allowy_context
12
- self
13
- end
14
-
15
- def current_allowy
16
- @current_allowy ||= ::Allowy::Registry.new(allowy_context)
17
- end
18
-
19
- def can?(action, subject, *args)
20
- current_allowy.access_control_for!(subject).can?(action, subject, *args)
21
- end
22
-
23
- def cannot?(*args)
24
- current_allowy.access_control_for!(subject).cannot?(action, subject, *args)
25
- end
26
-
27
- def authorize!(action, subject, *args)
28
- current_allowy.access_control_for!(subject).authorize!(action, subject, *args)
29
- end
30
- end
31
-
32
9
  end
33
10
  end
34
11
 
@@ -1,3 +1,3 @@
1
1
  module Allowy
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ module Allowy
4
+ module Context
5
+ extend ActiveSupport::Concern
6
+
7
+ module InstanceMethods
8
+
9
+ def allowy_context
10
+ self
11
+ end
12
+
13
+ def current_allowy
14
+ @current_allowy ||= ::Allowy::Registry.new(allowy_context)
15
+ end
16
+
17
+ def can?(action, subject, *args)
18
+ current_allowy.access_control_for!(subject).can?(action, subject, *args)
19
+ end
20
+
21
+ def cannot?(*args)
22
+ current_allowy.access_control_for!(subject).cannot?(action, subject, *args)
23
+ end
24
+
25
+ def authorize!(action, subject, *args)
26
+ current_allowy.access_control_for!(subject).authorize!(action, subject, *args)
27
+ end
28
+ end
29
+ end
30
+
31
+ class SampleContext
32
+ include Context
33
+ end
34
+
35
+ describe Context do
36
+
37
+ subject { SampleContext.new }
38
+ let(:access) { stub }
39
+
40
+ it "should create a registry" do
41
+ Registry.should_receive(:new).with(subject)
42
+ subject.current_allowy
43
+ end
44
+
45
+ it "should be able to check using can?" do
46
+ subject.current_allowy.should_receive(:access_control_for!).with(123).and_return access
47
+ access.should_receive(:can?).with(:edit, 123)
48
+ subject.can?(:edit, 123)
49
+ end
50
+
51
+ it "should call authorize!" do
52
+ access.should_receive(:authorize!).with :edit, 123
53
+ subject.current_allowy.stub(:access_control_for! => access)
54
+ subject.authorize! :edit, 123
55
+ end
56
+ end
57
+ end
58
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: allowy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-18 00:00:00.000000000 Z
12
+ date: 2012-01-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
16
- requirement: &70180722742160 !ruby/object:Gem::Requirement
16
+ requirement: &70143292970420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70180722742160
24
+ version_requirements: *70143292970420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70180722741040 !ruby/object:Gem::Requirement
27
+ requirement: &70143292969800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70180722741040
35
+ version_requirements: *70143292969800
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70180722740180 !ruby/object:Gem::Requirement
38
+ requirement: &70143292969140 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70180722740180
46
+ version_requirements: *70143292969140
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: pry
49
- requirement: &70180722739280 !ruby/object:Gem::Requirement
49
+ requirement: &70143292968580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70180722739280
57
+ version_requirements: *70143292968580
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: guard
60
- requirement: &70180722738300 !ruby/object:Gem::Requirement
60
+ requirement: &70143292967960 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70180722738300
68
+ version_requirements: *70143292967960
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard-rspec
71
- requirement: &70180722737260 !ruby/object:Gem::Requirement
71
+ requirement: &70143292967200 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70180722737260
79
+ version_requirements: *70143292967200
80
80
  description: Allowy provides CanCan-like way of checking permission but doesn't enforce
81
81
  a tight DSL giving you more control
82
82
  email:
@@ -94,12 +94,14 @@ files:
94
94
  - allowy.gemspec
95
95
  - lib/allowy.rb
96
96
  - lib/allowy/access_control.rb
97
+ - lib/allowy/context.rb
97
98
  - lib/allowy/controller_extensions.rb
98
99
  - lib/allowy/matchers.rb
99
100
  - lib/allowy/registry.rb
100
101
  - lib/allowy/rspec.rb
101
102
  - lib/allowy/version.rb
102
103
  - spec/access_control_spec.rb
104
+ - spec/context_spec.rb
103
105
  - spec/registry_spec.rb
104
106
  - spec/spec_helper.rb
105
107
  homepage: ''
@@ -128,6 +130,7 @@ specification_version: 3
128
130
  summary: Authorization with simplicity and explicitness in mind
129
131
  test_files:
130
132
  - spec/access_control_spec.rb
133
+ - spec/context_spec.rb
131
134
  - spec/registry_spec.rb
132
135
  - spec/spec_helper.rb
133
136
  has_rdoc: