Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

init commit

  • Loading branch information...
commit 72ea17b892fa7df1e2a905ea74d9d23c4d34872b 1 parent ea0fc35
@randx authored
View
1  .gitignore
@@ -0,0 +1 @@
+.rvmrc
View
1  .rspec
@@ -0,0 +1 @@
+--color
View
2  Gemfile
@@ -0,0 +1,2 @@
+source "http://rubygems.org"
+gem "rspec"
View
18 Gemfile.lock
@@ -0,0 +1,18 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ diff-lcs (1.1.2)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rspec
View
43 lib/six.rb
@@ -0,0 +1,43 @@
+require "rubygems"
+require "bundler/setup"
+require "singleton"
+
+module Six
+ class Guard
+ include Singleton
+
+ attr_accessor :rules_packs
+ attr_accessor :current_rule_pack
+
+ def initialize
+ @rules_packs = {}
+ end
+
+ def use(name)
+ @current_rule_pack = rules_packs[name.to_sym] if pack_exist?(name)
+ self
+ end
+
+ def add_pack(name, pack)
+ rules_packs[name.to_sym] = pack if valid_rules_object?(pack)
+ end
+
+ def remove_pack(name)
+ rules_packs.delete(name.to_sym)
+ end
+
+ def valid_rules_object?(object)
+ object.respond_to?(:allowed) ||
+ raise("Wrong Rule Pack. You must provide 'allowed' method")
+ end
+
+ def pack_exist?(name)
+ rules_packs.has_key?(name.to_sym) ||
+ raise("No such pack")
+ end
+
+ def protect!(action, object, subject)
+ current_rule_pack.allowed(object, subject).include?(action)
+ end
+ end
+end
View
43 spec/six_rules_packs_spec.rb
@@ -0,0 +1,43 @@
+require "./lib/six"
+
+describe Six do
+ describe "Rules Packs" do
+ class MyRules
+ def allowed(object, subject)
+ []
+ end
+ end
+
+ describe :use do
+ before do
+ @rules = MyRules.new
+ @guard = Six::Guard.instance
+ @guard.add_pack(:global, @rules)
+ end
+
+ it { @guard.use(:global).should be_true }
+ it { lambda { @guard.use(:noname)}.should raise_error("No such pack") }
+ end
+
+ describe :add_pack do
+ before do
+ @rules = MyRules.new
+ @guard = Six::Guard.instance
+ end
+
+ it { @guard.add_pack(:global, @rules).should be_true }
+ it { lambda { @guard.add_pack(:wrong, nil)}.should raise_error("Wrong Rule Pack. You must provide 'allowed' method") }
+ end
+
+ describe :remove_pack do
+ before do
+ @rules = MyRules.new
+ @guard = Six::Guard.instance
+ @guard.add_pack(:global, @rules)
+ end
+
+ it { @guard.remove_pack(:global).should be_true }
+ it { @guard.remove_pack(:zzz).should be_false }
+ end
+ end
+end
View
29 spec/six_spec.rb
@@ -0,0 +1,29 @@
+require "./lib/six"
+
+describe Six do
+ describe "protection" do
+ class MyRules
+ def allowed(object, subject)
+ rules = []
+ rules << :read_book if object == 1 && subject == 2
+ rules
+ end
+ end
+
+ describe "protect!" do
+ before do
+ @rules = MyRules.new
+ @guard = Six::Guard.instance
+ @guard.add_pack(:myrules, @rules)
+ end
+
+ it "should allow acceess" do
+ @guard.use(:myrules).protect!(:read_book, 1, 2).should be_true
+ end
+
+ it "should prevent unauthorized acceess" do
+ @guard.use(:myrules).protect!(:read_book, 2, 2).should be_false
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.