acts_as_authoritah 1.0.5 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.gitignore +15 -3
  2. data/.rspec +2 -0
  3. data/Gemfile +6 -0
  4. data/LICENSE +4 -2
  5. data/README.md +29 -0
  6. data/Rakefile +2 -56
  7. data/acts_as_authoritah.gemspec +15 -44
  8. data/lib/acts_as_authoritah.rb +15 -16
  9. data/lib/acts_as_authoritah/access_control_list.rb +32 -0
  10. data/lib/acts_as_authoritah/access_rule.rb +31 -0
  11. data/lib/acts_as_authoritah/core.rb +37 -0
  12. data/lib/acts_as_authoritah/identifier_parser.rb +28 -0
  13. data/lib/acts_as_authoritah/matchers/controller_matcher.rb +10 -0
  14. data/lib/acts_as_authoritah/matchers/direct_matcher.rb +9 -0
  15. data/lib/acts_as_authoritah/matchers/scope_matcher.rb +15 -0
  16. data/lib/acts_as_authoritah/spreadsheets/access_rights_mapper.rb +14 -0
  17. data/lib/acts_as_authoritah/spreadsheets/spreadsheet_header_parser.rb +9 -0
  18. data/lib/acts_as_authoritah/spreadsheets/spreadsheet_reader.rb +37 -0
  19. data/lib/acts_as_authoritah/spreadsheets/spreadsheet_row_parser.rb +23 -0
  20. data/lib/acts_as_authoritah/spreadsheets/spreadsheet_wrapper.rb +16 -0
  21. data/lib/acts_as_authoritah/version.rb +3 -0
  22. data/spec/acts_as_authoritah/access_control_list_spec.rb +78 -0
  23. data/spec/acts_as_authoritah/access_rule_spec.rb +39 -0
  24. data/spec/acts_as_authoritah/core_spec.rb +63 -0
  25. data/spec/acts_as_authoritah/identifier_parser_spec.rb +111 -0
  26. data/spec/acts_as_authoritah/matchers/controller_matcher_spec.rb +20 -0
  27. data/spec/acts_as_authoritah/matchers/direct_matcher_spec.rb +20 -0
  28. data/spec/acts_as_authoritah/matchers/scope_matcher_spec.rb +25 -0
  29. data/spec/acts_as_authoritah/spreadsheets/access_rights_mapper_spec.rb +13 -0
  30. data/spec/acts_as_authoritah/spreadsheets/spreadsheet_header_parser_spec.rb +8 -0
  31. data/spec/acts_as_authoritah/spreadsheets/spreadsheet_reader_spec.rb +29 -0
  32. data/spec/acts_as_authoritah/spreadsheets/spreadsheet_row_parser_spec.rb +24 -0
  33. data/spec/acts_as_authoritah/spreadsheets/spreadsheet_wrapper_spec.rb +15 -0
  34. data/spec/data/default.xls +0 -0
  35. data/spec/spec_helper.rb +5 -0
  36. metadata +91 -72
  37. data/.document +0 -5
  38. data/README.rdoc +0 -123
  39. data/VERSION +0 -1
  40. data/lib/access_control.rb +0 -30
  41. data/lib/access_rights.rb +0 -88
  42. data/lib/custom_exceptions.rb +0 -8
  43. data/lib/handler.rb +0 -38
  44. data/lib/loader.rb +0 -27
  45. data/test/acts_as_authoritah_test.rb +0 -23
  46. data/test/test_helper.rb +0 -22
