hone-lockdown 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/.gitignore +6 -0
  2. data/History.txt +195 -0
  3. data/README.txt +36 -0
  4. data/Rakefile +14 -0
  5. data/VERSION +1 -0
  6. data/lib/lockdown.rb +73 -0
  7. data/lib/lockdown/context.rb +48 -0
  8. data/lib/lockdown/database.rb +117 -0
  9. data/lib/lockdown/frameworks/rails.rb +105 -0
  10. data/lib/lockdown/frameworks/rails/controller.rb +163 -0
  11. data/lib/lockdown/frameworks/rails/view.rb +50 -0
  12. data/lib/lockdown/helper.rb +101 -0
  13. data/lib/lockdown/orms/active_record.rb +68 -0
  14. data/lib/lockdown/permission.rb +240 -0
  15. data/lib/lockdown/rules.rb +378 -0
  16. data/lib/lockdown/session.rb +57 -0
  17. data/lib/lockdown/system.rb +52 -0
  18. data/rails_generators/lockdown/lockdown_generator.rb +273 -0
  19. data/rails_generators/lockdown/templates/app/controllers/permissions_controller.rb +22 -0
  20. data/rails_generators/lockdown/templates/app/controllers/sessions_controller.rb +39 -0
  21. data/rails_generators/lockdown/templates/app/controllers/user_groups_controller.rb +122 -0
  22. data/rails_generators/lockdown/templates/app/controllers/users_controller.rb +117 -0
  23. data/rails_generators/lockdown/templates/app/helpers/permissions_helper.rb +2 -0
  24. data/rails_generators/lockdown/templates/app/helpers/user_groups_helper.rb +2 -0
  25. data/rails_generators/lockdown/templates/app/helpers/users_helper.rb +2 -0
  26. data/rails_generators/lockdown/templates/app/models/permission.rb +13 -0
  27. data/rails_generators/lockdown/templates/app/models/profile.rb +10 -0
  28. data/rails_generators/lockdown/templates/app/models/user.rb +95 -0
  29. data/rails_generators/lockdown/templates/app/models/user_group.rb +15 -0
  30. data/rails_generators/lockdown/templates/app/views/permissions/index.html.erb +16 -0
  31. data/rails_generators/lockdown/templates/app/views/permissions/show.html.erb +26 -0
  32. data/rails_generators/lockdown/templates/app/views/sessions/new.html.erb +12 -0
  33. data/rails_generators/lockdown/templates/app/views/user_groups/edit.html.erb +33 -0
  34. data/rails_generators/lockdown/templates/app/views/user_groups/index.html.erb +20 -0
  35. data/rails_generators/lockdown/templates/app/views/user_groups/new.html.erb +31 -0
  36. data/rails_generators/lockdown/templates/app/views/user_groups/show.html.erb +29 -0
  37. data/rails_generators/lockdown/templates/app/views/users/edit.html.erb +51 -0
  38. data/rails_generators/lockdown/templates/app/views/users/index.html.erb +22 -0
  39. data/rails_generators/lockdown/templates/app/views/users/new.html.erb +50 -0
  40. data/rails_generators/lockdown/templates/app/views/users/show.html.erb +33 -0
  41. data/rails_generators/lockdown/templates/config/initializers/lockit.rb +1 -0
  42. data/rails_generators/lockdown/templates/db/migrate/create_admin_user.rb +17 -0
  43. data/rails_generators/lockdown/templates/db/migrate/create_permissions.rb +19 -0
  44. data/rails_generators/lockdown/templates/db/migrate/create_profiles.rb +26 -0
  45. data/rails_generators/lockdown/templates/db/migrate/create_user_groups.rb +19 -0
  46. data/rails_generators/lockdown/templates/db/migrate/create_users.rb +17 -0
  47. data/rails_generators/lockdown/templates/lib/lockdown/README +42 -0
  48. data/rails_generators/lockdown/templates/lib/lockdown/init.rb +131 -0
  49. data/spec/lockdown/database_spec.rb +158 -0
  50. data/spec/lockdown/frameworks/rails/controller_spec.rb +224 -0
  51. data/spec/lockdown/frameworks/rails/view_spec.rb +87 -0
  52. data/spec/lockdown/frameworks/rails_spec.rb +175 -0
  53. data/spec/lockdown/permission_spec.rb +166 -0
  54. data/spec/lockdown/rules_spec.rb +109 -0
  55. data/spec/lockdown/session_spec.rb +89 -0
  56. data/spec/lockdown/system_spec.rb +59 -0
  57. data/spec/lockdown_spec.rb +19 -0
  58. data/spec/rcov.opts +5 -0
  59. data/spec/spec.opts +3 -0
  60. data/spec/spec_helper.rb +1 -0
  61. metadata +131 -0
