simple-authorisation 0.0.8 → 0.0.9

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/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple-authorisation (0.0.7)
4
+ simple-authorisation (0.0.9)
5
5
  sinatra (~> 1.2.6)
6
6
 
7
7
  GEM
@@ -1,5 +1,11 @@
1
+ require File.join(File.dirname(__FILE__), 'route_rule_finder')
2
+ require File.join(File.dirname(__FILE__), 'exact_route_rule_finder')
3
+ require File.join(File.dirname(__FILE__), 'no_rules_for_method')
4
+
1
5
  module Simple
2
6
  module Authorisation
7
+ @@match_style = :default
8
+
3
9
  def self.post(name, options)
4
10
  options[:method] = :post
5
11
  self.route(name, options)
@@ -22,12 +28,14 @@ module Simple
22
28
  @@routes = {}
23
29
  end
24
30
 
25
- def self.is_allowed?(route_name, options)
26
- matching_route = (@@routes.keys.sort.reverse.select{|route | route_name =~ /#{route.gsub('*', '.+')}/}).first
27
- matching_route = (@@routes.keys.sort.reverse.select{|route | route_name.start_with?(route) }).first if matching_route.nil?
28
31
 
29
- route_settings = @@routes[matching_route]
30
- raise NoSettingsForRoute.new(route_name) if route_settings.nil?
32
+ def self.is_allowed?(route_name, options)
33
+ match_styles = {
34
+ :default => RouteRuleFinder,
35
+ :exact => ExactRouteRuleFinder
36
+ }
37
+ route_matcher = match_styles[match_style].new(@@routes)
38
+ route_settings = route_matcher.find(route_name)
31
39
 
32
40
  method = options.fetch(:method, :any)
33
41
  route_rules = route_settings[method] || route_settings[:any]
@@ -37,7 +45,6 @@ module Simple
37
45
  deny = route_rules.fetch(:deny, [])
38
46
  user = options.fetch(:user, nil)
39
47
 
40
-
41
48
  anonymous_user_class = options.fetch(:anonymous_user_class, NilClass)
42
49
 
43
50
  return true if allow.index('?')
@@ -50,25 +57,12 @@ module Simple
50
57
  false
51
58
  end
52
59
 
53
- class NoRulesForMethod < Exception
54
- def initialize(route_name, method)
55
- @route_name = route_name
56
- @method = method
57
- end
58
-
59
- def message
60
- "no rules found for #{@route_name} method #{@method}"
61
- end
60
+ def self.match_style=(style)
61
+ @@match_style = style
62
62
  end
63
63
 
64
- class NoSettingsForRoute < Exception
65
- def initialize(route_name)
66
- @route_name = route_name
67
- end
68
-
69
- def message
70
- "No settings for route #{@route_name}"
71
- end
64
+ def self.match_style
65
+ @@match_style
72
66
  end
73
67
  end
74
68
  end
@@ -0,0 +1,19 @@
1
+ require 'uri'
2
+
3
+ module Simple
4
+ module Authorisation
5
+ class ExactRouteRuleFinder < RouteRuleFinder
6
+ def initialize(routes)
7
+ super(routes)
8
+ @find_by = [:route_by_wild_card, :route_matches]
9
+ p @find_by
10
+ end
11
+
12
+ def route_matches(route_name)
13
+ p "using route match"
14
+ route_name = URI.parse(route_name).path.gsub(/\/$/, '')
15
+ (@routes.keys.sort.reverse.select { |route| route =~ /#{route_name}\/?/ }).first
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ module Simple
2
+ module Authorisation
3
+ class NoRulesForMethod < Exception
4
+ def initialize(route_name, method)
5
+ @route_name = route_name
6
+ @method = method
7
+ end
8
+
9
+ def message
10
+ "no rules found for #{@route_name} method #{@method}"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ module Simple
2
+ module Authorisation
3
+ class NoSettingsForRoute < Exception
4
+ def initialize(route_name)
5
+ @route_name = route_name
6
+ end
7
+
8
+ def message
9
+ "No settings for route #{@route_name}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), 'no_setting_for_route')
2
+ module Simple
3
+ module Authorisation
4
+ class RouteRuleFinder
5
+ def initialize(routes)
6
+ @routes = routes
7
+ @find_by = [:route_by_wild_card, :route_starts_with]
8
+ end
9
+
10
+ def route_by_wild_card(route_name)
11
+ (@routes.keys.sort.reverse.select{|route | route_name =~ /^#{route.gsub('*', '.+')}$/}).first
12
+ end
13
+
14
+ def route_starts_with(route_name)
15
+ (@routes.keys.sort.reverse.select { |route| route_name.start_with?(route) }).first
16
+ end
17
+
18
+ def find(route_name)
19
+ matching_route = nil
20
+ @find_by.each do |method|
21
+ matching_route = send(method, route_name)
22
+ break unless matching_route.nil?
23
+ end
24
+
25
+ route_settings = @routes[matching_route]
26
+ raise NoSettingsForRoute.new(route_name) if route_settings.nil?
27
+
28
+ route_settings
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,2 +1,3 @@
1
+ require 'simple-authorisation/route_rule_finder'
1
2
  require 'simple-authorisation/authorisation'
2
3
  require 'simple-authorisation/sinatra'
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'simple-authorisation'
6
- s.version = '0.0.8'
6
+ s.version = '0.0.9'
7
7
  s.authors = ["Derek Ekins"]
8
8
  s.description = 'Handles authorisation only'
9
9
  s.summary = "simple-authorisation-#{s.version}"
@@ -85,5 +85,11 @@ module Simple
85
85
  Simple::Authorisation.route '/test/*/blah', :allow => ['test-action']
86
86
  Simple::Authorisation.is_allowed?('/test/something/blah', :method => :get, :user => user).should be_true
87
87
  end
88
+
89
+ it "should only match routes exactly when configured to do so" do
90
+ Simple::Authorisation.match_style = :exact
91
+ Simple::Authorisation.route '/test', :allow => ['?']
92
+ lambda{ Simple::Authorisation.is_allowed?('/test/page', :user => nil) }.should raise_error(Simple::Authorisation::NoSettingsForRoute)
93
+ end
88
94
  end
89
95
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-authorisation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-09 00:00:00.000000000Z
12
+ date: 2011-09-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
16
- requirement: &16703140 !ruby/object:Gem::Requirement
16
+ requirement: &10301960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.2.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *16703140
24
+ version_requirements: *10301960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &16702500 !ruby/object:Gem::Requirement
27
+ requirement: &10301360 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.9.2
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *16702500
35
+ version_requirements: *10301360
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &16701880 !ruby/object:Gem::Requirement
38
+ requirement: &10300840 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.6.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *16701880
46
+ version_requirements: *10300840
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sinatra
49
- requirement: &16701380 !ruby/object:Gem::Requirement
49
+ requirement: &10297800 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.2.6
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *16701380
57
+ version_requirements: *10297800
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rack-test
60
- requirement: &16682260 !ruby/object:Gem::Requirement
60
+ requirement: &10297100 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 0.6.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *16682260
68
+ version_requirements: *10297100
69
69
  description: Handles authorisation only
70
70
  email: derek@spathi.com
71
71
  executables: []
@@ -78,6 +78,10 @@ files:
78
78
  - Rakefile
79
79
  - lib/simple-authorisation.rb
80
80
  - lib/simple-authorisation/authorisation.rb
81
+ - lib/simple-authorisation/exact_route_rule_finder.rb
82
+ - lib/simple-authorisation/no_rules_for_method.rb
83
+ - lib/simple-authorisation/no_setting_for_route.rb
84
+ - lib/simple-authorisation/route_rule_finder.rb
81
85
  - lib/simple-authorisation/sinatra.rb
82
86
  - simple-authorisation.gemspec
83
87
  - spec/simple-authorisation/authorisation_spec.rb
@@ -117,7 +121,7 @@ rubyforge_project:
117
121
  rubygems_version: 1.8.6
118
122
  signing_key:
119
123
  specification_version: 3
120
- summary: simple-authorisation-0.0.8
124
+ summary: simple-authorisation-0.0.9
121
125
  test_files:
122
126
  - spec/simple-authorisation/authorisation_spec.rb
123
127
  - spec/simple-authorisation/sinatra_integration_spec.rb