ratify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0cebc395d3d5455371bcf40a2b097824d2fbf140504ceb819c47bf14dd3073a9
4
+ data.tar.gz: 70546ffb22562b86524567219f50a838b9af387ce5f3d9a8c21848347fdf7e27
5
+ SHA512:
6
+ metadata.gz: 9853b5ffeb4911df6ffa060e6c705b19f5fa201d0877991393e164e3645a815fe084a24ad54d886bb3399e8f84f38ebc63fe0170a8b6aa9d686c8e4f4deb0d75
7
+ data.tar.gz: 2e4c8c4d4923eab0a19934cfaf65c6c22b68e933f8cc2ba855bf2663257c79ffbac7638c5d0cddcb4a5fda77132288a062cff91ec1634b36d318e2901cefb8a0
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.ruby-version
10
+ /.ruby-gemset
11
+ /.idea/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ratify.gemspec
6
+ gemspec
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ratify (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.11.3)
10
+ rake (10.5.0)
11
+ yard (0.9.12)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.16)
18
+ minitest (~> 5.0)
19
+ rake (~> 10.0)
20
+ ratify!
21
+ yard (~> 0.9.12)
22
+
23
+ BUNDLED WITH
24
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Travis Haynes
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.
@@ -0,0 +1,91 @@
1
+ # Ratify
2
+
3
+ An easy to use, zero-dependency authorization gem.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ratify'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ratify
20
+
21
+ ## Usage
22
+
23
+ Here's an example of how you can use Ratify for basic user permissions.
24
+
25
+ ```ruby
26
+ class User
27
+ attr_accessor :admin
28
+
29
+ def initialize(admin: false)
30
+ @admin = admin
31
+ end
32
+
33
+ def admin?
34
+ admin && true
35
+ end
36
+ end
37
+
38
+ class Record
39
+ include Ratify
40
+
41
+ # Admins have full access to the records.
42
+ permit User, :create, :update, if: :admin?
43
+
44
+ # Users can create new records.
45
+ permit User, :create
46
+
47
+ # Users can update and destroy their own records.
48
+ permit User, :update, :destroy, if: -> (user) { self.user == user }
49
+
50
+ attr_accessor :user
51
+
52
+ def initialize(user)
53
+ @user = user
54
+ end
55
+ end
56
+
57
+ admin = User.new(admin: true)
58
+ user1 = User.new
59
+ user2 = User.new
60
+ record = Record.new(user: user1)
61
+
62
+ record.permits?(admin, :create) # => true
63
+ record.permits?(user1, :update) # => true
64
+ record.permits?(user2, :destroy) # => false
65
+
66
+ Record.permits?(admin, :create) # => true
67
+ Record.permits?(user1, :create) # => true
68
+ Record.permits?(user2, :create) # => true
69
+ ```
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies. Then,
74
+ run `rake test` to run the tests. You can also run `bin/console` for an
75
+ interactive prompt that will allow you to experiment.
76
+
77
+ To install this gem onto your local machine, run `bundle exec rake install`.
78
+
79
+ ## Contributing
80
+
81
+ Bug reports and pull requests are welcome on GitHub at
82
+ https://github.com/hi5dev/ratify.
83
+
84
+ This project is intended to be a safe, welcoming space for collaboration, and
85
+ contributors are expected to adhere to the
86
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the
91
+ [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'ratify'
5
+ require 'irb'
6
+
7
+ IRB.start(__FILE__)
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euo pipefail
4
+ IFS=$'\n\t'
5
+ set -vx
6
+
7
+ bundle install
@@ -0,0 +1,72 @@
1
+ # Provides permissions to any object that it is included in.
2
+ #
3
+ # @example Basic usage
4
+ # class User
5
+ # attr_accessor :role
6
+ # end
7
+ #
8
+ # class Record
9
+ # include Ratify
10
+ #
11
+ # permit User, :create, if: -> { role == :admin }
12
+ # end
13
+ #
14
+ # user = User.new
15
+ # Record.permit?(user, :create) # => false
16
+ #
17
+ # user.role = :admin
18
+ # Record.permit?(user, :create) # => true
19
+ module Ratify
20
+ autoload :Permission, 'ratify/permission'
21
+ autoload :VERSION, 'ratify/version'
22
+
23
+ # The methods in this module are added to the object's class methods when
24
+ # {Ratify} is included.
25
+ module ClassMethods
26
+ # All of the permissions for the object.
27
+ #
28
+ # @return [Array<Permission>] List of permissions.
29
+ def permissions
30
+ @permissions ||= []
31
+ end
32
+
33
+ # Creates a new permission for the object.
34
+ #
35
+ # @example Basic usage
36
+ # include Ratify
37
+ # permit User, :create, :read, :update, :destroy, if: :admin?
38
+ #
39
+ # @return [Array<Permission>] All of the permissions after they're updated.
40
+ def permit(object, *actions, **conditions)
41
+ permissions << Permission.new(object, *actions, **conditions)
42
+ end
43
+
44
+ # Checks if the given object is allowed to perform the requested action(s).
45
+ #
46
+ # @param [Object] object The object requesting the action(s).
47
+ # @param [*Symbol] actions The action(s) being requested.
48
+ # @param [Object] scope The scope of permissible object.
49
+ # @return [true | false] Whether or not the object is permitted.
50
+ def permits?(object, *actions, scope: nil)
51
+ permissions.any? { |m| m.permits?(object, *actions, scope: scope) }
52
+ end
53
+ end
54
+
55
+ # Called when the object is included in a class.
56
+ #
57
+ # @!visibility private
58
+ # @param [Class] klass The class {Ratify} is being included in.
59
+ # @return [void]
60
+ def self.included(klass)
61
+ klass.extend(Ratify::ClassMethods)
62
+ end
63
+
64
+ # Checks the permissions on an instance level.
65
+ #
66
+ # @param [Object] object The object requesting the action(s).
67
+ # @param [*Symbol] actions The actions being requested.
68
+ # @return [true | false] Whether or not the obejct is permitted.
69
+ def permits?(object, *actions)
70
+ self.class.permits?(object, *actions, scope: self)
71
+ end
72
+ end
@@ -0,0 +1,71 @@
1
+ # @!visibility private
2
+ class Permission
3
+ attr_reader :object, :actions, :conditions
4
+
5
+ # @param [Object] object
6
+ # @param [*Symbol] actions
7
+ # @param [**Hash] conditions
8
+ # @return [Permission] New instance of {Permission}.
9
+ def initialize(object, *actions, **conditions)
10
+ @object = object
11
+ @actions = actions
12
+ @conditions = conditions
13
+ end
14
+
15
+ # @param [Object] object
16
+ # @param [*Symbol] actions
17
+ # @param [Object] scope
18
+ # @return [true | false] Whether or not the object is permitted.
19
+ def permits?(object, *actions, scope: nil)
20
+ object_matches?(object) &&
21
+ action_matches?(actions) &&
22
+ conditions_match?(object, scope, actions)
23
+ end
24
+
25
+ private
26
+
27
+ # @param [Array<Symbol>] actions
28
+ # @return [true | false] If the given actions are all in the permission.
29
+ def action_matches?(actions)
30
+ actions.all? { |action| self.actions.include?(action) }
31
+ end
32
+
33
+ # @param [Symbol] name
34
+ # @param [Proc | Symbol | Object] condition
35
+ # @param [Object] object
36
+ # @param [Object] scope
37
+ # @param [Array<Symbol>] actions
38
+ # @return [true | false] Whether or not the condition matches the permission.
39
+ def condition_matches?(name, condition, object, scope, actions)
40
+ match = case condition
41
+ when Proc then scope.instance_exec(object, *actions, &condition)
42
+ when Symbol then scope.send(condition)
43
+ else scope.send(name) == condition
44
+ end
45
+
46
+ name == :unless ? !match : match && true
47
+ end
48
+
49
+ # @param [Object] object
50
+ # @param [Object] scope
51
+ # @param [Array<Symbol>] actions
52
+ # @return [true | false] Whether or not all of the conditions match.
53
+ def conditions_match?(object, scope, actions)
54
+ conditions.empty? || conditions.all? do |name, condition|
55
+ condition_matches?(name, condition, object, scope, actions)
56
+ end
57
+ end
58
+
59
+ # @return [Constant] The object as a constant.
60
+ def object_as_constant
61
+ return unless object.is_a?(String) || object.is_a?(Symbol)
62
+
63
+ Object.const_get(object) rescue nil
64
+ end
65
+
66
+ # @param [Object] object
67
+ # @return [true | false] Whether or not the object matches.
68
+ def object_matches?(object)
69
+ self.object === object || object_as_constant === object
70
+ end
71
+ end
@@ -0,0 +1,4 @@
1
+ module Ratify
2
+ # The gem's semantic version.
3
+ VERSION = '0.1.0'
4
+ end
@@ -0,0 +1,29 @@
1
+ ($: << File.expand_path('lib', __dir__)).uniq!
2
+
3
+ require 'ratify/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ratify'
7
+ spec.version = Ratify::VERSION
8
+ spec.authors = ['Travis Haynes']
9
+ spec.email = ['travis@hi5dev.com']
10
+ spec.summary = 'Zero-dependency authorization gem.'
11
+ spec.license = 'MIT'
12
+
13
+ spec.bindir = 'exe'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |file|
16
+ file.match(%r{^(test)/})
17
+ end
18
+
19
+ spec.executables = spec.files.grep(%r{^#{spec.bindir}/}) do |file|
20
+ File.basename(file)
21
+ end
22
+
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'minitest', '~> 5.0'
28
+ spec.add_development_dependency 'yard', '~> 0.9.12'
29
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ratify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Travis Haynes
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-26 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.12
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.12
69
+ description:
70
+ email:
71
+ - travis@hi5dev.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".ruby-gemset"
78
+ - ".ruby-version"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/ratify.rb
88
+ - lib/ratify/permission.rb
89
+ - lib/ratify/version.rb
90
+ - ratify.gemspec
91
+ homepage:
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.7.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Zero-dependency authorization gem.
115
+ test_files: []