dead_simple_authorization 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +2 -0
- data/Rakefile +8 -0
- data/dead_simple_authorization.gemspec +24 -0
- data/lib/dead_simple_authorization.rb +3 -0
- data/lib/dead_simple_authorization/errors.rb +7 -0
- data/lib/dead_simple_authorization/helpers.rb +32 -0
- data/lib/dead_simple_authorization/policy.rb +17 -0
- data/lib/dead_simple_authorization/version.rb +3 -0
- data/spec/helpers_spec.rb +28 -0
- data/spec/spec_helper.rb +15 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9812fb35a08d8185c0b59b0d0a5f2bf01e14f026
|
4
|
+
data.tar.gz: 7254a679a00df1c9dc5f848a4c7c8be61456034f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9861dffc528326f8d3ebcea54599497dfd3cad1f812328031f4488d547bd237ab3f6f8ec252c0a33c2f28a47d1b5f7f10356bb7a64865111e56ff85e57fdc359
|
7
|
+
data.tar.gz: 86f35c2ce743d69cda9a40523c16272a46c5239334ae29cc87190a2427c2b606f84973edb4ae1bf3b7012395a066509bb3418d7e040eccb14b6f451456977dd5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Pantelis Vratsalis
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dead_simple_authorization/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dead_simple_authorization"
|
8
|
+
spec.version = DeadSimpleAuthorization::VERSION
|
9
|
+
spec.authors = ["Pantelis Vratsalis"]
|
10
|
+
spec.email = ["pvratsalis@gmail.com"]
|
11
|
+
spec.summary = %q{A pretty simple solution for permissions on resources - framework agnostic}
|
12
|
+
spec.description = %q{a very simple authorization library with two main methods: can? and authorize, checking the permissions of a user to perform an action on a resource}
|
13
|
+
spec.homepage = "https://github.com/m1lt0n/dead_simple_authorization"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module DeadSimpleAuthorization
|
2
|
+
module Helpers
|
3
|
+
private
|
4
|
+
#
|
5
|
+
# Check if a user can perform an action for a specific resource.
|
6
|
+
# If the user shouldn't have access to the resource, a
|
7
|
+
# NotAuthorizedErrror is raised.
|
8
|
+
#
|
9
|
+
# Example: Is Bob allowed to view a Post ?
|
10
|
+
#
|
11
|
+
# Assuming bob = User.new('bob'), post = Post.new('post title', 'post content')
|
12
|
+
#
|
13
|
+
# authorize(bob, :view, post) will check for a PostPolicy by convention and will
|
14
|
+
# trigger the policy's view? method. In the PostPolicy's context, the user and post
|
15
|
+
# are available.
|
16
|
+
#
|
17
|
+
def authorize(user, action, resource)
|
18
|
+
raise ::DeadSimpleAuthorization::Errors::NotAuthorized unless can?(user, action, resource)
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Check if a user can perform an action for a specific resource.
|
23
|
+
# This method is not as strict as authorize in a sense that it does not raise an
|
24
|
+
# error, but returns the boolean outcome of the check
|
25
|
+
#
|
26
|
+
def can?(user, action, resource)
|
27
|
+
policy_class = "#{resource.class}Policy"
|
28
|
+
policy = Object::const_get(policy_class).new(resource, user)
|
29
|
+
policy.send("#{action.to_s}?")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DeadSimpleAuthorization
|
2
|
+
module Policy
|
3
|
+
class Base
|
4
|
+
attr_reader :resource
|
5
|
+
attr_reader :user
|
6
|
+
|
7
|
+
#
|
8
|
+
# The resource (e.g. a model) and user objects are available in all Policy objects
|
9
|
+
# that inherit from BasePolicy and can be used by authorize action methods.
|
10
|
+
#
|
11
|
+
def initialize(resource, user)
|
12
|
+
@user = user
|
13
|
+
@resource = resource
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DeadSimpleAuthorization::Helpers do
|
4
|
+
include DeadSimpleAuthorization::Helpers
|
5
|
+
|
6
|
+
let(:user) { double }
|
7
|
+
let(:post) { Post.new(user) }
|
8
|
+
|
9
|
+
describe 'authorize' do
|
10
|
+
it 'should raise error if user cannot access a resource' do
|
11
|
+
expect { authorize(user, :own, Post.new(double)) }.to raise_error(DeadSimpleAuthorization::Errors::NotAuthorized)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should do nothing if user can access a resource' do
|
15
|
+
expect(authorize(user, :own, post)).to be_nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe 'can?' do
|
20
|
+
it 'should return false if user cannot access a resource' do
|
21
|
+
expect(can?(user, :own, Post.new(double))).to equal false
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should return true if user can access a resource' do
|
25
|
+
expect(can?(user, :own, post)).to equal true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dead_simple_authorization
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pantelis Vratsalis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: 'a very simple authorization library with two main methods: can? and
|
56
|
+
authorize, checking the permissions of a user to perform an action on a resource'
|
57
|
+
email:
|
58
|
+
- pvratsalis@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- dead_simple_authorization.gemspec
|
69
|
+
- lib/dead_simple_authorization.rb
|
70
|
+
- lib/dead_simple_authorization/errors.rb
|
71
|
+
- lib/dead_simple_authorization/helpers.rb
|
72
|
+
- lib/dead_simple_authorization/policy.rb
|
73
|
+
- lib/dead_simple_authorization/version.rb
|
74
|
+
- spec/helpers_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
homepage: https://github.com/m1lt0n/dead_simple_authorization
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.2.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: A pretty simple solution for permissions on resources - framework agnostic
|
100
|
+
test_files:
|
101
|
+
- spec/helpers_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
has_rdoc:
|