concealer 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  Gemfile.lock
2
- pkg/*
2
+ pkg/*
3
+ doc/*
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - ruby-head
3
+ - 1.9.2
4
+ - 1.8.7
5
+ - ree
6
+ - jruby
7
+ - rbx
8
+ - rbx-2.0.0pre
data/Rakefile CHANGED
@@ -2,9 +2,21 @@ require 'bundler'
2
2
  require 'bundler/setup'
3
3
 
4
4
  require 'rspec/core/rake_task'
5
+ require 'rdoc/task'
5
6
 
6
7
  Bundler::GemHelper.install_tasks
7
8
 
9
+ spec = Gem::Specification.load("concealer.gemspec")
10
+
8
11
  RSpec::Core::RakeTask.new(:spec)
9
12
 
13
+ RDoc::Task.new do |rdoc|
14
+ rdoc.rdoc_dir = 'doc'
15
+ rdoc.main = "README.rdoc"
16
+ rdoc.options += spec.rdoc_options
17
+ rdoc.rdoc_files.include(spec.extra_rdoc_files)
18
+ rdoc.rdoc_files.include('lib/**/*.rb')
19
+ rdoc.title = "#{spec.name} #{spec.version}"
20
+ end
21
+
10
22
  task :default => :spec
data/concealer.gemspec CHANGED
@@ -1,4 +1,5 @@
1
- # -*- encoding: utf-8 -*-
1
+ # encoding: UTF-8
2
+
2
3
  $:.push File.expand_path("../lib", __FILE__)
3
4
  require "concealer/version"
4
5
 
@@ -19,6 +20,6 @@ Gem::Specification.new do |s|
19
20
 
20
21
  s.add_runtime_dependency('activesupport', ['~> 3.0'])
21
22
 
22
- s.add_development_dependency('rspec', ['~> 2.3'])
23
+ s.add_development_dependency('rspec', ['~> 2.6'])
23
24
  s.add_development_dependency('rake')
24
25
  end
data/lib/concealer.rb CHANGED
@@ -9,34 +9,70 @@ module Concealer
9
9
  autoload :Strategy, 'concealer/strategy'
10
10
  autoload :Fallback, 'concealer/fallback'
11
11
 
12
+ ##
13
+ # :singleton-method: default_fallback=
14
+ # :call-seq:
15
+ # default_fallback=(fallback)
16
+ #
17
+
18
+ ##
19
+ # :singleton-method: default_fallback
20
+ # Returns the set default fallback. If not explicitly set to something else
21
+ # it is Concealer::Fallback::Nil.
12
22
  mattr_accessor :default_fallback
13
23
  @@default_fallback = Concealer::Fallback::Nil.new
14
24
 
25
+ ##
26
+ # Sets the strategy concealer uses to check permissions
27
+ #
15
28
  def self.strategy=(strategy)
16
29
  Thread.current[:concealer_strategy] = strategy
17
30
  end
18
31
 
32
+ ##
33
+ # Returns the strategy that's set for concealer. Default is Concealer::Strategy::Allow
34
+ #
19
35
  def self.strategy
20
36
  Thread.current[:concealer_strategy] || Concealer::Strategy::Allow.new
21
37
  end
22
38
 
39
+ ##
40
+ # Executes the block with the given strategy.
41
+ #
23
42
  def self.with_strategy(strategy)
24
43
  save, self.strategy = self.strategy, strategy
25
44
  yield
26
45
  self.strategy = save
27
46
  end
28
47
 
48
+ ##
49
+ # Registers a new fallback for the given method on the given model
50
+ #
29
51
  def self.register_fallback(model, method, fallback)
52
+ name = model.is_a?(Symbol) ? model : model.to_s.to_sym
30
53
  @@fallbacks ||= {}
31
- @@fallbacks[model.to_s.to_sym] ||= {}
32
- @@fallbacks[model.to_s.to_sym][method] = fallback
54
+ @@fallbacks[name] ||= {}
55
+ @@fallbacks[name][method] = fallback
33
56
  end
34
57
 
58
+ ##
59
+ # Returns the set fallback for the given method on the given model
60
+ # If none is set it returns the default fallback set with Concealer.default_fallback=
61
+ #
35
62
  def self.fallback_for(model, method)
