andrewzielinski-lockdown 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/History.txt +195 -0
  2. data/README.txt +36 -0
  3. data/Rakefile +41 -0
  4. data/lib/lockdown.rb +70 -0
  5. data/lib/lockdown/context.rb +41 -0
  6. data/lib/lockdown/database.rb +105 -0
  7. data/lib/lockdown/frameworks/rails.rb +146 -0
  8. data/lib/lockdown/frameworks/rails/controller.rb +147 -0
  9. data/lib/lockdown/frameworks/rails/view.rb +61 -0
  10. data/lib/lockdown/helper.rb +95 -0
  11. data/lib/lockdown/orms/active_record.rb +68 -0
  12. data/lib/lockdown/permission.rb +204 -0
  13. data/lib/lockdown/rules.rb +289 -0
  14. data/lib/lockdown/session.rb +57 -0
  15. data/lib/lockdown/system.rb +57 -0
  16. data/rails_generators/lockdown/lockdown_generator.rb +273 -0
  17. data/rails_generators/lockdown/templates/app/controllers/permissions_controller.rb +22 -0
  18. data/rails_generators/lockdown/templates/app/controllers/sessions_controller.rb +39 -0
  19. data/rails_generators/lockdown/templates/app/controllers/user_groups_controller.rb +122 -0
  20. data/rails_generators/lockdown/templates/app/controllers/users_controller.rb +117 -0
  21. data/rails_generators/lockdown/templates/app/helpers/permissions_helper.rb +2 -0
  22. data/rails_generators/lockdown/templates/app/helpers/user_groups_helper.rb +2 -0
  23. data/rails_generators/lockdown/templates/app/helpers/users_helper.rb +2 -0
  24. data/rails_generators/lockdown/templates/app/models/permission.rb +13 -0
  25. data/rails_generators/lockdown/templates/app/models/profile.rb +10 -0
  26. data/rails_generators/lockdown/templates/app/models/user.rb +95 -0
  27. data/rails_generators/lockdown/templates/app/models/user_group.rb +15 -0
  28. data/rails_generators/lockdown/templates/app/views/permissions/index.html.erb +16 -0
  29. data/rails_generators/lockdown/templates/app/views/permissions/show.html.erb +26 -0
  30. data/rails_generators/lockdown/templates/app/views/sessions/new.html.erb +12 -0
  31. data/rails_generators/lockdown/templates/app/views/user_groups/edit.html.erb +33 -0
  32. data/rails_generators/lockdown/templates/app/views/user_groups/index.html.erb +20 -0
  33. data/rails_generators/lockdown/templates/app/views/user_groups/new.html.erb +31 -0
  34. data/rails_generators/lockdown/templates/app/views/user_groups/show.html.erb +29 -0
  35. data/rails_generators/lockdown/templates/app/views/users/edit.html.erb +51 -0
  36. data/rails_generators/lockdown/templates/app/views/users/index.html.erb +22 -0
  37. data/rails_generators/lockdown/templates/app/views/users/new.html.erb +50 -0
  38. data/rails_generators/lockdown/templates/app/views/users/show.html.erb +33 -0
  39. data/rails_generators/lockdown/templates/config/initializers/lockit.rb +1 -0
  40. data/rails_generators/lockdown/templates/db/migrate/create_admin_user.rb +17 -0
  41. data/rails_generators/lockdown/templates/db/migrate/create_permissions.rb +19 -0
  42. data/rails_generators/lockdown/templates/db/migrate/create_profiles.rb +26 -0
  43. data/rails_generators/lockdown/templates/db/migrate/create_user_groups.rb +19 -0
  44. data/rails_generators/lockdown/templates/db/migrate/create_users.rb +17 -0
  45. data/rails_generators/lockdown/templates/lib/lockdown/README +42 -0
  46. data/rails_generators/lockdown/templates/lib/lockdown/init.rb +122 -0
  47. data/spec/lockdown/database_spec.rb +158 -0
  48. data/spec/lockdown/frameworks/rails/controller_spec.rb +224 -0
  49. data/spec/lockdown/frameworks/rails/view_spec.rb +125 -0
  50. data/spec/lockdown/frameworks/rails_spec.rb +175 -0
  51. data/spec/lockdown/permission_spec.rb +156 -0
  52. data/spec/lockdown/rules_spec.rb +109 -0
  53. data/spec/lockdown/session_spec.rb +89 -0
  54. data/spec/lockdown/system_spec.rb +59 -0
  55. data/spec/lockdown_spec.rb +19 -0
  56. data/spec/rcov.opts +5 -0
  57. data/spec/spec.opts +3 -0
  58. data/spec/spec_helper.rb +1 -0
  59. metadata +112 -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,156 @@
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 "#equals" do
61
+ before do
62
+ @permission.to_model(:user).where(:current_user_id).equals(:id)
63
+ end
64
+
65
+ it "should set current_context to RootContext" do
66
+ @permission.current_context.class.should equal(Lockdown::RootContext)
67
+ end
68
+ end
69
+
70
+ describe "#is_in" do
71
+ before do
72
+ @permission.to_model(:user).where(:current_user_id).is_in(:manager_ids)
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 "#set_as_public_access" do
81
+ it "should raise an PermissionScopeCollision if already protected" do
82
+ @permission.set_as_protected_access
83
+ lambda{@permission.set_as_public_access}.
84
+ should raise_error(Lockdown::PermissionScopeCollision)
85
+ end
86
+ end
87
+
88
+
89
+ describe "#set_as_protected_access" do
90
+ it "should raise an PermissionScopeCollision if already public" do
91
+ @permission.set_as_public_access
92
+ lambda{@permission.set_as_protected_access}.
93
+ should raise_error(Lockdown::PermissionScopeCollision)
94
+ end
95
+ end
96
+
97
+ describe "while in RootContext" do
98
+ before do
99
+ @permission.with_controller(:users).only_methods(:show, :edit)
100
+ end
101
+
102
+ it "should raise InvalidRuleContext trying to access methods out of context" do
103
+ methods = [:only_methods, :except_methods, :where, :equals, :is_in, :includes]
104
+
105
+ methods.each do |method|
106
+ lambda{@permission.send(method, :sample_param)}.
107
+ should raise_error(Lockdown::InvalidRuleContext)
108
+ end
109
+ end
110
+ end
111
+
112
+ describe "while in ControllerContext" do
113
+ before do
114
+ @permission.with_controller(:users)
115
+ end
116
+
117
+ it "should raise InvalidRuleContext trying to access methods out of context" do
118
+ methods = [:where, :equals, :is_in, :includes]
119
+
120
+ methods.each do |method|
121
+ lambda{@permission.send(method, :sample_param)}.
122
+ should raise_error(Lockdown::InvalidRuleContext)
123
+ end
124
+ end
125
+ end
126
+
127
+ describe "while in ModelContext" do
128
+ before do
129
+ @permission.to_model(:user)
130
+ end
131
+
132
+ it "should raise InvalidRuleContext trying to access methods out of context" do
133
+ methods = [:with_controller, :and_controller, :only_methods, :except_methods, :to_model, :equals, :is_in, :includes]
134
+
135
+ methods.each do |method|
136
+ lambda{@permission.send(method, :sample_param)}.
137
+ should raise_error(Lockdown::InvalidRuleContext)
138
+ end
139
+ end
140
+ end
141
+
142
+ describe "while in ModelWhereContext" do
143
+ before do
144
+ @permission.to_model(:user).where(:current_user_id)
145
+ end
146
+
147
+ it "should raise InvalidRuleContext trying to access methods out of context" do
148
+ methods = [:with_controller, :and_controller, :only_methods, :except_methods, :to_model, :where]
149
+
150
+ methods.each do |method|
151
+ lambda{@permission.send(method, :sample_param)}.
152
+ should raise_error(Lockdown::InvalidRuleContext)
153
+ end
154
+ end
155
+ end
156
+ 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
@@ -0,0 +1,89 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ class TestAController
4
+ include Lockdown::Session
5
+ end
6
+
7
+ describe Lockdown::Session do
8
+ before do
9
+ @controller = TestAController.new
10
+
11
+ @actions = %w(posts/index posts/show posts/new posts/edit posts/create posts/update posts/destroy)
12
+
13
+ @session = {:access_rights => @actions}
14
+
15
+ @controller.stub!(:session).and_return(@session)
16
+ end
17
+
18
+ describe "#nil_lockdown_values" do
19
+ it "should nil access_rights" do
20
+ @controller.send :nil_lockdown_values
21
+ @session[:access_rights].should == nil
22
+ end
23
+ end
24
+
25
+ describe "#current_user_access_in_group?" do
26
+ it "should return true if current user is admin" do
27
+ @actions = :all
28
+ @session = {:access_rights => @actions}
29
+ @controller.stub!(:session).and_return(@session)
30
+
31
+ @controller.send(:current_user_access_in_group?,:group).should == true
32
+ end
33
+
34
+ it "should return true if current_user has access" do
35
+ user_groups = {:public_group => [:public_access]}
36
+ hash = {:public_access => ["posts/index", "posts/show"]}
37
+ Lockdown::System.stub!(:permissions).and_return(hash)
38
+
39
+ Lockdown::System.stub!(:user_groups).and_return(user_groups)
40
+ @controller.send(:current_user_access_in_group?,:public_group).should be_true
41
+ end
42
+
43
+ it "should return false if current_user has access" do
44
+ user_groups = {:public_group => [:public_access]}
45
+ hash = {:public_access => ["books/edit", "books/update"]}
46
+ Lockdown::System.stub!(:permissions).and_return(hash)
47
+
48
+ Lockdown::System.stub!(:user_groups).and_return(user_groups)
49
+ @controller.send(:current_user_access_in_group?,:public_group).should be_false
50
+ end
51
+ end
52
+
53
+ describe "#current_user_is_admin?" do
54
+ it "should return true if access_rights == :all" do
55
+ @actions = :all
56
+ @session = {:access_rights => @actions}
57
+ @controller.stub!(:session).and_return(@session)
58
+
59
+ @controller.send(:current_user_is_admin?).should == true
60
+ end
61
+ end
62
+
63
+ describe "#add_lockdown_session_values" do
64
+ it "should set the access_rights from the user list" do
65
+ array = ["posts/index", "posts/show"]
66
+ Lockdown::System.stub!(:access_rights_for_user).and_return(array)
67
+ @controller.stub!(:current_user).and_return(:user_object)
68
+ @controller.send(:add_lockdown_session_values)
69
+ @session[:access_rights].should == array
70
+ end
71
+ end
72
+
73
+
74
+ describe "#access_in_perm" do
75
+ it "should return false if permissions nil" do
76
+ Lockdown::System.stub!(:permissions).and_return({})
77
+ @controller.send(:access_in_perm?,:dummy).should be_false
78
+ end
79
+
80
+ it "should return true if permission found" do
81
+ hash = {:public => ["posts/index", "posts/show"]}
82
+ Lockdown::System.stub!(:permissions).and_return(hash)
83
+ @controller.send(:access_in_perm?,:public).should be_true
84
+ end
85
+ end
86
+
87
+ describe "#session_access_rights_include?" do
88
+ end
89
+ end