umpire 0.4.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: 6e96abcc48d489a4c4728288b851f08de6d3e313
4
+ data.tar.gz: b59784ad56555853627596ab056058cebb1722c5
5
+ SHA512:
6
+ metadata.gz: 326c327dc437578ecf8906c3d351948b096aeb2506c0ad57ba1aa51a764056f008ba52cf91c0c35b7ce4a06f4e98142502c17973bf75627d103d64143e7e3708
7
+ data.tar.gz: 34706cbe90b7865f9296eb1ac4f494c6c4f420f3d1574d32d0057543da9d44412333ce4b6c5e7a1a80e1b0ce1ab83461e754146de7965478829c2343b6b7e5d9
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in umpire.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Anthony Karasavov
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Umpire
2
+
3
+ Umpire is a very lightweight authorization lib that uses policy classes to define rules
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'umpire'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install umpire
20
+
21
+ ## Usage
22
+
23
+ Umpire consists of a base policy class that you need to extend and a helper that you can use in your app.
24
+ Because it's doesn't depend on rails, you need to include the Umpire helper in your `ApplicationHelper`
25
+ and your `ApplicationController` or whatever other class you might need to use it in:
26
+
27
+ include Umpire::AuthHelper
28
+
29
+ I use that in a rails lib, so by default the lib uses a `current_user` method to get the subject from,
30
+ but if you don't have it - no worries, you can still pas a subject on your own.
31
+
32
+ Let's make a simple Policy class and use it in a rails view:
33
+
34
+ ```ruby
35
+ class SchoolPolicy < Umpire::Policy
36
+ # the only method you need to overwrite
37
+ # return all the allowed actions here
38
+ # @subject is the subject (current_user) by default
39
+ # and the object is @object - in this exapmle it could be a
40
+ def rules
41
+ allowed = [:go_to_school]
42
+ allowed << :take_cs_201 @subject.has_taken(:cs_101)
43
+ allowed
44
+ end
45
+ end
46
+ ```
47
+
48
+ You can now use the policy class with the helper like this:
49
+ ```erb
50
+ <%= render partial: 'modules/cs_201' if can? :take_cs_201, using: SchoolPolicy %>
51
+ ```
52
+
53
+ This will check call `has_taken(:cs_101)` on the result of `current_user`
54
+
55
+ Other usage examples:
56
+
57
+ ```ruby
58
+ # with subject
59
+ can? User.find(1), :drive, car, using: HighwayCode
60
+
61
+ # without subject (assumes current_user if available)
62
+ can? :drive, car, using: HighwayCode
63
+
64
+ # multiple policies
65
+ can? :park, car, using: [HighwayCode, ParkingRules]
66
+
67
+ # without object
68
+ can? :cook_spaghetti, using: [KitchenPolicy]
69
+
70
+ # multiple actions
71
+ can? [:order, :drink], beer, using: BarPolicy
72
+ ```
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bundle` to install dependencies.
77
+ Then, run `bundle exec rake rspec` to run the tests.
78
+
79
+ To install this gem onto your local machine, run `bundle exec rake install`.
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at https://github.com/muxcmux/umpire.
84
+
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
89
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "umpire"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/umpire.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Umpire
2
+ class NoRulesFound < StandardError
3
+ end
4
+ end
5
+
6
+ require "umpire/version"
7
+ require "umpire/policy_missing_error"
8
+ require "umpire/auth_helper"
9
+ require "umpire/policy"
@@ -0,0 +1,47 @@
1
+ module Umpire
2
+ module AuthHelper
3
+ # Usage:
4
+ #
5
+ # with subject
6
+ # can? user, :drive, car, using: HighwayCode
7
+ #
8
+ # without subject (assumes current_user if available)
9
+ # can? :drive, car, using: HighwayCode
10
+ #
11
+ # multiple policies
12
+ # can? :park, car, using: [HighwayCode, ParkingRules]
13
+ #
14
+ # without object
15
+ # can? :cook_spaghetti, using: [KitchenPolicy]
16
+ #
17
+ # multiple actions
18
+ # can? [:order, :drink], beer, using: BarPolicy
19
+ #
20
+ def can? *args
21
+ options = args.last.is_a?(Hash) ? args.pop : {}
22
+ policies = *options[:using]
23
+ raise PolicyMissingError.new if policies.empty?
24
+
25
+ first_argument = args.shift
26
+ if first_argument.is_a?(Symbol) || first_argument.is_a?(Array)
27
+ subject = defined?(current_user) ? current_user : {}
28
+ actions = first_argument
29
+ else
30
+ subject = first_argument
31
+ actions = args.shift
32
+ end
33
+
34
+ next_argument = args.shift
35
+ object = next_argument.is_a?(Hash) ? nil : next_argument
36
+
37
+ policies.each do |policy|
38
+ return false unless policy.allows?(subject).to(actions, object)
39
+ end
40
+
41
+ true
42
+ end
43
+
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,41 @@
1
+ # A very basic policy class. Extend other policies with this
2
+ # by providing a rules method which returns the valid actions
3
+ # based on the subject and object
4
+ #
5
+ # Usage
6
+ #
7
+ # with a single action:
8
+ # DrivingPolicy.allows?(driver).to(:drive, car)
9
+ #
10
+ # with multiple actions
11
+ # MemberPolicy.allows?(member).to([:join, :leave], club)
12
+ #
13
+ # without object
14
+ # SchoolPolicy.allows?(student).to(:cheat_on_exam)
15
+ #
16
+ module Umpire
17
+ class Policy
18
+
19
+ include AuthHelper
20
+
21
+ def initialize subject
22
+ @subject = subject
23
+ end
24
+
25
+ def self.allows? subject
26
+ new subject
27
+ end
28
+
29
+ def to actions, object = nil
30
+ @object = object
31
+ @actions = *actions
32
+ @actions.all? { |a| rules.include?(a) }
33
+ end
34
+
35
+ # overwrite me
36
+ def rules
37
+ raise NoRulesFound.new("Please implement `rules` in your policy object")
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,7 @@
1
+ module Umpire
2
+ class PolicyMissingError < StandardError
3
+ def message
4
+ "Policy class needs to be supplied: { using: SomePolicy }"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Umpire
2
+ VERSION = "0.4.0"
3
+ end
data/umpire.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'umpire/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "umpire"
8
+ spec.version = Umpire::VERSION
9
+ spec.authors = ["Anthony Karasavov"]
10
+ spec.email = ["storm.bg@gmail.com"]
11
+
12
+ spec.summary = %q{Lightweight ruby authorization library based on policies}
13
+ spec.description = %q{Plain old ruby objects with a helper for use in your apps}
14
+ spec.homepage = "https://github.com/muxcmux/umpire"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.3"
33
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: umpire
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Karasavov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-08 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ description: Plain old ruby objects with a helper for use in your apps
56
+ email:
57
+ - storm.bg@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/umpire.rb
71
+ - lib/umpire/auth_helper.rb
72
+ - lib/umpire/policy.rb
73
+ - lib/umpire/policy_missing_error.rb
74
+ - lib/umpire/version.rb
75
+ - umpire.gemspec
76
+ homepage: https://github.com/muxcmux/umpire
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ allowed_push_host: https://rubygems.org
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Lightweight ruby authorization library based on policies
101
+ test_files: []