Permalink
Browse files

critical bug fix

  • Loading branch information...
1 parent 029addb commit d3dbd3febb2eac95d5c04954d87caa822d21ec40 @unnitallman unnitallman committed Aug 9, 2012
@@ -5,33 +5,38 @@
class ActsAsAuthoritah::AccessControlList
include ActsAsAuthoritah::Matchers
-
+
attr_accessor :store
-
+
def initialize(access_rules)
@store = {}
access_rules.each do |access_rule|
@store.merge! access_rule.to_rule
end
-
+
@matchers = [DirectMatcher, ControllerMatcher, ScopeMatcher]
end
-
+
def match(identifier)
@matchers.each do |matcher|
access_rights = matcher.new(@store).match(identifier)
return access_rights unless access_rights.nil?
end
nil
end
-
+
def match_identifier(identifier)
match(identifier) || {}
end
-
- def merge!(other_access_control_list)
- store.merge!(other_access_control_list.store)
- self
+
+ def merge(other_access_control_list)
+ a = self.deep_clone
+ a.store.merge!(other_access_control_list.store)
+ a
+ end
+
+ def deep_clone
+ Marshal::load(Marshal.dump(self))
end
-
-end
+
+end
@@ -19,25 +19,25 @@ def can?(identifier, options={})
h.empty? ? !klass.send(:whitelist) : h[self.usertype(options)]
end
end
-
+
module ClassMethods
def acts_as_authoritah(path, options={})
@@whitelist = options[:whitelist] ||= false
loader = ActsAsAuthoritah::AclLoader.new(path)
@@contexts = loader.contexts
- @@acls = loader.load
+ @@acls = loader.load
end
-
+
def valid_contexts
@@contexts
end
-
+
def get_acl(key = :default)
h = @@acls[key]
- h = @@acls[:default].clone.merge!(@@acls[key]) unless key.eql?(:default)
+ h = @@acls[:default].clone.merge(@@acls[key]) unless key.eql?(:default)
h
end
-
+
def whitelist
@@whitelist
end
@@ -1,3 +1,3 @@
module ActsAsAuthoritah
- VERSION = "2.0.4"
+ VERSION = "2.0.5"
end
@@ -5,11 +5,11 @@
it "should build list" do
rule1 = ActsAsAuthoritah::AccessRule.new("Admin::Projects", "Surveys","update",{:admin => true, :anonymous => false})
rule2 = ActsAsAuthoritah::AccessRule.new("Admin::Users", "Configurations","edit",{:admin => true, :anonymous => false})
-
+
ActsAsAuthoritah::AccessControlList.new([rule1, rule2]).store.should eq rule1.to_rule.merge(rule2.to_rule)
end
end
-
+
context "match" do
before :each do
rules = [
@@ -20,82 +20,85 @@
ActsAsAuthoritah::AccessRule.new("Projects", "Surveys", "update", "e"),
ActsAsAuthoritah::AccessRule.new("", "Projects", nil, "f")
]
-
+
@acl = ActsAsAuthoritah::AccessControlList.new(rules)
end
-
+
it "test1" do
@acl.match("Admin::ForumsController#index").should eq "a"
end
-
+
it "test2" do
@acl.match("Admin::Projects::SurveysController#index").should eq "d"
end
-
+
it "test3" do
@acl.match("Projects::SurveysController#index").should eq nil
end
-
+
it "test4" do
@acl.match("Projects::SurveysController#update").should eq "e"
end
it "test5" do
@acl.match("Projects::SurveysController#edit").should eq "b"
end
-
+
it "test6" do
@acl.match("Admin::Projects::HomeController#update").should eq "c"
end
-
+
it "test7" do
@acl.match("ProjectsController#create").should eq "f"
end
-
+
it "test8" do
@acl.match("Admin::ProjectsController#create").should eq "a"
end
-
+
end
-
+
context "match_identifier" do
before :each do
rules = [
ActsAsAuthoritah::AccessRule.new("Admin", nil, nil, "c"),
]
-
+
@acl = ActsAsAuthoritah::AccessControlList.new(rules)
end
-
+
it "should return {} if there is no match" do
@acl.match_identifier("LinksController#create").should eq({})
end
-
+
it "should return the matched value if there is a match" do
@acl.match_identifier("Admin::ProjectsController#create").should eq "c"
end
end
-
+
context "merge" do
before :each do
rules = [
ActsAsAuthoritah::AccessRule.new("Admin", nil, nil, "c"),
ActsAsAuthoritah::AccessRule.new("Admin", "Projects", nil, "d"),
]
@acl1 = ActsAsAuthoritah::AccessControlList.new(rules)
-
+
rules = [
ActsAsAuthoritah::AccessRule.new("Admin", nil, nil, "C")
]
@acl2 = ActsAsAuthoritah::AccessControlList.new(rules)
end
-
+
it "should merge two @acl2 into @acl1 overriding common rule using the one from @acl2" do
@acl1.store.should eq({"Admin"=>"c", "Admin::ProjectsController"=>"d"})
@acl2.store.should eq({"Admin"=>"C"})
- @acl1.merge!(@acl2).store.should eq({"Admin"=>"C", "Admin::ProjectsController"=>"d"})
- @acl1.store.should eq({"Admin"=>"C", "Admin::ProjectsController"=>"d"})
+
+ new_acl = @acl1.merge(@acl2)
+
+ new_acl.store.should eq({"Admin"=>"C", "Admin::ProjectsController"=>"d"})
+ @acl1.store.should eq({"Admin"=>"c", "Admin::ProjectsController"=>"d"})
@acl2.store.should eq({"Admin"=>"C"})
end
end
-end
+end

0 comments on commit d3dbd3f

Please sign in to comment.