dancan 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +7 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +25 -0
- data/README.md +96 -0
- data/Rakefile +2 -0
- data/dancan.gemspec +25 -0
- data/lib/dancan.rb +74 -0
- data/lib/dancan/policy_finder.rb +44 -0
- data/lib/dancan/version.rb +3 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dede25a9938f3deb15f4217853872762430b8fa8
|
4
|
+
data.tar.gz: add105fa36577552a22d54eeabc88965737b22d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8995b5fed456408d3a0618c42f9d959273830c55c2f160db06f908c68ee164f678526b24e12591ffc8478592fa3832e61a88cb78cc68c46164949f3d57c2ab39
|
7
|
+
data.tar.gz: 245559b6978b19c3a41fd615ed4485461210bab140774688ae4cc8db30a035f70135de1f05cb0a4627dc1d3feb7156196ad825ffeb27f6346dd23190a4b99b1b
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Copyright (c) 2012 Daniel K
|
2
|
+
|
3
|
+
Majority of credit goes to Pundit of Elabs as code was built off of Pundit:
|
4
|
+
https://github.com/elabs/pundit
|
5
|
+
|
6
|
+
MIT License
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
9
|
+
a copy of this software and associated documentation files (the
|
10
|
+
"Software"), to deal in the Software without restriction, including
|
11
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
12
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
13
|
+
permit persons to whom the Software is furnished to do so, subject to
|
14
|
+
the following conditions:
|
15
|
+
|
16
|
+
The above copyright notice and this permission notice shall be
|
17
|
+
included in all copies or substantial portions of the Software.
|
18
|
+
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
20
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
21
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
22
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
23
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
24
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
25
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
|
2
|
+
#Dancan
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Include 'dancan' in your Gemfile
|
7
|
+
|
8
|
+
``` ruby
|
9
|
+
gem 'dancan'
|
10
|
+
```
|
11
|
+
|
12
|
+
Include Dancan in your application controller:
|
13
|
+
|
14
|
+
``` ruby
|
15
|
+
class ApplicationController < ActionController::Base
|
16
|
+
include Dancan
|
17
|
+
protect_from_forgery
|
18
|
+
|
19
|
+
def self.restrict_access(roles, options=nil)
|
20
|
+
if options
|
21
|
+
before_filter(options) { restrict_access( :roles, roles) }
|
22
|
+
else
|
23
|
+
before_filter { restrict_access( :roles, roles) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
## Policies
|
31
|
+
|
32
|
+
In app/policies/role_policy.rb
|
33
|
+
|
34
|
+
``` ruby
|
35
|
+
class RolePolicy < Struct.new(:current_admin, :roles)
|
36
|
+
attr_reader :current_admin, :roles
|
37
|
+
|
38
|
+
def initialize(current_admin, policy)
|
39
|
+
@current_admin = current_admin
|
40
|
+
end
|
41
|
+
|
42
|
+
def customer_care
|
43
|
+
@current_admin.has_any_role?(:customer_care)
|
44
|
+
end
|
45
|
+
|
46
|
+
def culinary_ops
|
47
|
+
@current_admin.has_any_role?(:culinary_ops)
|
48
|
+
end
|
49
|
+
|
50
|
+
def fulfillment_ops
|
51
|
+
@current_admin.has_any_role?(:fulfillment_ops)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
##Controller
|
58
|
+
|
59
|
+
In your controller, call restrict_access with an optional second parameter unless you want to restrict the entire controller
|
60
|
+
|
61
|
+
``` ruby
|
62
|
+
# restricts access only su action to customer_care and fulfillment_ops
|
63
|
+
restrict_access [:customer_care, :fulfillment_ops] , :only => [:su]
|
64
|
+
|
65
|
+
# restricts access everything except su action to customer_care and fulfillment_ops
|
66
|
+
restrict_access [:customer_care, :fulfillment_ops] , :except => [:su]
|
67
|
+
|
68
|
+
# restricts access entire controller to customer_care and fulfillment_ops
|
69
|
+
restrict_access [:customer_care, :fulfillment_ops]
|
70
|
+
```
|
71
|
+
|
72
|
+
## Rescuing a denied Authorization in Rails
|
73
|
+
|
74
|
+
Dancan raises a `Dancan::NotAuthorizedError` you can
|
75
|
+
rescue_from in your `ApplicationController`. You can customize the `user_not_authorized`
|
76
|
+
method in every controller.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
class ApplicationController < ActionController::Base
|
80
|
+
protect_from_forgery
|
81
|
+
include Dancan
|
82
|
+
|
83
|
+
rescue_from Dancan::NotAuthorizedError, with: :user_not_authorized
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def user_not_authorized
|
88
|
+
flash[:alert] = "Access Denied."
|
89
|
+
redirect_to(request.referrer || root_path)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
# License
|
95
|
+
|
96
|
+
Licensed under the MIT license, see the separate LICENSE.txt file.
|
data/Rakefile
ADDED
data/dancan.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dancan/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "dancan"
|
8
|
+
gem.version = Dancan::VERSION
|
9
|
+
gem.authors = ["Daniel K"]
|
10
|
+
gem.email = ["dank@example.com"]
|
11
|
+
gem.description = %q{Object oriented authorization for Rails applications}
|
12
|
+
gem.summary = %q{OO authorization for Rails}
|
13
|
+
gem.homepage = "https://github.com/2011dkang1/dancan"
|
14
|
+
gem.license = "MIT"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_dependency "activesupport", ">= 3.0.0"
|
22
|
+
gem.add_development_dependency "activemodel", ">= 3.0.0"
|
23
|
+
gem.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
gem.add_development_dependency "pry"
|
25
|
+
end
|
data/lib/dancan.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require "dancan/version"
|
2
|
+
require "dancan/policy_finder"
|
3
|
+
require "active_support/concern"
|
4
|
+
require "active_support/core_ext/string/inflections"
|
5
|
+
require "active_support/core_ext/object/blank"
|
6
|
+
require "active_support/core_ext/module/introspection"
|
7
|
+
require "active_support/dependencies/autoload"
|
8
|
+
|
9
|
+
module Dancan
|
10
|
+
class NotAuthorizedError < StandardError
|
11
|
+
attr_accessor :roles, :record, :policy
|
12
|
+
end
|
13
|
+
class AuthorizationNotPerformedError < StandardError; end
|
14
|
+
class PolicyScopingNotPerformedError < AuthorizationNotPerformedError; end
|
15
|
+
class NotDefinedError < StandardError; end
|
16
|
+
|
17
|
+
extend ActiveSupport::Concern
|
18
|
+
|
19
|
+
class << self
|
20
|
+
|
21
|
+
def policy(user, record)
|
22
|
+
policy = PolicyFinder.new(record).policy
|
23
|
+
policy.new(user, record) if policy
|
24
|
+
end
|
25
|
+
|
26
|
+
def policy!(user, record)
|
27
|
+
PolicyFinder.new(record).policy!.new(user, record)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
included do
|
32
|
+
if respond_to?(:helper_method)
|
33
|
+
helper_method :policy
|
34
|
+
helper_method :dancan_admin
|
35
|
+
end
|
36
|
+
if respond_to?(:hide_action)
|
37
|
+
hide_action :policy
|
38
|
+
hide_action :policies
|
39
|
+
hide_action :restrict_access
|
40
|
+
hide_action :dancan_admin
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def restrict_access(record, permitted_roles=nil)
|
45
|
+
|
46
|
+
|
47
|
+
roles = permitted_roles
|
48
|
+
policy = policy(record)
|
49
|
+
unless roles.map().any? { |role| policy.public_send(role) }
|
50
|
+
error = NotAuthorizedError.new("#{record} does not have not roles: #{roles.to_s}")
|
51
|
+
error.roles, error.record, error.policy = roles.to_s, record, policy
|
52
|
+
|
53
|
+
raise error
|
54
|
+
end
|
55
|
+
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
def policy(record)
|
60
|
+
policies[record] ||= Dancan.policy!(dancan_admin, record)
|
61
|
+
end
|
62
|
+
|
63
|
+
def policies
|
64
|
+
@_dancan_policies ||= {}
|
65
|
+
end
|
66
|
+
|
67
|
+
def dancan_admin
|
68
|
+
current_admin
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Dancan
|
2
|
+
class PolicyFinder
|
3
|
+
attr_reader :object
|
4
|
+
|
5
|
+
def initialize(object)
|
6
|
+
@object = object
|
7
|
+
end
|
8
|
+
|
9
|
+
def policy
|
10
|
+
klass = find
|
11
|
+
klass = klass.constantize if klass.is_a?(String)
|
12
|
+
klass
|
13
|
+
rescue NameError
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def policy!
|
18
|
+
policy or raise NotDefinedError, "unable to find policy #{find} for #{object}"
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def find
|
24
|
+
if object.respond_to?(:policy_class)
|
25
|
+
object.policy_class
|
26
|
+
elsif object.class.respond_to?(:policy_class)
|
27
|
+
object.class.policy_class
|
28
|
+
else
|
29
|
+
klass = if object.respond_to?(:model_name)
|
30
|
+
object.model_name
|
31
|
+
elsif object.class.respond_to?(:model_name)
|
32
|
+
object.class.model_name
|
33
|
+
elsif object.is_a?(Class)
|
34
|
+
object
|
35
|
+
elsif object.is_a?(Symbol)
|
36
|
+
object.to_s.classify
|
37
|
+
else
|
38
|
+
object.class
|
39
|
+
end
|
40
|
+
"#{klass}Policy"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dancan
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel K
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activemodel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Object oriented authorization for Rails applications
|
70
|
+
email:
|
71
|
+
- dank@example.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- dancan.gemspec
|
83
|
+
- lib/dancan.rb
|
84
|
+
- lib/dancan/policy_finder.rb
|
85
|
+
- lib/dancan/version.rb
|
86
|
+
homepage: https://github.com/2011dkang1/dancan
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: OO authorization for Rails
|
110
|
+
test_files: []
|
111
|
+
has_rdoc:
|