36
63
  @@fallbacks ||= {}
37
64
  @@fallbacks.try(:[], model.class.to_s.to_sym).try(:[], method.to_sym) || @@default_fallback
38
65
  end
39
66
 
67
+ ##
68
+ # Accesses the current object using Concealer to check method calls
69
+ #
70
+ # Example:
71
+ # Concealer.strategy = Concealer::Strategy::Deny.new
72
+ # user = User.new(:name => "Christian")
73
+ # user.name # => Christian
74
+ # user.concealed.name # => nil
75
+ #
40
76
  def concealed
41
77
  Proxy.new(self, Concealer.strategy)
42
78
  end
@@ -10,7 +10,16 @@ module Concealer
10
10
  protected
11
11
 
12
12
  def method_missing(name, *args, &block)
13
- if @strategy.allow?(@target, name, args)
13
+
14
+ check_concealed = /(.+)_allowed\?$/.match(name.to_s).try(:[], 1).try(:to_sym)
15
+
16
+ if check_concealed && @target.respond_to?(check_concealed)
17
+ return @strategy.allow?(@target, check_concealed, args)
18
+ end
19
+
20
+ if !@target.respond_to?(name)
21
+ raise NoMethodError, "#{@target} does not respond to #{name}"
22
+ elsif @strategy.allow?(@target, name, args)
14
23
  @target.send(name, *args, &block)
15
24
  else
16
25
  Concealer.fallback_for(@target, name).call(@target, name, args)
@@ -7,7 +7,7 @@ class Concealer::Strategy::MultiLevel < Concealer::Strategy
7
7
 
8
8
  def allow?(model, method, args)
9
9
  required_level = required_level_for(model, method, args)
10
- actual_level = @current_user.relation_level_to(owner_of(model))
10
+ actual_level = @current_user.try(:relation_level_to, owner_of(model)) || @levels.last
11
11
 
12
12
  raise "The level #{required_level} is not valid." unless @levels.include?(required_level)
13
13
  raise "The level #{actual_level} is not valid." unless @levels.include?(actual_level)
@@ -1,3 +1,3 @@
1
1
  module Concealer
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -5,5 +5,9 @@ describe Concealer::Fallback::String do
5
5
  it "should return an empty string" do
6
6
  subject.call(nil, nil, nil).should eq("")
7
7
  end
8
+
9
+ it "should return the string given on initialization" do
10
+ Concealer::Fallback::String.new("some string").call(nil, nil, nil).should eq("some string")
11
+ end
8
12
  end
9
13
  end
@@ -12,7 +12,7 @@ describe Concealer::Proxy do
12
12
  end
13
13
 
14
14
  it "should accept a target object and a strategy" do
15
- expect { Concealer::Proxy.new(model, Concealer.strategy) }.to_not raise_error ArgumentError
15
+ expect { Concealer::Proxy.new(model, strategy) }.to_not raise_error ArgumentError
16
16
  end
17
17
  end
18
18
 
@@ -34,4 +34,17 @@ describe Concealer::Proxy do
34
34
 
35
35
  subject.allowed_method.should eq(:fallback)
36
36
  end
37
+
38
+ it "should return false for allowed_method_allowed?" do
39
+ subject.allowed_method_allowed?.should be_true
40
+ end
41
+
42
+ it "should return true for denied_method_allowed?" do
43
+ strategy.stub!(:allow?).and_return(false)
44
+ subject.denied_method_allowed?.should be_false
45
+ end
46
+
47
+ it "should raise an NoMethodError when the target doesn't respond to a given method" do
48
+ expect { subject.some_missing_method }.to raise_error NoMethodError, /does not respond to some_missing_method/
49
+ end
37
50
  end
@@ -9,6 +9,18 @@ describe Concealer::Strategy::MultiLevel do
9
9
 
10
10
  subject { Concealer::Strategy::MultiLevel.new(current_user, levels) }
11
11
 
12
+ context 'with no current_user given' do
13
+ subject { Concealer::Strategy::MultiLevel.new(nil, levels) }
14
+
15
+ before(:each) do
16
+ subject.stub!(:required_level_for).and_return(:friends_of_friends)
17
+ end
18
+
19
+ it "should deny the call" do
20
+ subject.allow?(other_user, :name, nil).should be_false
21
+ end
22
+ end
23
+
12
24
  context 'required level higher than actual level' do
