lockdown 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lockdown.rb +14 -9
- data/lib/lockdown/errors.rb +7 -0
- data/lib/lockdown/frameworks/rails.rb +3 -3
- data/lib/lockdown/frameworks/rails/controller.rb +1 -1
- data/lib/lockdown/helper.rb +2 -2
- data/lib/lockdown/permission.rb +3 -6
- data/lib/lockdown/references.rb +19 -0
- data/lib/lockdown/rspec_helper.rb +12 -0
- data/lib/lockdown/rules.rb +3 -5
- data/spec/lockdown/frameworks/rails_spec.rb +41 -13
- data/spec/lockdown/rules_spec.rb +96 -13
- data/spec/lockdown/session_spec.rb +3 -3
- data/spec/spec_helper.rb +7 -0
- metadata +4 -2
data/lib/lockdown.rb
CHANGED
@@ -1,11 +1,23 @@
|
|
1
1
|
$:.unshift File.dirname(__FILE__)
|
2
2
|
|
3
|
+
require File.join("lockdown", "errors")
|
4
|
+
|
3
5
|
require File.join("lockdown", "helper")
|
6
|
+
require File.join("lockdown", "session")
|
7
|
+
require File.join("lockdown", "context")
|
8
|
+
require File.join("lockdown", "permission")
|
9
|
+
require File.join("lockdown", "database")
|
10
|
+
require File.join("lockdown", "rules")
|
11
|
+
require File.join("lockdown", "system")
|
12
|
+
|
13
|
+
require File.join("lockdown", "references")
|
4
14
|
|
5
15
|
module Lockdown
|
16
|
+
extend Lockdown::References
|
6
17
|
extend Lockdown::Helper
|
7
18
|
|
8
|
-
|
19
|
+
# current version is 1.3.0
|
20
|
+
VERSION = '1.3.1'
|
9
21
|
|
10
22
|
# Returns the version string for the library.
|
11
23
|
def self.version
|
@@ -59,14 +71,7 @@ module Lockdown
|
|
59
71
|
end # mixin_resource?
|
60
72
|
end # Lockdown
|
61
73
|
|
62
|
-
require File.join("lockdown", "session")
|
63
|
-
require File.join("lockdown", "context")
|
64
|
-
require File.join("lockdown", "permission")
|
65
|
-
require File.join("lockdown", "database")
|
66
|
-
require File.join("lockdown", "rules")
|
67
|
-
require File.join("lockdown", "system")
|
68
|
-
|
69
74
|
puts "=> Mixing in Lockdown version: #{Lockdown.version} \n"
|
70
|
-
|
71
75
|
Lockdown.mixin
|
72
76
|
|
77
|
+
|
@@ -21,7 +21,7 @@ module Lockdown
|
|
21
21
|
include Lockdown::Frameworks::Rails::View
|
22
22
|
end
|
23
23
|
|
24
|
-
Lockdown
|
24
|
+
Lockdown.system.class_eval do
|
25
25
|
extend Lockdown::Frameworks::Rails::System
|
26
26
|
end
|
27
27
|
end
|
@@ -45,7 +45,7 @@ module Lockdown
|
|
45
45
|
|
46
46
|
klass.filter_parameter_logging :password, :password_confirmation
|
47
47
|
|
48
|
-
klass.rescue_from SecurityError, :with => proc{|e|
|
48
|
+
klass.rescue_from SecurityError, :with => proc{|e| ld_access_denied(e)}
|
49
49
|
end
|
50
50
|
end # class block
|
51
51
|
|
@@ -101,7 +101,7 @@ module Lockdown
|
|
101
101
|
include Lockdown::Frameworks::Rails::Controller
|
102
102
|
|
103
103
|
def skip_sync?
|
104
|
-
Lockdown
|
104
|
+
Lockdown.system.fetch(:skip_db_sync_in).include?(framework_environment)
|
105
105
|
end
|
106
106
|
|
107
107
|
def framework_environment
|
data/lib/lockdown/helper.rb
CHANGED
@@ -41,11 +41,11 @@ module Lockdown
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def user_group_model_string
|
44
|
-
Lockdown
|
44
|
+
Lockdown.system.fetch(:user_group_model) || "UserGroup"
|
45
45
|
end
|
46
46
|
|
47
47
|
def user_model_string
|
48
|
-
Lockdown
|
48
|
+
Lockdown.system.fetch(:user_model) || "User"
|
49
49
|
end
|
50
50
|
|
51
51
|
def get_string(value)
|
data/lib/lockdown/permission.rb
CHANGED
@@ -1,7 +1,4 @@
|
|
1
1
|
module Lockdown
|
2
|
-
class InvalidRuleContext < StandardError; end
|
3
|
-
class PermissionScopeCollision < StandardError; end
|
4
|
-
|
5
2
|
class Controller
|
6
3
|
attr_accessor :name, :access_methods, :only_methods, :except_methods
|
7
4
|
|
@@ -178,14 +175,14 @@ module Lockdown
|
|
178
175
|
|
179
176
|
def set_as_public_access
|
180
177
|
if protected_access?
|
181
|
-
raise PermissionScopeCollision, "Permission: #{name} already marked as protected and trying to set as public."
|
178
|
+
raise Lockdown::PermissionScopeCollision, "Permission: #{name} already marked as protected and trying to set as public."
|
182
179
|
end
|
183
180
|
@public_access = true
|
184
181
|
end
|
185
182
|
|
186
183
|
def set_as_protected_access
|
187
184
|
if public_access?
|
188
|
-
raise PermissionScopeCollision, "Permission: #{name} already marked as public and trying to set as protected."
|
185
|
+
raise Lockdown::PermissionScopeCollision, "Permission: #{name} already marked as public and trying to set as protected."
|
189
186
|
end
|
190
187
|
@protected_access = true
|
191
188
|
end
|
@@ -218,7 +215,7 @@ module Lockdown
|
|
218
215
|
method_trace = caller.first;
|
219
216
|
calling_method = caller.first[/#{__FILE__}:(\d+):in `(.*)'/,2]
|
220
217
|
unless current_context.allows?(calling_method)
|
221
|
-
raise InvalidRuleContext, "Method: #{calling_method} was called on wrong context #{current_context}. Allowed methods are: #{current_context.allowed_methods.join(',')}."
|
218
|
+
raise Lockdown::InvalidRuleContext, "Method: #{calling_method} was called on wrong context #{current_context}. Allowed methods are: #{current_context.allowed_methods.join(',')}."
|
222
219
|
end
|
223
220
|
end
|
224
221
|
end
|
@@ -9,11 +9,23 @@ module Lockdown
|
|
9
9
|
user_group_symbols.each do |ugs|
|
10
10
|
access_rights << Lockdown::System.access_rights_for_user_group(ugs)
|
11
11
|
end
|
12
|
+
login_user
|
12
13
|
controller.session[:access_rights] = access_rights.flatten
|
13
14
|
end
|
14
15
|
|
15
16
|
alias login_with_group login_with_groups
|
16
17
|
|
18
|
+
def login_with_permissions(*permissions_symbols)
|
19
|
+
access_rights = Lockdown::System.standard_authorized_user_rights
|
20
|
+
permissions_symbols.each do |ps|
|
21
|
+
access_rights << Lockdown::System.access_rights_for_permission(ps)
|
22
|
+
end
|
23
|
+
login_user
|
24
|
+
controller.session[:access_rights] = access_rights.flatten.uniq
|
25
|
+
end
|
26
|
+
|
27
|
+
alias login_with_permission login_with_permissions
|
28
|
+
|
17
29
|
def login_standard
|
18
30
|
login_user
|
19
31
|
end
|
data/lib/lockdown/rules.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
1
|
module Lockdown
|
2
|
-
class InvalidRuleAssignment < StandardError; end
|
3
|
-
|
4
2
|
module Rules
|
5
3
|
attr_accessor :options
|
6
4
|
attr_accessor :permissions
|
@@ -58,7 +56,7 @@ module Lockdown
|
|
58
56
|
perm.set_as_public_access
|
59
57
|
else
|
60
58
|
msg = "Permission not found: #{perm_symbol}"
|
61
|
-
raise
|
59
|
+
raise Lockdown::InvalidRuleAssignment, msg
|
62
60
|
end
|
63
61
|
end
|
64
62
|
end
|
@@ -75,7 +73,7 @@ module Lockdown
|
|
75
73
|
perm.set_as_protected_access
|
76
74
|
else
|
77
75
|
msg = "Permission not found: #{perm_symbol}"
|
78
|
-
raise
|
76
|
+
raise Lockdown::InvalidRuleAssignment, msg
|
79
77
|
end
|
80
78
|
end
|
81
79
|
end
|
@@ -290,7 +288,7 @@ module Lockdown
|
|
290
288
|
perms.each do |perm|
|
291
289
|
unless permission_exists?(perm)
|
292
290
|
msg ="User Group: #{user_group}, permission not found: #{perm}"
|
293
|
-
raise InvalidRuleAssignment, msg
|
291
|
+
raise Lockdown::InvalidRuleAssignment, msg
|
294
292
|
end
|
295
293
|
end
|
296
294
|
end
|
@@ -23,29 +23,57 @@ describe Lockdown::Frameworks::Rails do
|
|
23
23
|
|
24
24
|
describe "#mixin" do
|
25
25
|
it "should perform class_eval on controller view and system to inject itself" do
|
26
|
-
module ActionController; class Base; end end
|
27
|
-
module ActionView; class Base; end end
|
28
26
|
|
29
|
-
|
30
|
-
|
27
|
+
@view_helper = Mikey
|
28
|
+
@view_helper.should_receive(:include).
|
29
|
+
with( Lockdown::Frameworks::Rails::View )
|
31
30
|
|
32
|
-
|
31
|
+
Lockdown.should_receive(:view_helper) do
|
32
|
+
@view_helper
|
33
|
+
end
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
ActionController::Base.should_receive(:rescue_from)
|
35
|
+
@system = Mikey
|
36
|
+
@system.should_receive(:extend).
|
37
|
+
with( Lockdown::Frameworks::Rails::System )
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
Lockdown::System.should_receive(:class_eval)
|
39
|
+
Lockdown.should_receive(:system) do
|
40
|
+
@system
|
41
|
+
end
|
43
42
|
|
43
|
+
@rails.should_receive(:mixin_controller)
|
44
44
|
|
45
45
|
@rails.mixin
|
46
46
|
end
|
47
47
|
|
48
48
|
end
|
49
|
+
|
50
|
+
describe "#mixin_controller" do
|
51
|
+
|
52
|
+
it "should inject itself" do
|
53
|
+
klass = Mikey
|
54
|
+
|
55
|
+
klass.should_receive(:include).
|
56
|
+
with(Lockdown::Session)
|
57
|
+
|
58
|
+
klass.should_receive(:include).
|
59
|
+
with(Lockdown::Frameworks::Rails::Controller::Lock)
|
60
|
+
|
61
|
+
klass.should_receive(:helper_method).with(:authorized?)
|
62
|
+
|
63
|
+
klass.should_receive(:hide_action).with(:set_current_user, :configure_lockdown, :check_request_authorization, :check_model_authorization)
|
64
|
+
|
65
|
+
klass.should_receive(:before_filter).and_return do |c|
|
66
|
+
#not working yet. very frustrating trying to test this
|
67
|
+
end
|
68
|
+
|
69
|
+
klass.should_receive(:filter_parameter_logging)
|
70
|
+
|
71
|
+
klass.should_receive(:rescue_from)
|
72
|
+
|
73
|
+
@rails.mixin_controller(klass)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
49
77
|
end
|
50
78
|
|
51
79
|
RAILS_ROOT = "/shibby/dibby/do"
|
data/spec/lockdown/rules_spec.rb
CHANGED
@@ -15,13 +15,6 @@ describe Lockdown::Rules do
|
|
15
15
|
end
|
16
16
|
end
|
17
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
18
|
describe "#set_public_access" do
|
26
19
|
it "should define the permission as public" do
|
27
20
|
@rules.set_permission(:home_page)
|
@@ -31,9 +24,11 @@ describe Lockdown::Rules do
|
|
31
24
|
end
|
32
25
|
|
33
26
|
it "should raise and InvalidRuleAssignment if permission does not exist" do
|
34
|
-
msg = "Permission not found:
|
35
|
-
|
36
|
-
|
27
|
+
msg = "Permission not found: toy_management"
|
28
|
+
|
29
|
+
@rules.should_receive(:raise).with(Lockdown::InvalidRuleAssignment, msg)
|
30
|
+
|
31
|
+
@rules.set_public_access(:toy_management)
|
37
32
|
end
|
38
33
|
end
|
39
34
|
|
@@ -61,8 +56,10 @@ describe Lockdown::Rules do
|
|
61
56
|
|
62
57
|
it "should raise and InvalidRuleAssignment if permission does not exist" do
|
63
58
|
msg = "Permission not found: user_management"
|
64
|
-
|
65
|
-
|
59
|
+
|
60
|
+
@rules.should_receive(:raise).with(Lockdown::InvalidRuleAssignment, msg)
|
61
|
+
|
62
|
+
@rules.set_protected_access(:user_management)
|
66
63
|
end
|
67
64
|
end
|
68
65
|
|
@@ -144,8 +141,94 @@ describe Lockdown::Rules do
|
|
144
141
|
end
|
145
142
|
end
|
146
143
|
|
147
|
-
|
148
144
|
describe "#make_user_administrator" do
|
145
|
+
it "should add admin to user groups" do
|
146
|
+
ugc = mock('user_group_class',:find_or_create_by_name => :admin)
|
147
|
+
Lockdown.should_receive(:user_group_class).and_return(ugc)
|
148
|
+
|
149
|
+
usr = mock('user', :user_groups => [])
|
150
|
+
|
151
|
+
@rules.make_user_administrator(usr).should include(:admin)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe "#access_rights_for_user" do
|
156
|
+
it "should array of rights for user who is not an admin" do
|
157
|
+
@rules.should_receive(:administrator?).and_return(false)
|
158
|
+
|
159
|
+
@rules.set_permission(:register_account).
|
160
|
+
with_controller(:users).
|
161
|
+
only_methods(:new, :create)
|
162
|
+
|
163
|
+
@rules.set_public_access(:register_account)
|
164
|
+
|
165
|
+
perm = @rules.set_permission(:perm_one).
|
166
|
+
with_controller("a_controller").
|
167
|
+
only_methods("show","edit","update")
|
168
|
+
|
169
|
+
ug = @rules.set_user_group(:ug_one, :perm_one)
|
170
|
+
|
171
|
+
@rules.should_receive(:set_model_access)
|
172
|
+
@rules.process_rules
|
173
|
+
|
174
|
+
usr = mock('user', :user_groups => [:ug_one])
|
175
|
+
|
176
|
+
@rules.access_rights_for_user(usr).
|
177
|
+
should == ["users/new", "users/create", "a_controller/show", "a_controller/edit", "a_controller/update"]
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe "#access_rights_for_user_group" do
|
182
|
+
it "should return array of rights for user_group" do
|
183
|
+
perm = @rules.set_permission(:perm_one).
|
184
|
+
with_controller("a_controller").
|
185
|
+
only_methods("show","edit","update")
|
186
|
+
|
187
|
+
ug = @rules.set_user_group(:ug_one, :perm_one)
|
188
|
+
|
189
|
+
@rules.should_receive(:set_model_access)
|
190
|
+
@rules.process_rules
|
191
|
+
|
192
|
+
@rules.access_rights_for_user_group(:ug_one).
|
193
|
+
should == ["a_controller/show", "a_controller/edit", "a_controller/update"]
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
describe "#access_rights_for_permission" do
|
198
|
+
it "should return array of rights for permission" do
|
199
|
+
|
200
|
+
perm = @rules.set_permission(:perm_one).
|
201
|
+
with_controller("a_controller").
|
202
|
+
only_methods("show","edit","update")
|
203
|
+
|
204
|
+
@rules.should_receive(:set_model_access)
|
205
|
+
@rules.process_rules
|
206
|
+
|
207
|
+
@rules.access_rights_for_permission(perm).
|
208
|
+
should == ["a_controller/show", "a_controller/edit", "a_controller/update"]
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
describe "#standard_authorized_user_rights" do
|
213
|
+
it "should receive public_access + protected_access" do
|
214
|
+
@rules.set_permission(:register_account).
|
215
|
+
with_controller(:users).
|
216
|
+
only_methods(:new, :create)
|
217
|
+
|
218
|
+
@rules.set_permission(:my_profile).
|
219
|
+
with_controller(:users).
|
220
|
+
only_methods(:show, :edit, :update)
|
221
|
+
|
222
|
+
|
223
|
+
@rules.set_public_access(:register_account)
|
224
|
+
@rules.set_protected_access(:my_profile)
|
225
|
+
|
226
|
+
@rules.should_receive(:set_model_access)
|
227
|
+
@rules.process_rules
|
228
|
+
|
229
|
+
@rules.standard_authorized_user_rights.
|
230
|
+
should == ["users/new", "users/create", "users/show", "users/edit", "users/update"]
|
231
|
+
end
|
149
232
|
end
|
150
233
|
|
151
234
|
describe "#process_rules" do
|
@@ -77,9 +77,9 @@ describe Lockdown::Session do
|
|
77
77
|
it "should set the access_rights from the user list" do
|
78
78
|
array = ["posts/index", "posts/show"]
|
79
79
|
Lockdown::System.stub!(:access_rights_for_user).and_return(array)
|
80
|
-
usr = mock(
|
81
|
-
|
82
|
-
@controller.send(:add_lockdown_session_values)
|
80
|
+
usr = mock('user')
|
81
|
+
usr.should_receive(:id).and_return(1234)
|
82
|
+
@controller.send(:add_lockdown_session_values, usr)
|
83
83
|
@session[:access_rights].should == array
|
84
84
|
end
|
85
85
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lockdown
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Stone
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-02 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -38,12 +38,14 @@ files:
|
|
38
38
|
- lib/lockdown.rb
|
39
39
|
- lib/lockdown/context.rb
|
40
40
|
- lib/lockdown/database.rb
|
41
|
+
- lib/lockdown/errors.rb
|
41
42
|
- lib/lockdown/frameworks/rails.rb
|
42
43
|
- lib/lockdown/frameworks/rails/controller.rb
|
43
44
|
- lib/lockdown/frameworks/rails/view.rb
|
44
45
|
- lib/lockdown/helper.rb
|
45
46
|
- lib/lockdown/orms/active_record.rb
|
46
47
|
- lib/lockdown/permission.rb
|
48
|
+
- lib/lockdown/references.rb
|
47
49
|
- lib/lockdown/rspec_helper.rb
|
48
50
|
- lib/lockdown/rules.rb
|
49
51
|
- lib/lockdown/session.rb
|