patrick-lockdown 2.0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +42 -0
- data/Rakefile +54 -0
- data/lib/lockdown.rb +42 -0
- data/lib/lockdown/access.rb +108 -0
- data/lib/lockdown/configuration.rb +209 -0
- data/lib/lockdown/database.rb +122 -0
- data/lib/lockdown/delivery.rb +28 -0
- data/lib/lockdown/errors.rb +7 -0
- data/lib/lockdown/frameworks/rails.rb +77 -0
- data/lib/lockdown/frameworks/rails/controller.rb +145 -0
- data/lib/lockdown/frameworks/rails/view.rb +51 -0
- data/lib/lockdown/helper.rb +40 -0
- data/lib/lockdown/orms/active_record.rb +66 -0
- data/lib/lockdown/permission.rb +56 -0
- data/lib/lockdown/resource.rb +54 -0
- data/lib/lockdown/session.rb +50 -0
- data/lib/lockdown/user_group.rb +16 -0
- data/patrick-lockdown.gemspec +62 -0
- data/tags +142 -0
- data/test/helper.rb +10 -0
- data/test/lockdown/test_access.rb +80 -0
- data/test/lockdown/test_configuration.rb +195 -0
- data/test/lockdown/test_delivery.rb +224 -0
- data/test/lockdown/test_helper.rb +33 -0
- data/test/lockdown/test_permission.rb +73 -0
- data/test/lockdown/test_resource.rb +47 -0
- data/test/lockdown/test_session.rb +31 -0
- data/test/lockdown/test_user_group.rb +17 -0
- metadata +96 -0
data/test/helper.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestLockdownAccess < MiniTest::Unit::TestCase
|
4
|
+
include Lockdown::Access
|
5
|
+
|
6
|
+
def teardown
|
7
|
+
Lockdown::Configuration.reset
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_model_responds_to_permission
|
11
|
+
assert_respond_to self, :permission
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_permission_with_single_resource
|
15
|
+
perm = permission(:my_perm) do
|
16
|
+
resource :my_resource
|
17
|
+
end
|
18
|
+
|
19
|
+
resource = perm.resources.first
|
20
|
+
assert_equal 'my_resource', resource.name
|
21
|
+
assert_equal "\/my_resource(\/.*)?", resource.regex_pattern
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_permission_without_block
|
25
|
+
perm = permission(:users)
|
26
|
+
|
27
|
+
resource = perm.resources.first
|
28
|
+
assert_equal 'users', resource.name
|
29
|
+
assert_equal "\/users(\/.*)?", resource.regex_pattern
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_public_access
|
33
|
+
permission(:site)
|
34
|
+
public_access :site
|
35
|
+
|
36
|
+
assert_equal Lockdown::Configuration.public_access, "(\/site(\/.*)?)"
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_public_access_with_multiple_permissions
|
40
|
+
permission(:site)
|
41
|
+
permission(:registration)
|
42
|
+
permission(:view_posts)
|
43
|
+
public_access :site, :registration, :view_posts
|
44
|
+
|
45
|
+
assert_equal Lockdown::Configuration.public_access,
|
46
|
+
"(\/site(\/.*)?)|(\/registration(\/.*)?)|(\/view_posts(\/.*)?)"
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_protected_access
|
50
|
+
permission(:my_account)
|
51
|
+
protected_access :my_account
|
52
|
+
|
53
|
+
assert_equal Lockdown::Configuration.protected_access, "(\/my_account(\/.*)?)"
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_protected_access_with_multiple_permissions
|
57
|
+
permission(:my_account)
|
58
|
+
permission(:edit_posts)
|
59
|
+
protected_access :my_account, :edit_posts
|
60
|
+
|
61
|
+
assert_equal Lockdown::Configuration.protected_access,
|
62
|
+
"(\/my_account(\/.*)?)|(\/edit_posts(\/.*)?)"
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_user_group
|
66
|
+
permission(:site)
|
67
|
+
permission(:registration)
|
68
|
+
permission(:view_posts)
|
69
|
+
user_group(:all, :site, :registration, :view_posts)
|
70
|
+
|
71
|
+
ug = Lockdown::Configuration.find_or_create_user_group(:all)
|
72
|
+
|
73
|
+
assert_equal 'all', ug.name
|
74
|
+
|
75
|
+
assert_equal 'view_posts', ug.permissions.pop.name
|
76
|
+
assert_equal 'registration', ug.permissions.pop.name
|
77
|
+
assert_equal 'site', ug.permissions.pop.name
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
@@ -0,0 +1,195 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Authorization
|
4
|
+
include Lockdown::Access
|
5
|
+
end
|
6
|
+
|
7
|
+
class TestLockdownConfiguration < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@config = Lockdown::Configuration
|
11
|
+
end
|
12
|
+
|
13
|
+
def teardown
|
14
|
+
Lockdown::Configuration.reset
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_initial_state
|
18
|
+
assert_equal false, @config.configured
|
19
|
+
assert_equal "", @config.public_access
|
20
|
+
assert_equal "", @config.protected_access
|
21
|
+
assert_equal [], @config.permissions
|
22
|
+
assert_equal [], @config.user_groups
|
23
|
+
|
24
|
+
assert_equal :current_user_id, @config.who_did_it
|
25
|
+
assert_equal 1, @config.default_who_did_it
|
26
|
+
|
27
|
+
assert_equal "/", @config.access_denied_path
|
28
|
+
assert_equal "/", @config.successful_login_path
|
29
|
+
assert_equal false, @config.logout_on_access_violation
|
30
|
+
|
31
|
+
assert_equal "|", @config.link_separator
|
32
|
+
|
33
|
+
assert_equal "UserGroup", @config.user_group_model
|
34
|
+
assert_equal "User", @config.user_model
|
35
|
+
|
36
|
+
assert_equal ['test'] , @config.skip_db_sync_in
|
37
|
+
assert_nil @config.subdirectory
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_authenticated_access
|
41
|
+
Authorization.permission('home')
|
42
|
+
Authorization.permission('faq')
|
43
|
+
Authorization.permission('users')
|
44
|
+
|
45
|
+
Authorization.public_access('home', 'faq')
|
46
|
+
Authorization.protected_access('users')
|
47
|
+
|
48
|
+
assert_equal "(/home(/.*)?)|(/faq(/.*)?)|(/users(/.*)?)", @config.authenticated_access
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_permission
|
52
|
+
Authorization.permission('home')
|
53
|
+
Authorization.permission('faq')
|
54
|
+
|
55
|
+
perm = Lockdown::Permission.new('home')
|
56
|
+
|
57
|
+
assert_equal perm.name, @config.permission('home').name
|
58
|
+
|
59
|
+
assert_raises(Lockdown::PermissionNotFound){ @config.permission('delta') }
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_make_permission_public
|
63
|
+
Authorization.permission('home')
|
64
|
+
|
65
|
+
@config.make_permission_public('home')
|
66
|
+
|
67
|
+
perm = @config.permission('home')
|
68
|
+
|
69
|
+
assert_equal true, perm.public?
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_has_permission
|
73
|
+
Authorization.permission('home')
|
74
|
+
Authorization.permission('faq')
|
75
|
+
Authorization.permission('about')
|
76
|
+
|
77
|
+
perm = Lockdown::Permission.new('home')
|
78
|
+
perm2 = Lockdown::Permission.new('homey')
|
79
|
+
|
80
|
+
assert_equal true, @config.has_permission?(perm)
|
81
|
+
|
82
|
+
assert_equal false, @config.has_permission?(perm2)
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_permission_names
|
86
|
+
Authorization.permission('home')
|
87
|
+
Authorization.permission('faq')
|
88
|
+
Authorization.permission('about')
|
89
|
+
|
90
|
+
assert_equal 'about', @config.permissions.pop.name
|
91
|
+
assert_equal 'faq', @config.permissions.pop.name
|
92
|
+
assert_equal 'home', @config.permissions.pop.name
|
93
|
+
|
94
|
+
assert_equal true, @config.permissions.empty?
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_permission_assigned_automatically
|
98
|
+
Authorization.permission('home')
|
99
|
+
Authorization.permission('faq')
|
100
|
+
Authorization.permission('users')
|
101
|
+
|
102
|
+
Authorization.public_access('home', 'faq')
|
103
|
+
|
104
|
+
assert_equal true, @config.permission_assigned_automatically?('home')
|
105
|
+
assert_equal true, @config.permission_assigned_automatically?('faq')
|
106
|
+
assert_equal false, @config.permission_assigned_automatically?('users')
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_user_group
|
110
|
+
Authorization.permission('home')
|
111
|
+
Authorization.permission('faq')
|
112
|
+
|
113
|
+
Authorization.user_group 'all', 'home', 'faq'
|
114
|
+
|
115
|
+
ug = @config.user_group('all')
|
116
|
+
|
117
|
+
assert_equal 'faq', ug.permissions.pop.name
|
118
|
+
assert_equal 'home',ug.permissions.pop.name
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_maybe_add_user_group
|
122
|
+
Authorization.permission('home')
|
123
|
+
Authorization.permission('faq')
|
124
|
+
|
125
|
+
Authorization.user_group 'all', 'home', 'faq'
|
126
|
+
groups_1 = @config.user_groups
|
127
|
+
|
128
|
+
Authorization.user_group 'all', 'home', 'faq'
|
129
|
+
groups_2 = @config.user_groups
|
130
|
+
|
131
|
+
assert_equal groups_1, groups_2
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_find_or_create_user_group
|
135
|
+
Authorization.permission('home')
|
136
|
+
Authorization.permission('faq')
|
137
|
+
Authorization.permission('about')
|
138
|
+
|
139
|
+
Authorization.user_group 'testone', 'home', 'faq', 'about'
|
140
|
+
|
141
|
+
ug = @config.find_or_create_user_group('testone')
|
142
|
+
|
143
|
+
assert_equal 'testone', ug.name
|
144
|
+
|
145
|
+
assert_equal 'about', ug.permissions.pop.name
|
146
|
+
assert_equal 'faq', ug.permissions.pop.name
|
147
|
+
assert_equal 'home', ug.permissions.pop.name
|
148
|
+
|
149
|
+
assert_equal true, ug.permissions.empty?
|
150
|
+
|
151
|
+
ug2 = @config.find_or_create_user_group('testtwo')
|
152
|
+
|
153
|
+
assert_equal 'testtwo', ug2.name
|
154
|
+
assert_equal true, ug2.permissions.empty?
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_user_group_names
|
158
|
+
Authorization.permission('home')
|
159
|
+
Authorization.permission('faq')
|
160
|
+
Authorization.permission('about')
|
161
|
+
|
162
|
+
Authorization.user_group 'testone', 'home'
|
163
|
+
Authorization.user_group 'testtwo', 'faq', 'about'
|
164
|
+
|
165
|
+
assert_equal 'testtwo', @config.user_groups.pop.name
|
166
|
+
assert_equal 'testone', @config.user_groups.pop.name
|
167
|
+
|
168
|
+
assert_equal true, @config.user_groups.empty?
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_user_group_permission_names
|
172
|
+
Authorization.permission('home')
|
173
|
+
Authorization.permission('faq')
|
174
|
+
Authorization.permission('about')
|
175
|
+
|
176
|
+
Authorization.user_group 'testone', 'home'
|
177
|
+
Authorization.user_group 'testtwo', 'faq', 'about'
|
178
|
+
|
179
|
+
assert_equal ['home'], @config.user_group_permissions_names('testone')
|
180
|
+
assert_equal ['faq', 'about'], @config.user_group_permissions_names('testtwo')
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_access_rights_for_permissions
|
184
|
+
Authorization.permission('home')
|
185
|
+
Authorization.permission('faq')
|
186
|
+
Authorization.permission('about')
|
187
|
+
|
188
|
+
assert_equal "((/home(/.*)?))|((/faq(/.*)?))|((/about(/.*)?))",
|
189
|
+
@config.access_rights_for_permissions('home', 'faq', 'about')
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_skip_sync?
|
193
|
+
assert_equal true, @config.skip_sync?
|
194
|
+
end
|
195
|
+
end
|
@@ -0,0 +1,224 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Authorization
|
4
|
+
extend Lockdown::Access
|
5
|
+
end
|
6
|
+
|
7
|
+
class TestLockdown < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
Lockdown::Configuration.reset
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_it_allows_uri_without_beginning_slash
|
14
|
+
Authorization.permission :posts
|
15
|
+
Authorization.public_access :posts
|
16
|
+
|
17
|
+
assert_equal true, Lockdown::Delivery.allowed?('posts')
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_it_allows_uri_without_ending_slash
|
21
|
+
Authorization.permission :posts
|
22
|
+
Authorization.public_access :posts
|
23
|
+
|
24
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts')
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_it_allows_uri_with_ending_slash
|
28
|
+
Authorization.permission :posts
|
29
|
+
Authorization.public_access :posts
|
30
|
+
|
31
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/')
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_it_allows_uri_with_action
|
35
|
+
Authorization.permission :posts
|
36
|
+
Authorization.public_access :posts
|
37
|
+
|
38
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/new')
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_it_allows_uri_access_to_only_show
|
42
|
+
Authorization.permission :posts do
|
43
|
+
resource :posts do
|
44
|
+
only :show
|
45
|
+
end
|
46
|
+
end
|
47
|
+
Authorization.public_access :posts
|
48
|
+
|
49
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/show')
|
50
|
+
|
51
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/show')
|
52
|
+
|
53
|
+
assert_equal false, Lockdown::Delivery.allowed?('/postsshow')
|
54
|
+
|
55
|
+
assert_equal false, Lockdown::Delivery.allowed?('/posts/')
|
56
|
+
|
57
|
+
assert_equal false, Lockdown::Delivery.allowed?('/posts/edit')
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_it_allows_uri_access_to_all_except_show
|
61
|
+
Authorization.permission :posts do
|
62
|
+
resource :posts do
|
63
|
+
except :show
|
64
|
+
end
|
65
|
+
end
|
66
|
+
Authorization.public_access :posts
|
67
|
+
|
68
|
+
assert_equal false, Lockdown::Delivery.allowed?('/posts/show')
|
69
|
+
|
70
|
+
assert_equal false, Lockdown::Delivery.allowed?('/postsshow')
|
71
|
+
|
72
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts')
|
73
|
+
|
74
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/')
|
75
|
+
|
76
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/edit')
|
77
|
+
|
78
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/edit/')
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_it_allows_uri_access_to_create_as_post
|
82
|
+
Authorization.permission :posts do
|
83
|
+
resource :posts do
|
84
|
+
only :new, :create
|
85
|
+
end
|
86
|
+
end
|
87
|
+
Authorization.public_access :posts
|
88
|
+
|
89
|
+
|
90
|
+
assert_equal false, Lockdown::Delivery.allowed?('/posts')
|
91
|
+
|
92
|
+
assert_equal false, Lockdown::Delivery.allowed?('/posts/')
|
93
|
+
|
94
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/new')
|
95
|
+
|
96
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/new/')
|
97
|
+
|
98
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/create')
|
99
|
+
|
100
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/create/')
|
101
|
+
|
102
|
+
assert_equal false, Lockdown::Delivery.allowed?('/posts/show')
|
103
|
+
|
104
|
+
assert_equal false, Lockdown::Delivery.allowed?('/posts/show/')
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_it_allows_uri_access_to_update_as_put
|
108
|
+
Authorization.permission :posts do
|
109
|
+
resource :posts do
|
110
|
+
only :show, :edit, :update
|
111
|
+
end
|
112
|
+
end
|
113
|
+
Authorization.public_access :posts
|
114
|
+
|
115
|
+
|
116
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/update')
|
117
|
+
|
118
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/update/')
|
119
|
+
|
120
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/edit')
|
121
|
+
|
122
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/edit/')
|
123
|
+
|
124
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/show')
|
125
|
+
|
126
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/show/')
|
127
|
+
|
128
|
+
assert_equal false, Lockdown::Delivery.allowed?('/posts/')
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_it_denies_uri_access_to_destroy
|
132
|
+
Authorization.permission :posts do
|
133
|
+
resource :posts do
|
134
|
+
except :destroy
|
135
|
+
end
|
136
|
+
end
|
137
|
+
Authorization.public_access :posts
|
138
|
+
|
139
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/update')
|
140
|
+
|
141
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/edit')
|
142
|
+
|
143
|
+
assert_equal true, Lockdown::Delivery.allowed?('/posts/show')
|
144
|
+
|
145
|
+
assert_equal false, Lockdown::Delivery.allowed?('/posts/destroy')
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_it_denies_uri_access_to_new_create_and_destroy
|
149
|
+
Authorization.permission :users do
|
150
|
+
resource :users do
|
151
|
+
except :new, :create, :destroy
|
152
|
+
end
|
153
|
+
end
|
154
|
+
Authorization.public_access :users
|
155
|
+
|
156
|
+
assert_equal true, Lockdown::Delivery.allowed?('/users/show')
|
157
|
+
|
158
|
+
assert_equal false, Lockdown::Delivery.allowed?('/users/new')
|
159
|
+
|
160
|
+
assert_equal false, Lockdown::Delivery.allowed?('/users/create')
|
161
|
+
|
162
|
+
assert_equal false, Lockdown::Delivery.allowed?('/users/destroy')
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_it_denies_index_access_to_resource_assigned_to_administrators
|
166
|
+
Authorization.permission :register_account do
|
167
|
+
resource :users do
|
168
|
+
only :new, :create
|
169
|
+
end
|
170
|
+
end
|
171
|
+
Authorization.public_access :register_account
|
172
|
+
|
173
|
+
Authorization.permission :my_account do
|
174
|
+
resource :users do
|
175
|
+
only :show, :update
|
176
|
+
end
|
177
|
+
end
|
178
|
+
Authorization.protected_access :my_account
|
179
|
+
|
180
|
+
Authorization.permission 'users'
|
181
|
+
Authorization.user_group 'Administrators', 'users'
|
182
|
+
|
183
|
+
assert_equal true, Lockdown::Delivery.allowed?('/users/new')
|
184
|
+
assert_equal true, Lockdown::Delivery.allowed?('/users/create')
|
185
|
+
|
186
|
+
assert_equal false, Lockdown::Delivery.allowed?('/users/')
|
187
|
+
|
188
|
+
assert_equal false, Lockdown::Delivery.allowed?('/users/', Lockdown::Configuration.authenticated_access)
|
189
|
+
assert_equal false, Lockdown::Delivery.allowed?('/users', Lockdown::Configuration.authenticated_access)
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_it_handles_namespaced_routes_correctly
|
193
|
+
Authorization.permission :posts
|
194
|
+
Authorization.permission :users
|
195
|
+
Authorization.public_access :posts, :users
|
196
|
+
|
197
|
+
Authorization.permission :protected_users do
|
198
|
+
resource 'nested/users'
|
199
|
+
end
|
200
|
+
Authorization.protected_access :protected_users
|
201
|
+
|
202
|
+
assert_equal true, Lockdown::Delivery.allowed?('/users')
|
203
|
+
|
204
|
+
assert_equal false, Lockdown::Delivery.allowed?('/nested/users')
|
205
|
+
|
206
|
+
assert_equal true, Lockdown::Delivery.allowed?('/users', Lockdown::Configuration.authenticated_access)
|
207
|
+
assert_equal true, Lockdown::Delivery.allowed?('/nested/users', Lockdown::Configuration.authenticated_access)
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_it_matches_exact_paths_only
|
211
|
+
Authorization.permission :users
|
212
|
+
Authorization.public_access :users
|
213
|
+
|
214
|
+
Authorization.permission :users_that_should_be_protected
|
215
|
+
Authorization.protected_access :users_that_should_be_protected
|
216
|
+
|
217
|
+
assert_equal true, Lockdown::Delivery.allowed?('/users')
|
218
|
+
|
219
|
+
assert_equal false, Lockdown::Delivery.allowed?('/users_that_should_be_protected')
|
220
|
+
|
221
|
+
assert_equal true, Lockdown::Delivery.allowed?('/users', Lockdown::Configuration.authenticated_access)
|
222
|
+
assert_equal true, Lockdown::Delivery.allowed?('/users_that_should_be_protected', Lockdown::Configuration.authenticated_access)
|
223
|
+
end
|
224
|
+
end
|