role_based_authorization 0.1.13 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -34,6 +34,7 @@ begin
34
34
  gemspec.email = "boborbt@gmail.com"
35
35
  gemspec.homepage = "http://github.com/boborbt/role_based_authorization"
36
36
  gemspec.authors = ["Roberto Esposito"]
37
+ gemspec.add_dependency('rails', '>= 2.2')
37
38
  end
38
39
  rescue LoadError
39
40
  puts "Jeweler not available. Install it with: gem install jeweler"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.13
1
+ 0.1.14
@@ -35,8 +35,7 @@ module RoleBasedAuthorization
35
35
  # specifies that :product_id should be used instead of :id.
36
36
 
37
37
  def permit options
38
- options[:controller] ||= controller_name
39
- controller = options[:controller]
38
+ controller = options[:controller] || controller_name
40
39
  actions = [*options[:actions]] # create an array if options[:actions] is not already an array
41
40
 
42
41
  role_auth_rules[controller] ||= {}
@@ -44,7 +43,7 @@ module RoleBasedAuthorization
44
43
  actions.each do |action|
45
44
  action = action.to_sym # this allows for both symbols and strings to be used for action names
46
45
  role_auth_rules[controller][action] ||= []
47
- role_auth_rules[controller][action] << RoleBasedAuthorization::Rule.new(options[:to], options[:if], options[:object_id])
46
+ role_auth_rules[controller][action] << RoleBasedAuthorization::Rule.new(*options.values_at(:to,:if,:object_id))
48
47
  end
49
48
  end
50
49
  end
@@ -33,7 +33,7 @@ module RoleBasedAuthorization
33
33
 
34
34
  # Returns an hash options amenable to be passed to authorize_action?. It takes either
35
35
  # an option hash, or a path string