13
25
  before(:each) do
14
26
  subject.stub!(:required_level_for).and_return(:myself)
metadata CHANGED
@@ -1,81 +1,61 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: concealer
3
- version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 2
10
- version: 0.0.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Benedikt Deicke
14
- - "Christian B\xC3\xA4uerlein"
9
+ - Christian Bäuerlein
15
10
  autorequire:
16
11
  bindir: bin
17
12
  cert_chain: []
18
-
19
- date: 2011-03-08 00:00:00 +01:00
20
- default_executable:
21
- dependencies:
22
- - !ruby/object:Gem::Dependency
23
- prerelease: false
24
- version_requirements: &id001 !ruby/object:Gem::Requirement
13
+ date: 2012-02-15 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: &70240046174920 !ruby/object:Gem::Requirement
25
18
  none: false
26
- requirements:
19
+ requirements:
27
20
  - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 7
30
- segments:
31
- - 3
32
- - 0
33
- version: "3.0"
34
- requirement: *id001
35
- name: activesupport
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
36
23
  type: :runtime
37
- - !ruby/object:Gem::Dependency
38
24
  prerelease: false
39
- version_requirements: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: *70240046174920
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &70240046184280 !ruby/object:Gem::Requirement
40
29
  none: false
41
- requirements:
30
+ requirements:
42
31
  - - ~>
43
- - !ruby/object:Gem::Version
44
- hash: 5
45
- segments:
46
- - 2
47
- - 3
48
- version: "2.3"
49
- requirement: *id002
50
- name: rspec
32
+ - !ruby/object:Gem::Version
33
+ version: '2.6'
51
34
  type: :development
52
- - !ruby/object:Gem::Dependency
53
35
  prerelease: false
54
- version_requirements: &id003 !ruby/object:Gem::Requirement
55
- none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
- version: "0"
63
- requirement: *id003
36
+ version_requirements: *70240046184280
37
+ - !ruby/object:Gem::Dependency
64
38
  name: rake
39
+ requirement: &70240046608520 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
65
45
  type: :development
46
+ prerelease: false
47
+ version_requirements: *70240046608520
66
48
  description: Privacy control your models with ease
67
- email:
49
+ email:
68
50
  - benedikt@synatic.net
69
51
  - fabrik42@gmail.com
70
52
  executables: []
71
-
72
53
  extensions: []
73
-
74
54
  extra_rdoc_files: []
75
-
76
- files:
55
+ files:
77
56
  - .gitignore
78
57
  - .rspec
58
+ - .travis.yml
79
59
  - Gemfile
80
60
  - LICENSE
81
61
  - README.rdoc
@@ -114,41 +94,37 @@ files:
114
94
  - spec/support/concealed_model.rb
115
95
  - spec/support/paperclip.rb
116
96
  - spec/support/user.rb
117
- has_rdoc: true
118
97
  homepage: https://flinc.github.com/concealer
119
98
  licenses: []
120
-
121
99
  post_install_message:
122
100
  rdoc_options: []
123
-
124
- require_paths:
101
+ require_paths:
125
102
  - lib
126
- required_ruby_version: !ruby/object:Gem::Requirement
103
+ required_ruby_version: !ruby/object:Gem::Requirement
127
104
  none: false
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- hash: 3
132
- segments:
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
133
110
  - 0
134
- version: "0"
135
- required_rubygems_version: !ruby/object:Gem::Requirement
111
+ hash: -1897724190148084110
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
113
  none: false
137
- requirements:
138
- - - ">="
139
- - !ruby/object:Gem::Version
140
- hash: 3
141
- segments:
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
142
119
  - 0
143
- version: "0"
120
+ hash: -1897724190148084110
144
121
  requirements: []
145
-
146
122
  rubyforge_project:
147
- rubygems_version: 1.3.7
123
+ rubygems_version: 1.8.15
148
124
  signing_key:
149
125
  specification_version: 3
150
126
  summary: Privacy control your models with ease
151
- test_files:
127
+ test_files:
152
128
  - spec/concealer/fallback/nil_spec.rb
153
129
  - spec/concealer/fallback/paperclip_spec.rb
154
130
  - spec/concealer/fallback/string_spec.rb