@@ -0,0 +1,175 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. .. spec_helper])
2
+
3
+ describe Lockdown::Frameworks::Rails do
4
+ before do
5
+ @rails = Lockdown::Frameworks::Rails
6
+ @rails.stub!(:use_me?).and_return(true)
7
+
8
+ @lockdown = mock("lockdown")
9
+ end
10
+
11
+
12
+ describe "#included" do
13
+ it "should extend lockdown with rails environment" do
14
+ @lockdown.should_receive(:extend).
15
+ with(Lockdown::Frameworks::Rails::Environment)
16
+
17
+ @rails.should_receive(:mixin)
18
+
19
+ @rails.included(@lockdown)
20
+ end
21
+ end
22
+
23
+ describe "#mixin" do
24
+ it "should perform class_eval on controller view and system to inject itself" do
25
+ module ActionController; class Base; end end
26
+ module ActionView; class Base; end end
27
+
28
+ Lockdown.stub!(:controller_parent).and_return(ActionController::Base)
29
+ Lockdown.stub!(:view_helper).and_return(ActionView::Base)
30
+
31
+ ActionView::Base.should_receive(:class_eval)
32
+
33
+ ActionController::Base.should_receive(:helper_method)
34
+ ActionController::Base.should_receive(:before_filter)
35
+ ActionController::Base.should_receive(:filter_parameter_logging)
36
+ ActionController::Base.should_receive(:rescue_from)
37
+
38
+ ActionController::Base.should_receive(:class_eval)
39
+
40
+ Lockdown::System.should_receive(:class_eval)
41
+
42
+
43
+ @rails.mixin
44
+ end
45
+
46
+ end
47
+ end
48
+
49
+ describe Lockdown::Frameworks::Rails::Environment do
50
+
51
+ RAILS_ROOT = "/shibby/dibby/do"
52
+ before do
53
+ @env = class Test; extend Lockdown::Frameworks::Rails::Environment; end
54
+ end
55
+
56
+ describe "#project_root" do
57
+ it "should return rails root" do
58
+ @env.project_root.should == "/shibby/dibby/do"
59
+ end
60
+ end
61
+
62
+ describe "#init_file" do
63
+ it "should return path to init_file" do
64
+ @env.stub!(:project_root).and_return("/shibby/dibby/do")
65
+ @env.init_file.should == "/shibby/dibby/do/lib/lockdown/init.rb"
66
+ end
67
+ end
68
+
69
+ describe "#controller_class_name" do
70
+ it "should add Controller to name" do
71
+ @env.controller_class_name("user").should == "UserController"
72
+ end
73
+
74
+ it "should convert two underscores to a namespaced controller" do
75
+ @env.controller_class_name("admin__user").should == "Admin::UserController"
76
+ end
77
+ end
78
+
79
+ describe "#controller_parent" do
80
+ it "should return ActionController::Base" do
81
+ module ActionController; class Base; end end
82
+
83
+ @env.controller_parent.should == ActionController::Base
84
+ end
85
+ end
86
+
87
+ describe "#view_helper" do
88
+ it "should return ActionView::Base" do
89
+ module ActionView; class Base; end end
90
+
91
+ @env.view_helper.should == ActionView::Base
92
+ end
93
+ end
94
+ end
95
+
96
+ describe Lockdown::Frameworks::Rails::System do
97
+ class Test
98
+ extend Lockdown::Frameworks::Rails::System
99
+ class << self
100
+ attr_accessor :controller_classes
101
+ end
102
+ end
103
+
104
+ module Rails
105
+ module VERSION
106
+ MAJOR = 2
107
+ MINOR = 2
108
+ TINY = 2
109
+ end
110
+ end
111
+
112
+ before do
113
+ @env = Test
114
+ @env.controller_classes = {}
115
+ end
116
+
117
+ describe "#skip_sync?" do
118
+ end
119
+
120
+ describe "#load_controller_classes" do
121
+ end
122
+
123
+ describe "#maybe_load_framework_controller_parent" do
124
+ it "should call require_or_load with application.rb < 2.3" do
125
+ @env.should_receive(:require_or_load).with("application.rb")
126
+
127
+ @env.maybe_load_framework_controller_parent
128
+ end
129
+
130
+ it "should call require_or_load with application_controller.rb >= 2.3" do
131
+ module Rails
132
+ module VERSION
133
+ MINOR = 3
134
+ TINY = 0
135
+ end
136
+ end
137
+
138
+ @env.should_receive(:require_or_load).with("application_controller.rb")
139
+
140
+ @env.maybe_load_framework_controller_parent
141
+ end
142
+ end
143
+
144
+ describe "#lockdown_load" do
145
+ it "should add class to controller classes" do
146
+ @env.stub!(:class_name_from_file).and_return("controller_class")
147
+ Lockdown.stub!(:qualified_const_get).and_return(:controller_class)
148
+ @env.stub!(:require_or_load)
149
+
150
+ @env.lockdown_load("controller_file")
151
+
152
+ @env.controller_classes["ControllerFile"].should == :controller_class
153
+ end
154
+ end
155
+
156
+ describe "#require_or_load" do
157
+ it "should use Dependencies if not defined in ActiveSupport" do
158
+ module ActiveSupport; end
159
+ Dependencies = mock("dependencies") unless defined?(Dependencies)
160
+
161
+ Dependencies.should_receive(:require_or_load).with("controller_file")
162
+
163
+ @env.require_or_load("controller_file")
164
+ end
165
+
166
+ it "should use ActiveSupport::Dependencies if defined" do
167
+ module ActiveSupport; class Dependencies; end end
168
+
169
+ ActiveSupport::Dependencies.should_receive(:require_or_load).
170
+ with("controller_file")
171
+
172
+ @env.require_or_load("controller_file")
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,166 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ describe Lockdown::Permission do
4
+ before do
5
+
6
+ @permission = Lockdown::Permission.new(:user_management)
7
+ @permission.stub!(:paths_for).and_return([])
8
+ end
9
+
10
+ describe "#with_controller" do
11
+ before do
12
+ @permission.with_controller(:users)
13
+ end
14
+
15
+ it "should set current_context to ControllerContext" do
16
+ @permission.current_context.class.should equal(Lockdown::ControllerContext)
17
+ end
18
+ end
19
+
20
+ describe "#only_methods" do
21
+ before do
22
+ @permission.with_controller(:users).only_methods(:show, :edit)
23
+ end
24
+
25
+ it "should set current_context to RootContext" do
26
+ @permission.current_context.class.should equal(Lockdown::RootContext)
27
+ end
28
+ end
29
+
30
+ describe "#except_methods" do
31
+ before do
32
+ @permission.with_controller(:users).except_methods(:destroy)
33
+ end
34
+
35
+ it "should set current_context to RootContext" do
36
+ @permission.current_context.class.should equal(Lockdown::RootContext)
37
+ end
38
+ end
39
+
40
+ describe "#to_model" do
41
+ before do
42
+ @permission.to_model(:user)
43
+ end
44
+
45
+ it "should set current_context to ModelContext" do
46
+ @permission.current_context.class.should equal(Lockdown::ModelContext)
47
+ end
48
+ end
49
+
50
+ describe "#where" do
51
+ before do
52
+ @permission.to_model(:user).where(:current_user_id)
53
+ end
54
+
55
+ it "should set current_context to ModelWhereContext" do
56
+ @permission.current_context.class.should equal(Lockdown::ModelWhereContext)
57
+ end
58
+ end
59
+
60
+ describe "#with_proc" do
61
+ before do
62
+ @permission.to_model(:user).with_proc
63
+ end
64
+
65
+ it "should set current_context to ModelWithProcContext" do
66
+ @permission.current_context.should be_an_instance_of(Lockdown::ModelWithProcContext)
67
+ end
68
+ end
69
+
70
+ describe "#equals" do
71
+ before do
72
+ @permission.to_model(:user).where(:current_user_id).equals(:id)
73
+ end
74
+
75
+ it "should set current_context to RootContext" do
76
+ @permission.current_context.class.should equal(Lockdown::RootContext)
77
+ end
78
+ end
79
+
80
+ describe "#is_in" do
81
+ before do
82
+ @permission.to_model(:user).where(:current_user_id).is_in(:manager_ids)
83
+ end
84
+
85
+ it "should set current_context to RootContext" do
86
+ @permission.current_context.class.should equal(Lockdown::RootContext)
87
+ end
88
+ end
89
+
90
+ describe "#set_as_public_access" do
91
+ it "should raise an PermissionScopeCollision if already protected" do
92
+ @permission.set_as_protected_access
93
+ lambda{@permission.set_as_public_access}.
94
+ should raise_error(Lockdown::PermissionScopeCollision)
95
+ end
96
+ end
97
+
98
+
99
+ describe "#set_as_protected_access" do
100
+ it "should raise an PermissionScopeCollision if already public" do
101
+ @permission.set_as_public_access
102
+ lambda{@permission.set_as_protected_access}.
103
+ should raise_error(Lockdown::PermissionScopeCollision)
104
+ end
105
+ end
106
+
107
+ describe "while in RootContext" do
108
+ before do
109
+ @permission.with_controller(:users).only_methods(:show, :edit)
110
+ end
111
+
112
+ it "should raise InvalidRuleContext trying to access methods out of context" do
113
+ methods = [:only_methods, :except_methods, :where, :equals, :is_in, :includes]
114
+
115
+ methods.each do |method|
116
+ lambda{@permission.send(method, :sample_param)}.
117
+ should raise_error(Lockdown::InvalidRuleContext)
118
+ end
119
+ end
120
+ end
121
+
122
+ describe "while in ControllerContext" do
123
+ before do
124
+ @permission.with_controller(:users)
125
+ end
126
+
127
+ it "should raise InvalidRuleContext trying to access methods out of context" do
128
+ methods = [:where, :equals, :is_in, :includes]
129
+
130
+ methods.each do |method|
131
+ lambda{@permission.send(method, :sample_param)}.
132
+ should raise_error(Lockdown::InvalidRuleContext)
133
+ end
134
+ end
135
+ end
136
+
137
+ describe "while in ModelContext" do
138
+ before do
139
+ @permission.to_model(:user)
140
+ end
141
+
142
+ it "should raise InvalidRuleContext trying to access methods out of context" do
143
+ methods = [:with_controller, :and_controller, :only_methods, :except_methods, :to_model, :equals, :is_in, :includes]
144
+
145
+ methods.each do |method|
146
+ lambda{@permission.send(method, :sample_param)}.
147
+ should raise_error(Lockdown::InvalidRuleContext)
148
+ end
149
+ end
150
+ end
151
+
152
+ describe "while in ModelWhereContext" do
153
+ before do
154
+ @permission.to_model(:user).where(:current_user_id)
155
+ end
156
+
157
+ it "should raise InvalidRuleContext trying to access methods out of context" do
158
+ methods = [:with_controller, :and_controller, :only_methods, :except_methods, :to_model, :where]
159
+
160
+ methods.each do |method|
161
+ lambda{@permission.send(method, :sample_param)}.
162
+ should raise_error(Lockdown::InvalidRuleContext)
163
+ end
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,109 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ class TestSystem; extend Lockdown::Rules; end
4
+
5
+ describe Lockdown::Rules do
6
+ before do
7
+ @rules = TestSystem
8
+ @rules.set_defaults
9
+ end
10
+
11
+ describe "#set_permission" do
12
+ it "should create and return a Permission object" do
13
+ @rules.set_permission(:user_management).
14
+ should == Lockdown::Permission.new(:user_management)
15
+ end
16
+ end
17
+
18
+ describe "#set_public_access" do
19
+ it "should define the permission as public" do
20
+ @rules.set_permission(:user_management)
21
+ @rules.set_public_access(:user_management)
22
+ end
23
+ end
24
+
25
+ describe "#set_public_access" do
26
+ it "should define the permission as public" do
27
+ @rules.set_permission(:home_page)
28
+ @rules.set_public_access(:home_page)
29
+ perm = @rules.permission_objects.find{|name, object| name == :home_page}
30
+ perm[1].public_access?.should be_true
31
+ end
32
+
33
+ it "should raise and InvalidRuleAssignment if permission does not exist" do
34
+ msg = "Permission not found: user_management"
35
+ lambda{@rules.set_public_access(:user_management)}.should
36
+ raise_error(Lockdown::InvalidRuleAssignment, msg)
37
+ end
38
+ end
39
+
40
+ describe "#set_protected_access" do
41
+ it "should define the permission as protected" do
42
+ @rules.set_permission(:user_management)
43
+ @rules.set_protected_access(:user_management)
44
+ perm = @rules.permission_objects.find{|name, object| name == :user_management}
45
+ perm[1].protected_access?.should be_true
46
+ end
47
+
48
+ it "should raise and InvalidRuleAssignment if permission does not exist" do
49
+ msg = "Permission not found: user_management"
50
+ lambda{@rules.set_protected_access(:user_management)}.should
51
+ raise_error(Lockdown::InvalidRuleAssignment, msg)
52
+ end
53
+ end
54
+
55
+ describe "#get_permissions" do
56
+ it "should return array of permission names as symbols" do
57
+ @rules.set_permission(:home_page)
58
+ @rules.set_permission(:user_management)
59
+ @rules.process_rules
60
+ @rules.get_permissions.should include(:home_page)
61
+ @rules.get_permissions.should include(:user_management)
62
+ end
63
+ end
64
+
65
+ describe "#permission_exists?" do
66
+ it "should return true if permission exists" do
67
+ @rules.set_permission(:home_page)
68
+ @rules.process_rules
69
+ @rules.permission_exists?(:home_page).should be_true
70
+ end
71
+
72
+ it "should return false if permission does not exist" do
73
+ @rules.permission_exists?(:home_page).should be_false
74
+ end
75
+ end
76
+
77
+ describe "#get_user_groups" do
78
+ it "should return array of user group names as symbols" do
79
+ @rules.set_permission(:user_management)
80
+ @rules.set_user_group(:security_management, :user_management)
81
+ @rules.get_user_groups.should == [:security_management]
82
+ end
83
+ end
84
+
85
+ describe "#user_group_exists?" do
86
+ it "should return true if user_group exists" do
87
+ @rules.set_user_group(:user_management, :some_perm)
88
+ @rules.user_group_exists?(:user_management).should be_true
89
+ end
90
+
91
+ it "should return false if user_group does not exist" do
92
+ @rules.user_group_exists?(:user_management).should be_false
93
+ end
94
+ end
95
+
96
+
97
+ describe "#make_user_administrator" do
98
+ end
99
+
100
+ describe "#process_rules" do
101
+ it "should validate user_group permissions" do
102
+ @rules.set_user_group(:test_group, :a_perm)
103
+ error = "User Group: test_group, permission not found: a_perm"
104
+
105
+ lambda{@rules.process_rules}.
106
+ should raise_error(Lockdown::InvalidRuleAssignment, error)
107
+ end
108
+ end
109
+ end