36
- def RoleBasedAuthorization.cleanup_options(opts)
36
+ def RoleBasedAuthorization.path_or_options_to_options(opts)
37
37
  path_cleanup_regexp = %r{(#{ActionController::Base.relative_url_root})?}
38
38
 
39
39
  url_options = (opts.class == String) && ActionController::Routing::Routes.recognize_path(opts.gsub(path_cleanup_regexp,''))
@@ -112,7 +112,7 @@ module RoleBasedAuthorization
112
112
  # if_authorized?( edit_item_path ) { |opts| link_to('yyy', opts) }
113
113
 
114
114
  def if_authorized? opts, &block
115
- block.call(opts) if authorize_action?(RoleBasedAuthorization.cleanup_options(opts))
115
+ block.call(opts) if authorize_action?(RoleBasedAuthorization.path_or_options_to_options(opts))
116
116
  end
117
117
 
118
118
  # Returns true if the current user is authorized to perform the current action
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{role_based_authorization}
8
- s.version = "0.1.13"
8
+ s.version = "0.1.14"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Roberto Esposito"]
12
- s.date = %q{2010-02-18}
12
+ s.date = %q{2010-02-19}
13
13
  s.description = %q{Provides a simple DSL for specifying the authorization logic of your application. Install the gem, add a role attribute to your user model and your almost ready to go.}
14
14
  s.email = %q{boborbt@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "lib/role_based_authorization/rule.rb",
29
29
  "rails/init.rb",
30
30
  "role_based_authorization.gemspec",
31
+ "test/authorization_logger_test.rb",
31
32
  "test/role_based_authorization_test.rb",
32
33
  "test/test_helper.rb"
33
34
  ]
@@ -37,7 +38,8 @@ Gem::Specification.new do |s|
37
38
  s.rubygems_version = %q{1.3.5}
38
39
  s.summary = %q{Basic authorization module for rails}
39
40
  s.test_files = [
40
- "test/role_based_authorization_test.rb",
41
+ "test/authorization_logger_test.rb",
42
+ "test/role_based_authorization_test.rb",
41
43
  "test/test_helper.rb"
42
44
  ]
43
45
 
@@ -46,9 +48,12 @@ Gem::Specification.new do |s|
46
48
  s.specification_version = 3
47
49
 
48
50
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ s.add_runtime_dependency(%q<rails>, [">= 2.2"])
49
52
  else
53
+ s.add_dependency(%q<rails>, [">= 2.2"])
50
54
  end
51
55
  else
56
+ s.add_dependency(%q<rails>, [">= 2.2"])
52
57
  end
53
58
  end
54
59
 
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+ require 'role_based_authorization'
3
+
4
+ class AuthorizationLoggerTest < ActiveSupport::TestCase
5
+ def setup
6
+ @logger = AuthorizationLogger.new(nil)
7
+ end
8
+
9
+
10
+ test "Should include the log prefix string to each log entry" do
11
+ assert_match /#{AuthorizationLogger::AUTHORIZATION_SYSTEM_LOG_MSG_PREFIX}/, @logger.format_message(:info, Time.now, "progname", "msg")
12
+ end
13
+
14
+ test "Should include the current time in the db format" do
15
+ time = Time.now
16
+ assert_match /#{time.to_s(:db)}/, @logger.format_message(:info, time, "progname", "msg")
17
+ end
18
+
19
+ test "Should include the log level" do
20
+ assert_match /INFO/, @logger.format_message('INFO', Time.now, "progname", "msg")
21
+ end
22
+
23
+
24
+ test "Should include the output msg" do
25
+ assert_match /msg/, @logger.format_message('INFO', Time.now, "progname", "msg")
26
+ end
27
+
28
+ test "Should not include the program name" do
29
+ assert ! /progname/.match(@logger.format_message('INFO', Time.now, "progname", "msg"))
30
+ end
31
+
32
+ end
@@ -51,7 +51,6 @@ class DummyController < ApplicationController
51
51
 
52
52
  end
53
53
 
54
-
55
54
  class RoleBasedAuthorizationTest < ActiveSupport::TestCase
56
55
  def setup
57
56
  @controller = DummyController.new
@@ -131,6 +130,50 @@ class RoleBasedAuthorizationTest < ActiveSupport::TestCase
131
130
 
132
131
  assert got_inside
133
132
  end
133
+
134
+ test "path_or_options_to_options should leave untouched the options if they are already there" do
135
+ options = RoleBasedAuthorization.path_or_options_to_options({:controller => 'dummy', :action => 'very_low_security'})
136
+ assert_equal 'dummy', options[:controller]
137
+ assert_equal 'very_low_security', options[:action]
138
+ end
134
139
 
140
+ test "path_or_options_to_options should work also when paths contain the relative_url_root" do
141
+ ActionController::Base.relative_url_root = '/test'
142
+ options = RoleBasedAuthorization.path_or_options_to_options('/test/dummy/very_low_security')
143
+ assert_equal 'dummy', options[:controller]
144
+ assert_equal 'very_low_security', options[:action]
145
+ end
146
+
147
+ test "path_or_options_to_options should work with paths" do
148
+ options = RoleBasedAuthorization.path_or_options_to_options('/dummy/very_low_security')
149
+ assert_equal 'dummy', options[:controller]
150
+ assert_equal 'very_low_security', options[:action]
151
+ end
152
+
153
+
154
+ test "RoleBasedAuthorization.find_matching_rule should return nil if no rule matches" do
155
+ rules = { :action1 => mocked_rules([false]*4),
156
+ :action2 => mocked_rules([false]*2) }
157
+
158
+ assert_equal nil, RoleBasedAuthorization.find_matching_rule(rules, {:actions => [:action1, :action2, :action3, :action4]})
159
+ end
160
+
161
+
162
+ test "RoleBasedAuthorization.find_matching_rule should not return nil if some rule matches" do
163
+ rules = { :action1 => mocked_rules([false]*4),
164
+ :action2 => mocked_rules([true, false]) }
165
+
166
+ assert RoleBasedAuthorization.find_matching_rule(rules, {:actions => [:action1, :action2, :action3, :action4]})
167
+ end
168
+
169
+
170
+ private
171
+
172
+ def mocked_rules(values)
173
+ result = Array.new(values.size) { mock() }
174
+ result.each_with_index { |rule, index| rule.stubs(:match).returns(values[index]) }
175
+ result
176
+ end
177
+
135
178
 
136
179
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: role_based_authorization
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Esposito
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-18 00:00:00 +01:00
12
+ date: 2010-02-19 00:00:00 +01:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.2"
24
+ version:
16
25
  description: Provides a simple DSL for specifying the authorization logic of your application. Install the gem, add a role attribute to your user model and your almost ready to go.
17
26
  email: boborbt@gmail.com
18
27
  executables: []
@@ -34,6 +43,7 @@ files:
34
43
  - lib/role_based_authorization/rule.rb
35
44
  - rails/init.rb
36
45
  - role_based_authorization.gemspec
46
+ - test/authorization_logger_test.rb
37
47
  - test/role_based_authorization_test.rb
38
48
  - test/test_helper.rb
39
49
  has_rdoc: true
@@ -65,5 +75,6 @@ signing_key:
65
75
  specification_version: 3
66
76
  summary: Basic authorization module for rails
67
77
  test_files:
78
+ - test/authorization_logger_test.rb
68
79
  - test/role_based_authorization_test.rb
69
80
  - test/test_helper.rb