Authorizr 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +85 -0
- data/lib/authorizr/controller.rb +143 -0
- data/lib/authorizr/engine.rb +8 -0
- data/lib/authorizr/model.rb +57 -0
- data/lib/authorizr.rb +6 -0
- metadata +72 -0
data/README.rdoc
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
== Authorizr
|
2
|
+
A General purpose authorization framework.
|
3
|
+
|
4
|
+
Now sporting:
|
5
|
+
|
6
|
+
- Controller level, block based authorization of resources and actions
|
7
|
+
- Out of the box whitelist approach
|
8
|
+
- Easy debugging of authorization actions with logger output
|
9
|
+
|
10
|
+
== Implementing:
|
11
|
+
|
12
|
+
Simply add one line to your application controller to authorize all requests for all controllers:
|
13
|
+
|
14
|
+
app/controllers/application_controller.rb:
|
15
|
+
|
16
|
+
class ApplicationController < ActionController::Base
|
17
|
+
protect_from_forgery
|
18
|
+
authorize_all
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
Then in your controllers define an authorize block. Simply return true if the user is authorized to view the resoure and false if not. The block takes one parameter, the environment.
|
23
|
+
|
24
|
+
=== Examples:
|
25
|
+
|
26
|
+
app/controllers/posts_controller.rb:
|
27
|
+
|
28
|
+
#Example 1, deny everything always
|
29
|
+
authorize do |env|
|
30
|
+
false
|
31
|
+
end
|
32
|
+
|
33
|
+
#Example 2, approve everything always
|
34
|
+
authorize do |env|
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
#Example 3, approve index, show, create, new actions for everyone
|
39
|
+
# => and edit, update, destroy actions for the owner
|
40
|
+
authorize do |env|
|
41
|
+
case env[:action]
|
42
|
+
when :index, :show, :create, :new
|
43
|
+
true
|
44
|
+
when :edit, :update, :destroy
|
45
|
+
post = Post.find env[:params][:id]
|
46
|
+
post.owner == current_user
|
47
|
+
else
|
48
|
+
false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
#Example 4 [Advanced], Example 3 rewritten to use preloaded resource with user hook in place
|
53
|
+
authorize do |env|
|
54
|
+
case env[:action]
|
55
|
+
when :index, :show, :create, :new
|
56
|
+
true
|
57
|
+
when :edit, :update, :destroy
|
58
|
+
env[:resource].user == env[:user]
|
59
|
+
else
|
60
|
+
false
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
Full detail on the environment passed into the authorize block:
|
65
|
+
|
66
|
+
env = {
|
67
|
+
:user => current_user, #nil unless ApplicationController responds to current_user
|
68
|
+
:action => self.action_name, #
|
69
|
+
:controller => self, #
|
70
|
+
:params => params, #
|
71
|
+
:resource => resource, # RESTful actions will attempt to fill this with your resource
|
72
|
+
:model => model #
|
73
|
+
}
|
74
|
+
|
75
|
+
|
76
|
+
== License
|
77
|
+
This code is released under The MIT License (MIT)
|
78
|
+
Copyright (c) 2011 Robert L Carpenter
|
79
|
+
|
80
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
81
|
+
|
82
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
83
|
+
|
84
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
85
|
+
|
@@ -0,0 +1,143 @@
|
|
1
|
+
|
2
|
+
module Authorizr
|
3
|
+
module Controller
|
4
|
+
module ClassMethods
|
5
|
+
def authorize_all
|
6
|
+
before_filter :authorize!
|
7
|
+
end
|
8
|
+
|
9
|
+
#a hook for other object to authorize from outside the request object
|
10
|
+
def manually_authorize params={}
|
11
|
+
params[:user] ||= nil
|
12
|
+
params[:resource] ||= nil
|
13
|
+
params[:action] ||= nil
|
14
|
+
|
15
|
+
auth_block = @@authorization_blocks[self.to_s]
|
16
|
+
if auth_block.nil?
|
17
|
+
authorized = false
|
18
|
+
else
|
19
|
+
authorized = auth_block.call({
|
20
|
+
:user => params[:user],
|
21
|
+
:resource => params[:resource],
|
22
|
+
:action => params[:action],
|
23
|
+
:params => {},
|
24
|
+
:model => params[:resource].class
|
25
|
+
})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
#these methods will be called from the Class level, not the instance level
|
30
|
+
def authorize &block
|
31
|
+
@@authorization_blocks[self.to_s] = block unless block.nil?
|
32
|
+
end
|
33
|
+
|
34
|
+
def unauthorized &block
|
35
|
+
@@failure_blocks[self.to_s] = block unless block.nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
#build maintain a catalog of the auth/unauth blocks provided by the child controllers
|
39
|
+
def create_authblock_catalog
|
40
|
+
@@authorization_blocks = {}
|
41
|
+
@@failure_blocks = {}
|
42
|
+
end
|
43
|
+
|
44
|
+
mattr_reader :authorization_blocks
|
45
|
+
mattr_reader :failure_blocks
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.included to
|
49
|
+
to.extend ClassMethods
|
50
|
+
to.create_authblock_catalog
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
#the before-filter that gets called on every action
|
55
|
+
def authorize!
|
56
|
+
authorized = call_auth_block
|
57
|
+
|
58
|
+
logit authorized
|
59
|
+
return true if authorized
|
60
|
+
call_failure_block
|
61
|
+
end
|
62
|
+
|
63
|
+
def call_auth_block
|
64
|
+
auth_block = self.class.authorization_blocks[self.class.to_s]
|
65
|
+
return false if auth_block.nil?
|
66
|
+
|
67
|
+
params = request.parameters || nil
|
68
|
+
|
69
|
+
model, resource = build_resource params
|
70
|
+
|
71
|
+
auth_block.call({
|
72
|
+
:user => current_user,
|
73
|
+
:action => self.action_name,
|
74
|
+
:controller => self,
|
75
|
+
:params => params,
|
76
|
+
:resource => resource,
|
77
|
+
:model => model
|
78
|
+
})
|
79
|
+
end
|
80
|
+
|
81
|
+
def call_failure_block
|
82
|
+
failure_block = self.class.failure_blocks[self.class.to_s]
|
83
|
+
|
84
|
+
if failure_block.nil?
|
85
|
+
render_error and return false
|
86
|
+
else
|
87
|
+
abort_action = failure_block.call({:controller => self})
|
88
|
+
|
89
|
+
if !abort_action
|
90
|
+
# if a render has been declared by the abort action, don't call the default error render error
|
91
|
+
render_error unless performed?
|
92
|
+
return false
|
93
|
+
else
|
94
|
+
abort_action
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def logit authorized
|
100
|
+
if Rails.env == 'development'
|
101
|
+
if authorized
|
102
|
+
Rails.logger.warn "\033[32mGRANT:\033[0m #{self.controller_name} #{self.action_name}"
|
103
|
+
else
|
104
|
+
Rails.logger.warn "\033[31mDENY:\033[0m #{self.controller_name} #{self.action_name}"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
#override in application
|
110
|
+
def current_user
|
111
|
+
nil
|
112
|
+
end
|
113
|
+
|
114
|
+
#attempt to sort out a model from the url and controller name
|
115
|
+
def build_resource parameters
|
116
|
+
return [nil, nil] if parameters.nil? || parameters[:id].nil?
|
117
|
+
model_name = self.controller_name.classify
|
118
|
+
|
119
|
+
begin
|
120
|
+
model = Module.const_get model_name
|
121
|
+
if model.respond_to? :find
|
122
|
+
resource = model.find parameters[:id]
|
123
|
+
else
|
124
|
+
model = nil
|
125
|
+
end
|
126
|
+
rescue ActiveRecord::RecordNotFound
|
127
|
+
Rails.logger.warn "\033[31m Record not found. Model:#{model_name} ID:#{parameters[:id]}"
|
128
|
+
model = resource = nil
|
129
|
+
rescue NameError
|
130
|
+
Rails.logger.warn "\033[31m Name Error. Model:#{model_name} ID:#{parameters[:id]}"
|
131
|
+
model = resource = nil
|
132
|
+
end
|
133
|
+
|
134
|
+
[model, resource]
|
135
|
+
end
|
136
|
+
|
137
|
+
def render_error
|
138
|
+
render :text => '404'
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
ActionController::Base.send :include, Authorizr::Controller
|
@@ -0,0 +1,57 @@
|
|
1
|
+
|
2
|
+
module Authorizr
|
3
|
+
module Model
|
4
|
+
module ClassMethods
|
5
|
+
# Attempts to find the controller class for this model using railsy style
|
6
|
+
# Override this method if you use nonstandard controller names...
|
7
|
+
def controller
|
8
|
+
guess = ActiveSupport::Inflector.pluralize(self.name) + 'Controller'
|
9
|
+
begin
|
10
|
+
troller = Object.const_get guess
|
11
|
+
rescue NameError
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def authorize params={}
|
17
|
+
params[:user] ||= nil
|
18
|
+
params[:resource] ||= nil
|
19
|
+
params[:action] ||= nil
|
20
|
+
|
21
|
+
troller = self.controller
|
22
|
+
if troller.respond_to? :manually_authorize
|
23
|
+
troller.manually_authorize params
|
24
|
+
else
|
25
|
+
raise NoMethodError, "#{troller.name} does not respond to :manually_authorize"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def permissable
|
30
|
+
self.class_eval do
|
31
|
+
include Authorizr::Model::Permissable
|
32
|
+
end #block
|
33
|
+
end #def permissable
|
34
|
+
|
35
|
+
end #ClassMethods
|
36
|
+
|
37
|
+
#methods included in models which declare themselves to be permissable
|
38
|
+
module Permissable
|
39
|
+
def can? action, object
|
40
|
+
if object.respond_to?(:authorize)
|
41
|
+
object.authorize :user => self, :resource => nil, :action => action
|
42
|
+
elsif object.class.respond_to?(:authorize)
|
43
|
+
object.class.authorize :user => self, :resource => object, :action => action
|
44
|
+
else
|
45
|
+
false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.included to
|
51
|
+
to.extend ClassMethods
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
ActiveRecord::Base.send :include, Authorizr::Model
|
data/lib/authorizr.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: Authorizr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Robert Carpenter
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-13 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: Authorizr
|
16
|
+
requirement: &70346914310720 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70346914310720
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: orm_adapter
|
27
|
+
requirement: &70346914310240 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70346914310240
|
36
|
+
description: ! 'Authorizr: A simple controller-centric authorization framework.'
|
37
|
+
email:
|
38
|
+
executables: []
|
39
|
+
extensions: []
|
40
|
+
extra_rdoc_files:
|
41
|
+
- README.rdoc
|
42
|
+
files:
|
43
|
+
- lib/authorizr.rb
|
44
|
+
- lib/authorizr/controller.rb
|
45
|
+
- lib/authorizr/engine.rb
|
46
|
+
- lib/authorizr/model.rb
|
47
|
+
- README.rdoc
|
48
|
+
homepage:
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.10
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Authentication and Authorization
|
72
|
+
test_files: []
|