@@ -0,0 +1,23 @@
1
+ class ActsAsAuthoritah::SpreadsheetRowParser
2
+ def initialize(row)
3
+ @row = row
4
+ end
5
+
6
+ def scope
7
+ @row[0]
8
+ end
9
+
10
+ def controller
11
+ @row[1]
12
+ end
13
+
14
+ def action
15
+ @row[2]
16
+ end
17
+
18
+ def access_rights
19
+ @row[4..-1].collect{|x|
20
+ x == 'x' || x == "X" || x == "*"
21
+ }
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ class ActsAsAuthoritah::SpreadsheetWrapper
2
+ def initialize(path)
3
+ spreadsheet_reader = ActsAsAuthoritah::SpreadsheetReader.new(path)
4
+ @roles = ActsAsAuthoritah::SpreadsheetHeaderParser.new(spreadsheet_reader.header).roles
5
+ @rows = spreadsheet_reader.valid_rows.collect{|row| ActsAsAuthoritah::SpreadsheetRowParser.new(row)}
6
+ end
7
+
8
+ def to_access_rules
9
+ access_rules = []
10
+ @rows.each do |row|
11
+ mapped_access_rights = ActsAsAuthoritah::AccessRightsMapper.new(@roles, row.access_rights).map
12
+ access_rules << ActsAsAuthoritah::AccessRule.new(row.scope, row.controller, row.action, mapped_access_rights)
13
+ end
14
+ access_rules
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsAuthoritah
2
+ VERSION = "2.0.0"
3
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsAuthoritah::AccessControlList do
4
+ context "store" do
5
+ it "should build list" do
6
+ rule1 = ActsAsAuthoritah::AccessRule.new("Admin::Projects", "Surveys","update",{:admin => true, :anonymous => false})
7
+ rule2 = ActsAsAuthoritah::AccessRule.new("Admin::Users", "Configurations","edit",{:admin => true, :anonymous => false})
8
+
9
+ ActsAsAuthoritah::AccessControlList.new([rule1, rule2]).store.should eq rule1.to_rule.merge(rule2.to_rule)
10
+ end
11
+ end
12
+
13
+ context "match" do
14
+ before :each do
15
+ rules = [
16
+ ActsAsAuthoritah::AccessRule.new("Admin", nil, nil, "a"),
17
+ ActsAsAuthoritah::AccessRule.new("Projects", "Surveys","edit", "b"),
18
+ ActsAsAuthoritah::AccessRule.new("Admin::Projects", nil, nil, "c"),
19
+ ActsAsAuthoritah::AccessRule.new("Admin::Projects", "Surveys", nil, "d"),
20
+ ActsAsAuthoritah::AccessRule.new("Projects", "Surveys", "update", "e"),
21
+ ActsAsAuthoritah::AccessRule.new("", "Projects", nil, "f")
22
+ ]
23
+
24
+ @acl = ActsAsAuthoritah::AccessControlList.new(rules)
25
+ end
26
+
27
+ it "test1" do
28
+ @acl.match("Admin::ForumsController#index").should eq "a"
29
+ end
30
+
31
+ it "test2" do
32
+ @acl.match("Admin::Projects::SurveysController#index").should eq "d"
33
+ end
34
+
35
+ it "test3" do
36
+ @acl.match("Projects::SurveysController#index").should eq nil
37
+ end
38
+
39
+ it "test4" do
40
+ @acl.match("Projects::SurveysController#update").should eq "e"
41
+ end
42
+
43
+ it "test5" do
44
+ @acl.match("Projects::SurveysController#edit").should eq "b"
45
+ end
46
+
47
+ it "test6" do
48
+ @acl.match("Admin::Projects::HomeController#update").should eq "c"
49
+ end
50
+
51
+ it "test7" do
52
+ @acl.match("ProjectsController#create").should eq "f"
53
+ end
54
+
55
+ it "test8" do
56
+ @acl.match("Admin::ProjectsController#create").should eq "a"
57
+ end
58
+
59
+ end
60
+
61
+ context "match_identifier" do
62
+ before :each do
63
+ rules = [
64
+ ActsAsAuthoritah::AccessRule.new("Admin", nil, nil, "c"),
65
+ ]
66
+
67
+ @acl = ActsAsAuthoritah::AccessControlList.new(rules)
68
+ end
69
+
70
+ it "should return {} if there is no match" do
71
+ @acl.match_identifier("LinksController#create").should eq({})
72
+ end
73
+
74
+ it "should return the matched value if there is a match" do
75
+ @acl.match_identifier("Admin::ProjectsController#create").should eq "c"
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsAuthoritah::AccessRule do
4
+ context "after setup" do
5
+ before :each do
6
+ @access_rule = ActsAsAuthoritah::AccessRule.new("Admin::Projects","Surveys","edit",{:admin => true, :anonymous => false})
7
+ end
8
+
9
+ it "should be able to build a new access rule and return its scope" do
10
+ @access_rule.scope.should eq "Admin::Projects"
11
+ end
12
+
13
+ it "should be able to build a new access rule and return its controller" do
14
+ @access_rule.controller.should eq "Surveys"
15
+ end
16
+
17
+ it "should be able to build a new access rule and return its action" do
18
+ @access_rule.action.should eq "edit"
19
+ end
20
+
21
+ it "should be able to build a new access rule and return its access-rights" do
22
+ @access_rule.access_rights.should eq({:admin => true, :anonymous => false})
23
+ end
24
+ end
25
+
26
+ context "generate rule" do
27
+ it "should generate a valid rule: case1" do
28
+ access_rule = ActsAsAuthoritah::AccessRule.new("Admin::Projects", "BlogPosts", "create", {:admin => true, :anonymous => false})
29
+ access_rule.to_rule.should eq({"Admin::Projects::BlogPostsController#create" => {:admin => true, :anonymous => false}})
30
+ end
31
+
32
+ it "should generate a valid rule: case2" do
33
+ access_rule = ActsAsAuthoritah::AccessRule.new("", "Projects", nil, "f")
34
+ access_rule.to_rule.should eq({"ProjectsController" => "f"})
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,63 @@
1
+ describe ActsAsAuthoritah::Core do
2
+ before :each do
3
+ class Foo
4
+ include ActsAsAuthoritah::Core
5
+ acts_as_authoritah "spec/data/default.xls"
6
+
7
+ def usertype(options)
8
+ "admin"
9
+ end
10
+ end
11
+ end
12
+
13
+ it "should work" do
14
+ end
15
+
16
+ it "should be able to use 'can?' on instance of Foo class" do
17
+ Foo.new.should respond_to('can?')
18
+ end
19
+
20
+ it "should be able to add 'default_acl' method to Foo class" do
21
+ Foo.should respond_to('default_acl')
22
+ end
23
+
24
+ it "should be able to use 'can?' on Foo to check access rights - case1" do
25
+ Foo.new.can?("scope1::scope2::DummyController#edit").should eq true
26
+ end
27
+
28
+ context "whitelist=false" do
29
+ before :each do
30
+ class Foo
31
+ include ActsAsAuthoritah::Core
32
+ acts_as_authoritah "spec/data/default.xls"
33
+
34
+ def usertype(options)
35
+ "admin"
36
+ end
37
+ end
38
+ end
39
+
40
+ it "can? should return true when a matching rule is not found" do
41
+ Foo.new.can?("DummyController#edit").should eq true
42
+ end
43
+ end
44
+
45
+ context "whitelist=true" do
46
+ before :each do
47
+ class Foo
48
+ include ActsAsAuthoritah::Core
49
+ acts_as_authoritah "spec/data/default.xls", :whitelist => true
50
+
51
+ def usertype(options)
52
+ "admin"
53
+ end
54
+ end
55
+ end
56
+
57
+ it "can? should return false when a matching rule is not found" do
58
+ Foo.new.can?("DummyController#edit").should eq false
59
+ end
60
+ end
61
+
62
+
63
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsAuthoritah::IdentifierParser do
4
+
5
+ context "action" do
6
+ it "should return nil if identifier is empty" do
7
+ identifier_parser = ActsAsAuthoritah::IdentifierParser.new("")
8
+ identifier_parser.action.should be_nil
9
+ end
10
+
11
+ it "should return nil if identifier is nil" do
12
+ identifier_parser = ActsAsAuthoritah::IdentifierParser.new(nil)
13
+ identifier_parser.action.should be_nil
14
+ end
15
+
16
+ it "should return nil if only controller name is present" do
17
+ identifier_parser = ActsAsAuthoritah::IdentifierParser.new("BlogPosts")
18
+ identifier_parser.action.should be_nil
19
+ end
20
+
21
+ it "should return nil if only controller name and scope is present" do
22
+ identifier_parser = ActsAsAuthoritah::IdentifierParser.new("Admin::BlogPosts")
23
+ identifier_parser.action.should be_nil
24
+ end
25
+
26
+ it "should return nil if only controller name and '#' is present" do
27
+ identifier_parser = ActsAsAuthoritah::IdentifierParser.new("Admin::BlogPosts#")
28
+ identifier_parser.action.should be_nil
29
+ end
30
+
31
+ it "should return action name when controller and action are present" do
32
+ identifier_parser = ActsAsAuthoritah::IdentifierParser.new("BlogPosts#edit")
33
+ identifier_parser.action.should eq "edit"
34
+ end
35
+
36
+ it "should return action name when controller, one level scope and action are present" do
37
+ identifier_parser = ActsAsAuthoritah::IdentifierParser.new("Projects::BlogPosts#edit")
38
+ identifier_parser.action.should eq "edit"
39
+ end
40
+
41
+ it "should return action name when controller, two level scopes and action are present" do
42
+ identifier_parser = ActsAsAuthoritah::IdentifierParser.new("Admin::Projects::BlogPosts#edit")
43
+ identifier_parser.action.should eq "edit"
44
+ end
45
+ end
46
+
47
+ # context "controller" do
48
+ # it "should return nil if identifier is empty" do
49
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new("", "edit a blog post",{:admin => true, :anonymous => false})
50
+ # identifier_parser.controller.should be_nil
51
+ # end
52
+ #
53
+ # it "should return nil if identifier is nil" do
54
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new(nil, "edit a blog post",{:admin => true, :anonymous => false})
55
+ # identifier_parser.controller.should be_nil
56
+ # end
57
+ #
58
+ # it "should return controller name if only controller is present" do
59
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new("BlogPosts", "edit a blog post",{:admin => true, :anonymous => false})
60
+ # identifier_parser.controller.should eq "BlogPosts"
61
+ # end
62
+ #
63
+ # it "should return controller name if controller and action are present" do
64
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new("BlogPosts#edit", "edit a blog post",{:admin => true, :anonymous => false})
65
+ # identifier_parser.controller.should eq "BlogPosts"
66
+ # end
67
+ #
68
+ # it "should return controller name if controller, one level scope and action are present" do
69
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new("Projects::BlogPosts#edit", "edit a blog post",{:admin => true, :anonymous => false})
70
+ # identifier_parser.controller.should eq "Projects::BlogPosts"
71
+ # end
72
+ #
73
+ # it "should return controller name if controller, two level scopes and action are present" do
74
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new("Admin::Projects::BlogPosts#edit", "edit a blog post",{:admin => true, :anonymous => false})
75
+ # identifier_parser.controller.should eq "Admin::Projects::BlogPosts"
76
+ # end
77
+ #
78
+ # it "should return controller name if controller and '#' are present" do
79
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new("BlogPosts#", "edit a blog post",{:admin => true, :anonymous => false})
80
+ # identifier_parser.controller.should eq "BlogPosts"
81
+ # end
82
+ # end
83
+ #
84
+ # context "scopes" do
85
+ # it "should return nil if identifier is empty" do
86
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new("", "edit a blog post",{:admin => true, :anonymous => false})
87
+ # identifier_parser.scopes.should eq []
88
+ # end
89
+ #
90
+ # it "should return nil if identifier is nil" do
91
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new(nil, "edit a blog post",{:admin => true, :anonymous => false})
92
+ # identifier_parser.scopes.should eq []
93
+ # end
94
+ #
95
+ # it "should return [] if only controller is present" do
96
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new("BlogPosts", "edit a blog post",{:admin => true, :anonymous => false})
97
+ # identifier_parser.scopes.should eq []
98
+ # end
99
+ #
100
+ # it "should return scope array if controller and level one scope is present" do
101
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new("Projects::BlogPosts", "edit a blog post",{:admin => true, :anonymous => false})
102
+ # identifier_parser.scopes.should eq ["Projects"]
103
+ # end
104
+ #
105
+ # it "should return scope array if controller and level two scope is present" do
106
+ # identifier_parser = ActsAsAuthoritah::IdentifierParser.new("Admin::Projects::BlogPosts", "edit a blog post",{:admin => true, :anonymous => false})
107
+ # identifier_parser.scopes.should eq ["Admin","Projects"]
108
+ # end
109
+ # end
110
+
111
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsAuthoritah::Matchers::ControllerMatcher do
4
+ before :each do
5
+ @store = {
6
+ "Admin::Projects::ForumsController#index" => "abc",
7
+ "Admin::Projects" => "abc",
8
+ "BlogsController" => "abc",
9
+ "Admin" => "abc"
10
+ }
11
+ end
12
+
13
+ it "should return nil if controller din't match" do
14
+ ActsAsAuthoritah::Matchers::ControllerMatcher.new(@store).match("ForumsController#index").should be_nil
15
+ end
16
+
17
+ it "should match the controller and return the result" do
18
+ ActsAsAuthoritah::Matchers::ControllerMatcher.new(@store).match("BlogsController#index").should eq "abc"
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsAuthoritah::Matchers::DirectMatcher do
4
+ before :each do
5
+ @store = {
6
+ "Admin::Projects::BlogsController#index" => "abc",
7
+ "Admin::Projects" => "abc",
8
+ "Admin::Projects::BlogsController" => "abc",
9
+ "Admin" => "abc"
10
+ }
11
+ end
12
+
13
+ it "should return nil if identifier din't match" do
14
+ ActsAsAuthoritah::Matchers::DirectMatcher.new(@store).match("ProjectsController#home").should be_nil
15
+ end
16
+
17
+ it "should match the identifier and return the result" do
18
+ ActsAsAuthoritah::Matchers::DirectMatcher.new(@store).match("Admin::Projects::BlogsController#index").should eq "abc"
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsAuthoritah::Matchers::ScopeMatcher do
4
+ before :each do
5
+ @store = {
6
+ "Admin::Projects::ForumsController#index" => "abc",
7
+ "Admin::Projects" => "xyz",
8
+ "BlogsController" => "abc",
9
+ "Admin" => "abc"
10
+ }
11
+ end
12
+
13
+ it "should return nil if scope din't match" do
14
+ ActsAsAuthoritah::Matchers::ScopeMatcher.new(@store).match("Forums").should be_nil
15
+ end
16
+
17
+ it "should match the second level scope and return the result" do
18
+ ActsAsAuthoritah::Matchers::ScopeMatcher.new(@store).match("Admin::Projects::BlogsController#index").should eq "xyz"
19
+ end
20
+
21
+ it "should match the first level scope and return the result" do
22
+ ActsAsAuthoritah::Matchers::ScopeMatcher.new(@store).match("Admin::Surveys::BlogsController#index").should eq "abc"
23
+ end
24
+
25
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsAuthoritah::AccessRightsMapper do
4
+ it "should take roles and access rights and map them correctly" do
5
+ roles = ["admin", "super_admin", "anonymous"]
6
+ access_rights = [true, true, false]
7
+ ActsAsAuthoritah::AccessRightsMapper.new(roles, access_rights).map.should eq({
8
+ "admin" => true,
9
+ "super_admin" => true,
10
+ "anonymous" => false
11
+ })
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsAuthoritah::SpreadsheetHeaderParser do
4
+ it "should return the roles" do
5
+ header_row = ["scope", "controller", "action", "description", "admin", "anonymous", "super_admin"]
6
+ ActsAsAuthoritah::SpreadsheetHeaderParser.new(header_row).roles.should eq ["admin", "anonymous", "super_admin"]
7
+ end
8
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsAuthoritah::SpreadsheetReader do
4
+ it "should be able to open a spreadsheet give a valid path" do
5
+ ActsAsAuthoritah::SpreadsheetReader.new("spec/data/default.xls").valid?.should eq true
6
+ end
7
+
8
+ it "should be invalid if file not found" do
9
+ ActsAsAuthoritah::SpreadsheetReader.new("spec/data/no_such_file.xls").valid?.should eq false
10
+ end
11
+
12
+ it "should return all valid rows (skip first row and other empty rows)" do
13
+ reader = ActsAsAuthoritah::SpreadsheetReader.new("spec/data/default.xls")
14
+ reader.valid_rows.count.should eq 2
15
+ end
16
+
17
+ it "should returns rows in the expected format" do
18
+ reader = ActsAsAuthoritah::SpreadsheetReader.new("spec/data/default.xls")
19
+ reader.valid_rows.should eq [
20
+ ["scope1::scope2", "Dummy", "edit", "test", "x", nil, "x"],
21
+ ["scope3::scope4", "Another", "update", "test", nil, "x", "x"]
22
+ ]
23
+ end
24
+
25
+ it "should return the header row" do
26
+ reader = ActsAsAuthoritah::SpreadsheetReader.new("spec/data/default.xls")
27
+ reader.header.should eq ["scope", "controller", "action", "description", "admin", "anonymous", "super_admin"]
28
+ end
29
+ end