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 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
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in a.gemspec
4
+ gemspec
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
@@ -0,0 +1,2 @@
1
+ # dead_simple_authorization
2
+ A Very simple authorization gem
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ task.rspec_opts = ['--color', '--format', 'documentation']
6
+ end
7
+
8
+ task :default => :spec
@@ -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,3 @@
1
+ require 'dead_simple_authorization/errors'
2
+ require 'dead_simple_authorization/policy'
3
+ require 'dead_simple_authorization/helpers'
@@ -0,0 +1,7 @@
1
+ module DeadSimpleAuthorization
2
+ module Errors
3
+ # A Custom error class when authorization fails
4
+ class NotAuthorized < StandardError
5
+ end
6
+ end
7
+ 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,3 @@
1
+ module DeadSimpleAuthorization
2
+ VERSION = "0.1.0"
3
+ 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
@@ -0,0 +1,15 @@
1
+ require 'dead_simple_authorization'
2
+
3
+ class PostPolicy < DeadSimpleAuthorization::Policy::Base
4
+ def own?
5
+ @user == @resource.owner
6
+ end
7
+ end
8
+
9
+ class Post
10
+ attr_reader :owner
11
+
12
+ def initialize(owner)
13
+ @owner = owner
14
+ end
15
+ end
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: