action_authorization 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4f28b1848154905407d532747347e5a0c33dadc028c5fe84f4d78fc3cf8a14ba
4
+ data.tar.gz: 8525ff6cc7f6b9891c27b567c031385cd6a56dc0365847b8b5b8a10214e3e964
5
+ SHA512:
6
+ metadata.gz: 74efb7d0649a2a9d2474c3df0dd087e86469e3c5257f570a7d3cea093322a5e380c7ba3c5d9bd474b3d668b5014794624753d7f90ff321ac358752f429826f67
7
+ data.tar.gz: ce8d61ed43e321016c11780b3075434987d35dfeb780e6b79093b165b06e06b50dd84fb88d5a900f6461e362450287682069a028646268e029397eb5efb1eaa2
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
+ /*.gem
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=0ff5e542d77e2710a003d2ee7a11d4e5a50aaa08fe97ab009a72768885197905
4
+ language: ruby
5
+ rvm:
6
+ - 2.5.1
7
+ before_script:
8
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
9
+ - chmod +x ./cc-test-reporter
10
+ - ./cc-test-reporter before-build
11
+ script:
12
+ - bundle exec rake test
13
+ after_script:
14
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile ADDED
@@ -0,0 +1,10 @@
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 rexport.gemspec
6
+ gemspec
7
+
8
+ group :test do
9
+ gem 'simplecov'
10
+ end
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard :minitest do
2
+ watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
3
+ watch(%r{^test/.+_test\.rb$})
4
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Aaron Baldwin
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,84 @@
1
+ # ActionAuthorization
2
+
3
+ A base policy class for authorizing controller actions with access to the current_user and object.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'action_authorization'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install action_authorization
20
+
21
+ ## Requirements
22
+
23
+ ActionAuthorization requires a **current_user** method that returns the currently logged in user.
24
+
25
+ ## Usage
26
+
27
+ Include the ActionAuthorization module in your ApplicationController (or indvidual controller(s))
28
+
29
+ ```ruby
30
+ class ApplicationController < ActionController::Base
31
+ include ActionAuthorization
32
+ end
33
+ ```
34
+
35
+ Create an authorization policy for a resource.
36
+
37
+ ``` ruby
38
+ class DocumentPolicy
39
+ def show?
40
+ document.owner == user
41
+ end
42
+ end
43
+ ```
44
+
45
+ Call **authorize** method in controller action.
46
+
47
+ ```ruby
48
+ class DocumentController < ApplicationController
49
+ def show
50
+ @document = authorize(Document.find(params[:id]))
51
+ end
52
+ end
53
+ ```
54
+
55
+ Pass a **policy_class** to authorize to override the default resource based policy.
56
+ ```ruby
57
+ class DocumentController < ApplicationController
58
+ def show
59
+ @document = authorize(Document.find(params[:id]), policy_class: UserOwnerPolicy)
60
+ end
61
+ end
62
+ ```
63
+
64
+ Check if authorized before displaying a link in the view.
65
+
66
+ ```erb
67
+ <%= link_to(@document.name, @document) if policy(@document).show? %>
68
+ ```
69
+
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
74
+
75
+ 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).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wwidea/action_authorization.
80
+
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'action_authorization/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "action_authorization"
8
+ spec.version = ActionAuthorization::VERSION
9
+ spec.authors = ["Aaron Baldwin"]
10
+ spec.email = ["baldwina@brightwayslearning.org"]
11
+
12
+ spec.summary = %q{Rails controller object-level action authorization.}
13
+ spec.description = %q{A base policy class for authorizing controller actions with access to the current_user and object.}
14
+ spec.homepage = "https://github.com/wwidea/action_authorization"
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_runtime_dependency "activesupport", "~> 5.0"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ spec.add_development_dependency "rake", "~> 12.1"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ spec.add_development_dependency "guard", "~> 2.14"
30
+ spec.add_development_dependency "guard-minitest", "~> 2.4"
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "action_authorization"
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,30 @@
1
+ require "action_authorization/version"
2
+ require "action_authorization/base_policy"
3
+ require "action_authorization/authorization_failure"
4
+ require "active_support/core_ext/string/inflections"
5
+
6
+ module ActionAuthorization
7
+ def self.included(base)
8
+ base.class_eval do
9
+ helper_method :policy
10
+ end
11
+ end
12
+
13
+ protected
14
+
15
+ def authorize(object, action: action_name, policy_class: nil)
16
+ if policy(object, policy_class).send("#{action}?")
17
+ object
18
+ else
19
+ raise ActionAuthorization::AuthorizationFailure
20
+ end
21
+ end
22
+
23
+ def policy(object, policy_class = nil)
24
+ (policy_class || policy_class_for(object)).new(current_user, object)
25
+ end
26
+
27
+ def policy_class_for(object)
28
+ "#{object.class.name}Policy".constantize
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ module ActionAuthorization
2
+ class AuthorizationFailure < StandardError
3
+ end
4
+ end
@@ -0,0 +1,49 @@
1
+ module ActionAuthorization
2
+ class BasePolicy
3
+ attr_accessor :user, :object
4
+
5
+ def initialize(user, object)
6
+ self.user = user
7
+ self.object = object
8
+ end
9
+
10
+ # create alias to object from subclass name
11
+ def self.inherited(klass)
12
+ klass.send(:alias_method, klass.name.gsub('Policy', '').underscore, :object)
13
+ end
14
+
15
+ def index?
16
+ authorized?
17
+ end
18
+
19
+ def show?
20
+ authorized?
21
+ end
22
+
23
+ def new?
24
+ create?
25
+ end
26
+
27
+ def create?
28
+ authorized?
29
+ end
30
+
31
+ def edit?
32
+ update?
33
+ end
34
+
35
+ def update?
36
+ authorized?
37
+ end
38
+
39
+ def destroy?
40
+ create?
41
+ end
42
+
43
+ private
44
+
45
+ def authorized?
46
+ false
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module ActionAuthorization
2
+ VERSION = "0.2.0"
3
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_authorization
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Baldwin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.14'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.14'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.4'
97
+ description: A base policy class for authorizing controller actions with access to
98
+ the current_user and object.
99
+ email:
100
+ - baldwina@brightwayslearning.org
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - Guardfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - action_authorization.gemspec
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/action_authorization.rb
116
+ - lib/action_authorization/authorization_failure.rb
117
+ - lib/action_authorization/base_policy.rb
118
+ - lib/action_authorization/version.rb
119
+ homepage: https://github.com/wwidea/action_authorization
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.7.7
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Rails controller object-level action authorization.
143
+ test_files: []