checkpoint 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.Gemfile.swp +0 -0
- data/.Readme.markdown.swp +0 -0
- data/.checkpoint.gemspec.swp +0 -0
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/Readme.markdown +93 -0
- data/checkpoint.gemspec +25 -0
- data/lib/checkpoint.rb +7 -0
- data/lib/checkpoint/railtie.rb +76 -0
- data/lib/checkpoint/version.rb +3 -0
- metadata +57 -0
data/.Gemfile.swp
ADDED
Binary file
|
Binary file
|
Binary file
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/Readme.markdown
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
#Simple authorisation for Rails
|
2
|
+
|
3
|
+
##Installation
|
4
|
+
|
5
|
+
Add the following to your gem file:
|
6
|
+
|
7
|
+
```
|
8
|
+
gem 'checkpoint'
|
9
|
+
```
|
10
|
+
and then run bundle install from your shell.
|
11
|
+
|
12
|
+
## How to use
|
13
|
+
|
14
|
+
By default all users get denied access to everything.
|
15
|
+
|
16
|
+
To enable a user to access/use a resource you must specify an authorisation rule to grant access. This is done using the "authorise" (or "authorize" for americans) method in the application controller.
|
17
|
+
|
18
|
+
So if for instance you wanted to grant access (to all users) to your posts index action you could do the following:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
#grant access your posts controller 'index' action to all users
|
22
|
+
authorise "PostsController::index"
|
23
|
+
|
24
|
+
#or authorize "PostsController::index"
|
25
|
+
|
26
|
+
```
|
27
|
+
|
28
|
+
Noticed how the pattern above is in the format of "ControllerName::action"
|
29
|
+
|
30
|
+
So if you wanted to grant action to your posts view action you could do the following:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
#grant access to your posts controller 'view' action to all users
|
34
|
+
authorise "PostsController::view"
|
35
|
+
```
|
36
|
+
|
37
|
+
If you want to grant access to all actions in your post controller you can use a wildcard ('*') char and do the following
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
#grant access to your posts controller actions to all users
|
41
|
+
authorise "PostsController::*"
|
42
|
+
```
|
43
|
+
|
44
|
+
You can also do the same above by using a regular expression:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
#grant access to your posts controller actions to all users
|
48
|
+
authorise /\APostsController::.*\Z/
|
49
|
+
```
|
50
|
+
|
51
|
+
If you want to be able to grant access to your view action to only users who have signed in, you can do this by passing a block that returns true if the user is logged in.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
#grant access your posts controller 'view' action to all users who have signed in
|
55
|
+
authorise "PostsController::view" do
|
56
|
+
!current_user.nil?
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
In the example above the block uses the bindings of the controller that is being called, so therefore it can access anything that that particular controller access e.g. your current params hash etc...
|
61
|
+
|
62
|
+
Similarly you could grant access to everything to all admin users by doing the following:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
#grant access to everything to all admin users
|
66
|
+
authorise "*" do
|
67
|
+
!current_user.nil? && current_user.admin?
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
Finally by passing an array you can authorise a range of controller actions in one go:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
#grant access your posts controller 'create' and 'update' actions to all users who have signed in
|
75
|
+
authorise ["PostsController::create", "PostsController::update"] do
|
76
|
+
!current_user.nil?
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
## FAQ
|
81
|
+
|
82
|
+
### How do I enable devise?
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
authorise "Devise::*"
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
## License
|
90
|
+
|
91
|
+
Checkpoint is released under the MIT license:
|
92
|
+
|
93
|
+
* http://www.opensource.org/licenses/MIT
|
data/checkpoint.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "checkpoint/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "checkpoint"
|
7
|
+
s.version = Checkpoint::VERSION
|
8
|
+
s.authors = ["Leanbid LTD"]
|
9
|
+
s.email = ["it@leanbid.com"]
|
10
|
+
s.homepage = "https://github.com/leanbid/checkpoint"
|
11
|
+
s.summary = "Simple rails authorisation"
|
12
|
+
#s.description = %q{TODO: Write a gem description}
|
13
|
+
s.license = "MIT"
|
14
|
+
|
15
|
+
s.rubyforge_project = "checkpoint"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
# specify any dependencies here; for example:
|
23
|
+
# s.add_development_dependency "rspec"
|
24
|
+
# s.add_runtime_dependency "rest-client"
|
25
|
+
end
|
data/lib/checkpoint.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
class Checkpoint::Railtie < ::Rails::Railtie
|
4
|
+
config.before_initialize do
|
5
|
+
class ::ActionController::Base
|
6
|
+
|
7
|
+
def self.authorise_controllers_blocks
|
8
|
+
if @authorise_controllers_blocks.nil?
|
9
|
+
@authorise_controllers_blocks = {}
|
10
|
+
end
|
11
|
+
@authorise_controllers_blocks
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.authorise(arg1, &block)
|
15
|
+
|
16
|
+
if block.nil?
|
17
|
+
block = lambda {|c| true}
|
18
|
+
end
|
19
|
+
|
20
|
+
to_regexp = lambda do |pattern|
|
21
|
+
if arg1.class.to_s == 'Regexp'
|
22
|
+
arg1
|
23
|
+
else
|
24
|
+
Regexp.new('\A' + pattern.to_s.gsub(/[^\*]/){|char| Regexp.quote(char)}.gsub(/\*/){|| ".*?"} + '\Z')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
patterns = []
|
29
|
+
if arg1.class.to_s == 'Array'
|
30
|
+
arg1.each {|pattern| patterns.push to_regexp.call(pattern) }
|
31
|
+
else
|
32
|
+
patterns.push to_regexp.call(arg1)
|
33
|
+
end
|
34
|
+
|
35
|
+
authorise_controllers_blocks = ::ApplicationController.authorise_controllers_blocks
|
36
|
+
|
37
|
+
patterns.each do |pattern|
|
38
|
+
if authorise_controllers_blocks [pattern].nil?
|
39
|
+
authorise_controllers_blocks[pattern] = []
|
40
|
+
end
|
41
|
+
authorise_controllers_blocks[pattern].push(block)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
#for our american friends
|
46
|
+
def self.authorize(arg1, &block)
|
47
|
+
authorise(arg1, &block)
|
48
|
+
end
|
49
|
+
|
50
|
+
def authorised?
|
51
|
+
action = "#{self.class.to_s}::#{params[:action]}"
|
52
|
+
::ApplicationController.authorise_controllers_blocks.each do |pattern, blocks|
|
53
|
+
if action.match pattern
|
54
|
+
blocks.each do |block|
|
55
|
+
if instance_eval(&block)
|
56
|
+
return true
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
false
|
62
|
+
end
|
63
|
+
|
64
|
+
before_filter do |controller|
|
65
|
+
if !authorised?
|
66
|
+
logger.info "\n\n-----------------------------------------------"
|
67
|
+
logger.info " (401) Access Denied!"
|
68
|
+
logger.info " * see the above request for more info"
|
69
|
+
logger.info "-----------------------------------------------\n\n"
|
70
|
+
render :text => "Access Denied", :status => 401
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: checkpoint
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Leanbid LTD
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-16 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email:
|
16
|
+
- it@leanbid.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .Gemfile.swp
|
22
|
+
- .Readme.markdown.swp
|
23
|
+
- .checkpoint.gemspec.swp
|
24
|
+
- .gitignore
|
25
|
+
- Gemfile
|
26
|
+
- Rakefile
|
27
|
+
- Readme.markdown
|
28
|
+
- checkpoint.gemspec
|
29
|
+
- lib/checkpoint.rb
|
30
|
+
- lib/checkpoint/railtie.rb
|
31
|
+
- lib/checkpoint/version.rb
|
32
|
+
homepage: https://github.com/leanbid/checkpoint
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project: checkpoint
|
53
|
+
rubygems_version: 1.8.19
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Simple rails authorisation
|
57
|
+
test_files: []
|