rails_authorize 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: 027d91ec6d457200114228f09d8f8e17022c8eee
4
+ data.tar.gz: 71413e8ae25c57b7e93f7c1132a47b7afe680237
5
+ SHA512:
6
+ metadata.gz: 37dfd217674de5eafea89bf6a6c1b1548a958e0e9c1115b758e6c70d438adb403f0a5a95e696f09b9d10d31f7805ed2008e9cbf61ac0774739507b7cd617c751
7
+ data.tar.gz: a2aabe9427a0748094f9bf81ff8ec7b7b425a90cc829b9db57290630e4596ba2e7c29f2698e17913e4fb1d6a41a38c6abd9b70da52098cdfc7d3a41a4bf2ab5f
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.15.4
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 rails_authorize.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_authorize (0.1.0)
5
+ activesupport (>= 3.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.1.3)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (~> 0.7)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ concurrent-ruby (1.0.5)
16
+ diff-lcs (1.3)
17
+ i18n (0.8.6)
18
+ minitest (5.10.3)
19
+ rake (10.5.0)
20
+ rspec (3.6.0)
21
+ rspec-core (~> 3.6.0)
22
+ rspec-expectations (~> 3.6.0)
23
+ rspec-mocks (~> 3.6.0)
24
+ rspec-core (3.6.0)
25
+ rspec-support (~> 3.6.0)
26
+ rspec-expectations (3.6.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.6.0)
29
+ rspec-mocks (3.6.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.6.0)
32
+ rspec-support (3.6.0)
33
+ thread_safe (0.3.6)
34
+ tzinfo (1.2.3)
35
+ thread_safe (~> 0.1)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler (~> 1.15.4)
42
+ rails_authorize!
43
+ rake (~> 10.0)
44
+ rspec (~> 3.0)
45
+
46
+ BUNDLED WITH
47
+ 1.16.0.pre.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 rjurado
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,101 @@
1
+ # RailsAuthorize
2
+ ![Build Status](https://travis-ci.org/rjurado01/rails_authorize.svg?branch=master)
3
+
4
+ Simple and flexible authorization Rails system inspired by Pundit.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rails_authorize'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rails_authorize
21
+
22
+ ## Example
23
+
24
+ ```
25
+ # app/models/post.rb
26
+
27
+ class Post
28
+ def published?
29
+ return published == true
30
+ end
31
+ end
32
+ ```
33
+
34
+ ```
35
+ # app/authorizations/application_authorization.rb
36
+
37
+ class ApplicationAuthorization
38
+ attr_reader :user, :target, :context
39
+
40
+ def initialize(user, target, context)
41
+ @user = user
42
+ @target = target
43
+ @context = context
44
+ end
45
+ end
46
+ ```
47
+
48
+ ```
49
+ # app/authorizations/post_authorization.rb
50
+
51
+ class PostAuthorization < ApplicationAuthorization
52
+ def index?
53
+ true
54
+ end
55
+
56
+ def show?
57
+ user.is_admin? and target.published?
58
+ end
59
+
60
+ def scope
61
+ target.where(published: true)
62
+ end
63
+ end
64
+ ```
65
+
66
+ ```
67
+ # app/controllers/application_controller.rb
68
+
69
+ class ApplicationController < ActionController::Base
70
+ include RailsAuthorization
71
+ end
72
+ ```
73
+
74
+ ```
75
+ # app/controllers/posts_controller.rb
76
+
77
+ class PostController
78
+ def index
79
+ @posts = authorized_scope(Post)
80
+ end
81
+
82
+ def show
83
+ @post = Post.find(params[:id])
84
+ authorize @post
85
+ end
86
+ end
87
+ ```
88
+
89
+ ## Development
90
+
91
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
92
+
93
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rjurado01/rails_authorize.
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_authorize"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module RailsAuthorize
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,74 @@
1
+ require 'rails_authorize/version'
2
+
3
+ module RailsAuthorize
4
+ # Error that will be raised when authorization has failed
5
+ class NotAuthorizedError < StandardError; end
6
+
7
+ ##
8
+ # Finds authorization class for given target and returns new instance
9
+ #
10
+ # @param target [any] the target to load authorization
11
+ # @param options [Hash] key/value options (user, authorization, context)
12
+ # @param options[:user] [Object] the user that initiated the action
13
+ # @param options[:authorization] [Class] Authorization class to use for authenticate
14
+ # @param options[:context] [Hash] other key/value options to use in the authorization methods
15
+ #
16
+ # @return [Object] new authorization instance
17
+ #
18
+ def authorization(target, options={})
19
+ user = options[:user] || current_user
20
+ klass = options[:authorization] || "#{target.model_name.name}Authorization".constantize
21
+
22
+ klass.new(user, target, options[:context])
23
+ end
24
+
25
+ ##
26
+ # Throwing an error if the user is not authorized to perform the given action
27
+ #
28
+ # @param target [Object] the target we're checking permissions of
29
+ # @param options [Hash] key/value options (action, user, authorization, context)
30
+ # @param options[:action] [String] the method to check on the authorization (e.g. `:show?`)
31
+ #
32
+ # @raise [NotAuthorizedError] if the given action method returned false
33
+ # @return [Object] the passed target
34
+ #
35
+ def authorize(target, options={})
36
+ action = options.delete(:action) || "#{action_name}?"
37
+ authorization = authorization(target, options)
38
+
39
+ raise(NotAuthorizedError) unless authorization.public_send(action)
40
+
41
+ target
42
+ end
43
+
44
+ ##
45
+ # Retrieves the authorization scope for the given target
46
+ #
47
+ # @param target [Object] the target we're retrieving the policy scope for
48
+ # @param options [Hash] key/value options (user, authorization, context)
49
+ #
50
+ # @return [Scope] authorized scope
51
+ #
52
+ def authorization_scope(target, options={})
53
+ authorization(target, options).scope
54
+ end
55
+
56
+ ##
57
+ # Throwing an error if the user is not authorized to perform the given action
58
+ #
59
+ # @param target [Object] the target we're retrieving the policy scope for
60
+ # @param options [Hash] key/value options (action, user, authorization, context)
61
+ # @param options[:action] [String] the method to check on the authorization (e.g. `:show?`)
62
+ #
63
+ # @raise [NotAuthorizedError] if the given action method returned false
64
+ # @return [Scope] authorization scope
65
+ #
66
+ def authorized_scope(target, options={})
67
+ action = options.delete(:action) || "#{action_name}?"
68
+ authorization = authorization(target, options)
69
+
70
+ raise(NotAuthorizedError) unless authorization.public_send(action)
71
+
72
+ authorization.scope
73
+ end
74
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_authorize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rails_authorize'
8
+ spec.version = RailsAuthorize::VERSION
9
+ spec.authors = ['rjurado01']
10
+ spec.email = ['rjurado01@gmail.com']
11
+
12
+ spec.summary = 'Simple and flexible authorization Rails system'
13
+ spec.description = 'Authorization system for Rails with only few helpers and regular Ruby classes.'
14
+ spec.homepage = 'https://github.com/rjurado01/rails_authorize'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.15'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+
28
+ spec.add_dependency 'activesupport', '~> 3.0', '>= 3.0.0'
29
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_authorize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rjurado01
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-24 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 3.0.0
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '3.0'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.0.0
75
+ description: Authorization system for Rails with only few helpers and regular Ruby
76
+ classes.
77
+ email:
78
+ - rjurado01@gmail.com
79
+ executables: []
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - ".gitignore"
84
+ - ".rspec"
85
+ - ".travis.yml"
86
+ - Gemfile
87
+ - Gemfile.lock
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - bin/console
92
+ - bin/setup
93
+ - lib/rails_authorize.rb
94
+ - lib/rails_authorize/version.rb
95
+ - rails_authorize.gemspec
96
+ homepage: https://github.com/rjurado01/rails_authorize
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.5.2
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Simple and flexible authorization Rails system
120
+ test_files: []