simple-authorisation 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple-authorisation (0.0.3)
4
+ simple-authorisation (0.0.4)
5
5
  sinatra (~> 1.2.6)
6
6
 
7
7
  GEM
@@ -15,18 +15,22 @@ module Simple
15
15
  @@routes[name] = {} unless @@routes.has_key?(name)
16
16
 
17
17
  route_settings = @@routes[name]
18
- route_settings[options.delete(:method) || :default] = options
18
+ route_settings[options.delete(:method) || :any] = options
19
+ end
20
+
21
+ def self.clear
22
+ @@routes = {}
19
23
  end
20
24
 
21
25
  def self.is_allowed?(route_name, options)
22
26
  matching_route = (@@routes.keys.sort.reverse.select{|route | route_name.start_with?(route) }).first
23
27
 
24
28
  route_settings = @@routes[matching_route]
25
- raise "no settings found for #{route_name}" if route_settings.nil?
29
+ raise NoSettingsForRoute.new(route_name) if route_settings.nil?
26
30
 
27
- method = options.fetch(:method, :default)
28
- route_rules = route_settings[method] || route_settings[:default]
29
- raise "no rules found for #{route_name} method #{method}" if route_rules.nil?
31
+ method = options.fetch(:method, :any)
32
+ route_rules = route_settings[method] || route_settings[:any]
33
+ raise NoRulesForMethod.new(route_name, method) if route_rules.nil?
30
34
 
31
35
  allow = route_rules.fetch(:allow, [])
32
36
  deny = route_rules.fetch(:deny, [])
@@ -41,5 +45,26 @@ module Simple
41
45
 
42
46
  false
43
47
  end
48
+
49
+ class NoRulesForMethod < Exception
50
+ def initialize(route_name, method)
51
+ @route_name = route_name
52
+ @method = method
53
+ end
54
+
55
+ def message
56
+ "no rules found for #{@route_name} method #{@method}"
57
+ end
58
+ end
59
+
60
+ class NoSettingsForRoute < Exception
61
+ def initialize(route_name)
62
+ @route_name = route_name
63
+ end
64
+
65
+ def message
66
+ "No settings for route #{@route_name}"
67
+ end
68
+ end
44
69
  end
45
70
  end
@@ -9,7 +9,8 @@ module Sinatra
9
9
 
10
10
  app.before do
11
11
  route_name = request.path
12
- unless Simple::Authorisation.is_allowed?(route_name, :user => current_user, :anonymous_user_class => options.authorisation_anonymous_user_class)
12
+ user = send(options.authorisation_current_user)
13
+ unless Simple::Authorisation.is_allowed?(route_name, :user => user, :anonymous_user_class => options.authorisation_anonymous_user_class, :method => request.request_method.downcase.to_sym)
13
14
  session[:return_to] = request.fullpath unless request.fullpath.include?('favicon.ico')
14
15
  redirect options.authorisation_login
15
16
  return false
@@ -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.3'
6
+ s.version = '0.0.4'
7
7
  s.authors = ["Derek Ekins"]
8
8
  s.description = 'Handles authorisation only'
9
9
  s.summary = "simple-authorisation-#{s.version}"
@@ -2,6 +2,9 @@ require 'simple-authorisation/authorisation'
2
2
 
3
3
  module Simple
4
4
  describe Authorisation do
5
+ before do
6
+ Simple::Authorisation.clear
7
+ end
5
8
  it "should allow requests to anonymous users" do
6
9
  Simple::Authorisation.route '/test', :allow => ['?']
7
10
  Simple::Authorisation.is_allowed?('/test', :user => nil).should be_true
@@ -41,5 +44,15 @@ module Simple
41
44
  Simple::Authorisation.is_allowed?('/test', :method => :post, :user => nil).should be_false
42
45
  Simple::Authorisation.is_allowed?('/test', :method => :get, :user => nil).should be_true
43
46
  end
47
+
48
+ it "should apply rule to any method when none specified" do
49
+ Simple::Authorisation.route '/test', :allow => ['?']
50
+ Simple::Authorisation.is_allowed?('/test', :method => :get, :user => nil).should be_true
51
+ Simple::Authorisation.is_allowed?('/test', :method => :post, :user => nil).should be_true
52
+ end
53
+
54
+ it "should raise an exception when checking is_allowed for a route with no rules" do
55
+ lambda {Simple::Authorisation.is_allowed?('/test', :method => :get, :user => nil)}.should raise_error(Simple::Authorisation::NoSettingsForRoute)
56
+ end
44
57
  end
45
58
  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.3
4
+ version: 0.0.4
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-07-27 00:00:00.000000000Z
12
+ date: 2011-07-28 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
16
- requirement: &11063860 !ruby/object:Gem::Requirement
16
+ requirement: &15242400 !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: *11063860
24
+ version_requirements: *15242400
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &11063380 !ruby/object:Gem::Requirement
27
+ requirement: &15241800 !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: *11063380
35
+ version_requirements: *15241800
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &11060200 !ruby/object:Gem::Requirement
38
+ requirement: &15241240 !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: *11060200
46
+ version_requirements: *15241240
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sinatra
49
- requirement: &11059740 !ruby/object:Gem::Requirement
49
+ requirement: &15240680 !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: *11059740
57
+ version_requirements: *15240680
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rack-test
60
- requirement: &11059280 !ruby/object:Gem::Requirement
60
+ requirement: &15240120 !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: *11059280
68
+ version_requirements: *15240120
69
69
  description: Handles authorisation only
70
70
  email: derek@spathi.com
71
71
  executables: []
@@ -117,7 +117,7 @@ rubyforge_project:
117
117
  rubygems_version: 1.8.6
118
118
  signing_key:
119
119
  specification_version: 3
120
- summary: simple-authorisation-0.0.3
120
+ summary: simple-authorisation-0.0.4
121
121
  test_files:
122
122
  - spec/simple-authorisation/authorisation_spec.rb
123
123
  - spec/simple-authorisation/sinatra_integration_spec.rb